Add json schemas and match scene to frontend (#29)
- Add json schemas for lobby and match state messages - I did a few changes: Player and spectator each have a property client, which has the id/name. - Add the separatio between lobby and match for messages - Put the socket api into our own wrapper, to easily have a `MockSocket` to use in tests. - Rename gamescene to matchscene. - Add the url param `?mock=lobby` or `?mock=match` to jump to lobby or match with mocked data.
This commit is contained in:
1 parent
523d933ffd
commit
214e1bb3c4
27 files changed
+597
-130
No files matched your search
Generated
+84
-13
@@ -8,6 +8,7 @@
|
|||||||
"name": "frontend",
|
"name": "frontend",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"ajv": "^8.17.1",
|
||||||
"excalibur": "^0.30.3"
|
"excalibur": "^0.30.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -570,6 +571,30 @@
|
|||||||
"url": "https://opencollective.com/eslint"
|
"url": "https://opencollective.com/eslint"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@eslint/eslintrc/node_modules/ajv": {
|
||||||
|
"version": "6.12.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
||||||
|
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"fast-deep-equal": "^3.1.1",
|
||||||
|
"fast-json-stable-stringify": "^2.0.0",
|
||||||
|
"json-schema-traverse": "^0.4.1",
|
||||||
|
"uri-js": "^4.2.2"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/epoberezkin"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@eslint/eslintrc/node_modules/json-schema-traverse": {
|
||||||
|
"version": "0.4.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||||
|
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/@eslint/js": {
|
"node_modules/@eslint/js": {
|
||||||
"version": "9.39.1",
|
"version": "9.39.1",
|
||||||
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.39.1.tgz",
|
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.39.1.tgz",
|
||||||
@@ -1261,16 +1286,15 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/ajv": {
|
"node_modules/ajv": {
|
||||||
"version": "6.12.6",
|
"version": "8.17.1",
|
||||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz",
|
||||||
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
|
"integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==",
|
||||||
"dev": true,
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fast-deep-equal": "^3.1.1",
|
"fast-deep-equal": "^3.1.3",
|
||||||
"fast-json-stable-stringify": "^2.0.0",
|
"fast-uri": "^3.0.1",
|
||||||
"json-schema-traverse": "^0.4.1",
|
"json-schema-traverse": "^1.0.0",
|
||||||
"uri-js": "^4.2.2"
|
"require-from-string": "^2.0.2"
|
||||||
},
|
},
|
||||||
"funding": {
|
"funding": {
|
||||||
"type": "github",
|
"type": "github",
|
||||||
@@ -1604,6 +1628,30 @@
|
|||||||
"url": "https://opencollective.com/eslint"
|
"url": "https://opencollective.com/eslint"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/eslint/node_modules/ajv": {
|
||||||
|
"version": "6.12.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
||||||
|
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"fast-deep-equal": "^3.1.1",
|
||||||
|
"fast-json-stable-stringify": "^2.0.0",
|
||||||
|
"json-schema-traverse": "^0.4.1",
|
||||||
|
"uri-js": "^4.2.2"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/epoberezkin"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/eslint/node_modules/json-schema-traverse": {
|
||||||
|
"version": "0.4.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||||
|
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/espree": {
|
"node_modules/espree": {
|
||||||
"version": "10.4.0",
|
"version": "10.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/espree/-/espree-10.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/espree/-/espree-10.4.0.tgz",
|
||||||
@@ -1678,7 +1726,6 @@
|
|||||||
"version": "3.1.3",
|
"version": "3.1.3",
|
||||||
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
||||||
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
|
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
|
||||||
"dev": true,
|
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/fast-diff": {
|
"node_modules/fast-diff": {
|
||||||
@@ -1702,6 +1749,22 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/fast-uri": {
|
||||||
|
"version": "3.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.0.tgz",
|
||||||
|
"integrity": "sha512-iPeeDKJSWf4IEOasVVrknXpaBV0IApz/gp7S2bb7Z4Lljbl2MGJRqInZiUrQwV16cpzw/D3S5j5Julj/gT52AA==",
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/fastify"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/fastify"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"license": "BSD-3-Clause"
|
||||||
|
},
|
||||||
"node_modules/file-entry-cache": {
|
"node_modules/file-entry-cache": {
|
||||||
"version": "8.0.0",
|
"version": "8.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz",
|
||||||
@@ -1899,10 +1962,9 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/json-schema-traverse": {
|
"node_modules/json-schema-traverse": {
|
||||||
"version": "0.4.1",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
|
||||||
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
|
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==",
|
||||||
"dev": true,
|
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/json-stable-stringify-without-jsonify": {
|
"node_modules/json-stable-stringify-without-jsonify": {
|
||||||
@@ -2173,6 +2235,15 @@
|
|||||||
"node": ">=6"
|
"node": ">=6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/require-from-string": {
|
||||||
|
"version": "2.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
|
||||||
|
"integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/resolve-from": {
|
"node_modules/resolve-from": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz",
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
"dev": "vite"
|
"dev": "vite"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"ajv": "^8.17.1",
|
||||||
"excalibur": "^0.30.3"
|
"excalibur": "^0.30.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
import { GameState } from "./GameState";
|
import { LobbyState } from "./LobbyState";
|
||||||
import { Client, GameStateRaw, Id } from "./GameStateRaw";
|
import { Client, Id } from "./jsonTypes";
|
||||||
|
import {
|
||||||
|
validateClientSchema,
|
||||||
|
validateMessageSchema,
|
||||||
|
} from "./schemaValidators";
|
||||||
import { log } from "./utils/debugUtils";
|
import { log } from "./utils/debugUtils";
|
||||||
import { Event, Evented } from "./utils/Evented";
|
import { Event, Evented } from "./utils/Evented";
|
||||||
|
import { MatchState } from "./MatchState";
|
||||||
|
import { BaseSocket, MessageEvent } from "./sockets/BaseSocket";
|
||||||
|
import { MockSocket } from "./sockets/MockSocket";
|
||||||
|
import { Socket } from "./sockets/Socket";
|
||||||
|
|
||||||
type Action = "join" | "rejoin" | "toggleRole" | "toggleReady";
|
type Action = "join" | "rejoin" | "toggleRole" | "toggleReady";
|
||||||
|
|
||||||
@@ -27,8 +35,14 @@ interface ToggleReadyRequest {
|
|||||||
action: "toggleReady";
|
action: "toggleReady";
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GameMessageEvent extends Event {
|
export class LobbyMessageEvent extends Event {
|
||||||
constructor(public gameState: GameState) {
|
constructor(public lobbyState: LobbyState) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class MatchMessageEvent extends Event {
|
||||||
|
constructor(public matchState: MatchState) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,7 +57,8 @@ export class ConnectOrDisconnectEvent extends Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const ConnectionEvents = {
|
export const ConnectionEvents = {
|
||||||
"game-message": GameMessageEvent,
|
"lobby-message": LobbyMessageEvent,
|
||||||
|
"match-message": MatchMessageEvent,
|
||||||
joined: JoinedEvent,
|
joined: JoinedEvent,
|
||||||
connect: ConnectEvent,
|
connect: ConnectEvent,
|
||||||
disconnect: DisconnectEvent,
|
disconnect: DisconnectEvent,
|
||||||
@@ -51,30 +66,30 @@ export const ConnectionEvents = {
|
|||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export class Connection extends Evented<typeof ConnectionEvents> {
|
export class Connection extends Evented<typeof ConnectionEvents> {
|
||||||
private _socket: WebSocket | null = null;
|
private _socket: BaseSocket | null = null;
|
||||||
private _connected = false;
|
private _connected = false;
|
||||||
|
|
||||||
private _waitForJoin = false;
|
private _waitForJoin = false;
|
||||||
private _client: Client | null = null;
|
private _client: Client | null = null;
|
||||||
|
|
||||||
get connected(): boolean {
|
get connected(): boolean {
|
||||||
const { _socket } = this;
|
return this._socket?.connected ?? false;
|
||||||
return _socket !== null && _socket.readyState === _socket.OPEN;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(): void {
|
connect(mock?: MockSocket): void {
|
||||||
this._socket = new WebSocket("ws://127.0.0.1:8888");
|
this._socket = mock ?? new Socket();
|
||||||
|
|
||||||
this._socket.onopen = () => this._onConnect();
|
this._socket.on("open", () => this._onConnect());
|
||||||
this._socket.onmessage = (event) => this._onMessage(event);
|
this._socket.on("close", () => this._onDisconnect());
|
||||||
this._socket.onerror = (event) => this._onError(event);
|
this._socket.on("error", (event) => this._onError(event));
|
||||||
this._socket.onclose = () => this._onDisconnect();
|
this._socket.on("message", (event) => this._onMessage(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
private _onMessage(event: MessageEvent): void {
|
private _onMessage(message: MessageEvent): void {
|
||||||
log("Connection", "received", event);
|
log("Connection", "received", message);
|
||||||
|
|
||||||
const messageObject = JSON.parse(event.data);
|
// TODO: Print error if not valid json.
|
||||||
|
const messageObject = JSON.parse(message.json);
|
||||||
|
|
||||||
if (this._waitForJoin) {
|
if (this._waitForJoin) {
|
||||||
this._client = messageObject.player as Client;
|
this._client = messageObject.player as Client;
|
||||||
@@ -84,13 +99,30 @@ export class Connection extends Evented<typeof ConnectionEvents> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We only emit game message events when we have joined.
|
// We only accept state messages events when we have joined.
|
||||||
if (this._client) {
|
if (this._client) {
|
||||||
const gameState: GameStateRaw = messageObject.gamestate;
|
const validated = validateMessageSchema(messageObject);
|
||||||
const gameMessage = new GameMessageEvent(
|
if (!validated) {
|
||||||
new GameState(gameState, this._client),
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (messageObject.state.stateType) {
|
||||||
|
case "lobby":
|
||||||
|
this.emit(
|
||||||
|
"lobby-message",
|
||||||
|
new LobbyMessageEvent(
|
||||||
|
new LobbyState(messageObject.state, this._client),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
this.emit("game-message", gameMessage);
|
break;
|
||||||
|
case "match":
|
||||||
|
this.emit(
|
||||||
|
"match-message",
|
||||||
|
new MatchMessageEvent(
|
||||||
|
new MatchState(messageObject.state, this._client),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,15 +201,20 @@ export class Connection extends Evented<typeof ConnectionEvents> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function storedClient(): Client | null {
|
export function storedClient(): Client | null {
|
||||||
const clientRaw = localStorage.getItem("client");
|
const clientString = localStorage.getItem("client");
|
||||||
if (clientRaw == null) {
|
if (clientString == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO: Do schema check.
|
const clientRaw = JSON.parse(clientString) as Client;
|
||||||
return JSON.parse(clientRaw) as Client;
|
if (!validateClientSchema(clientRaw)) {
|
||||||
} catch {
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientRaw;
|
||||||
|
} catch (error) {
|
||||||
|
log("Connection", error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
export type Id = number;
|
|
||||||
|
|
||||||
export interface GameStateRaw {
|
|
||||||
players: Player[];
|
|
||||||
spectators: Spectator[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export type GameStateRawReadonly = Readonly<GameStateRaw>;
|
|
||||||
|
|
||||||
export interface Player {
|
|
||||||
player: Client;
|
|
||||||
ready: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Spectator {
|
|
||||||
player: Client;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Client {
|
|
||||||
id: Id;
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
import {
|
import {
|
||||||
Client,
|
Client,
|
||||||
GameStateRawReadonly,
|
LobbyStateRawReadonly,
|
||||||
Id,
|
Id,
|
||||||
Player,
|
Player,
|
||||||
Spectator,
|
Spectator,
|
||||||
} from "./GameStateRaw";
|
} from "./jsonTypes";
|
||||||
import { maxPlayers } from "./main";
|
import { maxPlayers } from "./main";
|
||||||
import { log } from "./utils/debugUtils";
|
import { log } from "./utils/debugUtils";
|
||||||
|
|
||||||
export class GameState {
|
export class LobbyState {
|
||||||
_errors: string[] = [];
|
_errors: string[] = [];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public readonly gameStateRaw: GameStateRawReadonly,
|
public readonly lobbyStateRaw: LobbyStateRawReadonly,
|
||||||
readonly client: Client,
|
readonly client: Client,
|
||||||
) {
|
) {
|
||||||
this._validate();
|
this._validate();
|
||||||
@@ -28,16 +28,16 @@ export class GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this._errors.length > 0) {
|
if (this._errors.length > 0) {
|
||||||
log("GameState", this._errors);
|
log("LobbyState", this._errors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get players(): Player[] {
|
get players(): Player[] {
|
||||||
return this.gameStateRaw.players;
|
return this.lobbyStateRaw.players;
|
||||||
}
|
}
|
||||||
|
|
||||||
get spectators(): Spectator[] {
|
get spectators(): Spectator[] {
|
||||||
return this.gameStateRaw.spectators;
|
return this.lobbyStateRaw.spectators;
|
||||||
}
|
}
|
||||||
|
|
||||||
get isFull(): boolean {
|
get isFull(): boolean {
|
||||||
@@ -45,7 +45,7 @@ export class GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get isPlayer(): boolean {
|
get isPlayer(): boolean {
|
||||||
return this.players.map((p) => p.player.id).includes(this.client.id);
|
return this.players.map((p) => p.client.id).includes(this.client.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
get isReady(): boolean {
|
get isReady(): boolean {
|
||||||
@@ -53,14 +53,14 @@ export class GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get isSpectator(): boolean {
|
get isSpectator(): boolean {
|
||||||
return this.spectators.map((p) => p.player.id).includes(this.client.id);
|
return this.spectators.map((p) => p.client.id).includes(this.client.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
getPlayerById(id: Id): Player | undefined {
|
getPlayerById(id: Id): Player | undefined {
|
||||||
return this.players.find((p) => p.player.id === id);
|
return this.players.find((p) => p.client.id === id);
|
||||||
}
|
}
|
||||||
|
|
||||||
getSpectatorById(id: Id): Spectator | undefined {
|
getSpectatorById(id: Id): Spectator | undefined {
|
||||||
return this.spectators.find((p) => p.player.id === id);
|
return this.spectators.find((p) => p.client.id === id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { Client, MatchStateRawReadonly } from "./jsonTypes";
|
||||||
|
import { log } from "./utils/debugUtils";
|
||||||
|
|
||||||
|
export class MatchState {
|
||||||
|
_errors: string[] = [];
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public readonly matchStateRaw: MatchStateRawReadonly,
|
||||||
|
readonly client: Client,
|
||||||
|
) {
|
||||||
|
this._validate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _validate(): void {
|
||||||
|
if (this._errors.length > 0) {
|
||||||
|
log("MatchState", this._errors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WIP more utility functions.
|
||||||
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
import * as ex from "excalibur";
|
|
||||||
import { GameState } from "../GameState";
|
|
||||||
|
|
||||||
export class GameStateScene extends ex.Scene {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
public updateFromGameState(_gameState: GameState): void {}
|
|
||||||
}
|
|
||||||
@@ -1,23 +1,22 @@
|
|||||||
import * as ex from "excalibur";
|
import * as ex from "excalibur";
|
||||||
import { spacingL, spacingM } from "../utils/formattingUtils";
|
import { spacingL, spacingM } from "../utils/formattingUtils";
|
||||||
import { Cups } from "../Cups";
|
import { Cups } from "../Cups";
|
||||||
import { GameStateScene } from "./GameStateScene";
|
|
||||||
import { PlayerListScreenElement } from "../ScreenElements/PlayerListScreenElement";
|
import { PlayerListScreenElement } from "../ScreenElements/PlayerListScreenElement";
|
||||||
import { ClientListScreenElement } from "../ScreenElements/ClientListScreenElement";
|
import { ClientListScreenElement } from "../ScreenElements/ClientListScreenElement";
|
||||||
import { Button } from "../ScreenElements/Button";
|
import { Button } from "../ScreenElements/Button";
|
||||||
import { PlayerNameInput } from "../UIElements/PlayerNameInput";
|
import { PlayerNameInput } from "../UIElements/PlayerNameInput";
|
||||||
import { Connection, GameMessageEvent } from "../Connection";
|
import { Connection, LobbyMessageEvent } from "../Connection";
|
||||||
import { Title } from "../UIElements/Title";
|
import { Title } from "../UIElements/Title";
|
||||||
import { ErrorList } from "../UIElements/ErrorList";
|
import { ErrorList } from "../UIElements/ErrorList";
|
||||||
import { GameState } from "../GameState";
|
import { LobbyState } from "../LobbyState";
|
||||||
import { maxPlayers } from "../main";
|
import { maxPlayers } from "../main";
|
||||||
import { Id } from "../GameStateRaw";
|
import { Id } from "../jsonTypes";
|
||||||
|
|
||||||
const tableWidth = 400;
|
const tableWidth = 400;
|
||||||
|
|
||||||
export type Screens = "join" | "lobby";
|
export type Screens = "join" | "lobby";
|
||||||
|
|
||||||
export class LobbyScene extends GameStateScene {
|
export class LobbyScene extends ex.Scene {
|
||||||
private _table: ex.ScreenElement = null!;
|
private _table: ex.ScreenElement = null!;
|
||||||
private _cups: ex.ScreenElement = null!;
|
private _cups: ex.ScreenElement = null!;
|
||||||
|
|
||||||
@@ -39,7 +38,7 @@ export class LobbyScene extends GameStateScene {
|
|||||||
private _playerNameInput: PlayerNameInput = null!;
|
private _playerNameInput: PlayerNameInput = null!;
|
||||||
|
|
||||||
private _errorList: ErrorList = null!;
|
private _errorList: ErrorList = null!;
|
||||||
private _noConnectionErrorHandle: (() => void) | undefined;
|
private _noConnectionErrorHandle: (() => void) | null = null;
|
||||||
|
|
||||||
private _screen: "join" | "lobby" = "join";
|
private _screen: "join" | "lobby" = "join";
|
||||||
|
|
||||||
@@ -51,8 +50,8 @@ export class LobbyScene extends GameStateScene {
|
|||||||
this._playerNameInput.clear();
|
this._playerNameInput.clear();
|
||||||
this.setScreen("join");
|
this.setScreen("join");
|
||||||
});
|
});
|
||||||
this._connection.on("game-message", (e: GameMessageEvent) =>
|
this._connection.on("lobby-message", (e: LobbyMessageEvent) =>
|
||||||
this.updateFromGameState(e.gameState),
|
this.updateFromMessage(e.lobbyState),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,9 +143,18 @@ export class LobbyScene extends GameStateScene {
|
|||||||
|
|
||||||
this.add(this._errorList);
|
this.add(this._errorList);
|
||||||
|
|
||||||
|
// Wait for two seconds before displaying an error.
|
||||||
|
const errorTimeout = setTimeout(() => {
|
||||||
|
this._noConnectionErrorHandle = this._errorList.addError(
|
||||||
|
"No server connection",
|
||||||
|
);
|
||||||
|
}, 2000);
|
||||||
|
|
||||||
this._connection.on("connect-or-disconnect", ({ connected }) => {
|
this._connection.on("connect-or-disconnect", ({ connected }) => {
|
||||||
if (connected) {
|
if (connected) {
|
||||||
|
clearTimeout(errorTimeout);
|
||||||
this._noConnectionErrorHandle?.();
|
this._noConnectionErrorHandle?.();
|
||||||
|
this._noConnectionErrorHandle = null;
|
||||||
} else if (this._noConnectionErrorHandle == null) {
|
} else if (this._noConnectionErrorHandle == null) {
|
||||||
this._noConnectionErrorHandle = this._errorList.addError(
|
this._noConnectionErrorHandle = this._errorList.addError(
|
||||||
"No server connection",
|
"No server connection",
|
||||||
@@ -178,7 +186,7 @@ export class LobbyScene extends GameStateScene {
|
|||||||
return bar;
|
return bar;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _updateVisibility(gameState?: GameState): void {
|
private _updateVisibility(gameState?: LobbyState): void {
|
||||||
const isJoinScreen = this._screen === "join";
|
const isJoinScreen = this._screen === "join";
|
||||||
const setVisible = (g: ex.Actor, visible: boolean): void => {
|
const setVisible = (g: ex.Actor, visible: boolean): void => {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
@@ -252,7 +260,7 @@ export class LobbyScene extends GameStateScene {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public updateFromGameState(gameState: GameState): void {
|
public updateFromMessage(gameState: LobbyState): void {
|
||||||
this._playerList.updateFromGameState(gameState.players, gameState.client);
|
this._playerList.updateFromGameState(gameState.players, gameState.client);
|
||||||
this._spectatorsList.updateFromGameState(
|
this._spectatorsList.updateFromGameState(
|
||||||
gameState.spectators,
|
gameState.spectators,
|
||||||
@@ -265,6 +273,7 @@ export class LobbyScene extends GameStateScene {
|
|||||||
public setScreen(screen: Screens): void {
|
public setScreen(screen: Screens): void {
|
||||||
this._screen = screen;
|
this._screen = screen;
|
||||||
this._updatePositions();
|
this._updatePositions();
|
||||||
|
this._updateVisibility();
|
||||||
}
|
}
|
||||||
|
|
||||||
private _join(name: string): void {
|
private _join(name: string): void {
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
import { Connection } from "../Connection";
|
import { Connection } from "../Connection";
|
||||||
import { GameStateScene } from "./GameStateScene";
|
|
||||||
import { Ground, groundColor } from "../GameElements/Ground";
|
import { Ground, groundColor } from "../GameElements/Ground";
|
||||||
import { defaultTableConfiguration, Table } from "../GameElements/Table";
|
import { defaultTableConfiguration, Table } from "../GameElements/Table";
|
||||||
import { Cup, defaultCupConfiguration } from "../GameElements/Cups";
|
import { Cup, defaultCupConfiguration } from "../GameElements/Cups";
|
||||||
import { Color, vec } from "excalibur";
|
import { Color, Scene, vec } from "excalibur";
|
||||||
import { Ball, defaultBallConfiguration } from "../GameElements/Ball";
|
import { Ball, defaultBallConfiguration } from "../GameElements/Ball";
|
||||||
import { groundHeight } from "../utils/physicsConstant";
|
import { groundHeight } from "../utils/physicsConstant";
|
||||||
import { Champion } from "../GameElements/Champion";
|
import { Champion } from "../GameElements/Champion";
|
||||||
import { randomColor } from "../utils/randomUtils";
|
import { randomColor } from "../utils/randomUtils";
|
||||||
import { colorDistance } from "../utils/colorUtils";
|
import { colorDistance } from "../utils/colorUtils";
|
||||||
|
|
||||||
export class GameScene extends GameStateScene {
|
export class MatchScene extends Scene {
|
||||||
private _ground: Ground = null!;
|
private _ground: Ground = null!;
|
||||||
private _table: Table = null!;
|
private _table: Table = null!;
|
||||||
private _ball: Ball = null!;
|
private _ball: Ball = null!;
|
||||||
@@ -18,7 +17,6 @@ export class GameScene extends GameStateScene {
|
|||||||
private _cups: Cup[] = [];
|
private _cups: Cup[] = [];
|
||||||
private _champions: Champion[] = [];
|
private _champions: Champion[] = [];
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
constructor(private _connection: Connection) {
|
constructor(private _connection: Connection) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@ import * as ex from "excalibur";
|
|||||||
import { spacingS } from "../utils/formattingUtils";
|
import { spacingS } from "../utils/formattingUtils";
|
||||||
import { headingFont, paragraphFont } from "../utils/fontUtil";
|
import { headingFont, paragraphFont } from "../utils/fontUtil";
|
||||||
import { Text } from "./Text";
|
import { Text } from "./Text";
|
||||||
import { Client, Player, Spectator } from "../GameStateRaw";
|
import { Client, Player, Spectator } from "../jsonTypes";
|
||||||
|
|
||||||
const clientColor = ex.Color.fromHex("40E0D0");
|
const clientColor = ex.Color.fromHex("40E0D0");
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ export class ClientListScreenElement extends ex.ScreenElement {
|
|||||||
let y = headingFont.size + spacingS;
|
let y = headingFont.size + spacingS;
|
||||||
|
|
||||||
for (const currentClient of this._clients) {
|
for (const currentClient of this._clients) {
|
||||||
const isClient = currentClient.player.id === this._client?.id;
|
const isClient = currentClient.client.id === this._client?.id;
|
||||||
const label = this.createLabel(currentClient, y, isClient);
|
const label = this.createLabel(currentClient, y, isClient);
|
||||||
|
|
||||||
y += label.font.size + spacingS;
|
y += label.font.size + spacingS;
|
||||||
@@ -77,7 +77,7 @@ export class ClientListScreenElement extends ex.ScreenElement {
|
|||||||
isClient: boolean,
|
isClient: boolean,
|
||||||
): Text {
|
): Text {
|
||||||
const color = isClient ? clientColor : undefined;
|
const color = isClient ? clientColor : undefined;
|
||||||
const { name } = client.player;
|
const { name } = client.client;
|
||||||
const label = new Text({
|
const label = new Text({
|
||||||
text: name,
|
text: name,
|
||||||
font: paragraphFont,
|
font: paragraphFont,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { spacingS } from "../utils/formattingUtils";
|
|||||||
import { disabledFont } from "../utils/fontUtil";
|
import { disabledFont } from "../utils/fontUtil";
|
||||||
import { Text } from "./Text";
|
import { Text } from "./Text";
|
||||||
import { Checkbox } from "./Checkbox";
|
import { Checkbox } from "./Checkbox";
|
||||||
import { Player } from "../GameStateRaw";
|
import { Player } from "../jsonTypes";
|
||||||
import { ClientListScreenElement } from "./ClientListScreenElement";
|
import { ClientListScreenElement } from "./ClientListScreenElement";
|
||||||
|
|
||||||
export class PlayerListScreenElement extends ClientListScreenElement {
|
export class PlayerListScreenElement extends ClientListScreenElement {
|
||||||
@@ -23,7 +23,7 @@ export class PlayerListScreenElement extends ClientListScreenElement {
|
|||||||
const clients = this._clients.slice(0, this._maxPlayers);
|
const clients = this._clients.slice(0, this._maxPlayers);
|
||||||
|
|
||||||
for (const currentClient of clients) {
|
for (const currentClient of clients) {
|
||||||
const { name, id } = currentClient.player;
|
const { name, id } = currentClient.client;
|
||||||
const isClient = id === this._client?.id;
|
const isClient = id === this._client?.id;
|
||||||
const label = this.createLabel(currentClient, y, isClient);
|
const label = this.createLabel(currentClient, y, isClient);
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ 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, storedClient } from "../Connection";
|
import { Connection, storedClient } from "../Connection";
|
||||||
import { Id } from "../GameStateRaw";
|
import { Id } from "../jsonTypes";
|
||||||
|
|
||||||
export class PlayerNameInput extends ex.ScreenElement {
|
export class PlayerNameInput extends ex.ScreenElement {
|
||||||
private _nameLabel: Text = null!;
|
private _nameLabel: Text = null!;
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
export type Id = number;
|
||||||
|
|
||||||
|
export interface MessageRaw {
|
||||||
|
state: LobbyStateRaw | MatchStateRaw;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LobbyStateRaw {
|
||||||
|
stateType: "lobby";
|
||||||
|
players: Player[];
|
||||||
|
spectators: Spectator[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type LobbyStateRawReadonly = Readonly<LobbyStateRaw>;
|
||||||
|
|
||||||
|
export interface Player {
|
||||||
|
client: Client;
|
||||||
|
ready: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Spectator {
|
||||||
|
client: Client;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Client {
|
||||||
|
id: Id;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MatchStateRaw {
|
||||||
|
stateType: "match";
|
||||||
|
history: (Table | Action)[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MatchStateRawReadonly = Readonly<MatchStateRaw>;
|
||||||
|
|
||||||
|
export interface Table {
|
||||||
|
cupsA: [boolean, boolean, boolean];
|
||||||
|
cupsB: [boolean, boolean, boolean];
|
||||||
|
teamA: [Id, Id, Id];
|
||||||
|
teamB: [Id, Id, Id];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Action {
|
||||||
|
turn: "a" | "b";
|
||||||
|
parabola: Parabola;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Parabola {
|
||||||
|
startPosition: Vector2;
|
||||||
|
startVelocity: Vector2;
|
||||||
|
stopTime: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Vector2 = [number, number];
|
||||||
+21
-29
@@ -1,25 +1,30 @@
|
|||||||
import * as ex from "excalibur";
|
import * as ex from "excalibur";
|
||||||
import { LobbyScene } from "./Scenes/LobbyScene";
|
import { LobbyScene } from "./Scenes/LobbyScene";
|
||||||
import { Connection } from "./Connection";
|
import { Connection } from "./Connection";
|
||||||
import { GameStateRaw } from "./GameStateRaw";
|
import { MatchScene } from "./Scenes/MatchScene";
|
||||||
import { GameState } from "./GameState";
|
import {
|
||||||
import { GameScene } from "./Scenes/GameScene";
|
mockGameMessage,
|
||||||
|
mockLobbyMessage,
|
||||||
|
MockSocket,
|
||||||
|
} from "./sockets/MockSocket";
|
||||||
|
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
const debugSkipTo = urlParams.get("skipTo");
|
const mocked = urlParams.get("mock");
|
||||||
|
|
||||||
export const maxPlayers = 2;
|
export const maxPlayers = 2;
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const connection = new Connection();
|
const connection = new Connection();
|
||||||
connection.connect();
|
|
||||||
|
const mockSocket = new MockSocket();
|
||||||
|
connection.connect(mocked != null ? mockSocket : undefined);
|
||||||
|
|
||||||
const lobbyScene = new LobbyScene(connection);
|
const lobbyScene = new LobbyScene(connection);
|
||||||
const gameScene = new GameScene(connection);
|
const matchScene = new MatchScene(connection);
|
||||||
|
|
||||||
const game = new ex.Engine({
|
const game = new ex.Engine({
|
||||||
displayMode: ex.DisplayMode.FillScreen,
|
displayMode: ex.DisplayMode.FillScreen,
|
||||||
scenes: { lobby: lobbyScene, game: gameScene },
|
scenes: { lobby: lobbyScene, match: matchScene },
|
||||||
backgroundColor: ex.Color.fromHex("23213b"),
|
backgroundColor: ex.Color.fromHex("23213b"),
|
||||||
});
|
});
|
||||||
game.toggleDebug();
|
game.toggleDebug();
|
||||||
@@ -27,27 +32,14 @@ export const maxPlayers = 2;
|
|||||||
await game.start();
|
await game.start();
|
||||||
await game.goToScene("lobby");
|
await game.goToScene("lobby");
|
||||||
|
|
||||||
if (debugSkipTo === "lobby") {
|
connection.on("lobby-message", () => game.goToScene("lobby"));
|
||||||
lobbyScene.setScreen("lobby");
|
connection.on("match-message", () => game.goToScene("match"));
|
||||||
lobbyScene.updateFromGameState(dummyGameState());
|
|
||||||
} else if (debugSkipTo === "game") {
|
if (mocked === "lobby") {
|
||||||
await game.goToScene("game");
|
connection.join("Dubbel");
|
||||||
|
mockSocket.sendToClient(mockLobbyMessage());
|
||||||
|
} else if (mocked === "match") {
|
||||||
|
connection.join("Dubbel");
|
||||||
|
mockSocket.sendToClient(mockGameMessage());
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
function dummyGameState(): GameState {
|
|
||||||
const you = { name: "Dubbel", id: 0 };
|
|
||||||
|
|
||||||
const dummyState: GameStateRaw = {
|
|
||||||
players: [
|
|
||||||
{ player: { name: "Dubbel", id: 0 }, ready: true },
|
|
||||||
{ player: { name: "Yeah", id: 1 }, ready: false },
|
|
||||||
],
|
|
||||||
spectators: [
|
|
||||||
{ player: { name: "Dubbel", id: 2 } },
|
|
||||||
{ player: { name: "Cocksucker", id: 3 } },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
return new GameState(dummyState, you);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import Ajv, { ValidateFunction } from "ajv";
|
||||||
|
|
||||||
|
import { log } from "./utils/debugUtils";
|
||||||
|
import { Client, MessageRaw } from "./jsonTypes";
|
||||||
|
import messageSchemaFile from "../../schema/message_schema.json";
|
||||||
|
import clientSchemaFile from "../../schema/client_schema.json";
|
||||||
|
import lobbyStateSchemaFile from "../../schema/lobby_state_schema.json";
|
||||||
|
import matchStateSchemaFile from "../../schema/match_state_schema.json";
|
||||||
|
import actionSchemaFile from "../../schema/action_schema.json";
|
||||||
|
import playerSchemaFile from "../../schema/player_schema.json";
|
||||||
|
import spectatorSchemaFile from "../../schema/spectator_schema.json";
|
||||||
|
import tableSchemaFile from "../../schema/table_schema.json";
|
||||||
|
|
||||||
|
const ajv = new Ajv({ strict: true });
|
||||||
|
|
||||||
|
ajv.addSchema(lobbyStateSchemaFile, "lobby_state_schema.json");
|
||||||
|
ajv.addSchema(matchStateSchemaFile, "match_state_schema.json");
|
||||||
|
ajv.addSchema(actionSchemaFile, "action_schema.json");
|
||||||
|
ajv.addSchema(clientSchemaFile, "client_schema.json");
|
||||||
|
ajv.addSchema(playerSchemaFile, "player_schema.json");
|
||||||
|
ajv.addSchema(spectatorSchemaFile, "spectator_schema.json");
|
||||||
|
ajv.addSchema(tableSchemaFile, "table_schema.json");
|
||||||
|
|
||||||
|
const validateMessage = ajv.compile(messageSchemaFile);
|
||||||
|
const validateClient = ajv.compile(clientSchemaFile);
|
||||||
|
|
||||||
|
export function validateMessageSchema(json: unknown): json is MessageRaw {
|
||||||
|
return validate(json, validateMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateClientSchema(json: unknown): json is Client {
|
||||||
|
return validate(json, validateClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validate(
|
||||||
|
json: unknown,
|
||||||
|
validateFunction: ValidateFunction,
|
||||||
|
): boolean {
|
||||||
|
const valid = validateFunction(json);
|
||||||
|
|
||||||
|
if (!valid) {
|
||||||
|
log("Connection", "invalid message received", {
|
||||||
|
json,
|
||||||
|
errors: validateFunction.errors,
|
||||||
|
});
|
||||||
|
console.log(validateFunction.errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { Event, Evented } from "../utils/Evented";
|
||||||
|
|
||||||
|
export class MessageEvent extends Event {
|
||||||
|
// TODO: Turn into union of all possible messages.
|
||||||
|
constructor(public json: string) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const BaseSocketEvents = {
|
||||||
|
open: Event,
|
||||||
|
close: Event,
|
||||||
|
error: globalThis.Event,
|
||||||
|
message: MessageEvent,
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface BaseSocket extends Evented<typeof BaseSocketEvents> {
|
||||||
|
send: (message: string) => void;
|
||||||
|
connected: boolean;
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import { Evented } from "../utils/Evented";
|
||||||
|
import { BaseSocket, BaseSocketEvents } from "./BaseSocket";
|
||||||
|
|
||||||
|
export class MockSocket
|
||||||
|
extends Evented<typeof BaseSocketEvents>
|
||||||
|
implements BaseSocket
|
||||||
|
{
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
setTimeout(() => {
|
||||||
|
this.emit("open", {});
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
get connected(): boolean {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendToClient(json: string): void {
|
||||||
|
this.emit("message", { json });
|
||||||
|
}
|
||||||
|
|
||||||
|
send(message: string): void {
|
||||||
|
if (message.includes("join")) {
|
||||||
|
this.sendToClient(JSON.stringify({ player: { id: 0, name: "Dubbel" } }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mockLobbyMessage(): string {
|
||||||
|
const lobbyUpdate = {
|
||||||
|
state: {
|
||||||
|
stateType: "lobby",
|
||||||
|
players: [
|
||||||
|
{ client: { name: "Dubbel", id: 0 }, ready: true },
|
||||||
|
{ client: { name: "Yeah", id: 1 }, ready: false },
|
||||||
|
],
|
||||||
|
spectators: [
|
||||||
|
{ client: { name: "Dubbel", id: 2 } },
|
||||||
|
{ client: { name: "Cocksucker", id: 3 } },
|
||||||
|
{ client: { name: "Cocksucker 2", id: 4 } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return JSON.stringify(lobbyUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mockGameMessage(): string {
|
||||||
|
const matchUpdate = {
|
||||||
|
state: {
|
||||||
|
stateType: "match",
|
||||||
|
history: [
|
||||||
|
{
|
||||||
|
cupsA: [true, true, true],
|
||||||
|
cupsB: [true, true, true],
|
||||||
|
teamA: [0, 1, 2],
|
||||||
|
teamB: [3, 1, 4],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return JSON.stringify(matchUpdate);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { assert } from "../utils/assertUtils";
|
||||||
|
import { Evented } from "../utils/Evented";
|
||||||
|
import { BaseSocket, BaseSocketEvents } from "./BaseSocket";
|
||||||
|
|
||||||
|
export class Socket
|
||||||
|
extends Evented<typeof BaseSocketEvents>
|
||||||
|
implements BaseSocket
|
||||||
|
{
|
||||||
|
private _webSocket: WebSocket;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this._webSocket = new WebSocket("ws://127.0.0.1:8888");
|
||||||
|
|
||||||
|
this._webSocket.onopen = () => this.emit("open", {});
|
||||||
|
this._webSocket.onclose = () => this.emit("close", {});
|
||||||
|
this._webSocket.onerror = (event) => this.emit("error", event);
|
||||||
|
this._webSocket.onmessage = (event) => {
|
||||||
|
assert(typeof event.data === "string");
|
||||||
|
this.emit("message", { json: event.data });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
get connected(): boolean {
|
||||||
|
return this._webSocket.readyState === this._webSocket.OPEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
send(message: string): void {
|
||||||
|
this._webSocket.send(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
export type LogSource = "Connection" | "GameState";
|
export type LogSource = "Connection" | "LobbyState" | "MatchState";
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
export function log(source: LogSource, ...data: any[]): void {
|
export function log(source: LogSource, ...data: any[]): void {
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"title": "Action",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"parabola": { "$ref": "#/$defs/parabola" },
|
||||||
|
"turn": { "$ref": "#/$defs/turn" }
|
||||||
|
},
|
||||||
|
"required": ["parabola", "turn"],
|
||||||
|
"additionalProperties": false,
|
||||||
|
"$defs": {
|
||||||
|
"turn": {
|
||||||
|
"enum": ["a", "b"]
|
||||||
|
},
|
||||||
|
"parabola": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"startPosition": { "$ref": "#/$defs/vector2" },
|
||||||
|
"startVelocity": { "$ref": "#/$defs/vector2" },
|
||||||
|
"stopTime": { "type": "number" }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"vector2": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "type": "number" },
|
||||||
|
"minItems": 2,
|
||||||
|
"maxItems": 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"title": "Client",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "number",
|
||||||
|
"minimum": 0
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["id", "name"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"title": "LobbyState",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"stateType": { "enum": ["lobby"] },
|
||||||
|
"players": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "./player_schema.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"spectators": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "./spectator_schema.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["stateType", "players", "spectators"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"title": "MatchState",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"stateType": { "enum": ["match"]},
|
||||||
|
"history": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"anyOf": [
|
||||||
|
{ "$ref": "./table_schema.json" },
|
||||||
|
{ "$ref": "./action_schema.json" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["stateType", "history"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"title": "Message",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"state": {
|
||||||
|
"anyOf": [
|
||||||
|
{ "$ref": "./lobby_state_schema.json"},
|
||||||
|
{ "$ref": "./match_state_schema.json"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["state"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"title": "Player",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"client": {
|
||||||
|
"$ref": "./client_schema.json"
|
||||||
|
},
|
||||||
|
"ready": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["client", "ready"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"title": "Spectator",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"client": {
|
||||||
|
"$ref": "./client_schema.json"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["client"],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"title": "Table",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"cupsA": { "$ref": "#/$defs/cups" },
|
||||||
|
"cupsB": { "$ref": "#/$defs/cups" },
|
||||||
|
"teamA": { "$ref": "#/$defs/team" },
|
||||||
|
"teamB": { "$ref": "#/$defs/team" }
|
||||||
|
},
|
||||||
|
"required": ["cupsA", "cupsB", "teamA", "teamB"],
|
||||||
|
"additionalProperties": false,
|
||||||
|
"$defs": {
|
||||||
|
"cups": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "type": "boolean" },
|
||||||
|
"minItems": 3,
|
||||||
|
"maxItems": 3
|
||||||
|
},
|
||||||
|
"team": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "type": "number" },
|
||||||
|
"minItems": 3,
|
||||||
|
"maxItems": 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user