forked from XpRienzo/DragonHeaven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room-game.js
180 lines (154 loc) · 4.8 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
/**
* 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 {
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;
}
send(data) {
let user = Users.getExact(this.userid);
if (user) user.send(data);
}
sendRoom(data) {
let user = Users.getExact(this.userid);
if (user) user.sendTo(this.game.id, data);
}
}
// globally Rooms.RoomGame
class RoomGame {
constructor(room) {
this.id = room.id;
this.room = room;
this.gameid = 'game';
this.title = 'Game';
this.allowRenames = false;
this.players = Object.create(null);
this.playerCount = 0;
this.playerCap = 0;
}
destroy() {
this.room = null;
for (let i in this.players) {
this.players[i].destroy();
}
}
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;
}
makePlayer(user) {
return new RoomGamePlayer(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;
}
// 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.
// onJoin(user)
// Called when a user joins a room. (i.e. when the user's first
// connection joins)
// onRename(user, oldUserid, isJoining)
// 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.
// onLeave(user)
// Called when a user leaves the room. (i.e. when the user's last
// connection leaves)
// onConnect(user, connection)
// Called each time a connection joins a room (after onJoin if
// applicable).
// onUpdateConnection(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.
onRename(user, oldUserid) {
if (!this.allowRenames) {
if (!(user.userid in this.players)) {
user.games.delete(this.id);
}
return;
}
if (!(oldUserid in this.players)) return;
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];
}
}
onUpdateConnection(user, connection) {
if (this.onConnect) this.onConnect(user, connection);
}
}
// these exports are traditionally attached to rooms.js
exports.RoomGame = RoomGame;
exports.RoomGamePlayer = RoomGamePlayer;