-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal.js
More file actions
770 lines (687 loc) · 26 KB
/
local.js
File metadata and controls
770 lines (687 loc) · 26 KB
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
// Server setup
let crypto = require('crypto'); //unused
let sql = require('./localsql'); //fkml change this everytim u go to server
let server = require('http').createServer();
let io = require('socket.io')(server);
server.listen(3000);
let serverVersion = "1.3.1"
// variable and object setup for objects read this https://code.tutsplus.com/tutorials/stop-nesting-functions-but-not-all-of-them--net-22315
let playerList = [];
let CardList = [];
let playerGames = [];
sql.GetCard.then(function (cards){
console.log(cards);
CardList = cards;
}).catch(function (error){
console.log(error);
});
//version X.X matches the client version after the last . it's server upgrades withing that client-server version
//functions
function randomCardsFromDeck(deck, number){
let max = Object.keys(deck).length;
let cards = [];
console.log("drawing this number of cards: " + number);
for(i=0;i<number;i++){
cards.push(deck[Math.floor(Math.random()*(max))].id); // returns a number between 0 and decksize-1
}
return cards;
}
function randomCardFromAllCards(){
let max = Object.keys(CardList).length;
return Math.floor((Math.random()*max)) + 1;
}
function GetMatchmakingArray(callingsocket){
let returnme = []
let i = 0;
const iMax = Object.keys(playerList).length;
for(; i<iMax;i++){
returnme.push(new Promise (function(resolve, reject){
if(playerList[i].gameState == "LFO" && playerList[i].sock != callingsocket){ //We may add more verifications here for a more complex matchmaking algorithm
this.i = i; //this.i scope refers to the promise therefore there will be a this.i for each player we created a promise for
playerList[i].ping().then(()=>{
console.log("player responded");
reject(this.i);
}).catch((error) =>{
console.log("player has disconnected");
resolve(this.i);
});
} else if(playerList[i].sock == callingsocket){ //we must resolve the promise to avoid blocking
resolve("self");
} else{
resolve("Not LFO");
}
}))
}
console.log(returnme);
return returnme;
}
function isEmpty(obj) {
// null and undefined are "empty"
if (obj == null) return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length && obj.length > 0) return false;
if (obj.length === 0) return true;
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and toValue enumeration bugs in IE < 9
for (let key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}
function getSHA256(input){
return crypto.createHash('sha256').update(JSON.stringify(input)).digest('hex')
}
//player object{
function player(id, name, gameState, activedeck, sock){
this.id = id; //primary key id in database possibly unique repeated with name might remove
this.name = name;
this.gameState = gameState;
this.activedeck = activedeck;
this.sock = sock; //primary key current connection id
this.connectionStatus = true;
this.currentTimeout; //mantains the timeout object so we can delete it in case the user responds
this.currentPromise;
this.currentGame;
}
player.prototype.ping = function(){
io.to(this.sock).emit('ping');
console.log("pinging " + this.sock);
this.connectionStatus = false;
this.currentTimeout = setTimeout(this.pingFailCheck.bind(this),5000); //bind(this) used because this function is called from setTimeout wich rebinds this to the timeout obj
this.currentPromise = new deferred();
return this.currentPromise.promise;
}
player.prototype.pingFailCheck = function(){
console.log("removing player")
console.log(this);
let index
if(this.connectionStatus == false){
index = playerList.indexOf(this);
if(index !== -1){
playerList.splice(index,1);
this.currentPromise.reject();
}
}
console.log(playerList);
}
player.prototype.pong = function(){
this.connectionStatus = true;
clearTimeout(this.currentTimeout);
console.log(this);
this.currentPromise.resolve();
console.log("cancelling pingfailcheck")
}
//}
//player status
let playerStatus = function (health ,energy, energygrowth, maxenergy){
this.health = health;
this.armor = 0;
this.maxHealth = health;
this.energy = energy;
this.curmaxenergy = energy;
this.energygrowth = energygrowth;
this.maxenergy = maxenergy;
this.turnsTimedOut = 0;
}
//game object{
let game = function(p1, p2, timeout){
this.p1 = p1;
this.p2 = p2;
this.p1Status;
this.p2Status;
this.currentPlayer;
this.p1deck;
this.p2deck;
this.p1loaded = false;
this.p2loaded = false;
this.gameStarted = false;
this.currentTimeout = timeout;
this.transmission = {
self_hp:0.0,
self_maxhp:0.0,
self_energy:0.0,
self_armor:0.0,
enemy_hp:0.0,
enemy_maxhp:0.0,
enemy_energy:0.0,
enemy_armor:0.0
}
game.prototype.setTransmission = function(){ //We will always transmit to player1 first every time, this is called after calculating damage.
this.transmission.self_maxhp = this.p1Status.maxHealth;
this.transmission.self_hp = this.p1Status.health;
this.transmission.self_energy = this.p1Status.energy;
this.transmission.self_armor = this.p1Status.armor;
this.transmission.enemy_maxhp = this.p2Status.maxHealth;
this.transmission.enemy_hp = this.p2Status.health;
this.transmission.enemy_energy = this.p2Status.energy;
this.transmission.enemy_armor = this.p2Status.armor;
}
game.prototype.switchTransmission = function(){ //makes the transmission for player2
this.transmission.self_maxhp = this.p2Status.maxHealth;
this.transmission.self_hp = this.p2Status.health;
this.transmission.self_energy = this.p2Status.energy;
this.transmission.self_armor = this.p2Status.armor;
this.transmission.enemy_maxhp = this.p1Status.maxHealth;
this.transmission.enemy_hp = this.p1Status.health;
this.transmission.enemy_energy = this.p1Status.energy;
this.transmission.enemy_armor = this.p1Status.armor;
}
game.prototype.transmitGameStatus = function(){ //transmit : both players health, self energy?
console.log("sending status");
this.setTransmission();
io.to(this.p1.sock).emit('gameStatus',this.transmission);
this.switchTransmission();
io.to(this.p2.sock).emit('gameStatus',this.transmission);
}
game.prototype.setUpGame = function(firstPlayer){
sql.GetPlayerStats(p1.id, p2.id).then((result) =>{
if(result[0].user_id == p1.id){
this.p1Status = new playerStatus(result[0].Char_Health, result[0].Char_Energy, result[0].Char_EnergyGrowth, result[0].Char_MaxEnergy);
this.p2Status = new playerStatus(result[1].Char_Health, result[1].Char_Energy, result[1].Char_EnergyGrowth, result[1].Char_MaxEnergy);
} else {
this.p2Status = new playerStatus(result[0].Char_Health, result[0].Char_Energy, result[0].Char_EnergyGrowth, result[0].Char_MaxEnergy);
this.p1Status = new playerStatus(result[1].Char_Health, result[1].Char_Energy, result[0].Char_EnergyGrowth, result[1].Char_MaxEnergy);
}
console.log("decks \n" + this.p1deck +"\n" + this.p2deck);
if (firstPlayer == 0){
this.currentPlayer = this.p1;
io.to(this.p2.sock).emit("disableEndTurnBT");
let cardsToDraw = randomCardsFromDeck(this.p1deck , 3);
this.p1hand = cardsToDraw; //kept to avoid cheaters
console.log(this.p1hand);
io.to(this.p1.sock).emit("drawCards", {cards: cardsToDraw, ammount: Object.keys(cardsToDraw).length});
}
else{
this.currentPlayer = this.p2;
io.to(this.p1.sock).emit("disableEndTurnBT");
let cardsToDraw = randomCardsFromDeck(this.p2deck , 3);
this.p2hand = cardsToDraw; //kept to avoid cheaters
console.log(this.p2hand);
io.to(this.p2.sock).emit("drawCards", {cards: cardsToDraw, ammount: Object.keys(cardsToDraw).length});
}
console.log("current player: " + this.currentPlayer.name);
//emit warning animation
//setup timeouts
this.transmitGameStatus();
this.setTurnTimer();
}).catch(function (error){
console.log(error);
});
}
game.prototype.setTurnTimer = function(){
console.log("setting timeout");
this.currentTimeout = setTimeout(this.turnTimerEnded.bind(this), 30 * 1000); //30 segs
}
game.prototype.turnTimerEnded = function(){
console.log("player let his turn pass by time");
io.to(this.currentPlayer.sock).emit('turnTimerEnded');
this.turnEnd();
}
game.prototype.playerEndedTurn = function(player, message){
if(player != this.currentPlayer)
return;
clearTimeout(this.currentTimeout);
this.turnEnd();
}
game.prototype.playCard = function(message){
if(this.currentPlayer == this.p1){
console.log("card cost " + CardList[message.card-1].cost);
if(this.p1Status.energy >= CardList[message.card-1].cost){
if(!(this.p1hand.includes(message.card-1))){ //check if he actually has that card in hand
io.to(this.p1.sock).emit("playCardNotAllowed");
}
this.p1hand.splice(this.p1hand.indexOf(message.card-1)); //remove an instance of that card from the hand
io.to(this.p1.sock).emit("playCardAllowed");
this.p1Status.energy -= CardList[message.card-1].cost;
this.gameLogic(this.p1Status, this.p1deck, this.p2Status, this.p2deck, CardList[message.card-1]);
this.transmitGameStatus();
} else{
io.to(this.p1.sock).emit("playCardNotAllowed");
}
} else {
if(this.p2Status.energy >= CardList[message.card-1].cost){
if(!(this.p2hand.includes(message.card-1))){ //check if he actually has that card in hand
io.to(this.p2.sock).emit("playCardNotAllowed");
}
this.p2hand.splice(this.p2hand.indexOf(message.card-1)); //remove an instance of that card from the hand
io.to(this.p2.sock).emit("playCardAllowed");
this.p2Status.energy -= CardList[message.card-1].cost;
this.gameLogic(this.p2Status, this.p2deck, this.p1Status, this.p1deck, CardList[message.card-1]);
this.transmitGameStatus();
} else{
io.to(this.p2.sock).emit("playCardNotAllowed");
}
}
}
game.prototype.turnEnd = function(){
this.buffTicker();
let targetPlayer,selfPlayer;
if(this.currentPlayer == this.p1){
targetPlayer = this.p2Status;
selfPlayer = this.p1Status;
} else {
targetPlayer = this.p1Status;
selfPlayer = this.p2Status;
}
//things that happen at the end of every turn (player currently playing is selfPlayer)
//increment his energy by his growth stat
if((targetPlayer.curmaxenergy + targetPlayer.energygrowth) > targetPlayer.maxenergy)
targetPlayer.curmaxenergy = targetPlayer.maxenergy;
else
targetPlayer.curmaxenergy += targetPlayer.energygrowth;
//set his energy to his curmaxenergy
targetPlayer.energy = targetPlayer.curmaxenergy;
if (this.currentPlayer == this.p1){
//switch server side variables!
this.currentPlayer = this.p2;
io.to(this.p2.sock).emit("yourTurn");
//get him some cards!
let cardsToDraw = randomCardsFromDeck(this.p2deck, 3);
this.p2hand = cardsToDraw //kept to avoid cheaters
console.log(cardsToDraw);
io.to(this.p2.sock).emit("drawCards", {cards: cardsToDraw, ammount: Object.keys(cardsToDraw).length});
console.log("new player is \n" + this.currentPlayer.name);
}
else{
//switch server side variables!
this.currentPlayer = this.p1;
io.to(this.p1.sock).emit("yourTurn");
//get him some cards!
let cardsToDraw = randomCardsFromDeck(this.p1deck, 3);
this.p1hand = cardsToDraw //kept to avoid cheaters
console.log(cardsToDraw);
io.to(this.p1.sock).emit("drawCards", {cards: cardsToDraw, ammount: Object.keys(cardsToDraw).length});
console.log("new player is \n" + this.currentPlayer.name);
}
//things that happen at the start of every turn (player currently playing is targetPlayer [reversed])
//remove armor from the player starting his turn
targetPlayer.armor = 0;
//check victories
console.log(this.p1.sock)
if(targetPlayer.health <= 0){
if (this.currentPlayer == this.p1)
this.victory(this.p2, this.p1);
else
this.victory(this.p1, this.p2);
return;
}
if(selfPlayer.health <= 0){
if (this.currentPlayer == this.p1)
this.victory(this.p1, this.p2);
else
this.victory(this.p2, this.p1);
return;
}
this.transmitGameStatus();
this.setTurnTimer();
}
game.prototype.victory = function(player, otherPlayer){
console.log("Player won");
console.log(player.name);
sql.rewardPlayer(player.id, randomCardFromAllCards()).then((result)=>{
console.log(result);
io.to(player.sock).emit("youWon", result);
}).catch(function(error){
console.log(error);
});
io.to(otherPlayer.sock).emit("youLost");
clearTimeout(this.currentTimeout);
player.currentGame = null;
otherPlayer.currentGame = null;
player.gameState = "mainmenu";
otherPlayer.gameState = "mainmenu";
let index = playerGames.indexOf(this);
if(index !== -1)
playerGames.splice(index,1);
console.log("list of games")
console.log(playerGames);
}
//game logic helper functions
game.prototype.dealDamage = function(damage, targetPlayer){
if (targetPlayer.armor>=damage){
targetPlayer.armor -= damage;
} else if(targetPlayer.armor < damage){
damage -= targetPlayer.armor;
targetPlayer.armor = 0;
targetPlayer.health -=damage;
}
console.log(this.p1Status);
console.log(this.p2Status);
}
game.prototype.buffManager = function(buffNumber, currentPlayerStatus, currentPlayerDeck){
switch(buffNumber) {
case '1'://draw a card
let cardsToDraw = randomCardsFromDeck(currentPlayerDeck, 1);
if(currentPlayerStatus==this.p1Status)
this.p1hand = this.p1hand.concat(cardsToDraw);
else
this.p2hand = this.p2hand.concat(cardsToDraw);
io.to(this.currentPlayer.sock).emit("drawCards", {cards: cardsToDraw, ammount: Object.keys(cardsToDraw).length});
break;
case '2': //apply burn
if(currentPlayerStatus == this.p1Status)
this.p2Status.exampleBurningBuff = {duration:4, damage:2};
else
this.p1Status.exampleBurningBuff = {duration:4, damage:2};
break;
}
}
game.prototype.buffTicker = function(){
if(this.p2Status.exampleBurningBuff != null){
this.p2Status.health -= this.p2Status.exampleBurningBuff.damage;
this.p2Status.exampleBurningBuff.duration -= 1;
if(this.p2Status.exampleBurningBuff.duration == 0)
this.p2Status.exampleBurningBuff = null;
}
if(this.p1Status.exampleBurningBuff != null){
this.p1Status.health -= this.p1Status.exampleBurningBuff.damage;
this.p1Status.exampleBurningBuff.duration -= 1;
if(this.p1Status.exampleBurningBuff.duration == 0)
this.p1Status.exampleBurningBuff = null;
}
}
//end game logic helper functions
game.prototype.gameLogic = function(selfPlayerStatus, selfPlayerDeck, targetPlayerStatus, targetPlayerDeck, card){
//the whole gameLogic system is coded in a confusing way and needs a remake
//we will do all the maths here
//general card logic
if (card.damage > 0 ){
let damage = 0;
console.log("damage: " + card.damage);
console.log("numberofstrikes: " + card.numberofstrikes);
for(let a=0 ; a<(card.numberofstrikes) ; a++){
damage += (card.damage);
}
this.dealDamage(damage, targetPlayerStatus);
console.log(damage);
}
//apply sum heals
if (card.heal > 0 ){
let heal = 0;
heal = card.heal;
selfPlayerStatus.health +=heal;
console.log("heal: " + heal);
}
//apply sum armor
if (card.armor > 0 ){
let armor = 0;
armor = card.armor;
selfPlayerStatus.armor += armor;
console.log("Armor: " + armor);
}
//apply sum buffs/debuffs
if (card.buff != null){
let buffs = card.buff.split(",")
const iterator = Object.keys(buffs).length;
for(let b = 0; b<iterator; b++){
console.log("applying buff: " + buffs[b]);
this.buffManager(buffs[b], selfPlayerStatus, selfPlayerDeck);
}
}
this.transmitGameStatus();
}
}
//}
//deferred object{
let deferred = function(){
this.promise = new Promise((resolve, reject)=> {
this.reject = reject
this.resolve = resolve
});
}
//}
//console.log(getSHA256('wasd'));
console.log ('Server Started');
//ping check sequence
//playerList.push(new player(1, "testname", "LFO", 1, "testsocketid"));
//setTimeout(test2, 10000);
/*console.log("pinging player");
function test(){
playerList[0].ping().then(function(){
console.log("everything worked out no ping fail check should be executed")
}).catch(function(error){
console.log("player has disconnected");
});
}
function test2(){
playerList[0].pong();
}
setTimeout(test,1000);
//setTimeout(test2,3000);*/
//looping http://www.andygup.net/fastest-way-to-find-an-item-in-a-javascript-array/
// Server methods
io.sockets.on('connection', function(socket)
{
console.log ('User connected: ' + socket.id);
socket.emit('connectionEstabilished', {id: socket.id, serverVersion:serverVersion});
socket.on('crashServer', function(message){
console.log("crashing");
process.exit();
});
socket.on('RefreshLogin', function(message){
sql.GetPlayer(message.user, message.pass).then(function (result){
console.log("refresh login:")
console.log(result);
const iMax = Object.keys(playerList).length;
for(let i = 0; i<iMax; i++){
console.log("comparing " + playerList[i].id + " with " + result[0].user_id);
if(playerList[i].id == result[0].user_id){
playerList[i].sock = socket.id;
console.log("refreshed login for " + result[0].user_username)
console.log(playerList);
}
}
}).catch(function (error){
console.log(error);
});
});
socket.on('deckedit_getCards', function(){
let i = 0; const iMax = Object.keys(playerList).length;
let userid;
for(;i<iMax;i++){
if(playerList[i].sock==socket.id){
userid=playerList[i].id;
sql.getPlayerOwnedCards(userid).then((result) =>{
console.log(result);
let cards = [];
let ammounts = [];
const oMax = Object.keys(result).length;
for(let o=0;o<oMax;o++){
cards[o] = {
id: result[o].id,
level: result[o].level,
type: result[o].type,
name: result[o].name,
cost: result[o].cost,
description: result[o].description
}
ammounts [o] = {
id: result[o].id,
ammount: result[o].ownedammount
}
}
console.log("dismantled result:");
console.log(cards);
console.log(ammounts);
socket.emit('cardsToLoadDeckEditor', {cards:cards, ammounts: ammounts});
});
break;
}
}
});
socket.on('deckSaveRequest', (message)=>{
console.log("user requested to save:")
console.log(message);
let i = 0; const iMax = Object.keys(playerList).length;
for(;i<iMax;i++){
if(playerList[i].sock==socket.id){
sql.SavePlayerDeck(message.cards, playerList[i].id).then(() =>{
socket.emit('deckSaveSucess');
});
break;
}
}
})
socket.on('registerUser', function(message){
sql.RegisterPlayer(message.user, message.pass, Object.keys(cards).length).then((result) =>{
socket.emit("registerSucess");
}).catch((result) =>{
console.log(result);
socket.emit("registerFail", {message: result});
});
});
socket.on('pong', function(){
console.log("pong from " + socket.id);
let i = 0; const iMax = Object.keys(playerList).length;
for(;i<iMax;i++){
if(playerList[i].sock==socket.id){
playerList[i].pong();
console.log("pong registered");
break;
}
}
});
socket.on('endTurn', function(message){
let i = 0; const iMax = Object.keys(playerList).length;
console.log(message);
for(;i<iMax;i++){
if(playerList[i].sock==socket.id){
playerList[i].currentGame.playerEndedTurn(playerList[i], message);
break;
}
}
});
socket.on('playCard',function(message){
let i = 0; const iMax = Object.keys(playerList).length;
for(;i<iMax;i++){
if(playerList[i].sock==socket.id){
playerList[i].currentGame.playCard(message);
}
}
})
socket.on('LoginAttempt', function(message){ //lacks security and persistence
console.log(message);
sql.GetPlayer(message.user, message.pass).then(function (result){
//we test if this user is already on the list for now we remove him, later we might want to put him back into the game
let i = 0; const iMax = Object.keys(playerList).length;
for(;i<iMax;i++){
if(playerList[i].id==result[0].user_id){
playerList.splice(i,1)
console.log("player was in the list already, log in renewed");
break;
}
}
console.log("addding player to list");
playerList.push(new player(result[0].user_id, result[0].user_username, "mainmenu", 1, socket.id));
console.log("player logged in -> " +result[0].user_id + " : " + result[0].user_username);
socket.emit('loginSucess', {id: socket.id, username:result[0].user_username});
}).catch(function (error){
if(error == 1){
socket.emit('loginFailed');
console.log("emmited login failed");
}
if(error == 2)
socket.emit('warningBox', {message:"server related issue", errCode:2}); //still need some way of displaying this
});
})
socket.on('gameLoaded', function(){
//we find the game this player is refering too
let i = 0; const iMax = Object.keys(playerGames).length; let flag=false;
for(;i<iMax;i++){
if(playerGames[i].p1.sock==socket.id){
playerGames[i].p1loaded = true;
flag = true;
console.log("p1 loaded");
break;
}
if(playerGames[i].p2.sock==socket.id){
playerGames[i].p2loaded = true;
flag = true;
console.log("p2 loaded");
break;
}
}
if(!playerGames[i].gameStarted){
if(playerGames[i].p1loaded && playerGames[i].p2loaded){ //both players are loaded so let's clear the timeout
playerGames[i].gameStarted = true;
clearTimeout(playerGames[i].currentTimeout);
console.log("timeout cleared game has started");
io.to(playerGames[i].p1.sock).emit('loadGame');
io.to(playerGames[i].p2.sock).emit('loadGame');
//define who plays first
let firstPlayer = Math.floor(Math.random() * Math.floor(2));
playerGames[i].setUpGame(firstPlayer);
}
}
});
socket.on('FindOpponent', function(){ //after setting user to LFO user needs to be able to revert back to not lfo
//this function runs code for every client the first one to reject it is the first player to answer to the ping with a gamestate of LFO, if no one
//rejects it, they will all be sovled within client timeout time, disconnecting all disconnected players in the process and setting this player to LFO
//we must test if this user is really logged in to avoid hacked clients from spamming this function.
let i = 0; const iMax = Object.keys(playerList).length;
let flag = false;
let indexOfPlayer = 0;
for(;i<iMax;i++){
if(playerList[i].sock==socket.id){
flag = true; //we have found him
console.log("we found this player on the list");
PlayerRef = playerList[i];
break;
}
}
if(!flag){ //let's disconnect from him
socket.disconnect();
console.log("fake player");
}
//Promise.all() returns the first reject if any, or all resolves, wich makes it perfect to use it inversely
//as soon as a player pongs that promise is rejected and the code progresses, on the other hand if all solves no user is in LFO mode so we set this one to LFO
Promise.all(GetMatchmakingArray(socket.id)).then(function(values) {
console.log("resolved " + values);
PlayerRef.gameState = "LFO";
console.log("setting self to lfo");
}).catch(function(value){ //hapens if someone answers to the game find call
console.log("rejected aka opponent found! " + value)
//we warn the player calling this to load the game and we find his deck
this.CurrentGameIndex;
io.to(playerList[value].sock).emit('foundOpp',{username:PlayerRef.name}); //we send him the oposing player name
sql.GetPlayerDeck(playerList[value].id).then((result) =>{ //we send him his deck
playerList[value].gameState = "FO";
console.log(playerList[value].name + "'s Deck");
console.log(result);
playerGames[this.CurrentGameIndex].p2deck=result;
io.to(playerList[value].sock).emit('cardList',{cards:result});
}).catch(function (error){
console.log(error);
});
//and the user that was waiting to load the game and we find his deck
socket.emit("foundOpp",{username:playerList[value].name}) //we send him the oposing player name
sql.GetPlayerDeck(PlayerRef.id).then((result) =>{
PlayerRef.gameState = "FO";
console.log(PlayerRef.name + "'s Deck");
console.log(result);
playerGames[this.CurrentGameIndex].p1deck=result;
socket.emit("cardList",{cards:result})
}).catch(function (error){
console.log(error);
});
let timeout = setTimeout(function(){ //we set a timeout for the players to load in
console.log("game timed out")
PlayerRef.gameState = "mainmenu";
playerList[value].gameState = "mainmenu";
socket.emit("reset")
io.to(playerList[value].sock).emit("reset");
playerGames.splice(CurrentGameIndex,1);
console.log(playerGames);
},10000)
//we push a new game into the games list
//this line is possible because .push returns the index it pushed the new object into
this.CurrentGameIndex = playerGames.push(new game(PlayerRef, playerList[value], timeout)) - 1;
console.log("current game index = " + CurrentGameIndex);
PlayerRef.currentGame = playerGames[CurrentGameIndex];
playerList[value].currentGame = playerGames[CurrentGameIndex];
console.log(playerGames[this.CurrentGameIndex]);
});
});
});