-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.js
More file actions
1336 lines (1090 loc) · 39.6 KB
/
game.js
File metadata and controls
1336 lines (1090 loc) · 39.6 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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var util = require("./util").util;
var game = {
// Max number of table
MAX_TABLE: 5
// Max number player in game
, MAX_PLAYER: 40
// Max Player in match
, MAX_PLAYER_PER_MATCH: 8
// max blind
, MAX_BLIND: 100
// max chip for player to play in match
, MAX_CHIP_4_PLAY: 2000
// Time out for a turn - ms
, TURN_TIME_OUT: 20000 // ~ 20 seconds
// LOCATION OF GAME
, REFRESH_TABLE_TIMEOUT: 10000
, LOCATION_LOBBY: 0
, LOCATION_TABLE: 1
// number of table
, table_number: 0
// List of match
, tables: {}
//number of player in game
, player_number: 0
// List of user
, players: {}
// available table id
// , available_table_id: 0
};
game.addPlayer = function(player){
if(game.player_number >= game.MAX_PLAYER){
return -1; // game full
}
game.players[player.username] = player;
game.player_number++;
return 1;
};
game.createTable = function(hostPlayer){
if(game.table_number >= game.MAX_TABLE){
return null;
}
var table_id = (new Date().getTime())%100000000;
game.tables[table_id] = new Table(table_id, hostPlayer);
game.table_number++;
return game.tables[table_id];
};
game.deleteTable = function(table_id){
if(game.tables[table_id] == undefined){
return false;
}
console.log('Table id [ ' + table_id + ' ] is deleted');
delete game.tables[table_id];
game.table_number--;
return true;
};
//s
// Table of game
//
function Table(_id, host){
this.id = _id;
this.state = util.TABLE_STATE_WAITING;
this.players = new Array(8);
this.viewers = [];
this.host_player = host;
this.blind = 10;
this.player_number = 0;
/* variable in match */
this.hasPlayerAllIn = false;
this.total_chip = 0;
this.current_bet = -1;
this.current_turn = -1;
this.slot_begin = -1; // no case to use
this.slot_can_play = -1;
this.lastest_winner = null;
this.pots = [];
this.current_pot = null;
this.card = new Card();
this.cards_on_board = new Array(5);
this.playersOrderWithBestCard = [];
}
Table.prototype.setPlayerJoinMatch = function(player){
if(this.player_number >= game.MAX_PLAYER_PER_MATCH){
return -1;
}
if(this.players.indexOf(player) != -1){
return -2;
}
this.deletePlayerFromViewers(player);
return this.addPlayerToPlayers(player);
};
Table.prototype.setPlayerBeViewer = function(player){
// player is playing, can not move to viewer list
// if(player.state && player.state == util.PLAYER_STATE_PLAYING){
// return -2;
// }
var result = this.deletePlayerFromPlayers(player) ? 1 : -1;
if(result == -1)
return -1;
result = this.addPlayerToViewers(player);
if(this.host_player.username == player.username)
this.updateNewHost(player);
return result;
};
Table.prototype.addPlayerToPlayers = function(player){
var slot = this.findAvailableSlot();
if(slot == -1){
return -3;
}
player.slot = slot;
player.state = util.PLAYER_STATE_WAITING;
this.players[slot] = player;
this.player_number++;
if(!this.host_player){
this.host_player = player;
this.hostChange();
}
return 1;
};
Table.prototype.addPlayerToViewers = function(player){
if(this.viewers.indexOf(player) != -1){
return -1;
}
this.viewers.push(player);
return 1;
};
Table.prototype.deletePlayerFromPlayers = function(player){
var idx = this.players.indexOf(player);
if( idx != -1){
player.slot = -1;
delete this.players[idx];
this.player_number--;
return true;
}
return false;
};
Table.prototype.deletePlayerFromViewers = function(player){
var idx = this.viewers.indexOf(player);
if(idx != -1){
this.viewers.splice(idx, 1);
return true;
}
return false;
};
Table.prototype.removePlayer = function(player){
// if(player.state && player.state == util.PLAYER_STATE_PLAYING){
// return false;
// }
// var idx = this.viewers.indexOf(player);
var result = this.deletePlayerFromViewers(player);
if(result){
console.log('Table: delete ' + player.username + ' from viewers');
return true;
}else if(this.deletePlayerFromPlayers(player)){
console.log('Table: delete ' + player.username + ' from players');
if(this.host_player.username == player.username)
this.updateNewHost(player);
return true;
}
return false;
};
Table.prototype.findNewHost = function(){
var player = null;
this.players.forEach( function(element){
if(!player && element){
player = element;
}
});
return player;
};
Table.prototype.findAvailableSlot = function(){
// var slot_id = -1;
for(var i = 0 ; i < this.players.length ; i++){
if(!this.players[i])
return i;
}
return -1;
};
Table.prototype.updateNewHost = function(){
// if leaver is host, update new host
if(this.player_number > 0){
this.host_player = this.findNewHost();
this.hostChange();
}
if(this.player_number == 0){
this.host_player = null;
this.hostChange();
}
};
Table.prototype.changeBlind = function(player, value){
if(this.host_player && this.host_player.username == player.username){
if(this.state == util.TABLE_STATE_WAITING && value >= 0 && value < game.MAX_BLIND){
this.blind = value;
return true;
}
return false;
}
return false;
};
Table.prototype.changeSlot = function(player, new_slot){
if(this.players.indexOf(player) == -1 || this.state == util.TABLE_STATE_PLAYING){
return -1;
}
// console.log('new slot: ' + new_slot);
if(this.players[new_slot]){
return -2;
}
var old_slot = player.slot;
delete this.players[old_slot];
this.players[new_slot] = player;
player.slot = new_slot;
return player.slot;
};
Table.prototype.processNextTurn = function(){
while(true){
var min_bet = 999999;
var isPlayAllIn = false;
var remainPlayerLength = 0;
var player_id = [];
var total_bet = 0;
this.players.forEach(function(element){
console.log('current bet : ' + element.current_bet);
if(element.current_bet > 0){
if(element.isPlayingAllIn || isPlayAllIn){
isPlayAllIn = true;
min_bet = Math.min(min_bet, element.current_bet);
}else{
total_bet += element.current_bet;
player_id.push(element.id);
}
}
});
console.log('min bet : ' + min_bet);
if(isPlayAllIn){
total_bet = 0;
player_id = [];
this.players.forEach(function(element){
if(element.current_bet != 0 && element.state == util.PLAYER_STATE_PLAYING){
console.log('username ' + element.username + ' current bet : ' + element.current_bet);
element.current_bet -= min_bet;
console.log('username ' + element.username + ' current bet : ' + element.current_bet);
total_bet += min_bet;
if(element.current_bet == 0){
element.isPlayingAllIn = false;
}
player_id.push(element.id);
}
});
console.log('player id: ' + player_id);
if(player_id.length == 0)
break;
this.current_pot.player_id_list = player_id;
this.current_pot.chip += total_bet;
this.current_pot = new Pot();
this.pots.push(this.current_pot);
}else{
console.log('isPlayAllIn false - player id: ' + player_id);
if(player_id.length == 1){
this.players.forEach(function(element){
if(element.current_bet != 0 && element.state == util.PLAYER_STATE_PLAYING){
element.playing_chip += element.current_bet;
}
});
break;
}else if(player_id.length > 1){
this.current_pot.player_id_list = player_id;
this.current_pot.chip += total_bet;
console.log('current pot chip: ' + this.current_pot.chip);
console.log('list pot chip: ' + this.pots[0].chip);
}
break;
}
}
// process to next turn;
this.current_turn++;
// set current bet to zero
this.current_bet = 0;
this.hasPlayerAllIn = false;
// reset playing properties of player
this.players.forEach( function(element){
element.current_bet = 0;
element.hasCompleteTurn = false;
element.isPlayingAllIn = false;
});
};
Table.prototype.getPlayingPlayerCount = function(){
var count = 0;
this.players.forEach( function(element){
if(element.state == util.PLAYER_STATE_PLAYING){
count++;
}
});
return count;
};
Table.prototype.getPlayablePlayerCount = function(){
var count = 0;
this.players.forEach( function(element){
if(element.state == util.PLAYER_STATE_PLAYING && element.playing_chip > 0){
count++;
}
});
return count;
};
Table.prototype.deal_card = function(){
var data = {};
var cardGenerator = this.card;
var slot = {};
switch(this.current_turn){
case util.TURN_DEAL_IN_HAND:
{
this.players.forEach( function(element){
slot = {};
element.cards = new Array(2);
element.cards[0] = cardGenerator.generateCard();
slot['1'] = element.cards[0];
element.cards[1] = cardGenerator.generateCard();
slot['2'] = element.cards[1];
data[element.slot] = slot;
});
}
break;
case util.TURN_THREE_CARD:
{
this.cards_on_board = new Array(5);
this.cards_on_board[0] = cardGenerator.generateCard();
this.cards_on_board[1] = cardGenerator.generateCard();
this.cards_on_board[2] = cardGenerator.generateCard();
slot = {};
slot['1'] = this.cards_on_board[0];
slot['2'] = this.cards_on_board[1];
slot['3'] = this.cards_on_board[2];
data['board'] = slot;
}
break;
case util.TURN_FOURTH_CARD:
{
this.cards_on_board[3] = cardGenerator.generateCard();
slot = {};
slot['1'] = this.cards_on_board[3];
data['board'] = slot;
}
break;
case util.TURN_FIFTH_CARD:
{
this.cards_on_board[4] = cardGenerator.generateCard();
slot = {};
slot['1'] = this.cards_on_board[4];
data['board'] = slot;
}
break;
}
return data;
};
Table.prototype.startMatch = function(player){
if(this.host_player.username != player.username){
return -1; // host mới đc start
}
if(this.state == util.TABLE_STATE_PLAYING){
return -3; // bàn đang chơi
}
// check for player have chip 4 play is larger than 0
var count = 0;
var _blind = this.blind;
var total_chip = 0;
this.players.forEach( function(element){
// console.log('count : ' + count);
// console.log('table blind : ' + _blind);
if(element.playing_chip > _blind * 2){ // chip của người chơi phải lớn hơn blind 2 lần
count++;
}
});
// check không đủ người chơi
if(count < 2){
this.total_chip = 0;
return -2;
}else{
this.players.forEach( function(element){
element.total_bet = 0;
if(element.playing_chip > _blind * 2){ // chip của người chơi phải lớn hơn blind 2 lần
element.current_bet = 0;
element.total_bet = 0;
element.state = util.PLAYER_STATE_PLAYING;
}
});
}
var _start_player;
this.state = util.TABLE_STATE_PLAYING;
this.hasPlayerAllIn = false;
this.current_turn = util.TURN_DEAL_IN_HAND;
// tính người sẽ chơi đầu tiên
if(!this.lastest_winner || !this.players[this.lastest_winner.slot]){
_start_player = this.host_player;
}else{
_start_player = this.lastest_winner;
}
_start_player.playing_chip -= this.blind;
_start_player.current_bet += this.blind;
_start_player.total_bet += this.blind;
total_chip += this.blind;
this.slot_can_play = this.findSlotForPlay(_start_player.slot);
_start_player = this.players[this.slot_can_play];
this.current_bet = this.blind * 2;
_start_player.playing_chip -= this.current_bet;
_start_player.current_bet += this.current_bet;
_start_player.total_bet += this.current_bet;
total_chip += this.current_bet;
this.total_chip = total_chip;
this.card.reset();
// get started time of match
this.started_time = new Date().getTime();
this.host_lastedMatch = this.host_player.username;
// catch user id play this match for logging
var played_slot = [];
this.players.forEach( function(element){
played_slot[element.slot] = element.id;
});
this.played_slot = played_slot;
this.pots = [];
this.current_pot = new Pot();
this.pots.push(this.current_pot);
return 1;
};
// tố hết
Table.prototype.beat_all_in = function(player){
if(this.slot_can_play != player.slot)
return {type: -1, content: ' not your turn'};
if(player.playing_chip <= 0)
return {type: -2, content: ' not enough chip to play'}; // check for out of money
if(player.isPlayingAllIn)
return {type: -4, content: ' player can not play all-in again : maybe he is hacking'}; // player is playing all - in : maybe he's hacking
this.slot_begin = player.slot;
// get smallest chip player in table use value of all in
// var value_all_in = this.findSmallerPlayingChip();
// if(value_all_in == -1)
// return {type: -3, content: ' maybe wrong some thing with play all-in'}; // error : maybe wrong something
// increase current bet of table by value
// this.current_bet += value_all_in;
//
// // increase total chip of table by value
// this.total_chip += value_all_in;
//
// // minus playing chip of player
// player.playing_chip -= value_all_in;
//
// // increase total bet of player
// player.current_bet += value_all_in;
// player.total_bet += value_all_in;
this.hasPlayerAllIn = true;
var dif = player.playing_chip - this.current_bet;
if(dif > 0){
this.current_bet += dif;
}
var value = player.playing_chip;
this.total_chip += value;
player.current_bet += value;
player.total_bet += value;
player.isPlayingAllIn = true;
player.playing_chip = 0;
/*make sure every player who has current bet is smaller than current bet of table, must beat again*/
var table_current_bet = this.current_bet;
this.players.forEach( function(element){
if(element.current_bet < table_current_bet)
element.hasCompleteTurn = false;
});
return {type: value, content: ' OK!!'};
};
// bỏ lượt, không tố
Table.prototype.beat_check = function(player){
if(this.slot_can_play != player.slot)
return {type: -1, content: ' not your turn'}; // not turn
if(player.playing_chip <= 0)
return {type: -2, content: ' not enough chip to play'};
if(player.current_bet < this.current_bet)
return {type: -4, content: ' current bet of player is smaller than current bet of table, can not check must call or raise'}; // can not check, you must call or raise
if(player.current_bet > this.current_bet)
return {type: -4, content: ' current bet of player is smaller than current bet of table, can not check must call or raise. Maybe something wrong!'}; // error maybe wrong something
return {type: 0, content: ' OK!!'};
};
// đầu hàng, bỏ ván
Table.prototype.beat_fold = function(player){
if(this.slot_can_play != player.slot)
return {type: -1, content: ' not your turn'};
player.state = util.PLAYER_STATE_WAITING;
return {type: 1, content: ' OK!!'};
};
// tăng mức tố lên mức cao hơn hiện tại
Table.prototype.beat_raise = function(player, new_value){
if(this.slot_can_play != player.slot)
return {type: -1, content: ' not your turn'};
if(player.playing_chip <= 0)
return {type: -2, content: ' not enough chip to play'};
// if(new_value <= this.current_bet)
// return {type: -4, content: ' new bet is equal or smaller than current bet of player'};;// wrong value
// console.log('player.playing_chip : ' + player.playing_chip);
// console.log('current_bet : ' + this.current_bet);
// console.log('player.current_bet : ' + player.current_bet);
// console.log('new_value : ' + new_value);
var dif = this.current_bet + new_value - player.current_bet;
if(player.playing_chip < dif)
dif = player.playing_chip;
// return {type: -3, content: ' player will play as all-in'}; // play as all-in
//
// // update current bet of table
this.current_bet += new_value;
// set slot begin a turn is this player
this.slot_begin = player.slot;
// increase total chip of table by dif
this.total_chip += dif;
// minus playing chip of player
player.playing_chip -= dif;
// increase total bet of player
player.current_bet += dif;
player.total_bet += dif;
// make sure every player who has current bet is smaller than current bet of table, must beat again
var table_current_bet = this.current_bet;
this.players.forEach( function(element){
if(element.current_bet < table_current_bet)
element.hasCompleteTurn = false;
});
return {type: dif, content: ' OK!!'};
};
// bắt đầu lượt mới, set mức tố cho lượt
Table.prototype.beat_bet = function(player, value){
if(this.slot_can_play != player.slot)
return {type: -1, content: ' not your turn'};
if(player.playing_chip <= 0)
return {type: -2, content: ' not enough chip to play'};
if(this.current_bet != 0)
return {type: -4, content: ' current bet of table is greater than 0'}; // only set new bet value when current bet is zero
if(this.current_bet >= value)
return -5; // new bet must is not equal or smaller than current bet
if(player.playing_chip <= value)
return -3; // play as all - in
// set bet by blind of table
this.current_bet = value;
// increase total chip of table by blind
this.total_chip += value;
// set slot begin a turn is this player
this.slot_begin = player.slot;
// minus playing chip of player
player.playing_chip -= value;
// increase total bet of player
player.current_bet += value;
player.total_bet += value;
// make sure every player who has current bet is smaller than current bet of table, must beat again
this.players.forEach( function(element){
if(element.current_bet < value)
element.hasCompleteTurn = false;
});
return this.current_bet;
};
// Theo mức tố hiện tại của bàn
Table.prototype.beat_call = function(player){
if(this.slot_can_play != player.slot)
return {type: -1, content: ' not your turn'};
if(player.playing_chip <= 0)
return {type: -2, content: ' not enough chip to play'};
if(this.current_bet <= 0)
return {type: -4, content: ' chip bet of table is smaller than 0'};
if(this.current_bet <= player.current_bet)
return {type: -5, content: ' chip bet of player is greater than chip bet of table'}; // wrong value
var dif = this.current_bet - player.current_bet;
// console.log('diff: ' + dif);
if(player.playing_chip < dif)
return {type: -3, content: ' player will play as all-in'}; // play as all-in
// increase total chip of table by dif
this.total_chip += dif;
// minus playing chip of player
player.playing_chip -= dif;
// increase total bet of player
player.current_bet += dif;
player.total_bet += dif;
return {type: dif, content: ' OK!!'};
};
// tìm slot kế tiếp có thể chơi
Table.prototype.findSlotForPlay = function(old_slot){
var next_slot = old_slot;
do{
next_slot = (next_slot + 1) % game.MAX_PLAYER_PER_MATCH;
// console.log('next slot is ' + next_slot);
if(next_slot == old_slot)
return -1;
}while(!this.players[next_slot]
|| (this.players[next_slot]
&& (this.players[next_slot].hasCompleteTurn // this player is done the turn
|| this.players[next_slot].state == util.PLAYER_STATE_WAITING // this player is not in match
|| this.players[next_slot].playing_chip <= 0 // this player is out of chip, can skip
|| this.players[next_slot].isPlayingAllIn) // this player is playing all-in
)
); //
return next_slot;
};
Table.prototype.findSmallerBettingChip = function(){
var value = 999999999;
this.players.forEach(function(element){
if(element && element.state == util.PLAYER_STATE_PLAYING && element.current_bet != 0){
value = Math.min(value, element.current_bet);
}
});
if(value == 999999999)
value = -1;
return value;
};
// Tính kết quả
Table.prototype.calculateResult = function(){
var cards_on_board = this.cards_on_board;
// var game_cards = this.card;
// var result_player = [];
// var order_result = {};
var winner;
// var winner_count = -1;
var this_table = this;
this.playersOrderWithBestCard = [];
var players = [];
this.players.forEach( function(element){
if(element.state == util.PLAYER_STATE_PLAYING){
element.pocker_hand = {type: '', best_cards:[]};
if(this_table.current_turn <= util.TURN_FIFTH_CARD){
console.log('one winner');
winner = element;
players.push(element);
return;
}
element.pocker_hand = Card.getBestPokerHand(element.cards, cards_on_board);
var idx = -1;
for(var i = 0 ; i < players.length ; i++ ){
if( players[i].pocker_hand['type'] < element.pocker_hand['type']
|| ( players[i].pocker_hand['type'] == element.pocker_hand['type']
&& ( players[i].pocker_hand['best_cards'][4] < element.pocker_hand['best_cards'][4]
|| ( players[i].pocker_hand['best_cards'][4] == element.pocker_hand['best_cards'][4]
&& players[i].pocker_hand['best_cards'][3] < element.pocker_hand['best_cards'][3] )
|| ( players[i].pocker_hand['best_cards'][4] == element.pocker_hand['best_cards'][4]
&& players[i].pocker_hand['best_cards'][3] == element.pocker_hand['best_cards'][3]
&& players[i].pocker_hand['best_cards'][2] < element.pocker_hand['best_cards'][2] )
|| ( players[i].pocker_hand['best_cards'][4] == element.pocker_hand['best_cards'][4]
&& players[i].pocker_hand['best_cards'][3] == element.pocker_hand['best_cards'][3]
&& players[i].pocker_hand['best_cards'][2] == element.pocker_hand['best_cards'][2]
&& players[i].pocker_hand['best_cards'][1] < element.pocker_hand['best_cards'][1] )
)
)
)
{
idx = i;
break;
}
}
if(idx == -1){
players.push(element);
}else{
players.splice(idx, 0, element);
}
}
});
if(players.length > 0){
winner = players[0];
this.playersOrderWithBestCard = players;
}
if(!winner){
this.winner = undefined;
return false;
}else{
this.winner = winner;
return true;
}
};
Table.prototype.getWinners = function(){
var winners = [];
var count = 0;
var winner = {};
while(0 < this.pots.length){
if(this.pots[0].chip == 0)
break;
var player = this.playersOrderWithBestCard[0];
winner = {};
winner.chip_win = 0;
winner.best_cards = [];
for(var i = 0 ; i < this.pots.length ;){
console.log('i : ' + i + ' chip pot : ' + this.pots[i].chip + ' player ' + this.pots[i].player_id_list);
if(this.pots[i].player_id_list.indexOf(player.id) != -1){
winner.username = player.username;
winner.slot = player.slot;
winner.chip_win += this.pots[i].chip;
if(!winner.content && player.pocker_hand){
winner.content = Card.getPokerHandRanking(player.pocker_hand['type']);
}
winner.best_cards = player.pocker_hand['best_cards'];
player.playing_chip += this.pots[i].chip;
winner.playing_chip = player.playing_chip;
this.pots.splice(0,1);
}else{
i++;
}
}
winners.push(winner);
this.playersOrderWithBestCard.splice(0,1);
}
return winners;
}
// lưu log của trận đấu
Table.prototype.saveMatchLog = function(db_connector){
// db_connector.query('INSERT INTO log_match VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'
// , [this.id, this.started_time, this.blind, this.total_chip
// , this.played_slot[0] ? this.played_slot[0] : -1
// , this.played_slot[1] ? this.played_slot[1] : -1
// , this.played_slot[2] ? this.played_slot[2] : -1
// , this.played_slot[3] ? this.played_slot[3] : -1
// , this.played_slot[4] ? this.played_slot[4] : -1
// , this.played_slot[5] ? this.played_slot[5] : -1
// , this.played_slot[6] ? this.played_slot[6] : -1
// , this.played_slot[7] ? this.played_slot[7] : -1
// , this.winner ? this.winner.id : -1 ]
// );
db_connector.query('INSERT INTO public.log_match' +
'(table_id, started_time, blind, host_name, slot_1, slot_2, slot_3, slot_4, slot_5, slot_6, slot_7, slot_8) ' +
'VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)'
, [this.id, this.started_time, this.blind, this.host_lastedMatch
, this.played_slot[0] ? this.played_slot[0] : -1
, this.played_slot[1] ? this.played_slot[1] : -1
, this.played_slot[2] ? this.played_slot[2] : -1
, this.played_slot[3] ? this.played_slot[3] : -1
, this.played_slot[4] ? this.played_slot[4] : -1
, this.played_slot[5] ? this.played_slot[5] : -1
, this.played_slot[6] ? this.played_slot[6] : -1
, this.played_slot[7] ? this.played_slot[7] : -1]
);
delete this.played_slot;
};
//
// Player information
//
function Player(id, username){
this.id = id;
this.username = username;
this.level = 1;
this.isActive = false;
this.isPlaying = false;
this.location = game.LOCATION_LOBBY;
// this.chip = 1000;
// value in playing
this.slot = -1;
this.isPlayingAllIn = false;
this.hasCompleteTurn = false;
// bet each turn
this.current_bet = 0;
// bet each match
this.total_bet = 0;
this.turn_type = util.BEAT_NONE;
this.playing_chip = 0;
this.chip = 10000;
this.cards = new Array(2);
}
Player.prototype.loadChipToPlay = function(chip){
if(chip <= 0) return -1;
// if(this.chip <= 0) return -1;
var actual_chip = -1;
actual_chip = game.MAX_CHIP_4_PLAY - this.playing_chip;
if(actual_chip <= 0)
return -2;
actual_chip = Math.min(actual_chip, chip);
// this.chip -= actual_chip;
this.playing_chip += actual_chip;
this.chip -= actual_chip;
return actual_chip;
};
Player.prototype.returnChip = function(chip){
if(chip <= 0) return -1;
var actual_chip = -1;
// actual_chip = game.MAX_CHIP_4_PLAY - ;
actual_chip = Math.min(this.playing_chip, chip);
this.playing_chip -= actual_chip;
this.chip += actual_chip;
return actual_chip;
};
/*
CARD OF GAME
expr of card is: value = rank * 4 + suit;
Min Rank is : 2
Minimum value of card is 8 which means 2*4 + 0;
*/
Card.MIN_VALUE = 2 * 4 + 0; // 8 is 2 bí
Card.MAX_VALUE = ( 2 + 12 ) * 4 + 3; // 59 - xì cơ
Card.MIN_RANK = 2;
Card.MAX_RANK = 2 + 12;
Card.SUIT_SPADE = 0;
Card.SUIT_CLUB = 1;
Card.SUIT_DIAMOND = 2;
Card.SUIT_HEART = 3;
// rank of poker hand
Card.HAND_ROYAL_PLUS = 9;
Card.HAND_STRAIGHT_PLUS = 8;
Card.HAND_FOUR_OF_A_KIND = 7;
Card.HAND_FULL_HOUSE = 6;
Card.HAND_PLUS = 5;
Card.HAND_STRAIGHT = 4;
Card.HAND_THREE_OF_A_KIND = 3;
Card.HAND_TWO_PAIR = 2;
Card.HAND_PAIR = 1;
Card.HAND_HIGHT_CARD = 0;