/
holdem.php
1532 lines (1392 loc) · 56.8 KB
/
holdem.php
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
<?php
/*
* This is the Texas Hold`em poker ruleset for aiEngine version 2
* ---
* view the README for more information regarding the functions and extra commands
* available in this module.
*/
class poker extends aiv2 {
public $spots = 0;
public $start;
public $starter = null;
public $turn = 0;
public $moves;
public $playerblinds = array(0,0);
public $bigblind;
public $smallblind;
public $totalgames = null;
public $game = null;
public $turntimer;
public $gamecards;
public $gamecardstable = array();
public $deck;
public $bet = false;
public $playersingame;
public $moneylog = array();
public $newgame = false;
public $playercalls = array();
public $return = 0;
# Variables added for pots
public $pot;
public $playerpot = array();
public $betamount;
public function __construct() {
parent :: __construct();
printf ("Loaded the Texas Hold `em Module succesfully!\n");
if (!is_writeable('logs')) {
echo "Attempting to change the directory's permissions\n";
chmod('logs', 777);
if (!is_writable('logs')) {
die("Fatal error: The logs directory is not writable");
}
}
if (!function_exists('imagecreate')) {
die ("Fatal error: Texas Hold `em requires the GD-library to be enabled in PHP.ini");
}
# Generate the deck only, this has to be done only once
$types = array('spades', 'clubs', 'hearts', 'diamonds');
$cards = array(2,3,4,5,6,7,8,9,10,'Jack', 'Queen', 'King', 'Ace');
$deck = array();
foreach($types as $type) {
reset($cards);
foreach ($cards as $card) {
$deck[] = sprintf("%s of %s", $card, $type);
}
}
$this->deck = $deck;
}
// Heartbeat
// Checks if a player takes too long about his turn or whenever we can start the
// game
public function heartbeat() {
if ($this->return == true) {
return;
}
// This is where the game logic is done, whenever the game is full
// Start a new game
if ( ($this->spots == $this->connections) && $this->game == 0) {
$this->startgame();
}
// Time the player's turns
if ($this->turn) {
$this->turntimer++;
if ($this->turntimer == 5) {
/*
* 20 seconds have passed, player is forced to fold
*/
$this->turntimer = 0;
$player = $this->players[$this->turn];
$this->broadcast(sprintf("%s has waited too long and is forced to fold!", parent :: playerName($player)));
$this->playerFold($player);
}
}
// New game ?
if ($this->newgame == true) {
$this->startgame();
$this->newgame = false;
}
}
/*
* checkstraight
* checks if the given array of cards contains a straight
*/
function checkstraight(array $cards) {
$am = count($cards);
$possible = $am - 4;
$straightcards = array();
if ($possible == 0) {
return false;
}
# Make sure our array doesn't go out of bounds
for ($i = 0; $i < $possible; $i++) {
$start = $cards[$i];
$straightcards = array();
$straightcards[] = $start;
$expect = $cards[$i] -1;
// We need four cards to match below the card we have
for ($j = 1; $j <= 4; $j++) {
$card = $cards[$j + $i];
if ($expect == $card) {
$expect = $expect -1;
$straightcards[] = $card;
# We have a straight, return the cards
if ($j == 4) {
return $straightcards;
}
}
else {
# This starter did not attain a straight
break;
}
}
}
return false;
}
/*
* cardName
* ---
* Returns the name of the card based on the number (reversed calculation)
*/
function cardName($card) {
$card = intval($card);
$return = array(1 => 'Ace', 14 => 'Ace', 2 => 'Two', 3 => 'Three', 4 => 'Four',
5 => 'Five', 6 => 'Six', 7 => 'Seven', 8 => 'Eight', 9 => 'Nine',
10 => 'Ten', 11 => 'Jack', 12 => 'Queen', 13 => 'King');
return $return[$card];
}
/*
* Calcpoker returns a score based on the deck of cards entered
* into the function!
*/
function calcPoker(array $cards, $player) {
$myCards = array();
$myColors = array('spades' => 0, 'diamonds' => 0, 'hearts' => 0, 'clubs' => 0);
$sortCards = array('spades' => array(), 'diamonds' => array(), 'hearts' => array(), 'clubs' => array());
$score = 0;
// The points one could gain for such an interesting event :-)
$straight_flush = 180;
$four_of_a_kind = 140;
$fullhouse = 120;
$flush = 100;
$straight = 80;
$three_pair = 40;
$two_pair = 20;
$one_pair = 10;
$high_card = 0;
# Analyse data given as input
foreach ($cards as $card) {
$card = explode(" ", $card);
$data = $card[0];
$type = strtolower($card[2]);
switch ($data) {
// No problems with normal cards
case 10:
case 9:
case 8:
case 7:
case 6:
case 5:
case 4:
case 3:
case 2:
$sortCards[$type][] = $data;
$myCards[] = $data;
break;
// Give a numeric value to king, queen and jack
case 'King':
case 'Queen':
case 'Jack':
$values = array('Jack' => 11, 'Queen' => 12, 'King' => 13);
$myCards[] = $values[$data];
$sortCards[$type][] = $values[$data];
break;
// Ace is a smart brother, he can count as both 1 and 14
case 'Ace':
$myCards[] = 1;
$myCards[] = 14;
$sortCards[$type][] = 14;
break;
}
$myColors[$type]++;
}
# Reverse sort cards so we actually have the highest cards first
rsort($myCards);
$tmp = $paired = array_count_values($myCards);
// Flush?
foreach ($sortCards as $key => $value) {
$tempArray = $sortCards[$key];
if (count($tempArray) >= 5) {
$flushcards = $sortCards[$key];
rsort($flushcards);
# Check for a straight flush
$check = $this->checkStraight($flushcards);
if (!$check) {
$score += $straight;
for ($i = 0; $i < 5; $i++) {
$score += ($flushcards[$i] / 100);
$flushscore = $score;
}
}
else {
// Straight flush!
$added = array_sum($check) /100;
$score += $straight_flush + $added;
$this->broadcast(sprintf("%s got a straight flush! score: %0.3f", parent :: playerName($player), $score));
# Highest possible score, we can return here!
return $score;
}
}
}
// Four of a kind?
if (in_array(4, $paired)) {
$card = array_search(4, $paired);
$score += $four_of_a_kind + (4 * ($card /100));
// Find the high card
for ($i = 0; $i <= 5; $i++) {
if ($myCards[$i] != $card) {
$score += $myCards[$i] / 1000;
break;
}
}
$this->broadcast(sprintf("%s got four of a kind! score: %0.3f\n", parent :: playerName($player), $score));
return $score;
}
// Full house
$three = 0;
$pair = 0;
foreach ($tmp as $card => $amount) {
switch ($amount) {
case 1:
unset($tmp[$card]);
break;
case 2:
if ($card != 1) {
$pair++;
}
break;
case 3:
$three++;
// If we already had a three of a kind, the two of three
// cards might still be viable for a higher pair!
if ($three > 1 && $card != 1) {
$tmp[$card]--;
$pair++;
}
break;
}
}
if ($three >= 1 && $pair >= 1) {
$scorethree = 0;
$scorepair = 0;
reset ($tmp);
foreach ($tmp as $card => $amount) {
# Find the highest pair
if ($scorepair == 0 && $amount == 2) {
$scorepair = $card;
}
# And the highest three of a kind
if ($scorethree == 0 && $amount == 3) {
$scorethree = $card;
}
}
$score += $fullhouse + ( ((3*$scorethree) /100) + ((2*$scorepair)) /1000) ;
$this->broadcast(sprintf("%s got a full house %ss and %ss score %0.3f", parent :: playerName($player), $this->cardname($scorethree), $this->cardname($scorepair), $score));
return $score;
}
# We have a flush, at this moment this is the highest we can attain
if (isSet($flushscore) && !empty($flushscore)) {
$this->broadcast(sprintf("%s got a flush with score %0.3f", parent :: playerName($player), $flushscore));
return $flushscore;
}
# Straight
$cards = $this->checkstraight($myCards);
if ($cards) {
$score += $straight;
$add = array_sum($cards) / 100;
$score += $add;
$this->broadcast(sprintf("%s got a straight with a score of %0.3f", parent :: playerName($player), $score));
return $score;
}
# Three of a kind
if ($three == 1) {
$highcard = array();
$gotcards = 0;
$threes = 0;
foreach ($myCards as $card) {
# Is our card a high card that needs to be taken into account?
if ($paired[$card] == 1 && $gotcards != 2) {
$highcard[$gotcards] = $card;
$gotcards++;
}
# Is this the three of a kind?!
if ($paired[$card] == 3 && !$threes) {
$threes = $card;
}
}
$score = $three_pair;
$add = (($threes * 3) + ($highcard[0] + $highcard[1])) /100;
$score += $add;
$this->broadcast(sprintf("%s got a three of a kind %s with score: %0.3f", parent :: playerName($player), $this->cardName($threes), $score));
return $score;
}
# Two pair
if ($pair == 2) {
$highcard = 0;
$pairs = 0;
$pairs = array();
foreach ($myCards as $card) {
if ($paired[$card] == 2 && $pairs != 2) {
$pairs++;
$pairs[] = $card;
}
else {
if ($highcard == 0) {
$highcard = $card;
}
}
}
$score = $two_pair;
$add = ((array_sum($pairs) *20) + $highcard) /100;
$score += $add;
$bcMsg = array();
foreach ($pairs as $value) {
$bcMsg[] = sprintf('%s%s', $this->cardName($value), $value == 6 ? 'es' : "s");
}
$bcMsg = array_unique($bcMsg);
$bcMsg = implode(" and ", $bcMsg);
$this->broadcast(sprintf("%s got two pair %s with score %0.3f", parent :: playerName($player), $bcMsg, $score));
return $score;
}
# One pair
if ($pair == 1) {
$highcards = array();
$hc = 0;
$pair = 0;
foreach ($myCards as $card) {
if ($paired[$card] ==1 && $hc != 3) {
$hc++;
$highcards[] = $card;
}
if ($paired[$card] == 2 && !$pair) {
$pair = $card;
}
}
$score = $one_pair;
$add = (array_sum($highcards) + ($pair *20)) / 100;
$score += $add;
$this->broadcast(sprintf("%s got a pair of %ss with score: %0.3f", parent :: playerName($player),$this->cardName($pair),$score));
return $score;
}
# High card
$score = $high_card;
$score += $myCards[0] /100;
$this->broadcast(sprintf("%s got high card %s with total score: %0.3f", parent :: playerName($player), $this->cardName($myCards[0]), $score));
return $score;
}
/*
* setRules
* Sets up the rules and the amount of hands played for each game!
*/
public function setRules($players = 2, $playermoney = 10, $bigblind = 1, $smallblind = .5, $games = 1000) {
if ($players < 2) {
printf("I'm sorry to report that Texas Hold `em requires at least 2 players.\n");
die();
}
if ($players > 10) {
printf("I am sorry to report that the maximum amount of players for Texas Hold `em is 10\n");
die();
}
$this->spots = $players;
$this->playermoney = $playermoney;
$this->bigblind = $bigblind;
$this->smallblind = $smallblind;
$this->totalgames = $games;
# Have the server accept new players
parent :: acceptclients($players);
}
public function addplayer($playerid) {
$player = new pokerplayer();
$name = sprintf('Player %d', $playerid);
$player->name = $name;
$this->names[] = $name;
$player->id = $playerid;
$player->money = $this->playermoney;
$this->moneylog[$player->id] = array($player->money);
$this->players[$playerid] = $player;
if ($this->spots != $this->connections) {
// Provide the connected members with a message that our game is waiting for x more player(s)
$required = $this->spots - $this->connections;
parent :: broadcast(sprintf("[SERVER] The game is waiting for %d more player%s", $required, $required != 1 ? 's' : ''));
}
$required = $this->spots - $this->connections;
if ($this->return == true && $required == 0) {
$this->restore();
}
return $player;
}
public function disconnect($player) {
// Cancel the game and wait for x amount of new players
$p = $player;
parent :: disconnect($player);
$required = $this->spots - $this->connections;
parent :: broadcast(sprintf("[SERVER] The game is waiting for %d more player%s", $required, $required != 1 ? 's' : ''));
$this->turn = 0;
$this->return = true;
}
/*
* Welcome
* ---
* Welcomes the player to the game and explains the rules
*/
public function welcome($player) {
if (!isSet($player->id)) {
return false;
}
parent :: welcome($player);
$this->send($player, "Texas Hold`em module - (C) 2010 Patrick Mennen <helmet@helmet.nl>\r\n");
$this->send($player, "You are playing Texas Hold`em (Limit!) using the following rules:\r\n");
$this->send($player, sprintf("- Players: %d\r\n- Big blind: %0.2f\r\n- Small blind: %0.2f\r\n- Games: %d\r\n\r\nYou receive $%0.2f\r\n", $this->spots, $this->bigblind, $this->smallblind, $this->totalgames,$this->playermoney));
$this->send($player, sprintf("RS=%d,%0.2f,%0.2f,%0.2f,%d", $this->spots, $this->playermoney, $this->bigblind, $this->smallblind, $this->totalgames));
$this->send($player, sprintf("ID=%d", $player->id));
}
/*
* AddMoney
*/
public function addMoney($player, $amount) {
if ($player->money >= $amount) {
// Player has sufficient funds, spread the money over the new pots created
$this->pot += $amount;
$player->money -= $amount;
$this->playerpot[$player->id] += $amount;
if ($player->money == 0) {
$player->tmpout = true;
}
}
else {
// Player's money is not sufficient, create a new pot in the current game
$this->pot += $player->money;
$this->playerpot[$player->id] += $player->money;
$player->money = 0;
$player->tmpout = true;
}
}
/*
* PlayersLeft
* returns the amount of players still playing in the current hand
*/
public function playersLeft() {
$i = 0;
foreach($this->players as $pl) {
if (!$pl->out && !$pl->fold && !$pl->tmpout) {
$i++;
}
}
return $i;
}
public function playersLeftArray() {
$players = array();
foreach ($this->players as $pl) {
if (!$pl->out) {
$players[] = $pl->id;
}
}
return $players;
}
/*
* Winner
* assigns the pot to the winner and starts a new game
*/
private function winner($scores) {
$max = 0;
$handout = array();
$return = array();
$wincount = 0;
foreach ($this->players as $player) {
if (!$player->out) {
$handout[$player->id] = 0;
$return[$player->id] = 0;
}
}
while ($this->pot > 0) {
$winners = array();
$high = null;
$j = 0;
reset($scores);
if (count($scores >= 0)) {
foreach ($scores as $i => $score) {
if (!isSet($this->playerpot[$i]) || $this->playerpot[$i] == 0) {
continue;
}
$j++;
if ($j == 1) {
$high = $score;
$winners[] = $i;
continue;
}
if ($score != $high) {
break;
}
else {
$winners[] = $i;
}
}
}
if (count($winners) != 0) {
$split = $this->pot / count($winners);
foreach ($winners as $id) {
$player = $this->players[$id];
if (!isSet($this->playerpot[$player->id]) || $this->playerpot[$player->id] == 0) {
unset($scores[$player->id]);
continue;
}
$max = $this->playersInHand() * $this->playerpot[$player->id];
if ($wincount == 0) {
$player->wins++;
}
if ($max >= $split) {
$player->money += $split;
$this->playerpot[$player->id] -= $split;
$this->pot -= $split;
$handout[$player->id] += $split;
}
else {
$player->money += $max;
unset($this->playerpot[$player->id]);
$handout[$player->id] -= $max;
$this->pot -=$max;
}
if (isSet($this->playerpot[$player->id]) && $this->playerpot[$player->id] <= 0) {
if (isSet($scores[$player->id])) {
unset($scores[$player->id]);
}
}
}
$wincount++;
}
if (count($winners) == 0) {
/*
* Dividing the pot isn't entirely fair therefore we randomize the player that gets
* the first share of the pot. this code is experimental and should be tested
*/
$ids = $this->playersLeftArray();
shuffle($ids);
foreach ($ids as $id) {
if (!isSet($this->playerpot[$id]) || !$this->playerpot[$id]) {
continue;
}
$max = $this->playerpot[$id];
$player = $this->players[$id];
if ($max > 0) {
if ($max < $this->pot) {
$player->money += $max;
$return[$player->id] += $max;
$this->pot -= $max;
}
else {
$player->money += $this->pot;
$return[$player->id] += $this->pot;
$this->pot = 0;
}
$this->playerpot[$player->id] = 0;
}
}
break;
}
}
arsort($handout);
foreach ($handout as $playerid => $value) {
if ($value > 0) {
$player = $this->players[$playerid];
$this->broadcast(sprintf("%s receives $%0.2f", parent :: playerName($player), $value));
//$this->send($player, sprintf("You receive $%0.2f", $value, $player->money));
}
}
foreach ($return as $playerid => $value) {
if ($value > 0) {
$player = $this->players[$playerid];
$this->broadcast(sprintf("$%0.2f is returned to %s", $value, parent :: playerName($player)), $player);
$this->send($player, sprintf("$%0.2f is returned to you!", $value));
}
}
$left = 0;
$pot = 0;
foreach ($this->players as $pl) {
if ($pl->money == 0 && !$pl->out) {
$this->broadcast(sprintf("%s is out of money and out of the game", parent :: playerName($pl)), $pl);
$this->broadcast(sprintf("OUT=%d", $pl->id));
$this->send($pl, "Sorry, you are out of money and thus out of the game :(");
$pl->out = true;
$this->moneylog[$pl->id][] = 0;
}
else {
if (!$pl->out) {
$left++;
$this->send($pl, sprintf("OM=%0.2f", $pl->money));
$this->send($pl, sprintf("You now have $%0.2f", $pl->money));
$pot += $pl->money;
// printf("%s is still in the game with $%0.3f\n", parent :: playerName($pl), $pl->money);
$this->moneylog[$pl->id][] = $pl->money;
}
}
}
if ($pot != ($this->connections * $this->playermoney)) {
/*
* This should never happen unless a player is deliberatly trying to fool the server
* in which case we abort the program
*/
printf("The pot of $0.2f doesn\'t add up with the required $0.2f\n", $pot, ($this->connections * $this->playermoney));
foreach ($this->players as $k => $p) {
printf("%d. %s $%0.2f%s\n", $p->id, parent :: playerName($p), $p->money, $p->out == true ? ' (out)' : '');
}
exit;
}
if ($left == 1) {
// Because there's only one player left with money, we can assume that the pot went to
// said player
$player = $this->players[$winners[0]];
$this->broadcast("The game was won by %s after %d hands", parent :: playerName($player), $this->game);
$this->broadcast("Generating log file...");
$this->generateLog($player);
}
else {
// Continue with the next hand
if ($this->game < $this->totalgames) {
$this->newgame = true;
}
else {
$this->generateLog();
$this->newgame = false;
$this->turn = 0;
}
}
}
/*
* generateLog
* --
* Generates a logfile whenever a game has come to its end
*/
public function generateLog($winner = false) {
$gameid = strtotime('now');
$template = file_get_contents('data/log.html');
$fp = fopen(sprintf('logs/log_%d.html', $gameid), 'a');
$htplayers = array();
foreach ($this->players as $pl) {
$htplayers[] = sprintf('<li>%s</li>', htmlspecialchars(parent :: playerName($pl)));
}
$players = implode("\n", $htplayers);
if ($winner) {
$data = sprintf("After %d hands played, the player <strong>%s</strong> won the total pot of <strong>$%0.2f</strong>", $this->game, parent :: playerName($winner), $winner->money);
}
else {
$htplayers = array();
foreach ($this->players as $pl) {
$htplayers[] = sprintf('<li>%s ($%0.2f)</li>', htmlspecialchars(parent :: playerName($pl)), $pl->money);
}
$htplayers = implode("\n", $htplayers);
$data = sprintf("After %d hands no winner was determined a summary of each individual player's money follows:<ol>%s</ol>", $this->game, $htplayers);
}
$template = str_replace("{players}", $players, $template);
$template = str_replace("{gameid}", $gameid, $template);
$template = str_replace("{winner}", $data, $template);
$template = str_replace("{version}", parent :: $version, $template);
$template = str_replace("{date}", date('d-m-Y h:i', $gameid), $template);
$template = str_replace("{startmoney}", sprintf('%0.2f', $this->playermoney), $template);
$template = str_replace("{bigblind}", sprintf('%0.2f', $this->bigblind), $template);
$template = str_replace("{smallblind}", sprintf('%0.2f', $this->smallblind), $template);
$template = str_replace("{maxgames}", $this->totalgames, $template);
$template = str_replace("{winner}", $data, $template, $template);
fputs($fp, $template);
fclose($fp);
require_once('data/jpgraph.php');
require_once('data/jpgraph_line.php');
$width = 900;
$height = 300;
$graph = new Graph($width, $height);
$graph->img->SetMargin(40,40,40,40);
$graph->SetShadow();
$graph->setScale('intlin');
$graph->title->Set('Amount of money per player, per hand');
$graph->xaxis->title->Set('Hand');
$graph->yaxis->title->Set('Money');
foreach ($this->moneylog as $id => $data) {
$p = $this->players[$id];
$lineplot = new LinePlot($data);
$lineplot->setweight ( $id );
$lineplot->SetLegend(parent :: playerName($p));
$graph->add($lineplot);
}
$graph->stroke(sprintf('logs/playermoney_%d.jpg', $gameid));
// Pie graph, showing checks, folds, bets, wins, calls and raises
require_once ("data/jpgraph_pie.php");
// Create the Pie Graph.
$height = ceil(count($this->players) /2) * 440;
$graph = new PieGraph($width, $height);
$graph->SetShadow();
// Set A title for the plot
$graph->title->Set("Moves per user");
// Create plots
$size=0.20;
$x = array(0.25, 0.75);
$y = 220;
$i = 1;
$legend = array("Check","Call","Bet","Raise","Fold");
foreach ($this->players as $id => $player) {
$plot = new PiePlot(array($player->checks, $player->calls, $player->bets, $player->raises, $player->folds));
if ($player->id == 1) {
$plot->SetLegends($legend);
}
$plot->SetLabelType(PIE_VALUE_ADJPERCENTAGE);
$plot->SetSize($size);
$plot->SetCenter($x[$i -1], $y);
if ($i == 2) {
$i = 0;
$y += 420;
}
$i++;
$plot->title->Set(parent :: playerName($player));
$graph->add($plot);
}
$graph->stroke(sprintf('logs/playermoves_%d.jpg', $gameid));
// Bargraph containing each player's wins
$height = 70 * count($this->players);
require_once ("data/jpgraph_bar.php");
$graph = new Graph($width,$height);
$graph->SetScale('textlin');
$graph->SetShadow();
$lbl = array();
$data = array();
foreach ($this->players as $pl) {
$lbl[] = parent :: PlayerName($pl);
$data[] = $pl->wins;
}
$top = 60;
$bottom = 30;
$left = 80;
$right = 30;
$graph->Set90AndMargin($left,$right,$top,$bottom);
$graph->xaxis->SetTickLabels($lbl);
$graph->xaxis->SetLabelAlign('right','center','right');
$graph->yaxis->SetLabelAlign('center','bottom');
$graph->title->Set('Amount of hands won by individual players');
$bplot = new BarPlot($data);
$bplot->SetFillGradient("navy","lightsteelblue",GRAD_HOR);
$bplot->SetWidth(0.5);
$bplot->SetYMin(0);
$graph->Add($bplot);
$graph->Stroke(sprintf('logs/playerwins_%d.jpg', $gameid));
$this->newgame = false;
// Remove the clients from the server
$this->broadcast("[SERVER] Thank you for playing Texas Hold`em (LIMIT)\n[SERVER] If you want to play another game, just reconnect to the server!");
$this->return = true;
foreach ($this->players as $player) {
$this->disconnect($player);
}
$this->return = true;
$this->restore();
}
/*
* Restore
* --
* Starts a new game as if nothing has happened :-)
*/
private function restore() {
// Resets the game
$this->game = 0;
$this->turn = 0;
$this->pot = 0;
$this->turntimer = 0;
$this->playercalls = array();
$this->playerpot = array();
$this->playerblinds = array(0,0);
unset($this->betamount);
$this->newgame = false;
$this->bet = false;
$this->moves = 0;
$this->starter = null;
$this->turntimer = 0;
foreach ($this->players as $player) {
if ($player->id) {
$player->out = false;
$player->fold = false;
$player->tmpout = false;
$player->cards = array();
$player->wins = $player->folds = $player->bets = $player->calls = $player->checks = $player->raises = 0;
$player->money = $this->playermoney;
$this->moneylog[$player->id] = array($player->money);
}
}
$this->broadcast("[SERVER] The game was succesfully reset.");
$this->return = false;
}
/*
* playerFold
* is executed when a player folds or gets folded automatically by the system
*/
private function playerFold($player) {
$player->folds++;
$this->broadcast(sprintf("P=FOLD"));
$this->broadcast(sprintf("%s folds.", parent :: playerName($player)));
$player->fold = true;
if ($this->playersLeft() == 1) {
// We have a winner, based on the amount of folds, determine the player that
// didn't fold
foreach ($this->players as $pl) {
if (!$pl->fold && !$pl->out) {
$score = array($pl->id => 120);
$this->winner($score);
break;
}
}
return $pl;
}
# We still have multiple players in the game
$this->nextTurn();
}
/*
* Overrides the default handlecommand function allowing the module to add it's own
* new commands in this case: bet, raise, fold and check
*/
public function handleCommand($player, $command, $args) {
$orig = $command;
$command = strtolower($command[0]);
switch ($command) {
case 'fold':
if ($this->turn == $player->id) {
$this->playerFold($player);
}
else {
$this->send($player, "Sorry, it is not your turn yet!");
}
break;
case 'call':
if ($this->turn == $player->id) {
if ($this->bet) {
$bet = $this->getbet();
if ($player->money <= $bet) {
// Player doesn't have the money to call the bet and has to go ALL-IN
$this->broadcast("P=CALL");
$this->broadcast(sprintf("%s is forced to go ALL-IN with only $%0.2f left", parent :: playerName($player), $player->money));
$this->betamount= $player->money;
$this->addMoney($player, $bet);
$this->playercalls[$player->id] = true;
$player->calls++;
if ($this->playersLeft() == 1 && $this->playersInHand() > 1) {
$player->tmpout = true;
$this->progress();
return;
}
$this->nextTurn();
return;
}
else {
$this->broadcast("P=CALL");
$this->broadcast(sprintf("%s CALLS the bet", parent :: playerName($player)));
$this->addMoney($player, $bet);
$this->playercalls[$player->id] = true;
$player->calls++;
if ($this->playersLeft() == 1 && $this->playersInHand() > 1) {
$player->tmpout = true;
$this->progress();
return;
}
$this->nextTurn();
return;
}
}
else {
$this->send($player, "There are no previous bets! would you like to BET?");
}
}
else {
$this->send($player, "Sorry, it is not your turn yet!");
}
break;
case 'bet':
if ($this->turn == $player->id) {
# Did some player already bet?
if ($this->bet) {
# Yes, provide a nice error message
$this->send($player, "Another player already made a bet!");
$this->send($player, "You can either CALL, RAISE or FOLD");
}
else {
// Player is still in the game but the only player left
if ($this->playersLeft() == 1 && $this->playersInHand() > 1) {
$this->send($player, "No other players can CALL a bet, you can only CHECK, CALL or FOLD!");
return;
}
# NO! place the bet
if ($this->moves == 1) {
$bet = $this->bigblind;
if ($player->id == $this->playerblinds[0]) {
$bet -= $this->smallblind;
}
if ($player->id == $this->playerblinds[1]) {
$bet = 0;
}
if ($player->money >= $bet && $bet != 0) {
$this->addMoney($player, $bet);
$this->playercalls[$player->id] = true;
$this->broadcast(sprintf("%s automatically pays %0.2f in order to pay the big blind", parent :: playerName($player), $bet));