diff --git a/src/client-entry.ts b/src/client-entry.ts index d0aaff9..da4607b 100644 --- a/src/client-entry.ts +++ b/src/client-entry.ts @@ -11,6 +11,17 @@ console.log('Gomoku client entry point loaded.'); const WS_URL = process.env.WS_URL || 'ws://localhost:3000/ws'; +// Function to get a query parameter from the URL +function getQueryParam(name: string): string | null { + const urlParams = new URLSearchParams(window.location.search); + return urlParams.get(name); +} + +// Get gameId from URL, if present +const gameIdFromUrl = getQueryParam('gameId'); + +let playerId: string; // Declare playerId here, accessible throughout the module + // Initialize components const gameStateManager = new GameStateManager(); const wsClient = new WebSocketClient(WS_URL); @@ -46,6 +57,12 @@ wsClient.onMessage((message) => { gameStateManager.updateGameState(msg.state as GameStateType); gameBoardUI.updateBoard(gameStateManager.getGameState()); console.log('Game state updated: ', gameStateManager.getGameState()); + + // Update player info with game ID and shareable link + if (playerInfoElement && msg.state.id) { + const gameLink = `${window.location.origin}/?gameId=${msg.state.id}`; + playerInfoElement.innerHTML = `You are: ${playerId}
Game ID: ${msg.state.id}
Share this link: ${gameLink}`; + } break; case 'move_result': if (msg.success) { @@ -106,11 +123,15 @@ gameBoardUI.setOnCellClick((row, col) => { // WebSocketClient connection status messages wsClient.onOpen(() => { console.log('Connected to game server.'); - const playerId = `player-${Math.random().toString(36).substring(2, 9)}`; - const joinMessage = { + playerId = `player-${Math.random().toString(36).substring(2, 9)}`; + const joinMessage: any = { type: 'join_game', playerId: playerId, }; + + if (gameIdFromUrl) { + joinMessage.gameId = gameIdFromUrl; + } wsClient.send(JSON.stringify(joinMessage)); if (playerInfoElement) { playerInfoElement.textContent = `You are: ${playerId} (Waiting for game state...)`;