Make the game joinable in two sessions
This commit is contained in:
parent
e984172918
commit
8b7d40b6f8
|
@ -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}<br/>Game ID: ${msg.state.id}<br/>Share this link: <a href="${gameLink}">${gameLink}</a>`;
|
||||
}
|
||||
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...)`;
|
||||
|
|
Loading…
Reference in New Issue