@@ -53,16 +53,42 @@ export function shuffleArray<T>(array: T[]): T[] {
5353}
5454
5555// Select images for a game (50/50 split, 12 total by default)
56- export function selectGameImages ( totalRounds : number = 12 ) : GameImage [ ] {
56+ // excludeIds: optional array of image IDs to exclude (for no-repeat functionality)
57+ export function selectGameImages (
58+ totalRounds : number = 12 ,
59+ excludeIds : string [ ] = [ ] ,
60+ ) : GameImage [ ] {
5761 const allImages = getAvailableImages ( ) ;
58- const realImages = shuffleArray (
59- allImages . filter ( ( img ) => img . type === "real" ) ,
62+
63+ // Filter out excluded images
64+ const availableImages = allImages . filter (
65+ ( img ) => ! excludeIds . includes ( img . id ) ,
6066 ) ;
61- const aiImages = shuffleArray ( allImages . filter ( ( img ) => img . type === "ai" ) ) ;
67+
68+ // Separate by type
69+ let realImages = availableImages . filter ( ( img ) => img . type === "real" ) ;
70+ let aiImages = availableImages . filter ( ( img ) => img . type === "ai" ) ;
6271
6372 const halfRounds = Math . floor ( totalRounds / 2 ) ;
64- const selectedReal = realImages . slice ( 0 , halfRounds ) ;
65- const selectedAI = aiImages . slice ( 0 , totalRounds - halfRounds ) ;
73+
74+ // If we don't have enough unseen images, reset and use all images
75+ if (
76+ realImages . length < halfRounds ||
77+ aiImages . length < totalRounds - halfRounds
78+ ) {
79+ console . log (
80+ "Not enough unseen images, resetting to use all available images" ,
81+ ) ;
82+ realImages = allImages . filter ( ( img ) => img . type === "real" ) ;
83+ aiImages = allImages . filter ( ( img ) => img . type === "ai" ) ;
84+ }
85+
86+ // Shuffle and select
87+ const shuffledReal = shuffleArray ( realImages ) ;
88+ const shuffledAI = shuffleArray ( aiImages ) ;
89+
90+ const selectedReal = shuffledReal . slice ( 0 , halfRounds ) ;
91+ const selectedAI = shuffledAI . slice ( 0 , totalRounds - halfRounds ) ;
6692
6793 // Combine and shuffle for random order
6894 return shuffleArray ( [ ...selectedReal , ...selectedAI ] ) ;
0 commit comments