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 { GameState } from './GameState';
|
||||||
import { Client, GameStateRaw } from './GameStateRaw';
|
import { Client, GameStateRaw, Id } from './GameStateRaw';
|
||||||
import { log } from './utils/debugUtils';
|
import { log } from './utils/debugUtils';
|
||||||
import { Event, Evented } from './utils/Evented';
|
import { Event, Evented } from './utils/Evented';
|
||||||
|
|
||||||
type Action = "join" | "toggleRole" | "toggleReady";
|
type Action = "join" | "rejoin" | "toggleRole" | "toggleReady";
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
action: Action,
|
action: Action,
|
||||||
@@ -14,6 +14,11 @@ interface JoinRequest {
|
|||||||
name: string,
|
name: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface RejoinRequest {
|
||||||
|
action: "rejoin",
|
||||||
|
id: Id,
|
||||||
|
}
|
||||||
|
|
||||||
interface ToggleRoleRequest {
|
interface ToggleRoleRequest {
|
||||||
action: "toggleRole"
|
action: "toggleRole"
|
||||||
}
|
}
|
||||||
@@ -73,6 +78,7 @@ export class Connection extends Evented<typeof ConnectionEvents> {
|
|||||||
|
|
||||||
if (this._waitForJoin) {
|
if (this._waitForJoin) {
|
||||||
this._client = messageObject.player as Client;
|
this._client = messageObject.player as Client;
|
||||||
|
storeClient(this._client);
|
||||||
this._waitForJoin = false;
|
this._waitForJoin = false;
|
||||||
this.emit("joined", new JoinedEvent());
|
this.emit("joined", new JoinedEvent());
|
||||||
return;
|
return;
|
||||||
@@ -134,6 +140,16 @@ export class Connection extends Evented<typeof ConnectionEvents> {
|
|||||||
this._send(joinRequest);
|
this._send(joinRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rejoin(id: Id): void {
|
||||||
|
const rejoinRequest = {
|
||||||
|
action: "rejoin",
|
||||||
|
id,
|
||||||
|
} as RejoinRequest;
|
||||||
|
|
||||||
|
this._waitForJoin = true;
|
||||||
|
this._send(rejoinRequest);
|
||||||
|
}
|
||||||
|
|
||||||
toggleRole(): void {
|
toggleRole(): void {
|
||||||
const toggleRoleRequest = {
|
const toggleRoleRequest = {
|
||||||
action: "toggleRole"
|
action: "toggleRole"
|
||||||
@@ -150,3 +166,21 @@ export class Connection extends Evented<typeof ConnectionEvents> {
|
|||||||
this._send(toggleReadyRequest)
|
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 { paragraphFont } from '../utils/fontUtil';
|
||||||
import { Text } from '../ScreenElements/Text';
|
import { Text } from '../ScreenElements/Text';
|
||||||
import { spacingM } from '../utils/formattingUtils';
|
import { spacingM } from '../utils/formattingUtils';
|
||||||
import { Connection } from '../Connection';
|
import { Connection, storedClient } from '../Connection';
|
||||||
|
import { Id } from '../GameStateRaw';
|
||||||
|
|
||||||
export class PlayerNameInput extends ex.ScreenElement {
|
export class PlayerNameInput extends ex.ScreenElement {
|
||||||
private _nameLabel: Text = null!;
|
private _nameLabel: Text = null!;
|
||||||
private _nameInput: TextInput = null!;
|
private _nameInput: TextInput = null!;
|
||||||
private _joinButton: Button = null!;
|
private _joinButton: Button = null!;
|
||||||
|
private _rejoinButton: Button | null = null;
|
||||||
|
|
||||||
private _height = 0;
|
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();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,10 +37,21 @@ export class PlayerNameInput extends ex.ScreenElement {
|
|||||||
onClick: () => this._joinCallback(this._nameInput.text),
|
onClick: () => this._joinCallback(this._nameInput.text),
|
||||||
disabled: true,
|
disabled: true,
|
||||||
});
|
});
|
||||||
this._nameInput.on(TextInputEvents.Textchanged, () => this._updateJoinButtonDisabled());
|
|
||||||
|
|
||||||
this.addChild(this._joinButton);
|
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;
|
let y = 0;
|
||||||
this._nameLabel.pos = ex.vec(0, y);
|
this._nameLabel.pos = ex.vec(0, y);
|
||||||
y += this._nameLabel.font.size + spacingM;
|
y += this._nameLabel.font.size + spacingM;
|
||||||
@@ -49,6 +62,11 @@ export class PlayerNameInput extends ex.ScreenElement {
|
|||||||
this._joinButton.pos = ex.vec(0, y);
|
this._joinButton.pos = ex.vec(0, y);
|
||||||
y += this._joinButton.height + spacingM;
|
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._height = y;
|
||||||
|
|
||||||
this._connection.on("connect",() => this._onConnect())
|
this._connection.on("connect",() => this._onConnect())
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { Title } from '../GameElements/Title';
|
|||||||
import { ErrorList } from '../GameElements/ErrorList';
|
import { ErrorList } from '../GameElements/ErrorList';
|
||||||
import { GameState } from '../GameState';
|
import { GameState } from '../GameState';
|
||||||
import { maxPlayers } from '../main';
|
import { maxPlayers } from '../main';
|
||||||
|
import { Id } from '../GameStateRaw';
|
||||||
|
|
||||||
const tableWidth = 400;
|
const tableWidth = 400;
|
||||||
|
|
||||||
@@ -124,7 +125,7 @@ export class LobbyScene extends GameStateScene {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Name input
|
// 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);
|
this._playerNameInput.anchor = ex.vec(0.5, 0);
|
||||||
|
|
||||||
// Error box
|
// Error box
|
||||||
@@ -246,8 +247,12 @@ export class LobbyScene extends GameStateScene {
|
|||||||
this._updatePositions();
|
this._updatePositions();
|
||||||
}
|
}
|
||||||
|
|
||||||
private _join(): void {
|
private _join(name: string): void {
|
||||||
this._connection.join(this._playerNameInput.playerName);
|
this._connection.join(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _rejoin(id: Id): void {
|
||||||
|
this._connection.rejoin(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _joinAsPlayer(): void {
|
private _joinAsPlayer(): void {
|
||||||
|
|||||||
Reference in new issue
Block a user