|
|
|
|
@@ -4,7 +4,7 @@ import { type ReactNode, useCallback, useEffect, useMemo, useReducer } from 'rea
|
|
|
|
|
import { useRouter } from 'next/navigation'
|
|
|
|
|
import { useArcadeRedirect } from '@/hooks/useArcadeRedirect'
|
|
|
|
|
import { useViewerId } from '@/hooks/useViewerId'
|
|
|
|
|
import { useGameMode } from '../../../../contexts/GameModeContext'
|
|
|
|
|
import { useUserPlayers } from '@/hooks/useUserPlayers'
|
|
|
|
|
import { generateGameCards } from '../utils/cardGeneration'
|
|
|
|
|
import { validateMatch } from '../utils/matchValidation'
|
|
|
|
|
import { MemoryPairsContext } from './MemoryPairsContext'
|
|
|
|
|
@@ -283,16 +283,35 @@ function localMemoryPairsReducer(state: MemoryPairsState, action: LocalAction):
|
|
|
|
|
export function LocalMemoryPairsProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
const { data: viewerId } = useViewerId()
|
|
|
|
|
const { activePlayerCount, activePlayers: activePlayerIds, players } = useGameMode()
|
|
|
|
|
|
|
|
|
|
// LOCAL-ONLY: Get only the current user's players (no room members)
|
|
|
|
|
const { data: userPlayers = [] } = useUserPlayers()
|
|
|
|
|
|
|
|
|
|
// Use arcade redirect to determine button visibility
|
|
|
|
|
const { canModifyPlayers } = useArcadeRedirect({ currentGame: 'matching' })
|
|
|
|
|
|
|
|
|
|
// Get active player IDs as array
|
|
|
|
|
const activePlayers = Array.from(activePlayerIds)
|
|
|
|
|
// Build players map from current user's players only
|
|
|
|
|
const players = useMemo(() => {
|
|
|
|
|
const map = new Map()
|
|
|
|
|
userPlayers.forEach((player) => {
|
|
|
|
|
map.set(player.id, {
|
|
|
|
|
id: player.id,
|
|
|
|
|
name: player.name,
|
|
|
|
|
emoji: player.emoji,
|
|
|
|
|
color: player.color,
|
|
|
|
|
isLocal: true,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
return map
|
|
|
|
|
}, [userPlayers])
|
|
|
|
|
|
|
|
|
|
// Get active player IDs from current user's players only
|
|
|
|
|
const activePlayers = useMemo(() => {
|
|
|
|
|
return userPlayers.filter((p) => p.isActive).map((p) => p.id)
|
|
|
|
|
}, [userPlayers])
|
|
|
|
|
|
|
|
|
|
// Derive game mode from active player count
|
|
|
|
|
const gameMode = activePlayerCount > 1 ? 'multiplayer' : 'single'
|
|
|
|
|
const gameMode = activePlayers.length > 1 ? 'multiplayer' : 'single'
|
|
|
|
|
|
|
|
|
|
// Pure client-side state with useReducer
|
|
|
|
|
const [state, dispatch] = useReducer(localMemoryPairsReducer, initialState)
|
|
|
|
|
|