Compare commits

...

6 Commits

Author SHA1 Message Date
semantic-release-bot
41d3e08fc6 chore(release): 2.19.1 [skip ci]
## [2.19.1](https://github.com/antialias/soroban-abacus-flashcards/compare/v2.19.0...v2.19.1) (2025-10-10)

### Bug Fixes

* prevent SSR hydration error in /arcade/room page ([cd38f42](cd38f42e9c))

### Code Refactoring

* migrate RoomMemoryPairsProvider to centralized ownership utilities ([10f4288](10f42887a3))
2025-10-10 14:09:22 +00:00
Thomas Hallock
cd38f42e9c fix: prevent SSR hydration error in /arcade/room page
Add mounted check to prevent QueryClientProvider errors during SSR.
React Query hooks cannot run during server-side rendering, so we
gate the hook calls until after the component has mounted on the client.

This fixes the "No QueryClient set" error that was appearing when
navigating to /arcade/room.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-10 09:08:26 -05:00
Thomas Hallock
10f42887a3 refactor: migrate RoomMemoryPairsProvider to centralized ownership utilities
Replace custom buildPlayerMetadata implementation with centralized
player-ownership utilities. This eliminates 33 lines of duplicate code
and ensures consistent player ownership logic across the codebase.

Changes:
- Replace inline playerOwnership map building with buildPlayerOwnershipFromRoomData()
- Use buildPlayerMetadata() from centralized module
- Remove unused useArcadeRedirect import
- Rename local callback to buildPlayerMetadataCallback for clarity

Benefits:
- Single source of truth for player ownership logic
- Reduced code duplication (33 lines eliminated)
- Consistent with session-manager and player-manager implementations
- Better maintainability and testability

Part of Phase 4 of the player ownership centralization plan.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-10 09:00:16 -05:00
semantic-release-bot
c7a660c153 chore(release): 2.19.0 [skip ci]
## [2.19.0](https://github.com/antialias/soroban-abacus-flashcards/compare/v2.18.0...v2.19.0) (2025-10-10)

### Features

* add player ownership helper to player-manager API ([6b59a82](6b59a828aa))

### Code Refactoring

* migrate session-manager.ts to use centralized player ownership ([d3b7cc2](d3b7cc25ca))
2025-10-10 13:57:10 +00:00
Thomas Hallock
6b59a828aa feat: add player ownership helper to player-manager API
Added getPlayerOwnershipMap() as a convenience re-export of the
centralized buildPlayerOwnershipMap() utility.

This allows modules to access player ownership data through the
player-manager API, which is the natural home for player-related
server-side operations.

New function:
- getPlayerOwnershipMap(roomId?) - Returns PlayerOwnershipMap

Benefits:
- Consistent API: player data + ownership through same module
- Discovery: developers naturally look in player-manager for player ops
- Flexibility: can add room-filtering logic in future if needed
- Documentation: JSDoc example shows usage pattern

Example usage:
  const ownership = await getPlayerOwnershipMap()
  const isOwned = ownership[playerId] === userId

This is phase 3 of the player ownership centralization plan.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-10 08:56:15 -05:00
Thomas Hallock
d3b7cc25ca refactor: migrate session-manager.ts to use centralized player ownership
Replaced inline player ownership logic with centralized utilities from
player-ownership.ts module.

Changes:
- Import buildPlayerOwnershipMap() and getUserIdFromGuestId() from
  player-ownership module
- Remove duplicate getUserIdFromGuestId() function (now exported from module)
- Replace inline DB query with buildPlayerOwnershipMap() call
- Use PlayerOwnershipMap type for consistency

Benefits:
- Eliminates 10 lines of duplicated code
- Single source of truth for ownership logic
- Consistent with validator and other components
- Better type safety with shared types

Before: Lines 232-238 built ownership map inline
After: Line 226 calls centralized utility

This is phase 2 of the player ownership centralization plan.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-10 08:55:24 -05:00
6 changed files with 90 additions and 56 deletions

View File

