-
Notifications
You must be signed in to change notification settings - Fork 0
/
daleofmerchants.game.php
5543 lines (5198 loc) · 260 KB
/
daleofmerchants.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 & Emmanuel Colin & BoardGameArena
* DaleOfMerchants implementation : © Bart Swinkels [email protected]
*
* 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.
* -----
*
* daleofmerchants.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 "modules/DaleEffects.php";
require_once "modules/DaleDeckSelection.php";
class DaleOfMerchants extends DaleTableBasic
{
var DaleDeckSelection $deckSelection;
var DaleDeck $cards;
var DaleEffects $effects;
var $chameleon_targets_cache = array();
var bool $inactUsePassiveAbility;
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();
$this->initGameStateLabels( array(
"inDeckSelection" => 10,
"resolvingCard" => 11,
"isPostCleanUpPhase" => 12,
"opponent_id" => 13,
"card_id" => 14,
"changeActivePlayer_player_id" => 15,
"changeActivePlayer_state_id" => 16,
"player_id_1" => 17,
"player_id_2" => 18,
"player_id_3" => 19,
"player_id_4" => 20,
"hand_size_before" => 21,
"active_player_id" => 22,
"die_value" => 23,
"debugMode" => 24,
"animalfolk_id" => 25
) );
$this->effects = new DaleEffects($this);
$this->cards = new DaleDeck($this, $this->effects, "onLocationExhausted");
$this->cards->init("card");
$this->deckSelection = new DaleDeckSelection($this);
}
protected function getGameName( )
{
// Used for translations and stuff. Please do not modify.
return "daleofmerchants";
}
/*
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
$gameinfos = $this->getGameinfos();
$default_colors = $gameinfos['player_colors'];
// 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_color, player_canal, player_name, player_avatar, player_coins) VALUES ";
$values = array();
foreach( $players as $player_id => $player )
{
$color = array_shift( $default_colors );
$values[] = "('".$player_id."','$color','".$player['player_canal']."','".addslashes( $player['player_name'] )."','".addslashes( $player['player_avatar'] )."',0)";
}
$sql .= implode( ',', $values );
$this->DbQuery( $sql );
$this->reattributeColorsBasedOnPreferences( $players, $gameinfos['player_colors'] );
$this->reloadPlayersBasicInfos();
/************ Start the game initialization *****/
// Init global values with their initial values
$this->setGameStateInitialValue("resolvingCard", -1);
$this->setGameStateInitialValue("inDeckSelection", 1);
$this->setGameStateInitialValue("isPostCleanUpPhase", 0);
$this->setGameStateInitialValue("opponent_id", -1);
$this->setGameStateInitialValue("card_id", -1);
$this->setGameStateInitialValue("changeActivePlayer_player_id", -1);
$this->setGameStateInitialValue("changeActivePlayer_state_id", -1);
$this->setGameStateInitialValue("player_id_1", -1);
$this->setGameStateInitialValue("player_id_2", -1);
$this->setGameStateInitialValue("player_id_3", -1);
$this->setGameStateInitialValue("player_id_4", -1);
$this->setGameStateInitialValue("hand_size_before", 0);
$this->setGameStateInitialValue("active_player_id", -1);
$this->setGameStateInitialValue("die_value", -1);
$this->setGameStateInitialValue("debugMode", 0);
$this->setGameStateInitialValue("animalfolk_id", 0);
// Init game statistics
$this->initStat("player", "number_of_turns", 0);
$this->initStat("player", "actions_purchase", 0);
$this->initStat("player", "actions_technique", 0);
$this->initStat("player", "actions_build", 0);
$this->initStat("player", "actions_inventory", 0);
$this->initStat("player", "cards_remaining", 0);
/************ 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();
$current_player_id = $this->getCurrentPlayerId();
//assert deck location prefixes are of length 4 (otherwise auto shuffling in the DaleOfMerchantsDeck will not work as intended)
if (strlen(MARKET) != 4 || strlen(DECK) != 4 || strlen(DISCARD) != 4 || strlen(HAND) != 4 || strlen(STALL) != 4 || strlen(JUNKRESERVE) != 4 || strlen(SCHEDULE) != 4 || strlen(LIMBO) != 4) {
throw new AssertionError("All location prefixes must be of length 4");
}
// Get information about players
// Note: you can retrieve some extra field you added for "player" table in "dbmodel.sql" if you need it.
$players = $this->loadPlayersBasicInfos();
$sql = "SELECT player_id id, player_score score FROM player ";
$result['players'] = $this->getCollectionFromDb( $sql );
//count the cards in each hand, but don't send the content (that information is hidden)
$result['handSizes'] = array();
foreach ( $players as $player_id => $player ) {
$result['handSizes'][$player_id] = $this->cards->countCardInLocation(HAND.$player_id);
}
//count the cards in each deck, but don't send the content (that information is hidden)
$result['deckSizes'] = array(
'market' => $this->cards->countCardInLocation(DECK.MARKET)
);
foreach ( $players as $player_id => $player ) {
$result['deckSizes'][$player_id] = $this->cards->countCardInLocation(DECK.$player_id);
}
//all discard piles are public information
$result['discardPiles'] = array(
'market' => $this->cards->getCardsInLocation(DISCARD.MARKET, null, 'location_arg')
);
foreach ( $players as $player_id => $player ) {
$result['discardPiles'][$player_id] = $this->cards->getCardsInLocation(DISCARD.$player_id, null, 'location_arg');
}
//get stalls
foreach ( $players as $player_id => $player ) {
$result['stalls'][$player_id] = $this->cards->getCardsInLocation(STALL.$player_id, null, 'location_arg');
}
//get schedules
foreach ( $players as $player_id => $player ) {
$result['schedules'][$player_id] = $this->cards->getCardsInLocation(SCHEDULE.$player_id, null, 'location_arg');
}
//other
$result['market'] = $this->cards->getCardsInLocation(MARKET);
$result['hand'] = $this->cards->getCardsInLocation(HAND.$current_player_id);
$result['limbo'] = $this->cards->getCardsInLocation(LIMBO.$current_player_id);
$result['cardTypes'] = $this->card_types;
$result['effects'] = $this->effects->loadFromDb();
$result['inDeckSelection'] = $this->getGameStateValue("inDeckSelection") == '1';
$result['animalfolkIds'] = $result['inDeckSelection'] ? array() : $this->deckSelection->getAnimalfolkIds();
$result['debugMode'] = $this->getGameStateValue("debugMode") == '1';
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()
{
$highest_stack_index = 0;
$players = $this->loadPlayersBasicInfos();
foreach ( $players as $player_id => $player ) {
$next_stack_index = $this->cards->getNextStackIndex($player_id);
$highest_stack_index = max($highest_stack_index, $next_stack_index);
}
return 100 * $highest_stack_index / MAX_STACKS;
}
//////////////////////////////////////////////////////////////////////////////
//////////// Utility functions
////////////
/**
* set the `"player_id_1"`, `"player_id_2"`, `"player_id_3"` and `"player_id_4"` game state labels
*/
function setGameStateValuePlayerIds($player_ids) {
if (count($player_ids) > 4) {
throw new BgaVisibleSystemException("Only a maximum of 4 player ids can be stored");
}
$index = 1;
foreach ($player_ids as $player_id) {
$this->setGameStateValue("player_id_".$index, $player_id);
$index += 1;
}
while ($index <= 4) {
$this->setGameStateValue("player_id_".$index, -1);
$index += 1;
}
}
/**
* get the $player_ids set via `setGameStateValuePlayerIds`
* @return array `$player_ids`
*/
function getGameStateValuePlayerIds() {
$player_ids = [];
$index = 1;
while ($index <= 4) {
$player_id = $this->getGameStateValue("player_id_".$index);
if ($player_id == -1) {
break;
}
$player_ids[] = intval($player_id);
$index += 1;
}
return $player_ids;
}
/**
* transition to the next state if all players are deactive, and make the player stored in `"changeActivePlayer_player_id"` the active player.
* IMPORTANT: `"changeActivePlayer_player_id"` should have been set before entering the multipleactiveplayer state
*/
function nextStateChangeActivePlayerFromMultiActive(string $transition, int $player_id) {
if (!array_key_exists($transition, $this->gamestate->states[29]['transitions'])) {
throw new BgaVisibleSystemException("'$transition' is not a valid transition in 'changeActivePlayer'");
}
$state_id = $this->gamestate->states[29]['transitions'][$transition];
//moving `active_player_id` => `changeActivePlayer_player_id` is redundant and performance-wise inefficient
//however, from an architectural POV it is better to let "changeActivePlayer_player_id" be a protected single-purpose game state label
$active_player_id = $this->getGameStateValue("active_player_id");
if ($active_player_id == -1) {
throw new BgaVisibleSystemException("Attempted to call 'nextStateChangeActivePlayerFromMultiActive' without setting 'active_player_id'");
}
$this->setGameStateValue("changeActivePlayer_player_id", $active_player_id);
$this->setGameStateValue("changeActivePlayer_state_id", $state_id);
$this->gamestate->setPlayerNonMultiactive($player_id, "trChangeActivePlayer");
}
/**
* transition to the next state and make `$player_id` the active player
*/
function nextStateChangeActivePlayer(string $transition, int $player_id) {
if (!array_key_exists($transition, $this->gamestate->states[29]['transitions'])) {
throw new BgaVisibleSystemException("'$transition' is not a valid transition in 'changeActivePlayer'");
}
$state_id = $this->gamestate->states[29]['transitions'][$transition];
$this->setGameStateValue("changeActivePlayer_player_id", $player_id);
$this->setGameStateValue("changeActivePlayer_state_id", $state_id);
$this->gamestate->nextState("trChangeActivePlayer");
}
/**
* Delay client notifications by 500ms multiple times
*/
function delay500ms(int $times = 1) {
for ($i = 0; $i < $times; $i++) {
$this->notifyAllPlayers('delay', '', array());
}
}
/**
* Return the unique `opponent_id`. (only available in a 2-player game)
*/
function getUniqueOpponentId() {
$player_id = $this->getActivePlayerId();
$players = $this->loadPlayersBasicInfos();
if (count($players) != 2) {
throw new BgaVisibleSystemException("getUniqueOpponentId is not defined for non-2-player games");
}
foreach ($players as $opponent_id => $opponent) {
if ($opponent_id != $player_id) {
return $opponent_id;
}
}
}
/**
* @param string $AT_numberlist
* @return array
* @example example
* numberListToArray("1;2;3;4;") = array(1, 2, 3, 4)
* @example example
* numberListToArray("1;2;3;4") = array(1, 2, 3, 4)
*/
function numberListToArray(string $AT_numberlist){
if( $AT_numberlist == '' )
return array();
if( substr( $AT_numberlist, -1 ) == ';' )
$AT_numberlist = substr( $AT_numberlist, 0, -1 );
return explode(';', $AT_numberlist);
}
/**
* Concatenate any number of $AT_numberlists and convert them to an array
* @param string[] ...$AT_numberlist
*/
function numberListsToArray(string ...$AT_numberlists) {
if (count($AT_numberlists) === 1) {
return $this->numberListToArray($AT_numberlists[0]);
}
$arrays = array();
foreach ($AT_numberlists as $AT_numberlist) {
$arrays[] = $this->numberListToArray($AT_numberlist);
}
return array_merge(...$arrays);
}
/**
* $a <= $b
* @param array $a array with unique values
* @param array $b array with unique values
* @return `true` if $a is a subset of $b
*/
function isSubset(array $a, array $b) {
return empty(array_diff($a, $b));
}
/**
* Converts an array of dbcards to an array of card ids
* @param array $dbcards `$dbcards`
* @return array `$card_ids`
*/
function toCardIds(array $dbcards){
$card_ids = array();
foreach ($dbcards as $card) {
$card_ids[] = $card["id"];
}
return $card_ids;
}
/**
* Updates the score for the specified player
* @param mixed $player_id player to modify the score of
* @param int $new_score new score to assign to this player
* @return bool `true` if the player is now the WINNER of the game
*/
function updateScore(mixed $player_id, int $new_score) {
$sql = "UPDATE player SET player_score=$new_score WHERE player_id='$player_id'";
$this->DbQuery($sql);
return $new_score >= MAX_STACKS;
}
/**
* Send a notification to all players, but only player args["player_id"] and args["opponent_id"] receive args["_private"]
* @param string $type
* @param string $message
* @param array $args requires at least "player_id" and "_private" keys
* @param string $private_message (optional) by default, send the public message - if provided, send a special private message
*/
function notifyAllPlayersWithPrivateArguments(string $type, string $message, array $args, string $private_message = null) {
$suffix = ""; //clienttranslate(" (private information)");
//the active player receives the notification with the private arguments
$private_player_id = $args["player_id"];
$this->notifyPlayer($private_player_id, $type, $private_message ? $private_message.$suffix : $message, array_merge($args, $args["_private"]));
//(optional) the involved opponent also receives the notification with the private arguments
$private_opponent_id = isset($args["opponent_id"]) ? $args["opponent_id"] : null;
if ($private_opponent_id) {
$private_opponent_id = $args["opponent_id"];
$this->notifyPlayer($private_opponent_id, $type, $private_message ? $private_message.$suffix : $message, array_merge($args, $args["_private"]));
}
//all players receive the notification without the private arguments. (the player and opponent will ignore this on the client-side)
unset($args["_private"]);
$this->notifyAllPlayers($type, $message, $args);
}
/**
* Place multiple cards on top of a player's deck
* @param int $deck_player_id player that will receive the cards on top of the deck or `MARKET`
* @param string $msg notification message for all players
* @param array $card_ids cards_ids to be placed on top in that order
* @param array $cards array with exactly the same keys as $card_ids
* @param array $unordered_cards (optional) - if provided, first place this collection of unordered cards on top of the deck
* @param bool $from_limbo (optional) - default false. If `false`, place from hand. If `true`, place from limbo.
*/
function placeOnDeckMultiple(mixed $deck_player_id, string $msg, array $card_ids, array $cards, array $unordered_cards = null, bool $from_limbo = false, $msg_args = array()) {
//1: move the unordered cards on top of the deck (no message)
$nbr_unordered_cards = 0;
if ($unordered_cards) {
$nbr_unordered_cards = count($unordered_cards);
$unordered_card_ids = array_keys($unordered_cards);
$this->cards->moveCardsOnTop($unordered_card_ids, DECK.$deck_player_id);
$this->notifyAllPlayersWithPrivateArguments('placeOnDeckMultiple', '', array_merge( array (
'player_id' => $this->getActivePlayerId(),
'player_name' => $this->getActivePlayerName(),
"_private" => array(
'card_ids' => $unordered_card_ids,
'cards' => $unordered_cards,
),
'deck_player_id' => $deck_player_id,
'nbr' => $nbr_unordered_cards,
'from_limbo' => $from_limbo
), $msg_args));
}
//2: move the ordered cards on top of the deck
if ($cards) {
$this->cards->moveCardsOnTop($card_ids, DECK.$deck_player_id);
$this->notifyAllPlayersWithPrivateArguments('placeOnDeckMultiple', '', array (
'player_id' => $this->getActivePlayerId(),
'player_name' => $this->getActivePlayerName(),
"_private" => array(
'card_ids' => $card_ids,
'cards' => $cards,
),
'deck_player_id' => $deck_player_id,
'nbr' => count($cards),
'from_limbo' => $from_limbo
));
}
//only send a single message to the players
$this->notifyAllPlayers('message', $msg, array (
'player_id' => $this->getActivePlayerId(),
'player_name' => $this->getActivePlayerName(),
'opponent_name' => $this->getPlayerNameById($deck_player_id),
'nbr' => count($cards) + $nbr_unordered_cards,
));
}
/**
* Draw $nbr cards from your deck and place them into your hand.
* @param string $msg notification message for all players
* @param int $nbr (optional) default 1. number of cards to draw from the specified player's draw pile
* @param bool $to_limbo (optional) default false. If true, the cards are placed in limbo. If false, the cards are placed in hand.
* @param string $from_player_id (optional) default active player. If provided, draw cards from this player's deck instead. May also be MARKET.
* @param string $to_player_id (optional) default active player. If provided, draw cards for this player instead.
* @param string $private_message (optional) by default, send the public message - if provided, send a special private message
* @return int how much cards were actually drawn (`<= $nbr`)
*/
function draw(string $msg, int $nbr = 1, bool $to_limbo = false, string $from_player_id = null, string $to_player_id = null, string $private_message = null) {
if ($from_player_id == null) {
$from_player_id = $this->getActivePlayerId();
}
if ($to_player_id == null) {
$to_player_id = $this->getActivePlayerId();
}
$to_location = $to_limbo ? LIMBO.$to_player_id : HAND.$to_player_id;
if ($nbr == 1) {
$card = $this->cards->pickCardForLocation(DECK.$from_player_id, $to_location);
if ($card) {
$this->notifyAllPlayersWithPrivateArguments('draw', $msg, array(
"player_id" => $to_player_id,
"player_name" => $this->getPlayerNameById($to_player_id),
"opponent_name" => ($from_player_id == MARKET) ? MARKET : $this->getPlayerNameById($from_player_id),
"nbr" => 1,
"_private" => array(
"card" => $card,
"card_name" => $this->getCardName($card)
),
"deck_player_id" => $from_player_id,
"to_limbo" => $to_limbo
), $private_message);
return 1;
}
return 0;
}
else {
$cards = $this->cards->pickCardsForLocation($nbr, DECK.$from_player_id, $to_location);
$actual_nbr = count($cards);
if ($actual_nbr > 0)
$this->notifyAllPlayersWithPrivateArguments('drawMultiple', $msg, array(
"player_id" => $to_player_id,
"player_name" => $this->getPlayerNameById($to_player_id),
"opponent_name" => ($from_player_id == MARKET) ? MARKET : $this->getPlayerNameById($from_player_id),
"nbr" => $actual_nbr,
"_private" => array(
"cards" => $cards
),
"deck_player_id" => $from_player_id,
"to_limbo" => $to_limbo
), $private_message);
return $actual_nbr;
}
}
/**
* Draw a specific card from your deck and place it into your hand. Then, shuffle the deck
* @param string $msg notification message for all players
* @param int $card_id the card to draw from the specified player's draw pile
* @param bool $to_limbo (optional) default false. If true, the cards are placed in limbo. If false, the cards are placed in hand.
* @param string $from_player_id (optional) default active player. If provided, draw cards from this player's deck instead. May also be MARKET.
* @param string $to_player_id (optional) default active player. If provided, draw cards for this player instead.
*/
function drawCardId(string $msg, int $card_id, bool $to_limbo = false, string $from_player_id = null, string $to_player_id = null) {
if ($from_player_id == null) {
$from_player_id = $this->getActivePlayerId();
}
if ($to_player_id == null) {
$to_player_id = $this->getActivePlayerId();
}
$card = $this->cards->getCardFromLocation($card_id, DECK.$from_player_id);
$this->cards->moveCard($card_id, HAND.$to_player_id);
$this->notifyAllPlayersWithPrivateArguments('draw', $msg, array(
"player_id" => $to_player_id,
"player_name" => $this->getPlayerNameById($to_player_id),
"opponent_name" => ($from_player_id == MARKET) ? MARKET : $this->getPlayerNameById($from_player_id),
"nbr" => 1,
"_private" => array(
"card" => $card
),
"deck_player_id" => $from_player_id,
"to_limbo" => $to_limbo
));
$this->cards->shuffle(DECK.$from_player_id);
}
/**
* Ditch a single specified card from the hand of the active player
* @param string $msg notification message for all players
* @param array $dbcard card that needs to be ditched
* @param bool $from_limbo (optional) - default false. If `false`, ditch from hand. If `true`, ditch from limbo.
* @param array $msg_args (optional) - additional args to display in the `$msg`
*/
function ditch(string $msg, array $dbcard, bool $from_limbo = false, $msg_args = array()) {
$player_id = $this->getActivePlayerId();
$destination = $this->isJunk($dbcard) ? JUNKRESERVE : DISCARD.MARKET;
$this->cards->moveCardOnTop($dbcard["id"], $destination);
$this->notifyAllPlayers('ditch', $msg, array_merge(array(
"player_id" => $player_id,
"player_name" => $this->getPlayerNameById($player_id),
"card_name" => $this->getCardName($dbcard),
"card" => $dbcard,
"from_limbo" => $from_limbo
), $msg_args));
}
/**
* Ditch multiple cards from the hand of the active player (only sends 1 notification for all ditched cards)
* @param string $msg notification message for all players
* @param array $dbcards cards that need to be ditched
* @param bool $from_limbo (optional) - default false. If `false`, ditch from hand. If `true`, ditch from limbo.
* @param array $ordered_card_ids (optional) - if provided, ditch these cards last
*/
function ditchMultiple(string $msg, array $dbcards, bool $from_limbo = false, mixed $ordered_card_ids = array()) {
$player_id = $this->getActivePlayerId();
$this->notifyAllPlayers('message', $msg, array (
'player_id' => $player_id,
'player_name' => $this->getActivePlayerName(),
'nbr' => count($dbcards)
));
//ditch the unordered cards
foreach ($dbcards as $dbcard) {
if (!in_array($dbcard["id"], $ordered_card_ids)) {
$this->ditch('', $dbcard, $from_limbo);
}
}
//ditch the ordered cards
foreach ($ordered_card_ids as $ordered_card_id) {
foreach ($dbcards as $dbcard) {
if ($dbcard["id"] == $ordered_card_id) {
$this->ditch('', $dbcard, $from_limbo);
break;
}
}
}
}
/**
* The active player ditches a card from their discard pile and notifies all players
* @param string $msg notification message
* @param int $card_id the id of a card in the discard pile to ditch
* @param array $msg_args
*/
function ditchFromDiscard(string $msg, int $card_id, array $msg_args = array()) {
//1. remove the card from the discard pile
$player_id = $this->getActivePlayerId();
$dbcards = $this->cards->removeCardsFromPile(array($card_id), DISCARD.$player_id);
if (count($dbcards) != 1) {
throw new BgaVisibleSystemException("'ditchFromDiscard' could not find unique card_id $card_id");
}
$dbcard = current($dbcards);
//2. ditch it
$destination = $this->isJunk($dbcard) ? JUNKRESERVE : DISCARD.MARKET;
$this->cards->moveCardOnTop($dbcard["id"], $destination);
$this->notifyAllPlayers('ditchFromDiscard', $msg, array_merge(array(
"player_id" => $player_id,
"player_name" => $this->getPlayerNameById($player_id),
"card_name" => $this->getCardName($dbcard),
"card" => $dbcard
), $msg_args));
}
/**
* The active player ditches a card from their deck and notifies all players
* @param string $msg notification message
* @param int $card_id the id of a card in the deck pile to ditch
* @param array $msg_args
*/
function ditchFromDeck(string $msg, int $card_id, array $msg_args = array()) {
//1. remove the card from the deck
$player_id = $this->getActivePlayerId();
$dbcard = $this->cards->getCardFromLocation($card_id, DECK.$player_id);
//2. ditch it
$destination = $this->isJunk($dbcard) ? JUNKRESERVE : DISCARD.MARKET;
$this->cards->moveCardOnTop($dbcard["id"], $destination);
$this->notifyAllPlayers('ditchFromDeck', $msg, array_merge(array(
"player_id" => $player_id,
"player_name" => $this->getPlayerNameById($player_id),
"card_name" => $this->getCardName($dbcard),
"card" => $dbcard
), $msg_args));
}
/**
* Discard multiple cards for a player in the given order
* @param string $msg notification message for all players
* @param int $player_id player that will discard the cards from hand
* @param array $card_ids cards_ids to be discarded in that order
* @param array $cards array with exactly the same keys as $card_ids
* @param array $unordered_cards (optional) - if provided, first discard this collection of unordered cards
* @param bool $from_limbo (optional) - default false. If `false`, discard from hand. If `true`, discard from limbo.
*/
function discardMultiple(string $msg, int $player_id, array $card_ids, array $cards, array $unordered_cards = null, bool $from_limbo = false) {
//1: move the unordered cards to the discard pile (no message)
$nbr_unordered_cards = 0;
if ($unordered_cards) {
$nbr_unordered_cards = count($unordered_cards);
$unordered_card_ids = array_keys($unordered_cards);
$this->cards->moveCardsOnTop($unordered_card_ids, DISCARD.$player_id);
$this->notifyAllPlayers('discardMultiple', '', array (
'player_id' => $player_id,
'player_name' => $this->getActivePlayerName(),
'card_ids' => $unordered_card_ids,
'cards' => $unordered_cards,
'nbr' => $nbr_unordered_cards,
'from_limbo' => $from_limbo
));
}
//2: move the ordered cards to the discard pile
if ($cards) {
$this->cards->moveCardsOnTop($card_ids, DISCARD.$player_id);
$this->notifyAllPlayers('discardMultiple', '', array (
'player_id' => $player_id,
'player_name' => $this->getActivePlayerName(),
'card_ids' => $card_ids,
'cards' => $cards,
'nbr' => count($cards),
'from_limbo' => $from_limbo
));
}
//only leave a single log message to the players
$this->notifyAllPlayers('message', $msg, array (
'player_id' => $player_id,
'player_name' => $this->getActivePlayerName(),
'nbr' => count($cards) + $nbr_unordered_cards,
));
}
/**
* The active player ditches a card from the market deck and notifies all players
* @param string $msg notification message
*/
function ditchFromMarketDeck(string $msg) {
$dbcard = $this->cards->pickCardForLocation(DECK.MARKET, 'unstable');
if ($dbcard) {
$this->cards->moveCardOnTop($dbcard["id"], DISCARD.MARKET);
$this->notifyAllPlayers('ditchFromMarketDeck', $msg, array (
'player_name' => $this->getActivePlayerName(),
'card' => $dbcard,
'card_name' => $this->getCardName($dbcard)
));
}
return $dbcard;
}
/**
* The active player ditches multiple cards from the market board
* @param string $msg notification message for all players
* @param array $card_ids cards_ids to be thrown away in that order
* @param array $cards array with exactly the same keys as $card_ids
* @param array $unordered_cards (optional) - if provided, first ditch these cards
*/
function ditchFromMarketBoard(string $msg, array $card_ids, array $cards, array $unordered_cards = null) {
//1: move the unordered cards to the market discard pile (no message)
$nbr_unordered_cards = 0;
if ($unordered_cards) {
$nbr_unordered_cards = count($unordered_cards);
$unordered_card_ids = array_keys($unordered_cards);
$this->cards->moveCardsOnTop($unordered_card_ids, DISCARD.MARKET);
$this->notifyAllPlayers('ditchFromMarketBoard', $cards ? '' : $msg, array (
'player_name' => $this->getActivePlayerName(),
'card_ids' => $unordered_card_ids,
'cards' => $unordered_cards,
'nbr' => $nbr_unordered_cards,
));
}
//2: move the ordered cards to the market discard pile
if ($cards) {
$this->cards->moveCardsOnTop($card_ids, DISCARD.MARKET);
$this->notifyAllPlayers('ditchFromMarketBoard', $msg, array (
'player_name' => $this->getActivePlayerName(),
'card_ids' => $card_ids,
'cards' => $cards,
'nbr' => count($cards) + $nbr_unordered_cards,
));
}
}
/**
* Callback method for when cards need to be drawn from a location, but the location is empty.
* This method is expected to increase in number of cards at the specified location.
* @param string location location in the deck that needs to be supplied with cards.
*/
function onLocationExhausted($location) {
$prefix = substr($location, 0, 4);
if ($prefix == DECK) {
$player_id = substr($location, 4);
$discard_pile = DISCARD.$player_id;
$nbr = $this->cards->countCardsInLocation($discard_pile);
if ($nbr > 0) {
$this->cards->expireChameleonTargetInDiscard($discard_pile);
$this->cards->moveAllCardsInLocation($discard_pile, $location);
$this->cards->shuffle($location);
if ($player_id == MARKET) {
$this->notifyAllPlayers('reshuffleDeck', clienttranslate('Shuffling the market discard pile to form a new market deck'), array(
"market" => true
));
}
else {
$this->notifyAllPlayers('reshuffleDeck', clienttranslate('${player_name} shuffles their discard pile to form a new deck'), array(
"market" => false,
"player_id" => $player_id,
"player_name" => $this->getPlayerNameById($player_id)
));
}
}
}
}
/**
* @param mixed $player_id
* @param array $hand_cards array of dbcards that are currently in the player's hand
* @param array $hand_cards array of dbcards that are currently in the player's stall
* @return int maximum hand size
*/
function getMaximumHandSize($player_id, array $hand_cards, array $stall_cards): int {
$bribes = $this->effects->countGlobalEffects(CT_BRIBE);
$cookies = $this->countTypeId($hand_cards, CT_COOKIES);
$sofas = $this->countTypeId($stall_cards, CT_SOFA);
return 5 + $bribes + $cookies + $sofas;
}
/**
* Refills your hand to the maximum hand size.
* @param bool $isPostCleanUpPhase indicates if this function is called an additional time (after postCleanUpPhase)
* @return `true` if any cards were drawn
*/
function refillHand($isPostCleanUpPhase) {
//get information about the hand
$player_id = $this->getActivePlayerId();
$hand_cards = $this->cards->getCardsInLocation(HAND.$player_id);
$stall_cards = $this->cards->getCardsInLocation(STALL.$player_id);
$maximum_hand_size = $this->getMaximumHandSize($player_id, $hand_cards, $stall_cards);
$new_hand_cards = array();
$hand_size_before = count($hand_cards);
//get hand_size_before for CT_LOSTSHIPMENTS
$lostShipmentsActive = $this->effects->countGlobalEffectsExcludeArg(CT_LOSTSHIPMENTS, $player_id);
if ($lostShipmentsActive) {
if ($isPostCleanUpPhase) {
$hand_size_before = $this->getGameStateValue("hand_size_before");
}
else {
$this->setGameStateValue("hand_size_before", $hand_size_before);
}
}
//draw cards
while (true) {
//draw cards from deck
$nbr = $maximum_hand_size - count($hand_cards);
if ($lostShipmentsActive) {
$nbr = min(1 + $hand_size_before - count($hand_cards), $nbr);
}
if ($nbr > 0) {
$cards = $this->cards->pickCardsForLocation($nbr, DECK.$player_id, HAND.$player_id);
$new_hand_cards = array_merge($new_hand_cards, $cards);
$hand_cards = array_merge($hand_cards, $cards);
}
//autobind chameleons to cookies
foreach ($hand_cards as $hand_card) {
$type_id = $this->getTypeId($hand_card);
if ($this->isChameleonTypeId($type_id)) {
$target_ids = $this->getChameleonTargets($hand_card["id"], $type_id);
$targets = $this->cards->getCards($target_ids);
$onlyCookies = count($targets) > 0;
foreach ($targets as $target) {
if ($this->getTypeId($target) != CT_COOKIES) {
$onlyCookies = false;
break;
}
}
if ($onlyCookies) {
if ($this->getTypeId(current($targets)) == CT_COOKIES) {
$this->effects->insertModification($hand_card["id"], $type_id, CT_COOKIES, current($target_ids));
$this->notifyAllPlayers('message', clienttranslate('${player_name}\'s ${chameleon_card_name} automatically copies Cookies'), array(
'chameleon_card_name' => $this->card_types[$type_id]['name'],
'player_name' => $this->getActivePlayerName()
));
}
}
}
}
//recompute the maximum hand size
$new_maximum_hand_size = $this->getMaximumHandSize($player_id, $hand_cards, $stall_cards);
if ($maximum_hand_size == $new_maximum_hand_size) {
break;
}
$maximum_hand_size = $new_maximum_hand_size;
//exit the loop for CT_LOSTSHIPMENTS
if ($lostShipmentsActive) {
if ($nbr > 1 || count($hand_cards) - $hand_size_before > 1) {
throw new BgaVisibleSystemException("Lost Shipments: the player drew more than 1 card");
}
if ($nbr == 1) {
break;
}
}
}
//notify players about CT_COOKIES
$number_of_cookies = $this->countTypeId($hand_cards, CT_COOKIES);
if ($number_of_cookies > 0) {
$this->notifyAllPlayers('message', clienttranslate('Cookies: ${player_name} increases their hand size by ${nbr}'), array(
"player_name" => $this->getPlayerNameById($player_id),
"nbr" => $number_of_cookies
));
}
//notify about the new cards from deck
if (count($new_hand_cards) > 0) {
$this->notifyAllPlayersWithPrivateArguments('drawMultiple', clienttranslate('${player_name} draws ${nbr} card(s) to refill their hand'), array(
"player_id" => $player_id,
"player_name" => $this->getPlayerNameById($player_id),
"nbr" => count($new_hand_cards),
"_private" => array(
"cards" => $new_hand_cards
)
));
}
//notify players about CT_LOSTSHIPMENTS (and reduce the maximum hand size to prevent drawing junk from the reserve)
if ($lostShipmentsActive) {
if ($maximum_hand_size > $hand_size_before + 1) {
$maximum_hand_size = $hand_size_before + 1;
if ($nbr == 1) {
$this->notifyAllPlayers('message', clienttranslate('Lost Shipments: ${player_name} cannot draw more than 1 card'), array(
"player_name" => $this->getPlayerNameById($player_id),
));
}
}
}
//draw missing cards from junk reserve
$nbr_junk_cards = $maximum_hand_size - count($hand_cards);
if ($nbr_junk_cards > 0) {
$junk_cards = $this->cards->getJunk($nbr_junk_cards);
$junk_ids = array_keys($junk_cards);
$this->cards->moveCards($junk_ids, HAND.$player_id);
$this->notifyAllPlayers('obtainNewJunkInHand', clienttranslate('${player_name} ran out of cards and receives ${nbr} junk cards'), array(
"player_id" => $player_id,
"player_name" => $this->getPlayerNameById($player_id),
"cards" => $junk_cards,
"nbr" => $nbr_junk_cards,
));
}
//return true if any cards were drawn
return ($hand_size_before < $maximum_hand_size);
}
/**
* Fills all the empty slots in the market
* @param bool $move if true, first move all cards to the right, then refillMarket
*/
function refillMarket(bool $move){
$cards = $this->cards->getCardsInLocation(MARKET, null, 'location_arg');
if (count($cards) > 5) {
throw new BgaVisibleSystemException("The market has more than 5 cards");
}
$free_slots = array();
$first_free_slot = 0;
if ($move) {
//move all cards to the right
$shouldNotifyMarketSlideRight = false;
foreach ($cards as $card) {
if ($card["location_arg"] != $first_free_slot) {
$this->cards->moveCardWithinLocation($card['id'], MARKET, $first_free_slot);
$shouldNotifyMarketSlideRight = true;
}
$first_free_slot++;
}
if ($shouldNotifyMarketSlideRight) {
$this->notifyAllPlayers('marketSlideRight', clienttranslate('Cards in the market move to the right'), array());
}
}
else {
//store gaps in $free_slots
foreach ($cards as $card) {
if ($card['location_arg'] >= 5) {
throw new BgaVisibleSystemException("Some card in the market is at an illegal position");
}
while($first_free_slot < $card['location_arg']) {
$free_slots[] = $first_free_slot;
$first_free_slot++;
}
$first_free_slot++;
}
}
//store leading $free_slots
for($i = $first_free_slot; $i < 5; $i++) {
$free_slots[] = $i;
}
//put a card in each free slot (1 by 1, because (a) the location arg matters; and (b) picking cards must trigger reshuffle;)
$new_cards = [];
foreach ($free_slots as $index => $location_arg) {
$card = $this->cards->pickCardForLocation(DECK.MARKET, MARKET, $location_arg);
if ($card === null) {
//unfortunately, not all free slots can be filled
$free_slots = array_slice($free_slots, 0, $index);
break;
} else {
$new_cards[] = $card;
}
}
if (count($new_cards) > 0) {
$this->notifyAllPlayers('fillEmptyMarketSlots', clienttranslate('Empty market slots get filled'), array(
"positions" => $free_slots,
"cards" => $new_cards
));
}
}
/**
* @return bool indicating if the given `type_id` is one of the 5 chameleon type ids
*/
function isChameleonTypeId(int $type_id) {
return (
$type_id == CT_FLEXIBLESHOPKEEPER ||
$type_id == CT_REFLECTION ||
$type_id == CT_GOODOLDTIMES ||
$type_id == CT_TRENDSETTING ||
$type_id == CT_SEEINGDOUBLES
);