Make the game joinable in two sessions

This commit is contained in:
sepia 2025-07-15 20:32:14 -05:00
parent e984172918
commit 8b7d40b6f8
1 changed files with 23 additions and 2 deletions

View File

@ -11,6 +11,17 @@ console.log('Gomoku client entry point loaded.');
const WS_URL = process.env.WS_URL || 'ws://localhost:3000/ws'; 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 // Initialize components
const gameStateManager = new GameStateManager(); const gameStateManager = new GameStateManager();
const wsClient = new WebSocketClient(WS_URL); const wsClient = new WebSocketClient(WS_URL);
@ -46,6 +57,12 @@ wsClient.onMessage((message) => {
gameStateManager.updateGameState(msg.state as GameStateType); gameStateManager.updateGameState(msg.state as GameStateType);
gameBoardUI.updateBoard(gameStateManager.getGameState()); gameBoardUI.updateBoard(gameStateManager.getGameState());
console.log('Game state updated: ', 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}<br/>Game ID: ${msg.state.id}<br/>Share this link: <a href="${gameLink}">${gameLink}</a>`;
}
break; break;
case 'move_result': case 'move_result':
if (msg.success) { if (msg.success) {
@ -106,11 +123,15 @@ gameBoardUI.setOnCellClick((row, col) => {
// WebSocketClient connection status messages // WebSocketClient connection status messages
wsClient.onOpen(() => { wsClient.onOpen(() => {
console.log('Connected to game server.'); console.log('Connected to game server.');
const playerId = `player-${Math.random().toString(36).substring(2, 9)}`; playerId = `player-${Math.random().toString(36).substring(2, 9)}`;
const joinMessage = { const joinMessage: any = {
type: 'join_game', type: 'join_game',
playerId: playerId, playerId: playerId,
}; };
if (gameIdFromUrl) {
joinMessage.gameId = gameIdFromUrl;
}
wsClient.send(JSON.stringify(joinMessage)); wsClient.send(JSON.stringify(joinMessage));
if (playerInfoElement) { if (playerInfoElement) {
playerInfoElement.textContent = `You are: ${playerId} (Waiting for game state...)`; playerInfoElement.textContent = `You are: ${playerId} (Waiting for game state...)`;