pokie
Version:
A server-side video slot game logic framework for JavaScript and TypeScript.
162 lines • 5.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Simulation = void 0;
class Simulation {
constructor(session, config) {
this.totalBetAmount = 0;
this.totalPayoutAmount = 0;
this.numberOfWiningRounds = 0;
this.rtpPerRound = [];
this.allPayouts = [];
this.nonZeroPayouts = [];
this.betsPerRound = [];
this.currentRoundNumber = 0;
this.session = session;
this.numberOfRounds = config.getNumberOfRounds();
this.changeBetStrategy = config.getChangeBetStrategy();
this.playStrategy = config.getPlayStrategy();
}
run() {
let i;
this.doPlay();
for (i = 0; i < this.numberOfRounds - 1; i++) {
if (this.canPlayNextGame()) {
this.doPlay();
}
else {
break;
}
}
this.onFinished();
}
runAsync(chunkSize = 1000, delayBetweenChunks = 0) {
return new Promise((resolve) => {
let currentRound = 0;
const playChunk = () => {
const chunkEnd = Math.min(currentRound + chunkSize, this.numberOfRounds);
for (let i = currentRound; i < chunkEnd; i++) {
if (this.canPlayNextGame()) {
this.doPlay();
}
else {
this.onFinished();
resolve();
return;
}
}
currentRound = chunkEnd;
if (currentRound < this.numberOfRounds) {
setTimeout(playChunk, delayBetweenChunks);
}
else {
this.onFinished();
resolve();
}
};
playChunk();
});
}
getLastRtp() {
return this.rtpPerRound[this.rtpPerRound.length - 1];
}
getAverageRtp() {
return this.rtpPerRound.reduce((sum, rtp) => sum + rtp, 0) / this.currentRoundNumber;
}
getHitFrequency() {
return this.numberOfWiningRounds / this.currentRoundNumber;
}
getPayoutsStandardDeviation(includeZeroPayouts = true) {
const payouts = includeZeroPayouts ? this.allPayouts : this.nonZeroPayouts;
const averagePayout = this.getAveragePayout(includeZeroPayouts);
const squaredDifferences = payouts.map((payout) => Math.pow((payout - averagePayout), 2));
const variance = squaredDifferences.reduce((acc, val) => acc + val, 0) / payouts.length;
return Math.sqrt(variance);
}
getTotalBetAmount() {
return this.totalBetAmount;
}
getTotalPayoutAmount() {
return this.totalPayoutAmount;
}
getCurrentRoundNumber() {
return this.currentRoundNumber;
}
getNumberOfWinningRounds() {
return this.numberOfWiningRounds;
}
getTotalNumberOfRounds() {
return this.numberOfRounds;
}
setBeforePlayCallback(callback) {
this.beforePlayCallback = callback;
}
removeBeforePlayCallback() {
this.beforePlayCallback = undefined;
}
setAfterPlayCallback(callback) {
this.afterPlayCallback = callback;
}
removeAfterPlayCallback() {
this.afterPlayCallback = undefined;
}
setOnFinishedCallback(callback) {
this.onFinishedCallback = callback;
}
removeOnFinishedCallback() {
this.onFinishedCallback = undefined;
}
getAllBets() {
return this.betsPerRound;
}
getPayouts(includeZeroPayouts = true) {
return includeZeroPayouts ? this.allPayouts : this.nonZeroPayouts;
}
getAllRtpValues() {
return this.rtpPerRound;
}
getAveragePayout(includeZeroPayouts = true) {
return this.totalPayoutAmount / (includeZeroPayouts ? this.currentRoundNumber : this.numberOfWiningRounds);
}
getAverageBet() {
return this.totalBetAmount / this.currentRoundNumber;
}
onFinished() {
if (this.onFinishedCallback) {
this.onFinishedCallback();
}
}
canPlayNextGame() {
let r = this.session.canPlayNextGame();
if (r && this.playStrategy) {
r = this.playStrategy.canPlayNextSimulationRound(this.session);
}
return r;
}
setBetBeforePlay() {
if (this.changeBetStrategy) {
this.changeBetStrategy.setBetForNextRound(this.session);
}
}
doPlay() {
if (this.beforePlayCallback) {
this.beforePlayCallback();
}
this.currentRoundNumber++;
this.setBetBeforePlay();
this.totalBetAmount += this.session.getBet();
this.betsPerRound.push(this.session.getBet());
this.session.play();
this.totalPayoutAmount += this.session.getWinAmount();
this.allPayouts.push(this.session.getWinAmount());
if (this.session.getWinAmount()) {
this.numberOfWiningRounds++;
this.nonZeroPayouts.push(this.session.getWinAmount());
}
this.rtpPerRound.push(this.totalPayoutAmount / this.totalBetAmount);
if (this.afterPlayCallback) {
this.afterPlayCallback();
}
}
}
exports.Simulation = Simulation;
//# sourceMappingURL=Simulation.js.map