-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
player.ts
121 lines (99 loc) · 3.26 KB
/
player.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import JsonRpc from "https://deno.land/x/[email protected]/mod.ts";
import Game, { InfoOptions } from "./game.ts";
import { GameType } from "../src/rules.ts";
import Board from "../src/board.ts";
import { sum } from "../src/score.ts";
export default class Player {
name = "";
score = 0;
key = Math.random().toString().replace(/\D/g, "");
game: Game | null = null;
roundEnded = false;
board: any | null = null;
bonusPool: any | null = null;
jsonrpc: JsonRpc;
constructor(ws: WebSocket) {
const io = {
sendData(str: string) { try { ws.send(str); } catch (e) {} },
onData(_str: string) {}
}
let jsonrpc = new JsonRpc(io);
this.jsonrpc = jsonrpc;
this._exposeInterface(jsonrpc);
ws.addEventListener("message", e => io.onData(e.data));
ws.addEventListener("close", () => {
const { game } = this;
this._log("disconnected");
if (game && game.state == "starting") { game.removePlayer(this); }
});
}
toJSON(options: InfoOptions) {
let data = {
name: this.name,
roundEnded: this.roundEnded,
board: null
};
if (options.board) { data.board = this.board; }
return data;
}
_log(msg: string, ...args: unknown[]) {
return console.log(`[player ${this.name}] ${msg}`, ...args);
}
_exposeInterface(jsonrpc: JsonRpc) {
// setup
jsonrpc.expose("create-game", (gameType: GameType, gameName: string, playerName: string) => {
this.name = playerName;
this.game = new Game(gameType, gameName, this);
return this.key;
});
jsonrpc.expose("join-game", (gameName: string, playerName: string) => {
const game = Game.find(gameName);
if (!game) { throw new Error(`Game "${gameName}" does not exist`); }
this.name = playerName;
this._log("joined game", gameName);
game.addPlayer(this); // will notify all
return this.key;
});
jsonrpc.expose("start-game", () => {
const game = this.game;
if (!game) { throw new Error("Cannot start a non-joined game"); }
if (game.owner != this) { throw new Error("Only the game owner can start it"); }
this._log("starting the game");
game.start(); // notify all
});
jsonrpc.expose("continue-game", (gameName: string, key: string) => {
const game = Game.find(gameName);
if (!game) { throw new Error(`Game "${gameName}" does not exist`); }
let previousPlayer = game.playerByKey(key);
if (!previousPlayer) { throw new Error("The continuation key is invalid"); }
game.replacePlayer(this, previousPlayer);
this._log("continued game", gameName);
return {
board: this.board,
bonusPool: this.bonusPool
};
});
// gameplay
jsonrpc.expose("end-round", (data:any) => {
const game = this.game;
if (!game) { throw new Error("Not playing yet"); }
this.roundEnded = true;
this.board = data.board;
this.bonusPool = data.bonusPool;
let score = new Board().fromJSON(this.board).getScore();
this.score = sum(score);
this._log("round ended, score", this.score);
game.checkRoundEnd(); // notify all
});
jsonrpc.expose("quit-game", () => {
const game = this.game;
if (!game) { throw new Error("Cannot quit a non-joined game"); }
this._log("left the game");
game.removePlayer(this); // notify others
});
jsonrpc.expose("game-info", () => {
const game = this.game;
return (game ? game.getInfo({board:false}) : null);
});
}
}