Remove flag names and default to 'New Player'

This commit is contained in:
sepia 2025-07-23 20:04:27 -05:00
parent 0faf0b04bc
commit b8e880cd29
4 changed files with 16 additions and 39 deletions

View file

@ -59,7 +59,7 @@ const app = new Elysia()
console.log(`Created new game without specific ID: ${gameId}`);
}
const displayName = wsHandler.playerNames.get(playerId) || playerId;
const displayName = wsHandler.getPlayerName(playerId);
const htmlTemplate = await Bun.file('./index.html').text();
let finalHtml = htmlTemplate

View file

@ -52,8 +52,9 @@ class GameServer {
this.connections = new Map();
}
public handleConnection(ws: WS, playerName: string) {
public handleConnection(ws: WS) {
const { playerId } = ws.data.query;
const playerName = this.webSocketHandler.getPlayerName(playerId); // Retrieve name or use ID
const conn = new PlayerConnection(playerId, playerName, ws);
this.connections.set(playerId, conn);
console.log(`Created connection with player ${conn.id} in game ${this.id}`);
@ -658,12 +659,8 @@ class GameServer {
}
conn.name = newDisplayName;
this.webSocketHandler.playerNames.set(conn.id, newDisplayName);
this.webSocketHandler.setPlayerName(conn.id, newDisplayName);
this.broadcastTitle();
conn.sendMessage(
'info',
`Your display name has been updated to "${newDisplayName}".`,
);
}
}
@ -677,11 +674,10 @@ export class WebSocketHandler {
}
public handleConnection(ws: WS): void {
const { gameId } = ws.data.query;
const { gameId, playerId } = ws.data.query;
if (this.games.has(gameId)) {
const game = this.games.get(gameId)!;
const playerName = this.playerNames.get(playerId) || playerId; // Retrieve name or use ID
game.handleConnection(ws, playerName);
game.handleConnection(ws);
} else {
ws.send('Error: game not found');
ws.close();
@ -708,6 +704,15 @@ export class WebSocketHandler {
}
}
public getPlayerName(playerId: string): string {
const name = this.playerNames.get(playerId);
return name ? name : 'New Player';
}
public setPlayerName(playerId: string, displayName: string) {
this.playerNames.set(playerId, displayName);
}
public handleMessage(ws: WS, messageUnparsed: any) {
let message = messageUnparsed as Message;
if (!message) {