refactor(game): unify GameState, standardize board props, and rename game components

- Replaced multiple App-level state fields with unified GameState
- Added INITIAL_GAME_STATE and migrated App.tsx to use single game state
- Introduced GameProps as shared base props for all turn-based board games
- Created TicTacToeGameProps and BattleshipGameProps extending GameProps
- Updated TicTacToe and Battleship components to use new props
- Replaced verbose prop passing with spread {...commonProps}
- Updated renderGameBoard to use game.metadata consistently
- Renamed TicTacToeBoard -> TicTacToeGame for clarity
- Renamed BattleShipBoard -> BattleShipGame for naming consistency
- Updated all import paths to reflect new component names
- Replaced MatchDataMessage with MatchDataModel
- Moved GameState definition from models.ts to interfaces/states.ts
- Removed old board-specific prop structures and per-field state management
- Increased type safety and reduced duplication across the codebase

This commit consolidates game state flow, introduces a clean component props
architecture, and standardizes naming convention
This commit is contained in:
2025-12-04 19:16:20 +05:30
parent 650d7b7ed6
commit 06bdc92190
8 changed files with 95 additions and 80 deletions

View File

@@ -5,7 +5,7 @@ export interface PlayerModel {
metadata: Record<string, string>; // e.g. { symbol: "X" }
}
export interface MatchDataMessage<T = any> {
export interface MatchDataModel<T = any> {
opCode: number;
data: T;
userId: string | null;
@@ -15,15 +15,6 @@ export interface Board {
grid: string[][];
}
export interface GameState {
boards: Record<string, Board>;
turn: number;
winner: string | null;
gameOver: boolean;
players: PlayerModel[];
metadata: Record<string, any>;
}
export interface GameMetadata {
game: string;
mode: string;

View File

@@ -1,7 +1,19 @@
import {
MatchDataMessage,
MatchDataModel,
} from './models'
import {
GameState
} from "./states";
export interface PlayerProps {
onMatchDataCallback: (msg:MatchDataMessage) => void;
onMatchDataCallback: (msg:MatchDataModel) => void;
}
export interface GameProps
extends Pick<
GameState,
"boards" | "turn" | "winner" | "gameOver" | "players"
> {
myUserId: string | null;
}

View File

@@ -3,9 +3,24 @@ import {
Socket
} from "@heroiclabs/nakama-js";
import {
Board,
PlayerModel,
} from "./models"
export interface NakamaProviderState {
session: Session | null;
socket: Socket | null;
matchId: string | null;
matchmakerTicket: string | null;
}
export interface GameState {
boards: Record<string, Board>;
turn: number;
winner: string | null;
gameOver: boolean;
players: PlayerModel[];
metadata: Record<string, any>;
}