UNPKG

pokie

Version:

A server-side video slot game logic framework for JavaScript and TypeScript.

176 lines 6.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SymbolsSequence = void 0; class SymbolsSequence { constructor() { this.sequence = []; } setSymbol(index, symbolId) { if (this.sequence.length === 0) { this.sequence = [symbolId]; } else { this.sequence[this.getIndex(index)] = symbolId; } return this; } setSymbols(index, symbols) { if (this.sequence.length < symbols.length) { this.sequence = [...symbols]; } else { symbols.forEach((id, i) => this.setSymbol(index + i, id)); } return this; } addSymbol(symbolId, stackSize, index) { if (index !== undefined && index < this.sequence.length) { this.sequence.splice(index, 0, ...Array(stackSize || 1).fill(symbolId)); } else { this.sequence.push(...Array(stackSize || 1).fill(symbolId)); } return this; } addSymbols(symbolsIds, index) { if (index !== undefined && index < this.sequence.length) { this.sequence.splice(index, 0, ...symbolsIds); } else { this.sequence.push(...symbolsIds); } return this; } getSymbol(index) { return this.sequence[this.getIndex(index)]; } getSymbols(index, symbolsNumber) { const symbols = []; let currentIndex = index; for (let i = 0; i < symbolsNumber; i++) { currentIndex = this.getIndex(currentIndex); symbols.push(this.getSymbol(currentIndex)); currentIndex++; } return symbols; } getSize() { return this.sequence.length; } getNumberOfSymbols(symbolId) { return this.sequence.filter((symbol) => symbol === symbolId).length; } getSymbolWeight(symbolId) { const symbolCount = this.getNumberOfSymbols(symbolId); return (symbolCount / this.sequence.length) * 100; } getSymbolsWeights() { const symbolsWeights = {}; const uniqueSymbols = [...new Set(this.sequence)]; for (const symbolId of uniqueSymbols) { symbolsWeights[symbolId] = this.getSymbolWeight(symbolId); } return symbolsWeights; } getSymbolsIndexes(symbolsIds) { const indexes = []; for (let i = 0; i < this.sequence.length; i++) { if (symbolsIds.includes(this.sequence[i])) { indexes.push(i); } } return indexes; } getSymbolsStacksIndexes() { const stacks = []; const length = this.sequence.length; if (length > 1 && new Set(this.sequence).size > 1) { for (let i = 0; i < length; i++) { let currentSymbol = this.sequence[i]; let nextIndex = this.getIndex(i + 1); let nextSymbol = this.sequence[nextIndex]; if (currentSymbol === nextSymbol) { const stack = { index: i, size: 1 }; while (currentSymbol === nextSymbol) { stack.size++; if (i < this.sequence.length - 1) { i = nextIndex; } else if (stacks[0] && stacks[0].index === 0) { stacks.shift(); } nextIndex = this.getIndex(nextIndex + 1); currentSymbol = this.sequence[i]; nextSymbol = this.sequence[nextIndex]; } stacks.push(stack); } } } return stacks; } shuffle() { for (let i = this.sequence.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [this.sequence[i], this.sequence[j]] = [this.sequence[j], this.sequence[i]]; } return this; } fromArray(symbolsArray) { this.sequence = [...symbolsArray]; return this; } fromSymbolsWeights(symbolsWeights, sequenceLength = 50) { const weightsSum = Object.values(symbolsWeights).reduce((sum, val) => sum + val, 0); if (weightsSum !== 100) { throw "Wrong weights data. Expected sum of values is 100, but actual is " + weightsSum; } const symbols = []; for (const symbolId in symbolsWeights) { if (symbolsWeights[symbolId] !== undefined) { const symbolCount = Math.round((symbolsWeights[symbolId] / 100) * sequenceLength); symbols.push(...Array(symbolCount).fill(symbolId)); } } this.sequence = symbols; return this; } fromNumbersOfSymbols(symbolsNumbers) { const symbols = []; for (const symbolId in symbolsNumbers) { if (symbolsNumbers[symbolId] !== undefined) { symbols.push(...Array(symbolsNumbers[symbolId]).fill(symbolId)); } } this.sequence = symbols; return this; } fromNumberOfEachSymbol(availableSymbols, symbolsNumber) { const symbols = []; for (const symbolId of availableSymbols) { symbols.push(...Array(symbolsNumber).fill(symbolId)); } this.sequence = symbols; return this; } toArray() { return [...this.sequence]; } removeAllSymbols(symbolIdToRemove) { return this.fromArray(this.toArray().filter((symbolId) => symbolId !== symbolIdToRemove)); } removeSymbol(index) { this.sequence = this.sequence.filter((symbol, i) => i !== index); return this; } getIndex(index) { if (index >= 0) { return index % this.sequence.length; } else { return this.sequence.length - 1 - (Math.abs(index + 1) % this.sequence.length); } } } exports.SymbolsSequence = SymbolsSequence; //# sourceMappingURL=SymbolsSequence.js.map