-
Notifications
You must be signed in to change notification settings - Fork 0
/
briscola.game.php
1100 lines (925 loc) · 44 KB
/
briscola.game.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
/**
*------
* BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
* Briscola implementation : © Tommaso Pifferi <p.tommy93@gmail.com> & Antonio <ai6chr+briscola@gmail.com>
*
* This code has been produced on the BGA studio platform for use on http://boardgamearena.com.
* See http://en.boardgamearena.com/#!doc/Studio for more information.
* -----
*
* briscola.game.php
*
* This is the main file for your game logic.
*
* In this PHP file, you are going to defines the rules of the game.
*
*/
require_once( APP_GAMEMODULE_PATH.'module/table/table.game.php' );
// Local constants
// - Number of rounds
// - Team pairing options
// - Various constants
define("SHORT_GAME", 1); // Short (1 win)
define("CLASSIC_GAME", 2); // Classic (3 wins)
define("LONG_GAME", 3); // Long (5 wins)
define("TEAM_RANDOM", 1); // At random
define("TEAM_1_3", 2); // By table order (1rst/3rd versus 2nd/4th)
define("TEAM_1_2", 3); // By table order (1rst/2nd versus 3rd/4th)
define("TEAM_1_4", 4); // By table order (1rst/4th versus 2nd/3rd)
// Assign a big negative value to briscola card, so that it's drawn as last card
define("BRISCOLA_LOCATION_ARG", -1000);
define("DRAW_SCORE", 60);
define("MINIMUM_WINNING_SCORE", 61);
define("NUMBER_OF_CARDS_IN_FULL_DECK", 40);
define("BIG_SCORE", 100);
define("PERFECT_SCORE", 120);
define("ALL_BRISCOLA_CARDS", 10);
class Briscola extends Table
{
function __construct( )
{
// Your global variables labels:
// Here, you can assign labels to global variables you are using for this game.
// You can use any number of global variables with IDs between 10 and 99.
// If your game has options (variants), you also have to associate here a label to
// the corresponding ID in gameoptions.inc.php.
// Note: afterwards, you can get/set the global variables with getGameStateValue/setGameStateInitialValue/setGameStateValue
parent::__construct();
self::initGameStateLabels(array(
"firstPlayedColor" => 10,
"briscolaColor" => 11,
"briscolaValue" => 12,
"briscolaCardId" => 13,
"handNumber" => 14,
"dealer" => 15,
"firstPlayer" => 16,
"showCardsPhaseDone" => 17,
"winningHandsToEndGame" => 18,
"roundsNumber" => 100,
"playersTeams" => 101
));
// Init $this->cards to be a deck
$this->cards = self::getNew("module.common.deck");
$this->cards->init("card");
}
protected function getGameName( )
{
// Used for translations and stuff. Please do not modify.
return "briscola";
}
/*
setupNewGame:
This method is called only once, when a new game is launched.
In this method, you must setup the game according to the game rules, so that
the game is ready to be played.
*/
protected function setupNewGame( $players, $options = array() )
{
// Set the colors of the players with HTML color code
// The default below is red/green/blue/orange/brown
// The number of colors defined here must correspond to the maximum number of players allowed for the gams
$default_colors = array(
"ff0000",
"0000ff",
"ff0000",
"0000ff"
);
// Create players
// Note: if you added some extra field on "player" table in the database (dbmodel.sql), you can initialize it there.
$sql = "INSERT INTO player (player_id, player_no, player_color, player_canal, player_name, player_avatar) VALUES ";
// Create some kind of sorted arrays with positions (?) using the undocumented `player_table_order` column
$order_values = array();
foreach($players as $player_id => $player) {
$order_values[] = $player["player_table_order"];
}
sort($order_values);
$position = array();
foreach($order_values as $key => $val) {
$position[$val] = $key + 1;
}
// Compute player table order based on teams
$counter = 0;
$random_dealer = mt_rand(1, count($players));
$values = array();
foreach($players as $player_id => $player)
{
$color = null;
$player_no = null;
$counter++;
$playersTeams = self::getGameStateValue('playersTeams');
if ($playersTeams == TEAM_RANDOM) {
// Random since the $players order is random
// N.B: 2-players match can only end up here!
$color = array_shift($default_colors);
$player_no = $counter;
} else if (isset($player["player_table_order"])) {
// By default TEAM_1_3
$table_order = $position[$player["player_table_order"]];
if ($playersTeams == TEAM_1_2) {
// If TEAM_1_2 swap 2 and 3
$table_order = ($table_order == 2 ? 3 : ($table_order == 3 ? 2 : $table_order));
} else if ($playersTeams == TEAM_1_4) {
// If TEAM_1_4 swap 4 and 3
$table_order = ($table_order == 3 ? 4 : ($table_order == 4 ? 3 : $table_order));
}
if (isset($default_colors[$table_order - 1])) {
$color = $default_colors[$table_order - 1];
// Adjust player_no for randomizing first player (dealer)
if ($table_order >= $random_dealer) {
$player_no = $table_order - $random_dealer + 1;
} else {
$player_no = 4 - ($random_dealer - $table_order) + 1;
}
}
}
$values[] = "('" . $player_id . "','" . $player_no . "','$color','" . $player['player_canal'] . "','" . addslashes($player['player_name']) . "','" . addslashes($player['player_avatar']) . "')";
}
$sql .= implode( $values, ',' );
self::DbQuery( $sql );
self::reloadPlayersBasicInfos();
/************ Start the game initialization *****/
// Init global values with their initial values
self::setGameStateInitialValue('firstPlayedColor', 0);
self::setGameStateInitialValue('briscolaColor', 0);
self::setGameStateInitialValue('briscolaValue', 0);
self::setGameStateInitialValue('briscolaCardId', 0);
self::setGameStateInitialValue('handNumber', 0);
self::setGameStateInitialValue('dealer', -1);
$roundsNumberOption = self::getGameStateValue('roundsNumber');
$roundsNumber = null;
if ($roundsNumberOption == SHORT_GAME) {
$roundsNumber = 1;
} else if ($roundsNumberOption == CLASSIC_GAME) {
$roundsNumber = 3;
} else if ($roundsNumberOption == LONG_GAME) {
$roundsNumber = 5;
}
self::setGameStateInitialValue('winningHandsToEndGame', $roundsNumber);
// Init game statistics
self::initStat("table", "handNbr", 0);
self::initStat("player", "bigScore", 0);
self::initStat("player", "perfectScore", 0);
self::initStat("player", "noBriscola", 0);
self::initStat("player", "allBriscola", 0);
// Create cards
$cards = array ();
$colors = $this->italianColors;
foreach ($colors as $colorId => $color) {
// spade, heart, diamond, club
for ($value = 2; $value <= 11; $value ++) {
// 2, 4, 5 ... K, 3, A
$cards [] = array ('type' => $colorId,'type_arg' => $value,'nbr' => 1);
}
}
// Create cards deck
$this->cards->createCards( $cards, 'deck');
// Activate next player and set first player global
$this->activeNextPlayer();
$firstPlayer = self::getActivePlayerId();
self::setGameStateInitialValue('firstPlayer', $firstPlayer);
/************ End of the game initialization *****/
}
/*
getAllDatas:
Gather all informations about current game situation (visible by the current player).
The method is called each time the game interface is displayed to a player, ie:
_ when the game starts
_ when a player refreshes the game page (F5)
*/
protected function getAllDatas()
{
$result = array();
// !! We must only return informations visible by this player !!
$currentPlayerId = self::getCurrentPlayerId();
// Get information about players
// Note: you can retrieve some extra field you added for "player" table in "dbmodel.sql" if you need it.
$sql = "SELECT player_id id, player_score score, player_tricks tricks FROM player ";
$result['players'] = self::getCollectionFromDb($sql);
// Gather all information about current game situation (visible by player $current_player_id).
// Cards in player hand
$result['hand'] = $this->cards->getCardsInLocation('hand', $currentPlayerId);
// Cards played on the table
$result['cardsontable'] = $this->cards->getCardsInLocation('cardsontable');
$cardsInDeck = $this->cards->getCardsInLocation('deck');
if (count($cardsInDeck) > 0) {
$result['cardsindeck'] = count($cardsInDeck) - 1;
$idCartaBriscola = self::getGameStateValue('briscolaCardId');
foreach ($cardsInDeck as $cardId => $card) {
if ($cardId == $idCartaBriscola) {
$result['briscola'] = $card;
break;
}
}
} else {
$result['cardsindeck'] = 0;
$result['briscola'] = null;
}
// Dealer info
$result['dealer'] = self::getGameStateValue('dealer');
return $result;
}
/*
getGameProgression:
Compute and return the current game progression.
The number returned must be an integer beween 0 (=the game just started) and
100 (= the game is finished or almost finished).
This method is called each time we are in a game state with the "updateGameProgression" property set to true
(see states.inc.php)
*/
function getGameProgression()
{
$winningHandsToEndGame = self::getGameStateValue('winningHandsToEndGame');
$currentCardsInDeck = $this->cards->countCardsInLocation('deck');
$drawnCardRatio = (NUMBER_OF_CARDS_IN_FULL_DECK - $currentCardsInDeck) / (float) NUMBER_OF_CARDS_IN_FULL_DECK;
$userScores = self::getCollectionFromDb("SELECT player_id, player_score FROM player", true );
$maxScore = 0;
foreach( $userScores as $playerId => $score ) {
if ($score > $maxScore) {
$maxScore = $score;
}
}
$base = 1 / (float) $winningHandsToEndGame * ($maxScore) * 100;
$current = $drawnCardRatio * 100 / (float) $winningHandsToEndGame;
$progress = $base + $current;
return $progress;
}
//////////////////////////////////////////////////////////////////////////////
//////////// Utility functions
////////////
/*
In this space, you can put any utility methods useful for your game logic
*/
function getPlayersToDirection() {
$result = array();
$players = self::loadPlayersBasicInfos();
$nextPlayer = self::createNextPlayerTable(array_keys($players));
$currentPlayer = self::getCurrentPlayerId();
if (count($players) == 2) {
$directions = array('S', 'N');
} else if (count($players) == 4) {
// Counterclockwise
$directions = array('S', 'E', 'N', 'W');
}
if(!isset($nextPlayer[$currentPlayer])) {
// Spectator mode: take any player for south
$player_id = $nextPlayer[0];
$result[$player_id] = array_shift($directions);
} else {
// Normal mode: current player is on south
$player_id = $currentPlayer;
$result[$player_id] = array_shift($directions);
}
while(count($directions) > 0) {
$player_id = $nextPlayer[$player_id];
$result[$player_id] = array_shift($directions);
}
return $result;
}
function getLogsPlayedCardValue($value) {
$italianValue = $this->italianValueLabel[$value];
$frenchValue = $this->frenchValueLabel[$value];
// We send both italian and french values, then
// the client will hide one of them using JS
return "<div class='briscola-logs-card-value italian' title='$italianValue'>$italianValue</div><div class='briscola-logs-card-value french' title='$frenchValue'>$frenchValue</div>";
}
function getLogsPlayedCardSuit($color) {
$italianSuit = ucfirst($this->italianColors[$color]['name']);
// Used for css class
$italianNonTranslatedSuit = $this->italianColors[$color]['nameorig'];
$frenchSuit = ucfirst($this->frenchColors[$color]['name']);
$frenchSuitIcon = $this->frenchIcons[$color];
return "<div class='briscola-logs-card-suit italian' title='$italianSuit'><div class='$italianNonTranslatedSuit'></div></div><div class='briscola-logs-card-suit french' title='$frenchSuit'>$frenchSuitIcon</div>";
}
//////////////////////////////////////////////////////////////////////////////
//////////// Player actions
////////////
/*
Each time a player is doing some game action, one of the methods below is called.
(note: each method below must match an input method in briscola.action.php)
*/
function playCard($cardId, $isZombie) {
self::checkAction("playCard");
$playerId = self::getActivePlayerId();
// Check if card is in actual player's hand
$cardIsInHand = false;
$playerHand = $this->cards->getCardsInLocation('hand', $playerId);
foreach($playerHand as $playerCard) {
if ($playerCard['id'] == $cardId) {
$cardIsInHand = true;
break;
}
}
if (!$cardIsInHand) {
throw new feException("This card is not in your hand");
}
$this->cards->moveCard($cardId, 'cardsontable', $playerId);
$currentCard = $this->cards->getCard($cardId);
$currentTrickColor = self::getGameStateValue('firstPlayedColor') ;
if( $currentTrickColor == 0 ) {
self::setGameStateValue('firstPlayedColor', $currentCard['type'] );
}
// Build played cards log
$cardValue = $currentCard['type_arg'];
$cardColor = $currentCard['type'];
$message = clienttranslate('${player_name} plays ${value_displayed} of ${color_displayed}');
if ($isZombie) {
$message = clienttranslate('${player_name} automatically plays ${value_displayed} of ${color_displayed}');
}
// And notify
self::notifyAllPlayers('playCard', $message,
array (
'i18n' => array('color_displayed','value_displayed'),
'card_id' => $cardId,
'player_id' => $playerId,
'player_name' => self::getActivePlayerName(),
'value' => $cardValue,
'value_displayed' => self::getLogsPlayedCardValue($cardValue),
'color' => $cardColor,
'color_displayed' => self::getLogsPlayedCardSuit($cardColor)
));
$this->gamestate->nextState('playCard');
}
function endShowCards() {
self::checkAction("endShowCards");
// Here we have to get CURRENT player (= player who send the request) and not
// active player, because we are in a multiple active player state and the "active player"
// corresponds to nothing.
$player_id = self::getCurrentPlayerId();
// Continue the game until the end
$this->gamestate->setPlayerNonMultiactive($player_id, "endShowCards");
}
//////////////////////////////////////////////////////////////////////////////
//////////// Game state arguments
////////////
/*
Here, you can create methods defined as "game state arguments" (see "args" property in states.inc.php).
These methods function is to return some additional information that is specific to the current
game state.
*/
function argShowCards() {
// Send each team all their won tricks, as well as teammate current hand
$players = self::loadPlayersBasicInfos();
$nextPlayer = self::createNextPlayerTable(array_keys($players));
$firstPlayerId = self::getGameStateValue('firstPlayer');
$secondPlayerId = $nextPlayer[$firstPlayerId];
$thirdPlayerId = $nextPlayer[$secondPlayerId];
$fourthPlayerId = $nextPlayer[$thirdPlayerId];
// This is just to remember how teams are made up
$playerToTeam = array();
$playerToTeam[$firstPlayerId] = 1;
$playerToTeam[$secondPlayerId] = 2;
$playerToTeam[$thirdPlayerId] = 1;
$playerToTeam[$fourthPlayerId] = 2;
$firstTeamCardsWon = array_merge(
$this->cards->getCardsInLocation('cardswon', $firstPlayerId),
$this->cards->getCardsInLocation('cardswon', $thirdPlayerId)
);
$secondTeamCardsWon = array_merge(
$this->cards->getCardsInLocation('cardswon', $secondPlayerId),
$this->cards->getCardsInLocation('cardswon', $fourthPlayerId)
);
return array(
// Using "_private" keyword, all data inside this array will be made private
'_private' => array(
// Data will be sent only to specific player IDs
$firstPlayerId => array(
'trickswon' => $firstTeamCardsWon,
'teammate_hand' => array_merge($this->cards->getCardsInLocation('hand', $thirdPlayerId), array())
),
$secondPlayerId => array(
'trickswon' => $secondTeamCardsWon,
'teammate_hand' => array_merge($this->cards->getCardsInLocation('hand', $fourthPlayerId), array())
),
$thirdPlayerId => array(
'trickswon' => $firstTeamCardsWon,
'teammate_hand' => array_merge($this->cards->getCardsInLocation('hand', $firstPlayerId), array())
),
$fourthPlayerId => array(
'trickswon' => $secondTeamCardsWon,
'teammate_hand' => array_merge($this->cards->getCardsInLocation('hand', $secondPlayerId), array())
)
)
);
}
//////////////////////////////////////////////////////////////////////////////
//////////// Game state actions
////////////
/*
Here, you can create methods defined as "game state actions" (see "action" property in states.inc.php).
The action method of state X is called everytime the current game state is set to X.
*/
function stNewHand() {
$players = self::loadPlayersBasicInfos();
// Stats and game states variables
self::incStat(1, "handNbr");
self::setGameStateValue('handNumber', self::getGameStateValue('handNumber') + 1);
self::setGameStateValue('showCardsPhaseDone', 0);
// Current player is the dealer
$oldDealer = self::getGameStateValue('dealer');
$currentDealer = null;
if ($oldDealer == -1) {
$currentDealer = self::getActivePlayerId();
// Next player is the first one to play
$this->activeNextPlayer();
} else {
$nextPlayer = self::createNextPlayerTable(array_keys($players));
$currentDealer = $nextPlayer[$oldDealer];
// Player after dealer is the one who plays
$this->gamestate->changeActivePlayer($nextPlayer[$currentDealer]);
}
self::setGameStateValue('dealer', $currentDealer);
self::notifyAllPlayers('dealCards', clienttranslate('${player_name} is the dealer of this hand') , array(
'player_id' => $currentDealer,
'player_name' => $players[$currentDealer]['player_name']
));
// Take back all cards (from any location => null) to deck
$this->cards->moveAllCardsInLocation(null, "deck");
$this->cards->shuffle('deck');
$totalCards = $this->cards->countCardsInLocation('deck');
// Choose briscola
// N.B: The briscola index goes from 1 to len(array) because cards are indexed using auto increment IDs
$briscolaIndex = rand(1, $totalCards);
$briscola = $this->cards->getCardsInLocation('deck')[$briscolaIndex];
$briscolaId = $briscola['id'];
self::setGameStateValue('briscolaColor', $briscola['type'] );
self::setGameStateValue('briscolaValue', $briscola['type_arg'] );
self::setGameStateValue('briscolaCardId', $briscolaId );
// Set big negative location_arg to briscola, so that it's drawn at last for sure
$briscolaLocationArg = BRISCOLA_LOCATION_ARG;
$sql = "UPDATE card SET card_location_arg='$briscolaLocationArg' WHERE card_id='$briscolaId'";
self::DbQuery($sql);
// Deal 3 cards to each players and give some other info to the client
// N.B: Subtract: 3 cards per player and the briscola
$cardsindeck = $totalCards - (count($players) * 3) - 1;
foreach ($players as $playerId => $player) {
// Reset player_tricks
$sql = "UPDATE player SET player_tricks=0 WHERE player_id='$playerId' ";
self::DbQuery($sql);
$cards = $this->cards->pickCards(3, 'deck', $playerId);
// Notify player about his cards, as well as other infos that are useful
// when passing from one hand to another, without refreshing
// (thus, without calling getAllData function)
self::notifyPlayer($playerId, 'newHand', '', array (
'cards' => $cards,
'cardsindeck' => $cardsindeck,
'briscola' => $briscola));
}
$this->gamestate->nextState("");
}
function stNewTrick() {
// Reset trick color to 0 (= no color)
self::setGameStateInitialValue('firstPlayedColor', 0);
// If there are 4 players, and there are 0 cards in the deck,
// we need to show each other' cards to companions as well as all taken cards during the game!
$players = self::loadPlayersBasicInfos();
$cardsInDeck = $this->cards->countCardInLocation('deck');
$showCardsPhaseDone = self::getGameStateValue('showCardsPhaseDone');
if ($cardsInDeck == 0 && count($players) == 4 && $showCardsPhaseDone == 0) {
self::setGameStateValue('showCardsPhaseDone', 1);
$this->gamestate->nextState("finalPhase");
} else {
$this->gamestate->nextState("nextPlayer");
}
}
function stDrawCards() {
// Draw cards from the deck and give them to each player.
// Instead of implementing a real turn, we make each player draw the first card
// on the deck, exception made for the briscola card.
$players = self::loadPlayersBasicInfos();
$numberOfPlayers = count($players);
$lastWinnerId = self::getActivePlayerId();
$cardsInDeckCount = $this->cards->countCardInLocation('deck');
$lastWinnerPosition = $players[$lastWinnerId]['player_no'];
for ($i = 0; $i < $numberOfPlayers; $i++) {
$playerIdGiveCardTo = null;
foreach ($players as $player_id => $player) {
$currentPlayerPosition = $players[$player_id]['player_no'];
$nextPlayerPosition = $lastWinnerPosition + $i;
if ($nextPlayerPosition > $numberOfPlayers) {
$nextPlayerPosition = $nextPlayerPosition % $numberOfPlayers;
}
if ($currentPlayerPosition == $nextPlayerPosition) {
$playerIdGiveCardTo = $player_id;
break;
}
}
// This is a convoluted way to represent deck draws:
// we create as many DOM nodes representing the deck as there are cards in the actual deck,
// and proceed to delete 2/4 of them after every draw.
$card = $this->cards->pickCard('deck', $playerIdGiveCardTo);
$currentRemainingCards = $cardsInDeckCount - $i - 1;
$remainingCardsAfterTurn = max($cardsInDeckCount - $numberOfPlayers - 1, 0);
if ($currentRemainingCards > 0) {
self::notifyPlayer($playerIdGiveCardTo, 'drawNewCard', '', array (
'card' => $card,
'card_index_to_pick'=> $cardsInDeckCount - 1,
'remaining_cards' => $remainingCardsAfterTurn,
'id_briscola' => self::getGameStateValue('briscolaCardId')));
} else {
self::notifyPlayer($playerIdGiveCardTo, 'drawNewCard', '', array (
'card' => $card,
'card_index_to_pick'=> 0,
'remaining_cards' => 0,
'id_briscola' => self::getGameStateValue('briscolaCardId')));
}
}
$this->gamestate->nextState("");
}
function stNextPlayer() {
$players = self::loadPlayersBasicInfos();
$numberOfPlayers = count($players);
// Active next player OR end the trick and go to the next trick OR end the hand
if ($this->cards->countCardInLocation('cardsontable') == $numberOfPlayers) {
// This is the end of the trick
$cardsOnTable = $this->cards->getCardsInLocation('cardsontable');
$bestValue = 0;
$bestValuePlayerId = null;
$currentTrickColor = self::getGameStateValue('firstPlayedColor');
$semeBriscola = self::getGameStateValue('briscolaColor');
$semeCorrente = $currentTrickColor;
$primaBriscolaTrovata = false;
foreach ( $cardsOnTable as $card ) {
if ($card ['type'] == $semeBriscola) {
if (!$primaBriscolaTrovata) {
$semeCorrente = $semeBriscola;
// Note: location_arg = player who played this card on table
$bestValuePlayerId = $card ['location_arg'];
// Note: type_arg = value of the card
$bestValue = $card ['type_arg'];
$primaBriscolaTrovata = true;
continue;
}
}
if ($card ['type'] == $semeCorrente) {
if ($bestValuePlayerId === null || $card ['type_arg'] > $bestValue) {
// Note: location_arg = player who played this card on table
$bestValuePlayerId = $card ['location_arg'];
// Note: type_arg = value of the card
$bestValue = $card ['type_arg'];
}
}
}
// Active this player => he's the one who starts the next trick
$this->gamestate->changeActivePlayer($bestValuePlayerId);
// Move all cards to "cardswon" of the given player
$this->cards->moveAllCardsInLocation('cardsontable', 'cardswon', null, $bestValuePlayerId);
// Update tricks info
$sql = "UPDATE player SET player_tricks=player_tricks+1 WHERE player_id='$bestValuePlayerId'";
self::DbQuery($sql);
$tricks = self::getUniqueValueFromDb("SELECT player_tricks FROM player WHERE player_id='$bestValuePlayerId'");
// Notify
// Note: we use 2 notifications here in order we can pause the display during the first notification
// before we move all cards to the winner (during the second)
self::notifyAllPlayers( 'trickWin', clienttranslate('${player_name} wins the trick'), array(
'player_id' => $bestValuePlayerId,
'player_name' => $players[$bestValuePlayerId]['player_name'],
'tricks' => $tricks
));
self::notifyAllPlayers( 'giveAllCardsToPlayer','', array(
'player_id' => $bestValuePlayerId
));
// Decide what state is next
if ($this->cards->countCardInLocation('hand') == 0) {
// End of the hand
$this->gamestate->nextState("endHand");
} else {
// End of the trick
if ($this->cards->countCardInLocation('deck') > 0) {
$this->gamestate->nextState("nextCards");
} else {
$this->gamestate->nextState("nextTrick");
}
}
} else {
// Standard case (not the end of the trick)
// => just active the next player
$playerId = self::activeNextPlayer();
self::giveExtraTime($playerId);
$this->gamestate->nextState('nextPlayer');
}
}
function stShowCards() {
// Active all players: everyone can have a peek at old cards/teammate cards
$this->gamestate->setAllPlayersMultiactive();
}
function stEndHand() {
// Count and score points, then end the game or go to the next hand.
$players = self::loadPlayersBasicInfos();
$playersToPoints = array();
$playersToBriscolaCards = array();
foreach ($players as $playerId => $player) {
$playersToPoints[$playerId] = 0;
$playersToBriscolaCards[$playerId] = 0;
}
$briscolaColor = self::getGameStateValue('briscolaColor');
$cards = $this->cards->getCardsInLocation("cardswon");
foreach ($cards as $card) {
$playerId = $card['location_arg'];
/**
* Summary of briscola's cards and their values
*
* Cards nominal values: 2 4 5 6 7 J Q K 3 A
* Cards internal values: 2 3 4 5 6 7 8 9 10 11
* Cards points: 0 0 0 0 0 2 3 4 10 11
*/
if ($card['type_arg'] == 7) {
/* J */
$playersToPoints[$playerId] += 2;
} else if ($card['type_arg'] == 8) {
/* Q */
$playersToPoints[$playerId] += 3;
} else if ($card['type_arg'] == 9) {
/* K */
$playersToPoints[$playerId] += 4;
} else if ($card['type_arg'] == 10) {
/* Tre */
$playersToPoints[$playerId] += 10;
} else if ($card['type_arg'] == 11) {
/* Asso */
$playersToPoints[$playerId] += 11;
}
// Compute briscola stats
if ($card['type'] == $briscolaColor) {
$playersToBriscolaCards[$playerId] += 1;
}
}
// Add team logic when dealing with 4 players
if (count($players) == 4) {
$nextPlayer = self::createNextPlayerTable(array_keys($players));
$firstPlayerId = self::getGameStateValue('firstPlayer');
$secondPlayerId = $nextPlayer[$firstPlayerId];
$thirdPlayerId = $nextPlayer[$secondPlayerId];
$fourthPlayerId = $nextPlayer[$thirdPlayerId];
$playerToTeam = array();
$playerToTeam[$firstPlayerId] = 1;
$playerToTeam[$secondPlayerId] = 2;
$playerToTeam[$thirdPlayerId] = 1;
$playerToTeam[$fourthPlayerId] = 2;
$teamToPoints = array(
1 => 0,
2 => 0
);
foreach ($playersToPoints as $playerId => $points) {
$teamToPoints[$playerToTeam[$playerId]] += $points;
}
$teamToBriscolaCards = array(
1 => 0,
2 => 0
);
foreach ($playersToBriscolaCards as $playerId => $briscolaCards) {
$teamToBriscolaCards[$playerToTeam[$playerId]] += $briscolaCards;
}
$teamToPlayers = array(
1 => array(
1 => $firstPlayerId,
2 => $thirdPlayerId
),
2 => array(
1 => $secondPlayerId,
2 => $fourthPlayerId
)
);
}
// Notify score to players
// N.B: All the parts of code regarding notifications/stats have been duplicated
// in order to take into account the differences between 2/4 players games
if (count($players) == 2) {
foreach ($playersToPoints as $playerId => $points) {
self::notifyAllPlayers("points", clienttranslate('${player_name} makes ${points} points'), array (
'player_id' => $playerId,
'player_name' => $players[$playerId]['player_name'],
'points' => $points));
if ($points >= BIG_SCORE) {
self::incStat(1, "bigScore", $playerId);
}
if ($points == PERFECT_SCORE) {
self::incStat(1, "perfectScore", $playerId);
}
}
} else if (count($players) == 4) {
foreach ($teamToPoints as $teamId => $points) {
$p1Id = $teamToPlayers[$teamId][1];
$p2Id = $teamToPlayers[$teamId][2];
self::notifyAllPlayers("points", clienttranslate('Team ${player_name_1} and ${player_name_2} make ${points} points'), array (
'player_name_1' => $players[$p1Id]['player_name'],
'player_name_2' => $players[$p2Id]['player_name'],
'points' => $points));
if ($points >= BIG_SCORE) {
self::incStat(1, "bigScore", $p1Id);
self::incStat(1, "bigScore", $p2Id);
}
if ($points == PERFECT_SCORE) {
self::incStat(1, "perfectScore", $p1Id);
self::incStat(1, "perfectScore", $p2Id);
}
}
}
// Apply score to players and notify hand winner or draw
if (count($players) == 2) {
foreach ($playersToPoints as $playerId => $points) {
if ($points >= MINIMUM_WINNING_SCORE) {
$sql = "UPDATE player SET player_score=player_score+1 WHERE player_id='$playerId'";
self::DbQuery($sql);
self::notifyAllPlayers("points", clienttranslate('${player_name} wins the hand'), array (
'player_id' => $playerId,
'player_name' => $players [$playerId] ['player_name']));
} else if ($points == DRAW_SCORE) {
self::notifyAllPlayers("points", clienttranslate('Draw!'));
break;
}
}
} else if (count($players) == 4) {
foreach ($teamToPoints as $teamId => $points) {
$p1Id = $teamToPlayers[$teamId][1];
$p2Id = $teamToPlayers[$teamId][2];
if ($points >= MINIMUM_WINNING_SCORE) {
$sql = "UPDATE player SET player_score=player_score+1 WHERE player_id='$p1Id'";
self::DbQuery($sql);
$sql = "UPDATE player SET player_score=player_score+1 WHERE player_id='$p2Id'";
self::DbQuery($sql);
self::notifyAllPlayers("points", clienttranslate('Team ${player_name_1} and ${player_name_2} wins the hand'), array (
'player_name_1' => $players[$p1Id]['player_name'],
'player_name_2' => $players[$p2Id]['player_name']));
} else if ($points == DRAW_SCORE) {
self::notifyAllPlayers("points", clienttranslate('Draw!'));
break;
}
}
}
// Check briscola stats
if (count($players) == 2) {
foreach ($playersToBriscolaCards as $playerId => $briscolaNumber) {
if ($briscolaNumber == 0) {
self::incStat(1, "noBriscola", $playerId);
} else if ($briscolaNumber == ALL_BRISCOLA_CARDS) {
self::incStat(1, "allBriscola", $playerId);
}
}
} else if (count($players) == 4) {
foreach ($teamToBriscolaCards as $teamId => $briscolaNumber) {
$p1Id = $teamToPlayers[$teamId][1];
$p2Id = $teamToPlayers[$teamId][2];
if ($briscolaNumber == 0) {
self::incStat(1, "noBriscola", $p1Id);
self::incStat(1, "noBriscola", $p2Id);
} else if ($briscolaNumber == ALL_BRISCOLA_CARDS) {
self::incStat(1, "allBriscola", $p1Id);
self::incStat(1, "allBriscola", $p2Id);
}
}
}
$newScores = self::getCollectionFromDb("SELECT player_id, player_score FROM player", true);
self::notifyAllPlayers("newScores", '', array('newScores' => $newScores));
if (count($players) == 4) {
$teamToNewScores = array(
1 => 0,
2 => 0
);
foreach ($newScores as $playerId => $score) {
// We override these variables twice, but we don't care since the score
// is the same for each member of the team
$teamToNewScores[$playerToTeam[$playerId]] = $score;
}
}
// Display table window with results
$table = array();
// Header line
$firstRow = array('');
if (count($players) == 2) {
foreach($players as $playerId => $player) {
$firstRow[] = array(
'str' => '${player_name}',
'args' => array('player_name' => $player['player_name']),
'type' => 'header'
);
}
} else if (count($players) == 4) {
foreach($teamToPlayers as $teamId => $teamPlayers) {
$p1Id = $teamPlayers[1];
$p2Id = $teamPlayers[2];
$teamName = $players[$p1Id]['player_name'] . " and " . $players[$p2Id]['player_name'];
$firstRow[] = array(
'str' => '${team_name}',
'args' => array('team_name' => $teamName),
'type' => 'header'
);
}
}
$table[] = $firstRow;
// Points
$newRow = array(array('str' => clienttranslate('Current game points'), 'args' => array()));
if (count($players) == 2) {
foreach($playersToPoints as $playerId => $points) {
if ($points >= MINIMUM_WINNING_SCORE) {
$newRow[] = clienttranslate("" . $points . " (<b>Win</b>)");
} else if ($points == DRAW_SCORE) {
$newRow[] = clienttranslate("" . $points . " (<b>Draw</b>)");
} else {
$newRow[] = clienttranslate("" . $points . " (<b>Lose</b>)");
}
}
} else if (count($players) == 4) {
foreach($teamToPoints as $teamId => $points) {
if ($points >= MINIMUM_WINNING_SCORE) {
$newRow[] = clienttranslate("" . $points . " (<b>Win</b>)");
} else if ($points == DRAW_SCORE) {
$newRow[] = clienttranslate("" . $points . " (<b>Draw</b>)");
} else {
$newRow[] = clienttranslate("" . $points . " (<b>Lose</b>)");
}
}
}
$table[] = $newRow;
// Final score
$newRow = array(array('str' => clienttranslate('Overall score'), 'args' => array()));
if (count($players) == 2) {
foreach($newScores as $playerId => $score) {
$newRow[] = "<b>" . $score . "</b>";
}
} else if (count($players) == 4) {
foreach($teamToNewScores as $teamId => $score) {
$newRow[] = "<b>" . $score . "</b>";
}
}