Add frontend support for rejoin (#20)
This commit is contained in:
1 parent
f2f4a9573f
commit
f938c4ebdf
3 files changed
+66
-9
No files matched your search
@@ -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<typeof ConnectionEvents> {
|
||||
|
||||
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<typeof ConnectionEvents> {
|
||||
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<typeof ConnectionEvents> {
|
||||
|
||||
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));
|
||||
}
|
||||
@@ -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())
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user