File tree Expand file tree Collapse file tree 2 files changed +19
-5
lines changed
Expand file tree Collapse file tree 2 files changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import {
1212import type { AutopelagoClientAndData } from '../data/slot-data' ;
1313import { targetLocationEvidenceFromJSONSerializable } from '../game/target-location-evidence' ;
1414import { makePlayerToken } from '../utils/make-player-token' ;
15+ import { shuffle } from '../utils/shuffle' ;
1516import { toWeighted } from '../utils/weighted-sampler' ;
1617import { withCleverTimer } from './with-clever-timer' ;
1718import { withGameState } from './with-game-state' ;
@@ -158,13 +159,17 @@ export const GameStore = signalStore(
158159 return message ;
159160 }
160161
162+ // list each player at least once before repeating any.
163+ let currentRandomPlayerBag : Player [ ] = [ ] ;
161164 return message . replaceAll ( '{RANDOM_PLAYER}' , ( ) => {
162- const idx = Math . floor ( Math . random ( ) * randomPlayers . size ) ;
163- const otherPlayer = randomPlayers . get ( idx ) ;
164- if ( ! otherPlayer ) {
165- throw new Error ( 'list.size is inconsistent with list.get(i). this is a bug in immutable.js' ) ;
165+ if ( currentRandomPlayerBag . length === 0 ) {
166+ currentRandomPlayerBag = shuffle ( [ ...randomPlayers ] ) ;
166167 }
167- return otherPlayer . alias ;
168+ const player = currentRandomPlayerBag . pop ( ) ;
169+ if ( player === undefined ) {
170+ throw new Error ( 'pop should have returned something here. this is a programming error' ) ;
171+ }
172+ return player . alias ;
168173 } ) ;
169174 }
170175 function _wrapMessageTemplate < T extends unknown [ ] > ( f : ( ( ...args : T ) => string ) | null ) {
Original file line number Diff line number Diff line change 1+ // Fisher-Yates shuffle
2+ // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#JavaScript_implementation
3+ export function shuffle < T > ( array : T [ ] ) : T [ ] {
4+ for ( let i = array . length - 1 ; i >= 1 ; i -- ) {
5+ const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
6+ [ array [ i ] , array [ j ] ] = [ array [ j ] , array [ i ] ] ;
7+ }
8+ return array ;
9+ }
You can’t perform that action at this time.
0 commit comments