|
| 1 | +import { useEffect, useState, useMemo } from 'react'; |
| 2 | +import { useShallow } from 'zustand/react/shallow'; |
| 3 | +import { useGameStore } from '../stores'; |
| 4 | +import { |
| 5 | + type AnyGame, |
| 6 | + type GameSummary, |
| 7 | + isNormalFormGame, |
| 8 | + isExtensiveFormGame, |
| 9 | + isMAIDGame, |
| 10 | +} from '../types'; |
| 11 | +import { |
| 12 | + computeZeroSum, |
| 13 | + computeConstantSum, |
| 14 | + computeSymmetric, |
| 15 | + computePerfectInformation, |
| 16 | + computeDeterministic, |
| 17 | +} from '../lib/gameProperties'; |
| 18 | + |
| 19 | +export interface GameProperties { |
| 20 | + twoPlayer: boolean | null; |
| 21 | + zeroSum: boolean | null; |
| 22 | + constantSum: boolean | null; |
| 23 | + symmetric: boolean | null; |
| 24 | + perfectInformation: boolean | null; |
| 25 | + deterministic: boolean | null; |
| 26 | +} |
| 27 | + |
| 28 | +type ConvertibleFormat = 'extensive' | 'normal'; |
| 29 | + |
| 30 | +/** |
| 31 | + * Check if a format is available (either native or via conversion). |
| 32 | + */ |
| 33 | +function isFormatAvailable( |
| 34 | + nativeGame: AnyGame | null, |
| 35 | + summary: GameSummary | null, |
| 36 | + format: ConvertibleFormat |
| 37 | +): boolean { |
| 38 | + if (!nativeGame) return false; |
| 39 | + |
| 40 | + // Check if native format matches |
| 41 | + if (format === 'normal' && isNormalFormGame(nativeGame)) return true; |
| 42 | + if (format === 'extensive' && isExtensiveFormGame(nativeGame)) return true; |
| 43 | + |
| 44 | + // Check if conversion is possible |
| 45 | + const conversionInfo = summary?.conversions?.[format]; |
| 46 | + return conversionInfo?.possible === true; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Get a game in the requested format (native or converted). |
| 51 | + */ |
| 52 | +function getGameInFormat( |
| 53 | + nativeGame: AnyGame | null, |
| 54 | + convertedGames: Map<ConvertibleFormat, AnyGame | null>, |
| 55 | + format: ConvertibleFormat |
| 56 | +): AnyGame | null { |
| 57 | + if (!nativeGame) return null; |
| 58 | + |
| 59 | + // Return native if it matches |
| 60 | + if (format === 'normal' && isNormalFormGame(nativeGame)) return nativeGame; |
| 61 | + if (format === 'extensive' && isExtensiveFormGame(nativeGame)) return nativeGame; |
| 62 | + |
| 63 | + // Return converted |
| 64 | + return convertedGames.get(format) ?? null; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Get player count from any game type. |
| 69 | + */ |
| 70 | +function getPlayerCount(game: AnyGame): number { |
| 71 | + if (isMAIDGame(game)) return game.agents.length; |
| 72 | + return game.players.length; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Hook that computes game properties, fetching conversions as needed. |
| 77 | + */ |
| 78 | +export function useGameProperties(): GameProperties { |
| 79 | + const { currentGame, currentGameId, games, fetchConverted } = useGameStore( |
| 80 | + useShallow((s) => ({ |
| 81 | + currentGame: s.currentGame, |
| 82 | + currentGameId: s.currentGameId, |
| 83 | + games: s.games, |
| 84 | + fetchConverted: s.fetchConverted, |
| 85 | + })) |
| 86 | + ); |
| 87 | + |
| 88 | + // Get the summary for conversion info |
| 89 | + const summary = useMemo( |
| 90 | + () => games.find((g) => g.id === currentGameId) ?? null, |
| 91 | + [games, currentGameId] |
| 92 | + ); |
| 93 | + |
| 94 | + // Track converted games |
| 95 | + const [convertedGames, setConvertedGames] = useState<Map<ConvertibleFormat, AnyGame | null>>( |
| 96 | + new Map() |
| 97 | + ); |
| 98 | + |
| 99 | + // Determine which formats we need |
| 100 | + const needsNormal = useMemo( |
| 101 | + () => |
| 102 | + isFormatAvailable(currentGame, summary, 'normal') && |
| 103 | + currentGame !== null && |
| 104 | + !isNormalFormGame(currentGame), |
| 105 | + [currentGame, summary] |
| 106 | + ); |
| 107 | + |
| 108 | + const needsExtensive = useMemo( |
| 109 | + () => |
| 110 | + isFormatAvailable(currentGame, summary, 'extensive') && |
| 111 | + currentGame !== null && |
| 112 | + !isExtensiveFormGame(currentGame), |
| 113 | + [currentGame, summary] |
| 114 | + ); |
| 115 | + |
| 116 | + // Fetch conversions when needed |
| 117 | + useEffect(() => { |
| 118 | + if (!currentGameId) { |
| 119 | + setConvertedGames(new Map()); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const fetchNeeded = async () => { |
| 124 | + const newConverted = new Map<ConvertibleFormat, AnyGame | null>(); |
| 125 | + |
| 126 | + if (needsNormal) { |
| 127 | + const normalGame = await fetchConverted(currentGameId, 'normal'); |
| 128 | + newConverted.set('normal', normalGame); |
| 129 | + } |
| 130 | + |
| 131 | + if (needsExtensive) { |
| 132 | + const extensiveGame = await fetchConverted(currentGameId, 'extensive'); |
| 133 | + newConverted.set('extensive', extensiveGame); |
| 134 | + } |
| 135 | + |
| 136 | + setConvertedGames(newConverted); |
| 137 | + }; |
| 138 | + |
| 139 | + fetchNeeded(); |
| 140 | + }, [currentGameId, needsNormal, needsExtensive, fetchConverted]); |
| 141 | + |
| 142 | + // Compute properties |
| 143 | + return useMemo(() => { |
| 144 | + const nullProps: GameProperties = { |
| 145 | + twoPlayer: null, |
| 146 | + zeroSum: null, |
| 147 | + constantSum: null, |
| 148 | + symmetric: null, |
| 149 | + perfectInformation: null, |
| 150 | + deterministic: null, |
| 151 | + }; |
| 152 | + |
| 153 | + if (!currentGame) return nullProps; |
| 154 | + |
| 155 | + // Player count - available from any game |
| 156 | + const twoPlayer = getPlayerCount(currentGame) === 2; |
| 157 | + |
| 158 | + // Get games in needed formats |
| 159 | + const normalGame = getGameInFormat(currentGame, convertedGames, 'normal'); |
| 160 | + const extensiveGame = getGameInFormat(currentGame, convertedGames, 'extensive'); |
| 161 | + |
| 162 | + // Compute properties from whichever format is available |
| 163 | + // For zero-sum and constant-sum, prefer NFG if available (simpler), else EFG |
| 164 | + const zeroSum = normalGame |
| 165 | + ? computeZeroSum(normalGame) |
| 166 | + : extensiveGame |
| 167 | + ? computeZeroSum(extensiveGame) |
| 168 | + : null; |
| 169 | + |
| 170 | + const constantSum = normalGame |
| 171 | + ? computeConstantSum(normalGame) |
| 172 | + : extensiveGame |
| 173 | + ? computeConstantSum(extensiveGame) |
| 174 | + : null; |
| 175 | + |
| 176 | + // Symmetric requires NFG |
| 177 | + const symmetric = normalGame ? computeSymmetric(normalGame) : null; |
| 178 | + |
| 179 | + // Perfect information and deterministic require EFG |
| 180 | + const perfectInformation = extensiveGame ? computePerfectInformation(extensiveGame) : null; |
| 181 | + const deterministic = extensiveGame ? computeDeterministic(extensiveGame) : null; |
| 182 | + |
| 183 | + return { |
| 184 | + twoPlayer, |
| 185 | + zeroSum, |
| 186 | + constantSum, |
| 187 | + symmetric, |
| 188 | + perfectInformation, |
| 189 | + deterministic, |
| 190 | + }; |
| 191 | + }, [currentGame, convertedGames]); |
| 192 | +} |
0 commit comments