-
Notifications
You must be signed in to change notification settings - Fork 0
/
games.js
460 lines (396 loc) · 13.7 KB
/
games.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
let Player = require('./player').Player;
/*
* Questa classe gestisce, crea e rimuove tutte le sessioni di gioco
*/
class Games {
constructor() {
this.game_session = {}
this.users = {}
}
get(number) {
return this.game_session[number];
}
add(number) {
if(!this.has(number))
this.game_session[number] = new GameSession(number);
}
remove(number) {
if(this.has(number))
delete this.game_session[number];
}
length() { return Object.keys(this.game_session).length; }
has(number) {
return (number in this.game_session);
}
hasUser(number, socket_id) {
if(!this.has(number))
return false
return this.game_session[number].has(socket_id);
}
addUser(game_number, socket_id) {
if(this.hasUser(game_number, socket_id)) {
return 'Client already in game';
}
if(!this.has(game_number)) {
return 'Game number does not exist';
}
if(this.game_session[game_number].started) {
return 'Game in progress';
}
if(!this.game_session[game_number].add(socket_id)) {
return 'Full game session';
}
this.users[socket_id] = game_number;
return '';
}
removeUser(game_number, socket_id) {
if(game_number in this.game_session) {
if(this.game_session[game_number].remove(socket_id)) {
if(!(socket_id in this.users))
delete this.users[socket_id];
if(this.game_session[game_number].length() === 0)
this.remove(game_number);
return true;
}
}
return false;
}
setUserNickname(game_number, socket_id, nickname) {
if(this.hasUser(game_number, socket_id))
this.game_session[game_number].participants[socket_id].setNickname(nickname);
}
getUserGameNumber(socket_id) {
if(socket_id in this.users)
return this.users[socket_id];
}
getUsersInAGameSession(game_number) {
if(this.has(game_number))
return this.game_session[game_number].participants;
}
findUserInAGameSessionByNickname(game_number, nickname) {
if(this.has(game_number)){
let participants = this.game_session[game_number].participants;
for(let id in participants)
if(participants[id].nickname === nickname)
return id;
}
return null;
}
setGameStarted(game_number, started) {
if(this.has(game_number)) {
this.game_session[game_number].setStarted(started);
}
}
generateGame(socket_id) {
let game_number = null;
do {
game_number = "" + ~~(Math.random() * (99999 - 10000) + 10000);
} while(this.has(game_number));
this.add(game_number);
this.addUser(game_number, socket_id);
return game_number;
}
checkGameNumber(game_number, socket_id) {
return this.addUser(game_number, socket_id);
}
checkNickname(game_number, nickname) {
if(this.findUserInAGameSessionByNickname(game_number, nickname) !== null)
return "Nickname already used";
if(nickname.length > 8)
return "Nickname too long";
if(nickname.length < 3)
return "Nickname too short";
if(nickname.match("^[A-Za-z0-9]+$") === null)
return "Nickname can only have letters and numbers";
return "";
}
getGamesAvailable() {
let list = [];
for(let number in this.game_session) {
if(this.get(number).length() < 4 && !this.get(number).started) {
list.push({ game_number: number, length: this.get(number).length()});
}
}
return list;
}
}
const ANIMATION_T = 3500;
const VULNERABILITY_T = 7000;
const LOW_VULNERABILITY_T = 3500;
const HIGH_VULNERABILITY = 2;
const LOW_VULNERABILITY = 1;
const NO_VULNERABILITY = 0;
/*
* Questa classe gestisce una singola sessione di gioco e
* gli utenti che ne fanno parte
*/
class GameSession {
constructor(number) {
this.number = number;
this.participants = {};
this.started = false;
this.map = null;
this.pacman_number = 0;
this.game_name = 'classic game';
switch(this.game_name) {
case 'catch the pellets':
break;
case 'classic game':
this.initAnimationTime();
this.initVulnerabilityTime();
break;
}
}
updateGameProgress() {
if(this.started) {
switch(this.game_name) {
case 'catch the pellets':
this.catchThePellets();
break;
case 'classic game':
this.classicGame();
break;
default:
break;
}
}
}
catchThePellets() {
for(let key in this.participants) {
let user = this.participants[key];
if(user.player) {
if(this.map.isPellet(user.player.pos.x, user.player.pos.y)) {
user.player.increasePoints(1);
this.map.switchPelletToVoid(user.player.pos.x, user.player.pos.y);
}
else if(this.map.isCherry(user.player.pos.x, user.player.pos.y)) {
user.player.increasePoints(10);
this.map.setVoid(user.player.pos.x, user.player.pos.y);
}
}
}
}
classicGame() {
for(let i = this.pacman_number; i === this.pacman_number || i % this.length() !== this.pacman_number; i++) {
let user = this.participants[Object.keys(this.participants)[i % this.length()]];
if(user.player) {
if(user.player.role === 'pacman') {
this.pacman = user;
if(this.map.isPellet(user.player.pos.x, user.player.pos.y)) {
user.player.increasePoints(1);
this.map.switchPelletToVoid(user.player.pos.x, user.player.pos.y);
}
else if(this.map.isPowerPellet(user.player.pos.x, user.player.pos.y)) {
user.player.increasePoints(5);
this.setVulnerableGhosts(HIGH_VULNERABILITY);
this.setVulnerabilityTime();
this.map.switchPelletToVoid(user.player.pos.x, user.player.pos.y);
}
else if(this.map.isCherry(user.player.pos.x, user.player.pos.y)) {
user.player.increasePoints(10);
this.map.setVoid(user.player.pos.x, user.player.pos.y);
}
} else if(user.player.role === 'ghost') {
if(this.pacman &&
this.pacman.socket_id in this.participants &&
this.pacman.player.pos.x === user.player.pos.x &&
this.pacman.player.pos.y === user.player.pos.y) {
if(user.player.vulnerable) {
this.pacman.player.increasePoints(20);
user.player.setDefaultPosition(1);
user.player.direction = 3;
user.player.vulnerable = false;
user.player.initRecoveryTime();
} else {
this.pacman.player.life--;
this.lost_life = true;
this.setAnimationTime();
this.setVulnerableGhosts(NO_VULNERABILITY);
}
}
}
}
}
}
setVulnerableGhosts(x) {
for(let key in this.participants) {
let user = this.participants[key];
if(user.player && user.player.role === 'ghost')
user.player.vulnerable = x;
}
}
setLowVulnerableGhosts() {
for(let key in this.participants) {
let player = this.participants[key].player;
if(player && player.role === 'ghost' && player.vulnerable === HIGH_VULNERABILITY)
player.vulnerable = LOW_VULNERABILITY;
}
}
decreaseVulnerabilityTime(t) {
if(!this.vulnerabilityTimeOff()) {
this.vulnerability_time -= t;
if(this.vulnerability_time < 0) {
this.initVulnerabilityTime();
this.setVulnerableGhosts(NO_VULNERABILITY);
}
}
}
vulnerabilityTimeOff() {
return (this.vulnerability_time === -1);
}
lowVulnerabilityTime() {
if(this.vulnerability_time < LOW_VULNERABILITY_T && !this.vulnerabilityTimeOff())
this.setLowVulnerableGhosts();
}
setVulnerabilityTime() { this.vulnerability_time = VULNERABILITY_T;}
initVulnerabilityTime() {
this.vulnerability_time = -1;
}
repositionPlayers() {
this.lost_life = false;
for(let key in this.participants) {
let user = this.participants[key];
if(user.player) {
let default_number = (Object.keys(this.participants).indexOf(user.socket_id) + this.pacman_number) % this.length();
user.player.setDefaultPosition(default_number);
user.player.setDefaultDirection(default_number);
}
}
}
endGameCheck() {
if(this.started) {
switch(this.game_name) {
case 'catch the pellets':
return this.endCatchThePellets();
break;
case 'classic game':
return this.endClassicGame();
break;
default:
break;
}
}
return false;
}
endCatchThePellets() {
if(!this.map)
return false;
return !(this.map.num_pellets);
}
endClassicGame() {
if(!(this.pacman.socket_id in this.participants)) {
this.pacman.player.life = 0;
return true;
}
return ((this.pacman.player && !this.pacman.player.life) || this.endCatchThePellets());
}
getWinningUser() {
let winning_user = this.participants[Object.keys(this.participants)[0]];
for(let key in this.participants) {
let user = this.participants[key];
if(user.player.points > winning_user.player.points)
winning_user = user;
}
return winning_user;
}
getRanking() {
let sortScores = (a, b) => {
let user_a = this.participants[a];
let user_b = this.participants[b];
return user_b.player.points - user_a.player.points;
}
let ranking = []
Object.keys(this.participants).sort(sortScores).forEach((key) => {
ranking.push({ nickname: this.participants[key].nickname, score: this.participants[key].player.points });
});
return ranking;
}
setStarted(started) {
this.started = started;
}
getPacmanNumber() {
let i = 0;
for(let key in this.participants) {
let user = this.participants[key];
if(user.player && user.player.role === 'pacman')
return i;
i++;
}
return 0;
}
add(socket_id) {
if(this.length() < 4) {
this.participants[socket_id] = new User(socket_id);
let random = (Math.round(Math.random() * 10)) % this.length();
this.pacman_number = random;
return true;
}
return false;
}
remove(socket_id) {
if(socket_id in this.participants) {
delete this.participants[socket_id];
this.pacman_number = this.getPacmanNumber();
return true;
}
return false;
}
length() { return Object.keys(this.participants).length; }
has(socket_id) {
return (socket_id in this.participants);
}
userPlays(socket_id) {
if(this.has(socket_id)) {
this.participants[socket_id].plays((Object.keys(this.participants).indexOf(socket_id) + this.pacman_number) % this.length());
}
}
playersReady() {
let players_ready = true;
for(let key in this.participants)
players_ready = players_ready && Boolean(this.participants[key].player);
return players_ready;
}
decreaseAnimationTime(t) {
this.animation_time -= t;
if(this.animation_time < 0)
this.animation_time = -1;
}
setAnimationTime() { this.animation_time = ANIMATION_T;}
animationEnd(t) {
if(Boolean(this.animation_time))
this.decreaseAnimationTime(t);
return !Boolean(this.animation_time);
}
animationHasStartedNow() {
return (this.animation_time === ANIMATION_T);
}
animationIsOverNow() {
if(this.animation_time === -1) {
this.initAnimationTime();
return true;
}
return false;
}
initAnimationTime() {
this.animation_time = 0;
}
}
/*
* la classe User rappresenta un utente che partecipa a una sessione di gioco
*/
class User {
constructor(socket_id) {
this.nickname = null;
this.socket_id = socket_id;
this.player = null;
}
setNickname(nickname) {
this.nickname = nickname;
}
plays(x) {
this.player = new Player(x);
}
}
module.exports = {
Games: Games
}