refactor: use centralized ownership in RoomMemoryPairsProvider

Replace manual buildPlayerMetadata implementation with centralized utilities.

Changes:
- Import buildPlayerMetadataUtil and buildPlayerOwnershipFromRoomData
- Replace manual ownership map building (15 lines) with utility call (2 lines)
- Simplify buildPlayerMetadata: 33 lines → 8 lines
- Use shared utilities for consistency

Part of player ownership centralization plan (Phase 4/7).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Thomas Hallock
2025-10-10 10:36:30 -05:00
parent e85f800c3e
commit 6c66bb27b7

View File

@@ -5,6 +5,10 @@ import { useArcadeRedirect } from '@/hooks/useArcadeRedirect'
import { useArcadeSession } from '@/hooks/useArcadeSession'
import { useRoomData } from '@/hooks/useRoomData'
import { useViewerId } from '@/hooks/useViewerId'
import {
buildPlayerMetadata as buildPlayerMetadataUtil,
buildPlayerOwnershipFromRoomData,
} from '@/lib/arcade/player-ownership'
import type { GameMove } from '@/lib/arcade/validation'
import { useGameMode } from '../../../../contexts/GameModeContext'
import { generateGameCards } from '../utils/cardGeneration'
@@ -366,38 +370,14 @@ export function RoomMemoryPairsProvider({ children }: { children: ReactNode }) {
}, [state.pausedGamePhase, state.pausedGameState, hasConfigChanged])
// Helper to build player metadata with correct userId ownership
// This uses roomData.memberPlayers to determine which user owns which player
// Uses centralized ownership utilities
const buildPlayerMetadata = useCallback(
(playerIds: string[]) => {
const playerMetadata: { [playerId: string]: any } = {}
// Build ownership map from room data
const playerOwnership = buildPlayerOwnershipFromRoomData(roomData)
// Build reverse mapping: playerId -> userId from roomData.memberPlayers
const playerOwnership = new Map<string, string>()
if (roomData?.memberPlayers) {
for (const [userId, userPlayers] of Object.entries(roomData.memberPlayers)) {
for (const player of userPlayers) {
playerOwnership.set(player.id, userId)
}
}
}
for (const playerId of playerIds) {
const playerData = players.get(playerId)
if (playerData) {
// Get the actual owner userId from roomData, or use local viewerId as fallback
const ownerUserId = playerOwnership.get(playerId) || viewerId || ''
playerMetadata[playerId] = {
id: playerId,
name: playerData.name,
emoji: playerData.emoji,
userId: ownerUserId, // CORRECT: Use actual owner's userId
color: playerData.color,
}
}
}
return playerMetadata
// Use centralized utility to build metadata
return buildPlayerMetadataUtil(playerIds, playerOwnership, players, viewerId)
},
[players, roomData, viewerId]
)