@@ -1,3 +1,27 @@
## [2.19.1](https://github.com/antialias/soroban-abacus-flashcards/compare/v2.19.0...v2.19.1) (2025-10-10)
### Bug Fixes
* prevent SSR hydration error in /arcade/room page ([cd38f42](https://github.com/antialias/soroban-abacus-flashcards/commit/cd38f42e9c90ff8bab28f7a677c1f9307406d013))
### Code Refactoring
* migrate RoomMemoryPairsProvider to centralized ownership utilities ([10f4288](https://github.com/antialias/soroban-abacus-flashcards/commit/10f42887a323a755e2741ee1b927a0bc16eb69fc))
## [2.19.0](https://github.com/antialias/soroban-abacus-flashcards/compare/v2.18.0...v2.19.0) (2025-10-10)
### Features
* add player ownership helper to player-manager API ([6b59a82](https://github.com/antialias/soroban-abacus-flashcards/commit/6b59a828aabe687abb797c1f1d69d8a4d0abe49b))
### Code Refactoring
* migrate session-manager.ts to use centralized player ownership ([d3b7cc2](https://github.com/antialias/soroban-abacus-flashcards/commit/d3b7cc25caee7e005de046792202aa474edbc90f))
## [2.18.0](https://github.com/antialias/soroban-abacus-flashcards/compare/v2.17.3...v2.18.0) (2025-10-10)

View File

@@ -1,10 +1,13 @@
'use client'
import { type ReactNode, useCallback, useEffect, useMemo } from 'react'
import { useArcadeRedirect } from '@/hooks/useArcadeRedirect'
import { useArcadeSession } from '@/hooks/useArcadeSession'
import { useRoomData } from '@/hooks/useRoomData'
import { useViewerId } from '@/hooks/useViewerId'
import {
buildPlayerMetadata,
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 +369,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
const buildPlayerMetadata = useCallback(
// Uses centralized utilities from player-ownership module
const buildPlayerMetadataCallback = useCallback(
(playerIds: string[]) => {
const playerMetadata: { [playerId: string]: any } = {}
// Build ownership map from roomData
const ownershipMap = 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
// Build player metadata with correct ownership
return buildPlayerMetadata(playerIds, ownershipMap, players, viewerId)
},
[players, roomData, viewerId]
)
@@ -412,7 +391,7 @@ export function RoomMemoryPairsProvider({ children }: { children: ReactNode }) {
// Capture player metadata from local players map
// This ensures all room members can display player info even if they don't own the players
const playerMetadata = buildPlayerMetadata(activePlayers)
const playerMetadata = buildPlayerMetadataCallback(activePlayers)
// Use current session state configuration (no local state!)
const cards = generateGameCards(state.gameType, state.difficulty)
@@ -427,7 +406,7 @@ export function RoomMemoryPairsProvider({ children }: { children: ReactNode }) {
playerMetadata,
},
})
}, [state.gameType, state.difficulty, activePlayers, buildPlayerMetadata, sendMove])
}, [state.gameType, state.difficulty, activePlayers, buildPlayerMetadataCallback, sendMove])
const flipCard = useCallback(
(cardId: string) => {
@@ -464,7 +443,7 @@ export function RoomMemoryPairsProvider({ children }: { children: ReactNode }) {
}
// Capture player metadata with correct userId ownership
const playerMetadata = buildPlayerMetadata(activePlayers)
const playerMetadata = buildPlayerMetadataCallback(activePlayers)
// Use current session state configuration (no local state!)
const cards = generateGameCards(state.gameType, state.difficulty)
@@ -479,7 +458,7 @@ export function RoomMemoryPairsProvider({ children }: { children: ReactNode }) {
playerMetadata,
},
})
}, [state.gameType, state.difficulty, activePlayers, buildPlayerMetadata, sendMove])
}, [state.gameType, state.difficulty, activePlayers, buildPlayerMetadataCallback, sendMove])
const setGameType = useCallback(
(gameType: typeof state.gameType) => {

View File

@@ -1,5 +1,6 @@
'use client'
import { useEffect, useState } from 'react'
import { useRoomData } from '@/hooks/useRoomData'
import { MemoryPairsGame } from '../matching/components/MemoryPairsGame'
import { RoomMemoryPairsProvider } from '../matching/context/RoomMemoryPairsProvider'
@@ -13,8 +14,31 @@ import { RoomMemoryPairsProvider } from '../matching/context/RoomMemoryPairsProv
* - useArcadeRedirect on /arcade page handles redirecting to active sessions
*/
export default function RoomPage() {
const [mounted, setMounted] = useState(false)
const { roomData, isLoading } = useRoomData()
// Prevent SSR hydration mismatch
useEffect(() => {
setMounted(true)
}, [])
if (!mounted) {
return (
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
height: '100vh',
fontSize: '18px',
color: '#666',
}}
>
Loading...
</div>
)
}
// Show loading state
if (isLoading) {
return (

View File

@@ -6,6 +6,7 @@
import { and, eq } from 'drizzle-orm'
import { db, schema } from '@/db'
import type { Player } from '@/db/schema/players'
import { type PlayerOwnershipMap, buildPlayerOwnershipMap } from './player-ownership'
/**
* Get all players for a user (regardless of isActive status)
@@ -128,3 +129,21 @@ export async function getPlayers(playerIds: string[]): Promise<Player[]> {
return players
}
/**
* Get player ownership map for a room
*
* Convenience re-export of the centralized player ownership utility.
* This allows other modules to get player ownership data through
* the player-manager API.
*
* @param roomId - Optional room ID (currently unused by underlying utility)
* @returns Promise resolving to playerOwnership map (playerId -> userId)
*
* @example
* const ownership = await getPlayerOwnershipMap()
* const isOwned = ownership[playerId] === userId
*/
export async function getPlayerOwnershipMap(roomId?: string): Promise<PlayerOwnershipMap> {
return buildPlayerOwnershipMap(roomId)
}

View File

@@ -5,6 +5,11 @@
import { eq } from 'drizzle-orm'
import { db, schema } from '@/db'
import {
buildPlayerOwnershipMap,
getUserIdFromGuestId,
type PlayerOwnershipMap,
} from './player-ownership'
import { type GameMove, type GameName, getValidator } from './validation'
export interface CreateSessionOptions {
@@ -25,18 +30,6 @@ export interface SessionUpdateResult {
const TTL_HOURS = 24
/**
* Helper: Get database user ID from guest ID
* The API uses guestId (from cookies) but database FKs use the internal user.id
*/
async function getUserIdFromGuestId(guestId: string): Promise<string | undefined> {
const user = await db.query.users.findFirst({
where: eq(schema.users.guestId, guestId),
columns: { id: true },
})
return user?.id
}
/**
* Get arcade session by room ID (for room-based multiplayer games)
* Returns the shared session for all room members
@@ -215,7 +208,7 @@ export async function applyGameMove(
})
// Fetch player ownership for authorization checks (room-based games)
let playerOwnership: Record<string, string> | undefined
let playerOwnership: PlayerOwnershipMap | undefined
let internalUserId: string | undefined
if (session.roomId) {
try {
@@ -229,13 +222,8 @@ export async function applyGameMove(
}
}
const players = await db.query.players.findMany({
columns: {
id: true,
userId: true,
},
})
playerOwnership = Object.fromEntries(players.map((p) => [p.id, p.userId]))
// Use centralized player ownership utility
playerOwnership = await buildPlayerOwnershipMap(session.roomId)
console.log('[SessionManager] Player ownership map:', playerOwnership)
console.log('[SessionManager] Internal userId for authorization:', internalUserId)
} catch (error) {

View File

@@ -1,6 +1,6 @@
{
"name": "soroban-monorepo",
"version": "2.18.0",
"version": "2.19.1",
"private": true,
"description": "Beautiful Soroban Flashcard Generator - Monorepo",
"workspaces": [