diff --git a/public/scripts/profile-editor.js b/public/scripts/profile-editor.js index e0de2b0..726ddc6 100644 --- a/public/scripts/profile-editor.js +++ b/public/scripts/profile-editor.js @@ -8,7 +8,7 @@ document.addEventListener('DOMContentLoaded', () => { // Get playerId from meta tag const displayNameMeta = document.querySelector('meta[name="displayName"]'); - const initialDisplayName = displayNameMeta ? displayNameMeta.content : 'UnknownPlayer'; + const initialDisplayName = displayNameMeta ? displayNameMeta.content : 'New Player'; // Initialize display name with initial name displayNameSpan.textContent = initialDisplayName; diff --git a/public/scripts/send-ws-messages.js b/public/scripts/send-ws-messages.js index 9543616..40f93d6 100644 --- a/public/scripts/send-ws-messages.js +++ b/public/scripts/send-ws-messages.js @@ -81,31 +81,3 @@ document.addEventListener('htmx:wsConfigSend', function (e) { }; } }); - -// Set the user's name to their flag by default -document.body.addEventListener('htmx:wsOpen', function (evt) { - const locale = navigator.language || navigator.userLanguage; - const countryCode = locale.split('-')[1] || locale.split('_')[1] || locale; - const countryCodeToFlagEmoji = (code) => { - if (code.length === 2) { - return code - .toUpperCase() - .split('') - .map((char) => String.fromCodePoint(127397 + char.charCodeAt(0))) - .join(''); - } - return null; - }; - const flagEmoji = countryCodeToFlagEmoji(countryCode); - - if (flagEmoji) { - const message = { - type: 'update_display_name', - displayName: flagEmoji, - }; - evt.detail.socketWrapper.send(JSON.stringify(message)); - - const displayNameSpan = document.getElementById('display-name'); - displayNameSpan.textContent = flagEmoji; - } -}); diff --git a/src/index.ts b/src/index.ts index 189ed9a..9417ee2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/web-socket-handler.tsx b/src/web-socket-handler.tsx index ac0eb38..acf5d11 100644 --- a/src/web-socket-handler.tsx +++ b/src/web-socket-handler.tsx @@ -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) {