forked from CreaturePhil/Showdown-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 31
/
room-game.js
246 lines (221 loc) · 6.24 KB
/
room-game.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* Room games
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Room games are an abstract representation of an activity that a room
* can be focused on, such as a battle, tournament, or chat game like
* Hangman. Rooms are limited to one roomgame at a time.
*
* Room games can keep track of designated players. If a user is a player,
* they will not be allowed to change name until their games are complete.
*
* The player system is optional: Some games, like Hangman, don't designate
* players and just allow any user in the room to play.
*
* @license MIT license
*/
'use strict';
// globally Rooms.RoomGamePlayer
class RoomGamePlayer {
/**
* @param {User} user
* @param {RoomGame} game
*/
constructor(user, game) {
// we explicitly don't hold a reference to the user
this.userid = user.userid;
this.name = user.name;
this.game = game;
user.games.add(this.game.id);
user.updateSearch();
}
destroy() {
let user = Users.getExact(this.userid);
if (user) {
user.games.delete(this.game.id);
user.updateSearch();
}
}
toString() {
return this.userid;
}
/**
* @param {string} data
*/
send(data) {
let user = Users.getExact(this.userid);
if (user) user.send(data);
}
/**
* @param {string} data
*/
sendRoom(data) {
let user = Users.getExact(this.userid);
if (user) user.sendTo(this.game.id, data);
}
}
// globally Rooms.RoomGame
class RoomGame {
/**
* @param {ChatRoom | GameRoom} room
*/
constructor(room) {
this.id = room.id;
/** @type {ChatRoom | GameRoom} */
this.room = room;
this.gameid = 'game';
this.title = 'Game';
this.allowRenames = false;
this.players = Object.create(null);
this.playerCount = 0;
this.playerCap = 0;
this.ended = false;
}
destroy() {
this.room.game = null;
this.room = /** @type {any} */ (null);
for (let i in this.players) {
this.players[i].destroy();
}
}
/**
* @param {User} user
* @param {any[]} rest
*/
addPlayer(user, ...rest) {
if (user.userid in this.players) return false;
if (this.playerCount >= this.playerCap) return false;
let player = this.makePlayer(user, ...rest);
if (!player) return false;
this.players[user.userid] = player;
this.playerCount++;
return true;
}
/**
* @param {User} user
* @param {any[]} rest
*/
makePlayer(user, ...rest) {
return new RoomGamePlayer(user, this);
}
/**
* @param {User} user
*/
removePlayer(user) {
if (!this.allowRenames) return false;
if (!(user.userid in this.players)) return false;
this.players[user.userid].destroy();
delete this.players[user.userid];
this.playerCount--;
return true;
}
/**
* @param {User} user
* @param {string} oldUserid
*/
renamePlayer(user, oldUserid) {
if (user.userid === oldUserid) {
this.players[user.userid].name = user.name;
} else {
this.players[user.userid] = this.players[oldUserid];
this.players[user.userid].userid = user.userid;
this.players[user.userid].name = user.name;
delete this.players[oldUserid];
}
}
// Commands:
// These are all optional to implement:
// forfeit(user)
// Called when a user uses /forfeit
// Also planned to be used for some force-forfeit situations, such
// as when a user changes their name and .allowRenames === false
// This is strongly recommended to be supported, as the user is
// extremely unlikely to keep playing after this function is
// called.
// choose(user, text)
// Called when a user uses /choose [text]
// If you have buttons, you are recommended to use this interface
// instead of making your own commands.
// undo(user, text)
// Called when a user uses /undo [text]
// joinGame(user, text)
// Called when a user uses /joingame [text]
// leaveGame(user, text)
// Called when a user uses /leavegame [text]
// Events:
// Note:
// A user can have multiple connections. For instance, if you have
// two tabs open and connected to PS, those tabs represent two
// connections, but a single PS user. Each tab can be in separate
// rooms.
/**
* Called when a user joins a room. (i.e. when the user's first
* connection joins)
*
* While connection is passed, it should not usually be used:
* Any handling of connections should happen in onConnect.
* @param {User} user
* @param {Connection} connection
*/
onJoin(user, connection) {}
/**
* Called when a user is banned from the room this game is taking
* place in.
* @param {User} user
*/
removeBannedUser(user) {}
/**
* Called when a user in the game is renamed. `isJoining` is true
* if the user was previously a guest, but now has a username.
* Check `!user.named` for the case where a user previously had a
* username but is now a guest. By default, updates a player's
* name as long as allowRenames is set to true.
* @param {User} user
* @param {string} oldUserid
* @param {boolean} isJoining
* @param {boolean} isForceRenamed
*/
onRename(user, oldUserid, isJoining, isForceRenamed) {
if (!this.allowRenames || (!user.named && !isForceRenamed)) {
if (!(user.userid in this.players)) {
user.games.delete(this.id);
user.updateSearch();
}
return;
}
if (!(oldUserid in this.players)) return;
this.renamePlayer(user, oldUserid);
}
/**
* Called when a user leaves the room. (i.e. when the user's last
* connection leaves)
* @param {User} user
*/
onLeave(user) {}
/**
* Called each time a connection joins a room (after onJoin if
* applicable). By default, this is also called when connection
* is updated in some way (such as by changing user or renaming).
* If you don't want this behavior, override onUpdateConnection
* and/or onRename.
* @param {User} user
* @param {Connection} connection
*/
onConnect(user, connection) {}
/**
* Called for each connection in a room that changes users by
* merging into a different user. By default, runs the onConnect
* handler.
* Player updates and an up-to-date report of what's going on in
* the game should be sent during `onConnect`. You should rarely
* need to handle the other events.
* @param {User} user
* @param {Connection} connection
*/
onUpdateConnection(user, connection) {
this.onConnect(user, connection);
}
}
// these exports are traditionally attached to rooms.js
exports.RoomGame = RoomGame;
exports.RoomGamePlayer = RoomGamePlayer;