From f938c4ebdfd993d306d7e2d3c953fbbe24aa76dc Mon Sep 17 00:00:00 2001 From: Lucas Habersaat Date: Mon, 27 Oct 2025 21:19:04 +0100 Subject: [PATCH] Add frontend support for rejoin (#20) --- src/frontend/src/Connection.ts | 38 ++++++++++++++++++- .../src/GameElements/PlayerNameInput.ts | 26 +++++++++++-- src/frontend/src/Scenes/LobbyScene.ts | 11 ++++-- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/frontend/src/Connection.ts b/src/frontend/src/Connection.ts index 8d863d6..b19236c 100644 --- a/src/frontend/src/Connection.ts +++ b/src/frontend/src/Connection.ts @@ -1,9 +1,9 @@ import { GameState } from './GameState'; -import { Client, GameStateRaw } from './GameStateRaw'; +import { Client, GameStateRaw, Id } from './GameStateRaw'; import { log } from './utils/debugUtils'; import { Event, Evented } from './utils/Evented'; -type Action = "join" | "toggleRole" | "toggleReady"; +type Action = "join" | "rejoin" | "toggleRole" | "toggleReady"; interface Request { action: Action, @@ -14,6 +14,11 @@ interface JoinRequest { name: string, } +interface RejoinRequest { + action: "rejoin", + id: Id, +} + interface ToggleRoleRequest { action: "toggleRole" } @@ -73,6 +78,7 @@ export class Connection extends Evented { if (this._waitForJoin) { this._client = messageObject.player as Client; + storeClient(this._client); this._waitForJoin = false; this.emit("joined", new JoinedEvent()); return; @@ -134,6 +140,16 @@ export class Connection extends Evented { this._send(joinRequest); } + rejoin(id: Id): void { + const rejoinRequest = { + action: "rejoin", + id, + } as RejoinRequest; + + this._waitForJoin = true; + this._send(rejoinRequest); + } + toggleRole(): void { const toggleRoleRequest = { action: "toggleRole" @@ -149,4 +165,22 @@ export class Connection extends Evented { this._send(toggleReadyRequest) } +} + +export function storedClient(): Client | null { + const clientRaw = localStorage.getItem("client"); + if (clientRaw == null) { + return null; + } + + try { + // TODO: Do schema check. + return JSON.parse(clientRaw) as Client; + } catch { + return null; + } +} + +function storeClient(client: Client) { + localStorage.setItem("client", JSON.stringify(client)); } \ No newline at end of file diff --git a/src/frontend/src/GameElements/PlayerNameInput.ts b/src/frontend/src/GameElements/PlayerNameInput.ts index 85821f1..a451268 100644 --- a/src/frontend/src/GameElements/PlayerNameInput.ts +++ b/src/frontend/src/GameElements/PlayerNameInput.ts @@ -4,16 +4,18 @@ import { Button } from '../ScreenElements/Button'; import { paragraphFont } from '../utils/fontUtil'; import { Text } from '../ScreenElements/Text'; import { spacingM } from '../utils/formattingUtils'; -import { Connection } from '../Connection'; +import { Connection, storedClient } from '../Connection'; +import { Id } from '../GameStateRaw'; export class PlayerNameInput extends ex.ScreenElement { private _nameLabel: Text = null!; private _nameInput: TextInput = null!; private _joinButton: Button = null!; + private _rejoinButton: Button | null = null; private _height = 0; - constructor(private _connection: Connection, private _joinCallback: (name: string) => void) { + constructor(private _connection: Connection, private _joinCallback: (name: string) => void, private _rejoinCallback: (id: Id) => void) { super(); } @@ -35,10 +37,21 @@ export class PlayerNameInput extends ex.ScreenElement { onClick: () => this._joinCallback(this._nameInput.text), disabled: true, }); - this._nameInput.on(TextInputEvents.Textchanged, () => this._updateJoinButtonDisabled()); - this.addChild(this._joinButton); + const rejoinClient = storedClient(); + if (rejoinClient != null) { + this._rejoinButton = new Button({ + text: "Rejoin as " + rejoinClient.name, + anchor: ex.vec(0.5, 0), + onClick: () => this._rejoinCallback(rejoinClient.id), + }); + + this.addChild(this._rejoinButton); + } + + this._nameInput.on(TextInputEvents.Textchanged, () => this._updateJoinButtonDisabled()); + let y = 0; this._nameLabel.pos = ex.vec(0, y); y += this._nameLabel.font.size + spacingM; @@ -49,6 +62,11 @@ export class PlayerNameInput extends ex.ScreenElement { this._joinButton.pos = ex.vec(0, y); y += this._joinButton.height + spacingM; + if (this._rejoinButton != null) { + this._rejoinButton.pos = ex.vec(0, y); + y += this._rejoinButton.height + spacingM; + } + this._height = y; this._connection.on("connect",() => this._onConnect()) diff --git a/src/frontend/src/Scenes/LobbyScene.ts b/src/frontend/src/Scenes/LobbyScene.ts index 8197b4a..86f7331 100644 --- a/src/frontend/src/Scenes/LobbyScene.ts +++ b/src/frontend/src/Scenes/LobbyScene.ts @@ -11,6 +11,7 @@ import { Title } from '../GameElements/Title'; import { ErrorList } from '../GameElements/ErrorList'; import { GameState } from '../GameState'; import { maxPlayers } from '../main'; +import { Id } from '../GameStateRaw'; const tableWidth = 400; @@ -124,7 +125,7 @@ export class LobbyScene extends GameStateScene { }); // Name input - this._playerNameInput = new PlayerNameInput(this._connection, (_name: string) => this._join()); + this._playerNameInput = new PlayerNameInput(this._connection, (name: string) => this._join(name), (id: Id) => this._rejoin(id)); this._playerNameInput.anchor = ex.vec(0.5, 0); // Error box @@ -246,8 +247,12 @@ export class LobbyScene extends GameStateScene { this._updatePositions(); } - private _join(): void { - this._connection.join(this._playerNameInput.playerName); + private _join(name: string): void { + this._connection.join(name); + } + + private _rejoin(id: Id): void { + this._connection.rejoin(id); } private _joinAsPlayer(): void {