Skip to content

Commit 762ac48

Browse files
committed
feat: improve lobby chat UX with local/remote message styling
- Expose username via window.__username in ClientGameRunner - Add local vs remote message detection in LobbyChatPanel - Style local messages right-aligned with blue tint background - Style remote messages left-aligned with dark background - Fix GameServer to use validated clientMsg.text instead of re-parsing - Improves code safety by avoiding redundant JSON.parse calls
1 parent 255a767 commit 762ac48

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/client/ClientGameRunner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export function joinLobby(
7878
(window as any).__eventBus = eventBus;
7979
// Expose current clientID for UI alignment decisions
8080
(window as any).__clientID = lobbyConfig.clientID;
81+
// Expose username for message identification
82+
(window as any).__username = lobby.playerName;
8183
// Notify components that the EventBus is ready
8284
document.dispatchEvent(
8385
new CustomEvent("event-bus:ready", { bubbles: true, composed: true }),

src/client/components/LobbyChatPanel.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class LobbyChatPanel extends LitElement {
1616
@state() private inputText: string = "";
1717

1818
private bus: EventBus | null = null;
19+
private username: string | null = null;
1920

2021
connectedCallback(): void {
2122
super.connectedCallback();
@@ -24,6 +25,7 @@ export class LobbyChatPanel extends LitElement {
2425
if (globalBus) {
2526
this.bus = globalBus;
2627
}
28+
this.username = (window as any).__username ?? null;
2729
document.addEventListener("event-bus:ready", this.onBusReady as any);
2830
}
2931

@@ -54,6 +56,7 @@ export class LobbyChatPanel extends LitElement {
5456
if (globalBus) {
5557
this.bus = globalBus;
5658
}
59+
this.username ??= (window as any).__username ?? null;
5760
};
5861

5962
private sendMessage() {
@@ -80,7 +83,12 @@ export class LobbyChatPanel extends LitElement {
8083
<div class="lcp-messages">
8184
${this.messages.map((m) => {
8285
const displayName = m.isHost ? `${m.username} (Host)` : m.username;
83-
return html`<div class="lcp-msg">
86+
const isLocal =
87+
this.username !== null && m.username === this.username;
88+
const msgClass = isLocal
89+
? "lcp-msg lcp-msg--local"
90+
: "lcp-msg lcp-msg--remote";
91+
return html`<div class="${msgClass}">
8492
<span class="lcp-sender">${displayName}:</span> ${m.text}
8593
</div>`;
8694
})}
@@ -141,6 +149,16 @@ style.textContent = `
141149
border-radius: 10px;
142150
background: rgba(0, 0, 0, 0.6);
143151
}
152+
.lcp-msg--local {
153+
align-self: flex-end;
154+
text-align: right;
155+
background: rgba(36, 59, 85, 0.7);
156+
}
157+
.lcp-msg--remote {
158+
align-self: flex-start;
159+
text-align: left;
160+
background: rgba(0, 0, 0, 0.6);
161+
}
144162
.lcp-sender {
145163
color: #9ae6b4;
146164
margin-right: 4px;

src/server/GameServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export class GameServer {
326326
type: "lobby_chat",
327327
username: client.username,
328328
isHost,
329-
text: (JSON.parse(message) as any).text,
329+
text: clientMsg.text,
330330
});
331331
this.activeClients.forEach((c) => c.ws.send(payload));
332332
break;

0 commit comments

Comments
 (0)