Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | 'use client' import { createContext, type ReactNode, useCallback, useContext, useEffect, useMemo } from 'react' import { useGameMode } from '@/contexts/GameModeContext' import { TEAM_MOVE, useArcadeSession, useUpdateGameConfig, useUserId } from '@/lib/arcade/game-sdk' import type { AmbushContext, Color, HarmonyType, RelationKind, RithmomachiaConfig, RithmomachiaState, } from './types' import { useToast } from '@/components/common/ToastContext' import { parseError, shouldShowToast, getToastType, getMoveActionName, type EnhancedError, type RetryState, } from '@/lib/arcade/error-handling' /** * Context value for Rithmomachia game. */ export type RithmomachiaRosterStatus = | { status: 'ok'; activePlayerCount: number; localPlayerCount: number } | { status: 'tooFew' activePlayerCount: number localPlayerCount: number missingWhite: boolean missingBlack: boolean } | { status: 'noLocalControl' activePlayerCount: number localPlayerCount: number } interface RithmomachiaContextValue { // State state: RithmomachiaState lastError: string | null retryState: RetryState // Player info viewerId: string | null playerColor: Color | null isMyTurn: boolean rosterStatus: RithmomachiaRosterStatus localActivePlayerIds: string[] whitePlayerId: string | null blackPlayerId: string | null localTurnPlayerId: string | null isSpectating: boolean localPlayerColor: Color | null // Game actions startGame: () => void makeMove: ( from: string, to: string, pieceId: string, pyramidFace?: number, capture?: CaptureData, ambush?: AmbushContext ) => void declareHarmony: ( pieceIds: string[], harmonyType: HarmonyType, params: Record<string, string> ) => void resign: () => void offerDraw: () => void acceptDraw: () => void claimRepetition: () => void claimFiftyMove: () => void // Config actions setConfig: (field: keyof RithmomachiaConfig, value: any) => void // Player assignment actions assignWhitePlayer: (playerId: string | null) => void assignBlackPlayer: (playerId: string | null) => void swapSides: () => void // Game control actions resetGame: () => void goToSetup: () => void exitSession: () => void // Error handling clearError: () => void } interface CaptureData { relation: RelationKind targetPieceId: string helperPieceId?: string } const RithmomachiaContext = createContext<RithmomachiaContextValue | null>(null) /** * Hook to access Rithmomachia game context. */ export function useRithmomachia(): RithmomachiaContextValue { const context = useContext(RithmomachiaContext) if (!context) { throw new Error('useRithmomachia must be used within RithmomachiaProvider') } return context } /** * Provider for Rithmomachia game state and actions. */ export function RithmomachiaProvider({ children }: { children: ReactNode }) { const { data: viewerId } = useUserId() const { activePlayers: activePlayerIds, players, roomData } = useGameMode() const { mutate: updateGameConfig } = useUpdateGameConfig() const { showToast } = useToast() const activePlayerList = useMemo(() => Array.from(activePlayerIds), [activePlayerIds]) const localActivePlayerIds = useMemo( () => activePlayerList.filter((id) => { const player = players.get(id) return player?.isLocal !== false }), [activePlayerList, players] ) // Merge saved config from room data const mergedInitialState = useMemo(() => { const gameConfig = roomData?.gameConfig as Record<string, unknown> | null const savedConfig = gameConfig?.rithmomachia as Partial<RithmomachiaConfig> | undefined // Use validator to create initial state with config const config: RithmomachiaConfig = { pointWinEnabled: savedConfig?.pointWinEnabled ?? false, pointWinThreshold: savedConfig?.pointWinThreshold ?? 30, repetitionRule: savedConfig?.repetitionRule ?? true, fiftyMoveRule: savedConfig?.fiftyMoveRule ?? true, allowAnySetOnRecheck: savedConfig?.allowAnySetOnRecheck ?? true, timeControlMs: savedConfig?.timeControlMs ?? null, whitePlayerId: savedConfig?.whitePlayerId ?? null, blackPlayerId: savedConfig?.blackPlayerId ?? null, } // Import validator dynamically to get initial state return { ...require('./Validator').rithmomachiaValidator.getInitialState(config), } }, [roomData?.gameConfig]) // Use arcade session hook const { state, sendMove, lastError, clearError, retryState } = useArcadeSession<RithmomachiaState>({ userId: viewerId || '', roomId: roomData?.id, initialState: mergedInitialState, applyMove: (state) => state, // No optimistic updates for v1 - rely on server validation }) // Get player assignments from config (with fallback to auto-assignment) const whitePlayerId = useMemo(() => { const configWhite = state.whitePlayerId // If explicitly set in config and still valid, use it if (configWhite !== undefined && configWhite !== null) { return activePlayerList.includes(configWhite) ? configWhite : null } // Fallback to auto-assignment: first active player return activePlayerList[0] ?? null }, [state.whitePlayerId, activePlayerList]) const blackPlayerId = useMemo(() => { const configBlack = state.blackPlayerId // If explicitly set in config and still valid, use it if (configBlack !== undefined && configBlack !== null) { return activePlayerList.includes(configBlack) ? configBlack : null } // Fallback to auto-assignment: second active player return activePlayerList[1] ?? null }, [state.blackPlayerId, activePlayerList]) // Compute roster status based on white/black assignments (not player count) const rosterStatus = useMemo<RithmomachiaRosterStatus>(() => { const activeCount = activePlayerList.length const localCount = localActivePlayerIds.length // Check if white and black are assigned const hasWhitePlayer = whitePlayerId !== null const hasBlackPlayer = blackPlayerId !== null // Status is 'tooFew' only if white or black is missing if (!hasWhitePlayer || !hasBlackPlayer) { return { status: 'tooFew', activePlayerCount: activeCount, localPlayerCount: localCount, missingWhite: !hasWhitePlayer, missingBlack: !hasBlackPlayer, } } // Check if current user has control over either white or black const localControlsWhite = localActivePlayerIds.includes(whitePlayerId) const localControlsBlack = localActivePlayerIds.includes(blackPlayerId) if (!localControlsWhite && !localControlsBlack) { return { status: 'noLocalControl', // Observer mode activePlayerCount: activeCount, localPlayerCount: localCount, } } // All good - white and black assigned, and user controls at least one return { status: 'ok', activePlayerCount: activeCount, localPlayerCount: localCount, } }, [activePlayerList.length, localActivePlayerIds, whitePlayerId, blackPlayerId]) const localTurnPlayerId = useMemo(() => { const currentId = state.turn === 'W' ? whitePlayerId : blackPlayerId if (!currentId) return null return localActivePlayerIds.includes(currentId) ? currentId : null }, [state.turn, whitePlayerId, blackPlayerId, localActivePlayerIds]) const playerColor = useMemo((): Color | null => { if (localTurnPlayerId) { return state.turn } if (localActivePlayerIds.length === 1) { const soleLocalId = localActivePlayerIds[0] if (soleLocalId === whitePlayerId) return 'W' if (soleLocalId === blackPlayerId) return 'B' } return null }, [localTurnPlayerId, localActivePlayerIds, whitePlayerId, blackPlayerId, state.turn]) // Check if it's my turn const isMyTurn = useMemo(() => { if (rosterStatus.status !== 'ok') return false return localTurnPlayerId !== null }, [rosterStatus.status, localTurnPlayerId]) // Action: Start game const startGame = useCallback(() => { // Block observers from starting game const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return if (!viewerId || !localTurnPlayerId) return sendMove({ type: 'START_GAME', playerId: localTurnPlayerId, userId: viewerId, data: { playerColor: playerColor || 'W', activePlayers: activePlayerList, }, }) }, [ sendMove, viewerId, localTurnPlayerId, playerColor, activePlayerList, whitePlayerId, blackPlayerId, localActivePlayerIds, ]) // Action: Make a move const makeMove = useCallback( ( from: string, to: string, pieceId: string, pyramidFace?: number, capture?: CaptureData, ambush?: AmbushContext ) => { // Block observers from making moves const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return if (!viewerId || !localTurnPlayerId) return sendMove({ type: 'MOVE', playerId: localTurnPlayerId, userId: viewerId, data: { from, to, pieceId, pyramidFaceUsed: pyramidFace ?? null, capture: capture ? { relation: capture.relation, targetPieceId: capture.targetPieceId, helperPieceId: capture.helperPieceId, } : undefined, ambush, }, }) }, [sendMove, viewerId, localTurnPlayerId, whitePlayerId, blackPlayerId, localActivePlayerIds] ) // Action: Declare harmony const declareHarmony = useCallback( (pieceIds: string[], harmonyType: HarmonyType, params: Record<string, string>) => { // Block observers from declaring harmony const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return if (!viewerId || !localTurnPlayerId) return sendMove({ type: 'DECLARE_HARMONY', playerId: localTurnPlayerId, userId: viewerId, data: { pieceIds, harmonyType, params, }, }) }, [sendMove, viewerId, localTurnPlayerId, whitePlayerId, blackPlayerId, localActivePlayerIds] ) // Action: Resign const resign = useCallback(() => { // Block observers from resigning const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return if (!viewerId || !localTurnPlayerId) return sendMove({ type: 'RESIGN', playerId: localTurnPlayerId, userId: viewerId, data: {}, }) }, [sendMove, viewerId, localTurnPlayerId, whitePlayerId, blackPlayerId, localActivePlayerIds]) // Action: Offer draw const offerDraw = useCallback(() => { // Block observers from offering draw const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return if (!viewerId || !localTurnPlayerId) return sendMove({ type: 'OFFER_DRAW', playerId: localTurnPlayerId, userId: viewerId, data: {}, }) }, [sendMove, viewerId, localTurnPlayerId, whitePlayerId, blackPlayerId, localActivePlayerIds]) // Action: Accept draw const acceptDraw = useCallback(() => { // Block observers from accepting draw const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return if (!viewerId || !localTurnPlayerId) return sendMove({ type: 'ACCEPT_DRAW', playerId: localTurnPlayerId, userId: viewerId, data: {}, }) }, [sendMove, viewerId, localTurnPlayerId, whitePlayerId, blackPlayerId, localActivePlayerIds]) // Action: Claim repetition const claimRepetition = useCallback(() => { // Block observers from claiming repetition const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return if (!viewerId || !localTurnPlayerId) return sendMove({ type: 'CLAIM_REPETITION', playerId: localTurnPlayerId, userId: viewerId, data: {}, }) }, [sendMove, viewerId, localTurnPlayerId, whitePlayerId, blackPlayerId, localActivePlayerIds]) // Action: Claim fifty-move rule const claimFiftyMove = useCallback(() => { // Block observers from claiming fifty-move const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return if (!viewerId || !localTurnPlayerId) return sendMove({ type: 'CLAIM_FIFTY_MOVE', playerId: localTurnPlayerId, userId: viewerId, data: {}, }) }, [sendMove, viewerId, localTurnPlayerId, whitePlayerId, blackPlayerId, localActivePlayerIds]) // Action: Set config const setConfig = useCallback( (field: keyof RithmomachiaConfig, value: any) => { // During gameplay, restrict config changes if (state.gamePhase === 'playing') { // Allow host to change player assignments at any time const isHost = roomData?.members?.some((m) => m.userId === viewerId && m.isCreator) const isPlayerAssignment = field === 'whitePlayerId' || field === 'blackPlayerId' if (isPlayerAssignment && isHost) { // Host can always reassign players } else { // Other config changes require being an active player const localColor = whitePlayerId && localActivePlayerIds.includes(whitePlayerId) ? 'W' : blackPlayerId && localActivePlayerIds.includes(blackPlayerId) ? 'B' : null if (!localColor) return } } // Send move to update state immediately sendMove({ type: 'SET_CONFIG', playerId: TEAM_MOVE, userId: viewerId || '', data: { field, value }, }) // Persist to database (room mode only) if (roomData?.id) { const currentGameConfig = (roomData.gameConfig as Record<string, any>) || {} const currentConfig = (currentGameConfig.rithmomachia as Record<string, any>) || {} updateGameConfig( { roomId: roomData.id, gameConfig: { ...currentGameConfig, rithmomachia: { ...currentConfig, [field]: value, }, }, }, { onError: (error) => { console.error('[Rithmomachia] Failed to update game config:', error) // Surface 403 errors specifically if (error.message.includes('Only the host can change')) { console.warn('[Rithmomachia] 403 Forbidden: Only host can change room settings') // The error will be visible in console - in the future, we could add toast notifications } }, } ) } }, [ viewerId, sendMove, roomData, updateGameConfig, state.gamePhase, whitePlayerId, blackPlayerId, localActivePlayerIds, ] ) // Action: Reset game (start new game with same config) const resetGame = useCallback(() => { if (!viewerId) return sendMove({ type: 'RESET_GAME', playerId: TEAM_MOVE, userId: viewerId, data: {}, }) }, [sendMove, viewerId]) // Action: Go to setup (return to setup phase) const goToSetup = useCallback(() => { if (!viewerId) return sendMove({ type: 'GO_TO_SETUP', playerId: TEAM_MOVE, userId: viewerId, data: {}, }) }, [sendMove, viewerId]) // Action: Exit session (no-op for now, handled by PageWithNav) const exitSession = useCallback(() => { // PageWithNav handles the actual navigation // This is here for API compatibility }, []) // Action: Assign white player const assignWhitePlayer = useCallback( (playerId: string | null) => { setConfig('whitePlayerId', playerId) }, [setConfig] ) // Action: Assign black player const assignBlackPlayer = useCallback( (playerId: string | null) => { setConfig('blackPlayerId', playerId) }, [setConfig] ) // Action: Swap white and black assignments const swapSides = useCallback(() => { const currentWhite = whitePlayerId const currentBlack = blackPlayerId setConfig('whitePlayerId', currentBlack) setConfig('blackPlayerId', currentWhite) }, [whitePlayerId, blackPlayerId, setConfig]) // Observer detection const isSpectating = useMemo(() => { return rosterStatus.status === 'noLocalControl' }, [rosterStatus.status]) const localPlayerColor = useMemo<Color | null>(() => { if (!whitePlayerId || !blackPlayerId) return null if (localActivePlayerIds.includes(whitePlayerId)) return 'W' if (localActivePlayerIds.includes(blackPlayerId)) return 'B' return null }, [localActivePlayerIds, whitePlayerId, blackPlayerId]) // Auto-assign players when they join and a color is missing useEffect(() => { // Only auto-assign if we have active players if (activePlayerList.length === 0) return // Check if we're missing white or black const missingWhite = !whitePlayerId const missingBlack = !blackPlayerId // Only auto-assign if at least one color is missing if (!missingWhite && !missingBlack) return if (missingWhite && missingBlack) { // Both missing - auto-assign first two players if (activePlayerList.length >= 2) { // Assign both at once to avoid double render setConfig('whitePlayerId', activePlayerList[0]) // Use setTimeout to batch the second assignment setTimeout(() => setConfig('blackPlayerId', activePlayerList[1]), 0) } else if (activePlayerList.length === 1) { // Only one player - assign to white by default setConfig('whitePlayerId', activePlayerList[0]) } return } // One color is missing - find an unassigned player const assignedPlayers = [whitePlayerId, blackPlayerId].filter(Boolean) as string[] const unassignedPlayer = activePlayerList.find((id) => !assignedPlayers.includes(id)) if (unassignedPlayer) { if (missingWhite) { setConfig('whitePlayerId', unassignedPlayer) } else { setConfig('blackPlayerId', unassignedPlayer) } } }, [activePlayerList, whitePlayerId, blackPlayerId]) // Note: setConfig is intentionally NOT in dependencies to avoid infinite loop // setConfig is stable (defined with useCallback) so this is safe // Toast notifications for errors useEffect(() => { if (!lastError) return // Parse the error to get enhanced information const enhancedError: EnhancedError = parseError( lastError, retryState.move ?? undefined, retryState.retryCount ) // Show toast if appropriate if (shouldShowToast(enhancedError)) { const toastType = getToastType(enhancedError.severity) const actionName = retryState.move ? getMoveActionName(retryState.move) : 'performing action' showToast({ type: toastType, title: enhancedError.userMessage, description: enhancedError.suggestion ? `${enhancedError.suggestion} (${actionName})` : `Error while ${actionName}`, duration: enhancedError.severity === 'fatal' ? 10000 : 7000, }) } }, [lastError, retryState, showToast]) // Toast for retry state changes (progressive feedback) useEffect(() => { if (!retryState.isRetrying || !retryState.move) return // Parse the error as a version conflict const enhancedError: EnhancedError = parseError( 'version conflict', retryState.move, retryState.retryCount ) // Show toast for 3+ retries (progressive disclosure) if (retryState.retryCount >= 3 && shouldShowToast(enhancedError)) { const actionName = getMoveActionName(retryState.move) showToast({ type: 'info', title: enhancedError.userMessage, description: `Retrying ${actionName}... (attempt ${retryState.retryCount})`, duration: 3000, }) } }, [retryState, showToast]) const value: RithmomachiaContextValue = { state, lastError, retryState, viewerId: viewerId ?? null, playerColor, isMyTurn, rosterStatus, localActivePlayerIds, whitePlayerId, blackPlayerId, localTurnPlayerId, isSpectating, localPlayerColor, startGame, makeMove, declareHarmony, resign, offerDraw, acceptDraw, claimRepetition, claimFiftyMove, setConfig, assignWhitePlayer, assignBlackPlayer, swapSides, resetGame, goToSetup, exitSession, clearError, } return <RithmomachiaContext.Provider value={value}>{children}</RithmomachiaContext.Provider> } |