Remove flag names and default to 'New Player'
This commit is contained in:
parent
0faf0b04bc
commit
b8e880cd29
|
@ -8,7 +8,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
|
||||||
// Get playerId from meta tag
|
// Get playerId from meta tag
|
||||||
const displayNameMeta = document.querySelector('meta[name="displayName"]');
|
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
|
// Initialize display name with initial name
|
||||||
displayNameSpan.textContent = initialDisplayName;
|
displayNameSpan.textContent = initialDisplayName;
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ const app = new Elysia()
|
||||||
console.log(`Created new game without specific ID: ${gameId}`);
|
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();
|
const htmlTemplate = await Bun.file('./index.html').text();
|
||||||
let finalHtml = htmlTemplate
|
let finalHtml = htmlTemplate
|
||||||
|
|
|
@ -52,8 +52,9 @@ class GameServer {
|
||||||
this.connections = new Map();
|
this.connections = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
public handleConnection(ws: WS, playerName: string) {
|
public handleConnection(ws: WS) {
|
||||||
const { playerId } = ws.data.query;
|
const { playerId } = ws.data.query;
|
||||||
|
const playerName = this.webSocketHandler.getPlayerName(playerId); // Retrieve name or use ID
|
||||||
const conn = new PlayerConnection(playerId, playerName, ws);
|
const conn = new PlayerConnection(playerId, playerName, ws);
|
||||||
this.connections.set(playerId, conn);
|
this.connections.set(playerId, conn);
|
||||||
console.log(`Created connection with player ${conn.id} in game ${this.id}`);
|
console.log(`Created connection with player ${conn.id} in game ${this.id}`);
|
||||||
|
@ -658,12 +659,8 @@ class GameServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.name = newDisplayName;
|
conn.name = newDisplayName;
|
||||||
this.webSocketHandler.playerNames.set(conn.id, newDisplayName);
|
this.webSocketHandler.setPlayerName(conn.id, newDisplayName);
|
||||||
this.broadcastTitle();
|
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 {
|
public handleConnection(ws: WS): void {
|
||||||
const { gameId } = ws.data.query;
|
const { gameId, playerId } = ws.data.query;
|
||||||
if (this.games.has(gameId)) {
|
if (this.games.has(gameId)) {
|
||||||
const game = this.games.get(gameId)!;
|
const game = this.games.get(gameId)!;
|
||||||
const playerName = this.playerNames.get(playerId) || playerId; // Retrieve name or use ID
|
game.handleConnection(ws);
|
||||||
game.handleConnection(ws, playerName);
|
|
||||||
} else {
|
} else {
|
||||||
ws.send('Error: game not found');
|
ws.send('Error: game not found');
|
||||||
ws.close();
|
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) {
|
public handleMessage(ws: WS, messageUnparsed: any) {
|
||||||
let message = messageUnparsed as Message;
|
let message = messageUnparsed as Message;
|
||||||
if (!message) {
|
if (!message) {
|
||||||
|
|
Loading…
Reference in New Issue