-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketserver.js
303 lines (262 loc) · 11.9 KB
/
socketserver.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const {waitFor} = require('./helper');
const Room = require('./room');
const Dice = require('./dice');
class SocketServer{
constructor(socket,io,server){
this.socket = socket;
this.io = io;
this.server = server;
this.curRoom;
this.playerIndex = -1;
this.player;
this.playerRole;
this.addSocket = this.addSocket.bind(this);
this.endTurn = this.endTurn.bind(this);
this.roll = this.roll.bind(this);
this.reroll = this.reroll.bind(this);
this.sendMessage = this.sendMessage.bind(this);
this.disconnect = this.disconnect.bind(this);
this.socket.on('addSocket',this.addSocket);
this.socket.on('endTurn', this.endTurn);
this.socket.on('roll', this.roll);
this.socket.on('reroll', this.reroll);
this.socket.on('sendMessage', this.sendMessage);
this.socket.on('disconnect', this.disconnect);
waitFor(() => this.curRoom && this.curRoom.started).then(()=>{
this.setupAllConnected();
});
}
//Adds a socket to the room
addSocket(roomName){
this.curRoom = this.server.rooms.find(x => x.name === roomName);
this.socket.join(this.curRoom.name);
for (let i in this.curRoom.connections){
if (this.curRoom.connections[i] === null){
this.playerIndex = parseInt(i);
break;
}
}
if (this.playerIndex === -1) return
this.curRoom.connections[this.playerIndex] = this.socket;
if (this.curRoom.allPlayersConnected()){
this.curRoom.start();
console.log(this.curRoom.name+": "+"a játék elindult");
}
this.io.to(this.curRoom.name).emit('updatePlayerNumber',this.curRoom.playerLimit,this.curRoom.playerLimit-this.curRoom.playersLeft);
}
//Sets the player's data, then sends it to the client-side
setupAllConnected(){
this.player = this.curRoom.players[this.playerIndex];
this.player.index = this.playerIndex;
this.playerRole = this.curRoom.roles[this.playerIndex];
this.io.to(this.curRoom.name).emit('updateChat',this.curRoom.chat);
this.socket.emit('playersDataSetup',this.curRoom.players,this.playerIndex,this.playerRole,this.curRoom.arrowsLeft);
}
//Checks if the player's health reached 0, and removes them from the game if yes.
checkIfKilled(player){
if (player.life<=0){
if (!player.killed){
player.killed = true;
this.curRoom.chat.push(player.name+" meghalt.");
this.curRoom.arrowsLeft += player.arrows;
player.arrows = 0;
player.life = 0;
player.role = this.curRoom.roles[player.index];
//Checks if the killed player is the current one
if (player.name==this.player.name){
this.endTurn([]);
}
this.io.to(this.curRoom.name).emit('playersDataRefresh',this.curRoom.players, this.curRoom.arrowsLeft, this.curRoom.chat);
}
}
}
//Checks if there is a winner, and ends the game if so
checkForWinner(){
let winner = this.curRoom.checkWinConditions(this.curRoom.playerLimit)
if (winner){
console.log(this.curRoom.name+": a nyertes: "+winner)
this.io.to(this.curRoom.name).emit('gameEnd', winner);
this.server.rooms.splice(this.server.rooms.indexOf(this.curRoom), 1);
}
}
//Handles the ending of turns, performs the actions on the dices
endTurn(selections){
//Ending turn
if (this.player.curTurn){
console.log(this.curRoom.name+": "+this.player.name+" körének vége!");
if (!this.player.killed){
//Resolving the dices
//Character check: suzy lafayette
if (this.player.character.name === 'suzy_lafayette' && selections.filter(x => x.type === 2 || x.type === 3).length === 0){
if (this.player.life<this.player.startingLife-2){
this.player.life+=2;
} else if (this.player.life===this.player.startingLife-1){
this.player.life++;
}
}
//Character check: el gringo
if (selections.some(x => (x.type === 2 || x.type === 3) && this.curRoom.players[x.selection].character.name === 'el_gringo')){
this.addArrows(this.player,1);
this.curRoom.chat.push(this.player.name+' kapott egy nyilat.');
}
for (let s of selections){
if (s != null){
let dice_type = s.type;
let selectedPlayer = this.curRoom.players[s.selection];
if (dice_type === 2 || dice_type === 3){
selectedPlayer.life--;
this.curRoom.chat.push(selectedPlayer.name + ' kapott egy lövést: '+ '<img class="smallDices" src="'+s.image+'">');
} else if (dice_type === 4 && selectedPlayer.life < selectedPlayer.startingLife){
//Character check: jesse jones
if (selectedPlayer.character.name === 'jesse_jones' && selectedPlayer === this.player && selectedPlayer.life <= 4){
selectedPlayer.life+=2;
this.curRoom.chat.push(selectedPlayer.name + ' kapott két sört: '+ '<img class="smallDices" src="'+s.image+'">');
} else {
if (selectedPlayer.life<selectedPlayer.startingLife) selectedPlayer.life++;
this.curRoom.chat.push(selectedPlayer.name + ' kapott egy sört: '+ '<img class="smallDices" src="'+s.image+'">');
}
}
}
}
let gatlingDices = selections.filter(x => x.type === 5);
//Character check: willy the kid
if (gatlingDices.length >= 3 || (gatlingDices.length === 2 && this.player.character.name === 'willy_the_kid')){
this.curRoom.chat.push(this.player.name + ' gatlingot használt, így eldobja a nyilait.');
this.curRoom.arrowsLeft += this.player.arrows;
this.player.arrows = 0;
for (let p of this.curRoom.players){
//Character check: paul regret
if (p != this.player && !p.killed && p.character.name != 'paul_regret'){
p.life--;
this.curRoom.chat.push(p.name + ' sebezve lett gatling által');
}
}
}
for (let p of this.curRoom.players){
this.checkIfKilled(p);
}
this.checkForWinner();
}
this.curRoom.nextPlayer(this.player);
}
this.io.to(this.curRoom.name).emit('playersDataRefresh',this.curRoom.players, this.curRoom.arrowsLeft, this.curRoom.chat);
}
//adds arrows to the player
addArrows(player,arrows){
while (this.curRoom.arrowsLeft>0 && arrows>0){
player.arrows++;
this.curRoom.arrowsLeft--;
arrows--;
}
this.checkArrowsLeft();
}
//Check if no arrows are left, and damages the players if they have arrows
checkArrowsLeft(){
if (this.curRoom.arrowsLeft <= 0){
this.io.to(this.curRoom.name).emit('noArrowsLeft');
for (let p of this.curRoom.players){
if (!p.killed){
//Character check: jourdonnais
if (p.character.name != 'jourdonnais'){
p.life -= p.arrows;
this.curRoom.chat.push(p.name+' kapott ' + p.arrows +' sebzést a nyilaktól.');
} else {
if (p.arrows!=0) {
p.life--;
this.curRoom.chat.push(p.name+' kapott 1 sebzést a nyilaktól.');
}
}
p.arrows = 0;
this.checkIfKilled(p)
}
}
this.checkForWinner();
this.curRoom.arrowsLeft = 9;
}
this.io.to(this.curRoom.name).emit('playersDataRefresh',this.curRoom.players, this.curRoom.arrowsLeft, this.curRoom.chat);
}
//Dice roll
roll(types=[-1,-1,-1,-1,-1]){
if (this.player.rolled === false){
let rollResults = [];
for (let i = 0; i < 5; i++){
let cur_dice = new Dice(i,types[i]);
rollResults.push(cur_dice);
if (cur_dice.type === 0 || cur_dice.type === 1 || cur_dice.type === 5){ //add all to selections except bullet1/2, beer
this.player.selections[i] = cur_dice;
} else {
this.player.selections[i] = null;
}
}
let dynamiteDices = rollResults.filter(x => x.type === 1);
for (let d of rollResults){
if (d.type === 0){
this.addArrows(this.player,1);
}
else if (d.type === 1){
d.rerollsLeft = 0;
}
}
if (dynamiteDices.length >= 3){
this.player.life--;
this.checkIfKilled(this.player)
this.checkForWinner();
dynamiteDices.forEach(x=>x.abilityActivated = true);
rollResults.forEach(x=>x.rerollsLeft = 0);
}
this.player.rolled = true;
console.log(this.curRoom.name+": "+this.player.name + ' dobott: ',rollResults.map(x => x.name));
this.curRoom.chat.push(this.player.name + ' dobott: '+ rollResults.map(x => '<img class="smallDices" src="'+x.image+'">'));
this.player.curDices = rollResults;
this.socket.emit('rollResults',this.player.curDices, this.player.selections);
this.io.to(this.curRoom.name).emit('playersDataRefresh',this.curRoom.players, this.curRoom.arrowsLeft, this.curRoom.chat);
}
}
//Handles the rerolling of the dice
reroll(rerolledDiceIndex,type=-1){
let rerolledDice = this.player.curDices[rerolledDiceIndex];
let originalDice = {...rerolledDice};
rerolledDice.roll(type);
console.log(this.curRoom.name+": "+this.player.name + ' újradobott: '+ rerolledDice.name, rerolledDice.type);
this.curRoom.chat.push(this.player.name + ' újradobott: '+ '<img class="smallDices" src="'+originalDice.image+'">'+'->'+'<img class="smallDices" src="'+rerolledDice.image+'">');
if (rerolledDice.type === 0 || rerolledDice.type === 1 ||rerolledDice.type === 5){
this.player.selections[rerolledDiceIndex] = rerolledDice;
} else {
this.player.selections[rerolledDiceIndex] = null;
}
rerolledDice.rerollsLeft--;
let dynamiteDices = this.player.curDices.filter(x => x.type === 1 && x.abilityActivated === false);
if (rerolledDice.type === 0){
this.curRoom.chat.push(this.player.name+' kapott egy nyilat.');
this.addArrows(this.player,1);
}
else if (rerolledDice.type === 1){
rerolledDice.rerollsLeft = 0;
}
if (dynamiteDices.length >= 3){
this.curRoom.chat.push(this.player.name + ' 3 dinamitot dobott.');
this.player.life--;
this.checkIfKilled(this.player);
this.checkForWinner();
dynamiteDices.forEach(x => x.abilityActivated = true);
this.player.curDices.forEach(x => x.rerollsLeft = 0);
}
this.socket.emit('rollResults', this.player.curDices, this.player.selections);
this.io.to(this.curRoom.name).emit('playersDataRefresh', this.curRoom.players, this.curRoom.arrowsLeft, this.curRoom.chat);
}
//Send a message to the chat
sendMessage(message){
console.log(this.curRoom.name+": "+this.player.name+" küldött egy üzenetet: "+message);
this.curRoom.chat.push(this.player.name+": "+message);
this.io.to(this.curRoom.name).emit('updateChat', this.curRoom.chat);
}
//Disconnect from the game, and remove the room from the server
disconnect(){
this.curRoom.connections[this.playerIndex] = null;
this.io.to(this.curRoom.name).emit("aPlayerDisconnected");
if (this.server.rooms.indexOf(this.curRoom) != -1){
this.server.rooms.splice(this.server.rooms.indexOf(this.curRoom), 1);
}
}
}
module.exports = SocketServer;