forked from ably-labs/Ablingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbingo.lib.server.js
144 lines (114 loc) · 4.8 KB
/
bingo.lib.server.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class BingoServer {
constructor(identity, gameId, ably, onHostRejection) {
this.identity = identity;
this.gameId = gameId;
this.ably = ably;
this.ticker = null;
this.onHostRejection = onHostRejection || (() => { });
this.state = {
settings: { server: identity, gameId: gameId, automark: false },
status: "not-started",
players: [],
prizes: this.defaultPrizesObject()
};
this.caller = null;
this.playerBingoCards = {};
}
async connect() {
await this.ably.connectToGameChannel(this.identity, this.gameId);
this.ably.sendMessage({ kind: "host-offer", serverState: this.state });
}
start(tickIntervalMs) {
this.state.status = "running";
this.stop();
this.caller = new BingoCaller();
this.state.prizes = this.defaultPrizesObject();
this.ably.sendMessage({ kind: "new-game", serverState: this.state });
for (let player of this.state.players) {
this.issueBingoCardToPlayer(player);
}
this.sendGameInfo();
this.ticker = setInterval(() => { this.onGameTick(); }, tickIntervalMs);
}
stop() {
if (this.ticker) { clearInterval(this.ticker); }
}
onGameTick() {
const nextCall = this.caller.getNextCall();
if (nextCall.exhausted) {
this.gameOver(null, "We ran out of numbers!");
return;
}
this.ably.sendMessage({ kind: "bingo-caller-message", serverState: this.state, text: `${nextCall.call}`, number: nextCall.number });
}
onReceiveMessage(message) {
switch(message.kind) {
case "host-offer": this.onReceivedHostOffer(message); break;
case "host-reject": this.onReceivedHostRejection(message); break;
case "connected": this.onClientConnected(message); break;
case "bingo": this.onBingo(message); break;
default: () => { };
}
}
onReceivedHostOffer(message) {
if (!this.ably.messageIsFromMyself(message)) {
this.removePlayer(message.metadata.clientId);
this.ably.sendMessage({ kind: "host-reject", serverState: this.state }, message.metadata.clientId);
}
}
onReceivedHostRejection(message) {
this.onHostRejection("I cannot host in this channel, there is already a host!");
}
onClientConnected(message) {
if (this.state.status === "running") {
this.ably.sendMessage({ kind: "game-already-started", serverState: this.state }, message.metadata.clientId);
return;
}
const alreadyConnected = this.state.players.filter(x => x.clientId === message.metadata.clientId).length > 0;
if (!alreadyConnected) {
this.state.players.push(message.metadata);
this.ably.sendMessage({ kind: "connection-acknowledged", serverState: this.state }, message.metadata.clientId);
}
this.sendGameInfo();
}
onBingo(message) {
const numbersPlayerObserved = message.numbers || [];
const validMarks = this.caller.calledNumbers.slice().filter(number => numbersPlayerObserved.indexOf(number) > -1);
const thisPlayer = this.state.players.filter(p => p.clientId == message.metadata.clientId)[0];
const playerBingoCard = this.playerBingoCards[message.metadata.clientId];
const highestAward = playerBingoCard.checkForAwards(validMarks).award;
if (highestAward == "none") {
return;
}
const winnerForThisAward = this.state.prizes[highestAward];
if (winnerForThisAward != null) {
return; // Prize already claimed
}
this.state.prizes[highestAward] = thisPlayer;
this.ably.sendMessage({ kind: "prize-awarded", serverState: this.state, prize: highestAward, player: thisPlayer });
if (highestAward === "full-house") {
this.gameOver(message.metadata, "Full house")
}
}
sendGameInfo() {
this.ably.sendMessage({ kind: "game-info", serverState: this.state });
}
gameOver(winner, reason) {
this.stop();
this.state.status = "complete";
this.ably.sendMessage({ kind: "game-complete", serverState: this.state, reason: reason, winner: winner });
}
issueBingoCardToPlayer(clientMetadata) {
const newBingoCard = new BingoCard();
this.playerBingoCards[clientMetadata.clientId] = newBingoCard;
this.ably.sendMessage({ kind: "bingo-card-issued", serverState: this.state, card: newBingoCard }, clientMetadata.clientId);
}
removePlayer(clientId) {
this.state.players = this.state.players.filter(p => p.clientId !== clientId);
delete this.playerBingoCards[clientId];
}
defaultPrizesObject() { return { "one-line": null, "two-line": null, "full-house": null } };
}
try {
module.exports = { BingoServer };
} catch { }