@slot-engine/core 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -189,15 +189,14 @@ var RandomNumberGenerator = class {
189
189
  return float * (high - low) + low;
190
190
  }
191
191
  weightedRandom(weights) {
192
- const totalWeight = Object.values(weights).reduce(
193
- (sum, weight) => sum + weight,
194
- 0
195
- );
196
- const randomValue = this.randomFloat(0, 1) * totalWeight;
197
- let cumulativeWeight = 0;
198
- for (const [key, weight] of Object.entries(weights)) {
199
- cumulativeWeight += weight;
200
- if (randomValue < cumulativeWeight) {
192
+ let totalWeight = 0;
193
+ for (const key in weights) {
194
+ totalWeight += weights[key];
195
+ }
196
+ let remaining = this.randomFloat(0, 1) * totalWeight;
197
+ for (const key in weights) {
198
+ remaining -= weights[key];
199
+ if (remaining < 0) {
201
200
  return key;
202
201
  }
203
202
  }
@@ -502,27 +501,23 @@ var Board = class {
502
501
  countSymbolsOnBoard(symbolOrProperties) {
503
502
  let total = 0;
504
503
  const onReel = {};
504
+ const isGameSymbol = symbolOrProperties instanceof GameSymbol;
505
505
  for (const [ridx, reel] of this.reels.entries()) {
506
506
  for (const symbol of reel) {
507
- let matches = true;
508
- if (symbolOrProperties instanceof GameSymbol) {
509
- if (symbol.id !== symbolOrProperties.id) matches = false;
507
+ if (isGameSymbol) {
508
+ if (symbol.id !== symbolOrProperties.id) continue;
510
509
  } else {
510
+ let matches = true;
511
511
  for (const [key, value] of Object.entries(symbolOrProperties)) {
512
512
  if (!symbol.properties.has(key) || symbol.properties.get(key) !== value) {
513
513
  matches = false;
514
514
  break;
515
515
  }
516
516
  }
517
+ if (!matches) continue;
517
518
  }
518
- if (matches) {
519
- total++;
520
- if (onReel[ridx] === void 0) {
521
- onReel[ridx] = 1;
522
- } else {
523
- onReel[ridx]++;
524
- }
525
- }
519
+ total++;
520
+ onReel[ridx] = (onReel[ridx] || 0) + 1;
526
521
  }
527
522
  }
528
523
  return [total, onReel];
@@ -621,8 +616,9 @@ var Board = class {
621
616
  ...opts,
622
617
  ...this.reelsLocked.length && { reelsLocked: this.reelsLocked }
623
618
  });
624
- const reelsAmount = opts.reelsAmount ?? opts.ctx.services.game.getCurrentGameMode().reelsAmount;
625
- const symbolsPerReel = opts.symbolsPerReel ?? opts.ctx.services.game.getCurrentGameMode().symbolsPerReel;
619
+ const gameMode = opts.ctx.services.game.getCurrentGameMode();
620
+ const reelsAmount = opts.reelsAmount ?? gameMode.reelsAmount;
621
+ const symbolsPerReel = opts.symbolsPerReel ?? gameMode.symbolsPerReel;
626
622
  const padSymbols = opts.padSymbols ?? opts.ctx.config.padSymbols;
627
623
  const finalReelStops = Array.from(
628
624
  { length: reelsAmount },
@@ -665,15 +661,16 @@ var Board = class {
665
661
  this.lastUsedReels = opts.reels;
666
662
  for (let ridx = 0; ridx < reelsAmount; ridx++) {
667
663
  const reelPos = finalReelStops[ridx];
668
- const reelLength = opts.reels[ridx].length;
664
+ const reel = opts.reels[ridx];
665
+ const reelLength = reel.length;
669
666
  for (let p = padSymbols - 1; p >= 0; p--) {
670
667
  const topPos = ((reelPos - (p + 1)) % reelLength + reelLength) % reelLength;
671
- this.paddingTop[ridx].push(opts.reels[ridx][topPos].clone());
668
+ this.paddingTop[ridx].push(reel[topPos].clone());
672
669
  const bottomPos = (reelPos + symbolsPerReel[ridx] + p) % reelLength;
673
- this.paddingBottom[ridx].unshift(opts.reels[ridx][bottomPos].clone());
670
+ this.paddingBottom[ridx].unshift(reel[bottomPos].clone());
674
671
  }
675
672
  for (let row = 0; row < symbolsPerReel[ridx]; row++) {
676
- const symbol = opts.reels[ridx][(reelPos + row) % reelLength];
673
+ const symbol = reel[(reelPos + row) % reelLength];
677
674
  if (!symbol) {
678
675
  throw new Error(`Failed to get symbol at pos ${reelPos + row} on reel ${ridx}`);
679
676
  }
@@ -689,8 +686,9 @@ var Board = class {
689
686
  }
690
687
  tumbleBoard(opts) {
691
688
  assert3(this.lastDrawnReelStops.length > 0, "Cannot tumble board before drawing it.");
692
- const reelsAmount = opts.reelsAmount ?? opts.ctx.services.game.getCurrentGameMode().reelsAmount;
693
- const symbolsPerReel = opts.symbolsPerReel ?? opts.ctx.services.game.getCurrentGameMode().symbolsPerReel;
689
+ const gameMode = opts.ctx.services.game.getCurrentGameMode();
690
+ const reelsAmount = opts.reelsAmount ?? gameMode.reelsAmount;
691
+ const symbolsPerReel = opts.symbolsPerReel ?? gameMode.symbolsPerReel;
694
692
  const padSymbols = opts.padSymbols ?? opts.ctx.config.padSymbols;
695
693
  if (opts.startingStops) {
696
694
  assert3(
@@ -736,11 +734,12 @@ var Board = class {
736
734
  const stopBeforePad = previousStop - padSymbols - 1;
737
735
  const symbolsNeeded = symbolsPerReel[ridx] - this.reels[ridx].length;
738
736
  for (let s = 0; s < symbolsNeeded; s++) {
739
- const symbolPos = (stopBeforePad - s + reels[ridx].length) % reels[ridx].length;
740
- let newSymbol = reels[ridx][symbolPos];
737
+ const reel = reels[ridx];
738
+ const symbolPos = (stopBeforePad - s + reel.length) % reel.length;
739
+ let newSymbol = reel[symbolPos];
741
740
  const startStops = opts.startingStops;
742
741
  if (startStops) {
743
- const forcedSym = reels[ridx][startStops?.[ridx]];
742
+ const forcedSym = reel[startStops?.[ridx]];
744
743
  assert3(
745
744
  forcedSym,
746
745
  `Failed to get forced symbol for tumbling. Tried to get symbol for position ${startStops?.[ridx]} on reel ${ridx}.`
@@ -761,8 +760,9 @@ var Board = class {
761
760
  const firstSymbolPos = newFirstSymbolPositions[ridx];
762
761
  if (firstSymbolPos === void 0) continue;
763
762
  for (let p = 1; p <= padSymbols; p++) {
764
- const topPos = (firstSymbolPos - p + reels[ridx].length) % reels[ridx].length;
765
- const padSymbol = reels[ridx][topPos]?.clone();
763
+ const reel = reels[ridx];
764
+ const topPos = (firstSymbolPos - p + reel.length) % reel.length;
765
+ const padSymbol = reel[topPos]?.clone();
766
766
  assert3(padSymbol, "Failed to get new padding symbol for tumbling.");
767
767
  this.paddingTop[ridx].unshift(padSymbol);
768
768
  if (!newPaddingTopSymbols[ridx]) {
@@ -4251,7 +4251,7 @@ var ManywaysWinType = class extends WinType {
4251
4251
  possibleWaysWins.set(baseSymbol.id, symbolList);
4252
4252
  }
4253
4253
  }
4254
- for (const [baseSymbolId, symbolList] of possibleWaysWins.entries()) {
4254
+ for (const [, symbolList] of possibleWaysWins.entries()) {
4255
4255
  const wayLength = this.getWayLength(symbolList);
4256
4256
  let baseSymbol = Object.values(symbolList).flatMap((l) => l.map((s) => s)).find((s) => !this.isWild(s.symbol))?.symbol;
4257
4257
  if (!baseSymbol) baseSymbol = symbolList[Object.keys(symbolList)[0]][0].symbol;