-
Notifications
You must be signed in to change notification settings - Fork 1
/
fleet.game.php
2474 lines (2179 loc) · 94.6 KB
/
fleet.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>
* Fleet implementation : © Dan Marcus <bga.marcuda@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.
* -----
*
* fleet.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' );
class fleet 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(
'fish_cubes' => 10, // number fish cubes left in game
'auction_card' => 11, // card_id of currently selected card in auction
'current_phase' => 12, // current phase number, always increasing
'first_player' => 13, // player_id of current first player
'final_round' => 14, // flag for final round being triggered
'auction_winner' => 15, // player_id of player the won current aution
'current_player_launches' => 16, // number of boats launch by current player this phase to track cod bonus
'current_player_hires' => 17, // number of captains hired by current player this phase to track lobster bonus
// Game options
'gone_fishing' => 100,
'fast_passing' => 101,
'simultaneous_launch_hire' => 102
) );
// Deck compontent for all cards
$this->cards = self::getNew("module.common.deck");
$this->cards->init("card");
// Game phases for determining next player logic and states
// N.B. Phase Two is split into two phases here
$this->phases = array(
PHASE_AUCTION,
PHASE_LAUNCH,
PHASE_HIRE,
PHASE_FISHING,
PHASE_PROCESSING,
PHASE_TRADING,
PHASE_DRAW
);
$this->nbr_phases = count($this->phases);
}
protected function getGameName( )
{
// Used for translations and stuff. Please do not modify.
return "fleet";
}
/*
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 = self::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) 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'] )."')";
}
$sql .= implode( $values, ',' );
self::DbQuery( $sql );
self::reattributeColorsBasedOnPreferences( $players, $gameinfos['player_colors'] );
self::reloadPlayersBasicInfos();
/************ Start the game initialization *****/
// Init global values with their initial values
self::setGameStateInitialValue('auction_card', 0);
self::setGameStateInitialValue('auction_winner', 0);
self::setGameStateInitialValue("current_phase", 0);
self::setGameStateInitialValue("final_round", 0);
self::setGameStateInitialValue("current_player_launches", 0);
self::setGameStateInitialValue("current_player_hires", 0);
// Init game statistics
self::initStat('table', 'rounds_number', 1); // count first round
self::initStat('player', 'vp_total', 0);
self::initStat('player', 'vp_boats', 0);
self::initStat('player', 'vp_licenses', 0);
self::initStat('player', 'vp_fish', 0);
self::initStat('player', 'vp_bonus', 0);
self::initStat('player', 'auctions_passed', 0);
self::initStat('player', 'licenses_bought', 0);
self::initStat('player', 'boats_launched', 0);
self::initStat('player', 'captains_hired', 0);
self::initStat('player', 'fish_gained', 0);
self::initStat('player', 'fish_processed', 0);
self::initStat('player', 'fish_traded', 0);
self::initStat('player', 'cards_drawn', 0);
self::initStat('player', 'overpaid', 0);
// Card decks
$cards = array();
foreach ($this->card_types as $idx => $card) {
$cards[] = array(
'type' => $card['type'],
'type_arg' => $idx,
'nbr' => $card['nbr']
);
}
$this->cards->createCards($cards);
// Gone Fishin' bonus cards
if ($this->optGoneFishing()) {
$loc = 'gonefishing';
} else {
$loc = 'box';
}
$cards = $this->cards->getCardsOfType(CARD_BONUS);
$this->cards->moveCards(array_column($cards, 'id'), $loc);
// Separate licenses from boats
$licenses = $this->cards->getCardsOfType(CARD_LICENSE);
$this->cards->moveCards(array_column($licenses, 'id'), 'licenses');
// Setup license deck
// Separate all premium and 8-10 common licenses
$nbr_players = count($players);
foreach ($this->premium_license_types as $type_arg) {
$cards = $this->cards->getCardsOfType(CARD_LICENSE, $type_arg);
$this->cards->moveCards(array_column($cards, 'id'), 'setup_premium');
}
$nbr_common = $nbr_players == 2 ? 10 : 8;
$this->cards->pickCardsForLocation($nbr_common, 'licenses', 'setup_common');
$this->cards->shuffle('setup_premium');
$this->cards->shuffle('setup_common');
// Remove some licenses from game based on number of players
if ($nbr_players == 2) {
$this->cards->pickCardsForLocation(3, 'setup_premium', 'box');
$this->cards->pickCardsForLocation(6, 'setup_common', 'box');
} else if ($nbr_players == 3) {
$this->cards->pickCardsForLocation(2, 'setup_premium', 'box');
$this->cards->pickCardsForLocation(2, 'setup_common', 'box');
}
// Shuffle premium back into deck and put common on top
$this->cards->moveAllCardsInLocation('setup_premium', 'licenses');
$this->cards->shuffle('licenses');
foreach ($this->cards->getCardsInLocation('setup_common') as $card) {
$this->cards->insertCardOnExtremePosition($card['id'], 'licenses', true);
}
// Draw initial licenses for auction
$this->cards->pickCardsForLocation($nbr_players, 'licenses', 'auction');
// Give each player one of each boat
foreach ($this->boat_types as $type_arg) {
$cards = $this->cards->getCardsOfType(CARD_BOAT, $type_arg);
foreach ($players as $player_id => $player) {
$this->cards->moveCard(array_shift($cards)['id'], 'hand', $player_id);
}
}
// Shuffle boat deck and auto shuffle discard pile as needed
$this->cards->shuffle('deck');
//$this->cards->autoreshuffle = true; //XXX not working?! see drawCards
// 25 fish crates in the game for each player
self::setGameStateInitialValue("fish_cubes", $nbr_players * 25);
// Activate first player (which is in general a good idea :) )
$player_id = $this->activeNextPlayer();
self::setGameStateInitialValue('first_player', $player_id);
/************ 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 = self::getCurrentPlayerId(); // !! We must only return informations visible by this player !!
// 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, auction_bid bid, auction_pass pass, passed done FROM player ";
$result['players'] = self::getCollectionFromDb( $sql );
$result['first_player'] = self::getGameStateValue('first_player');
// Get player cards and fish on table
$players = self::loadPlayersBasicInfos();
$boats = array();
$licenses = array();
$fish = array();
$hands = array();
foreach ($players as $player_id => $player) {
$boats[$player_id] = $this->getBoats($player_id);
$licenses[$player_id] = $this->getLicenses($player_id);
$fish[$player_id] = $this->getFishCrates($player_id);
$hands[$player_id] = count($this->cards->getPlayerHand($player_id)) + $this->cards->countCardInLocation('draw', $player_id);
}
$result['boats'] = $boats;
$result['licenses'] = $licenses;
$result['processed_fish'] = $fish;
$result['hand_cards'] = $hands;
$result['draw'] = $this->cards->getCardsInLocation('draw', $current_player_id);
// Private info - player's hand cards and total coins, possible moves if active
$result['hand'] = $this->cards->getPlayerHand($current_player_id);
$result['coins'] = $this->getCoins($current_player_id);
$state = $this->gamestate->state();
$result['moves'] = $this->possibleMoves($current_player_id, $state['name']); // may be launch_hire
// Each Shrimp License reduces the cost by one
$result['discount'] = count($this->getLicenses($current_player_id, LICENSE_SHRIMP));
// Card counds
$result['cards'] = $this->cards->countCardsInLocations();
// Auction cards and status
$result['auction'] = $this->cards->getCardsInLocation('auction');
$result['auction_card'] = self::getGameStateValue('auction_card');
$result['auction_winner'] = self::getGameStateValue('auction_winner');
$result['auction_bid'] = $this->getHighBid();
// Remaining fish cubes
$result['fish_cubes'] = self::getGameStateValue('fish_cubes');
// Constants
$result['card_infos'] = $this->card_types;
$result['constants'] = array(
'shrimp' => LICENSE_SHRIMP,
'tuna' => LICENSE_TUNA,
);
// Game options
$result['gone_fishing'] = $this->optGoneFishing();
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()
{
// Game ends when licenses or fish crates run out
// Set progression to precent missing of whichever has fewer
// Total number of resources based on number players
$nbr_players = self::getPlayersNumber();
if ($nbr_players == 2) {
$nbr_lic = 17;
$nbr_fish = 50;
} else if ($nbr_players == 3) {
$nbr_lic = 17;
$nbr_fish = 75;
} else {
$nbr_lic = 26;
$nbr_fish = 100;
}
// License progression
$lic_prog = $this->cards->countCardInLocation('licenses') + $this->cards->countCardInLocation('auction');
$lic_prog = $lic_prog / $nbr_lic;
// Fish progression
$fish_prog = self::getGameStateValue('fish_cubes') / $nbr_fish;
// Use smaller of the two then take inverse and convert to %
return 100 * (1 - min($lic_prog, $fish_prog));
}
//////////////////////////////////////////////////////////////////////////////
//////////// Utility functions
////////////
/*
* Return an array of players in natural turn order starting
* with the current player. This is used to build the player
* tables in the same order as the player boards.
*/
function getPlayersInOrder()
{
$result = array();
$players = self::loadPlayersBasicInfos();
$next_player = self::getNextPlayerTable();
$player_id = self::getCurrentPlayerId();
// Check for spectator
if (!key_exists($player_id, $players)) {
$player_id = $next_player[0];
}
// Build array starting with current player
for ($i=0; $i<count($players); $i++) {
$result[] = $player_id;
$player_id = $next_player[$player_id];
}
return $result;
}
/*
* Increments the current phase counter and returns name of new phase
*/
function nextPhase()
{
self::DbQuery("UPDATE player SET passed = 0"); // clear player actions
$phase = self::incGameStateValue('current_phase', 1) % $this->nbr_phases;
return $this->phases[$phase];
}
/*
* Decrements the current phase counter and return name of new phase
* This allows one player to complete multiple phases (e.g. launch boats and
* hire captains) and then return to the correct phase for the next player
*/
function prevPhase()
{
self::DbQuery("UPDATE player SET passed = 0"); // clear player actions
$phase = self::incGameStateValue('current_phase', -1) % $this->nbr_phases;
return $this->phases[$phase];
}
/*
* Returns the name of the current phase
*/
function getCurrentPhase()
{
$phase = self::getGameStateValue('current_phase') % $this->nbr_phases;
return $this->phases[$phase];
}
/*
* Returns player Id based on simultaneous game option
*/
function getPlayerIdForAction()
{
$state = $this->gamestate->state();
if ($state['type'] == 'multipleactiveplayer') {
return self::getCurrentPlayerId();
}
return self::getActivePlayerId();
}
/*
* Returns player name based on simultaneous game option
*/
function getPlayerNameForAction()
{
$state = $this->gamestate->state();
if ($state['type'] == 'multipleactiveplayer') {
return self::getCurrentPlayerName();
}
return self::getActivePlayerName();
}
/*
* Returns additional card information for the given card.
* This information is stored outside of the in db order to make
* use of the standard Deck implementation and functions.
*/
function getCardInfo($card)
{
return $this->card_types[$card['type_arg']];
}
/*
* Returns additional card information for the given card ID.
* See getCardInfo
*/
function getCardInfoById($card_id)
{
$card = $this->cards->getCard($card_id);
return $this->getCardInfo($card);
}
/*
* Returns the written name of the given card.
* Convenience function mainly used for notifications.
*/
function getCardName($card)
{
return $this->getCardInfo($card)['name'];
}
/*
* Returns true if player is able to bid in the current auction round,
* false otherwise (passed or won previous)
*/
function canBid($player_id)
{
$sql = "SELECT (auction_pass + passed) AS passed FROM player WHERE player_id = $player_id";
return self::getUniqueValueFromDB($sql) == 0;
}
/*
* Returns true if player already passed this phase
*/
function hasPassed($player_id)
{
$sql = "SELECT passed FROM player WHERE player_id = $player_id";
return self::getUniqueValueFromDB($sql) == 1;
}
/*
* Returns all boat cards a player owns with additional information
*/
function getBoats($player_id)
{
$sql = "SELECT";
// Standard deck query
foreach (array('id', 'type', 'type_arg', 'location', 'location_arg') as $col) {
$sql .= " card_$col AS $col,";
}
// Extra columns
$sql .= ' nbr_fish AS fish, has_captain FROM card';
$sql .= " WHERE card_location = 'table' AND card_location_arg = $player_id AND card_type = '" . CARD_BOAT . "'";
return self::getCollectionFromDB($sql);
}
/*
* Returns maximum number of coins a player has to spend
* from cards in hand, processed fish, and Shrimp bonus
*/
function getCoins($player_id)
{
$coins = 0;
// Add up coins from all cards in hand
$cards = $this->cards->getPlayerHand($player_id);
foreach ($cards as $card) {
$card_info = $this->getCardInfo($card);
$coins += $card_info['coins'];
}
// During draw phase cards are held in hand, so add those coins
$cards = $this->cards->getCardsInLocation('draw', $player_id);
foreach ($cards as $card) {
$card_info = $this->getCardInfo($card);
$coins += $card_info['coins'];
}
// Each processed fish crate can be sold for $1
$coins += $this->getFishCrates($player_id);
// Add one for each Shrimp License as it effectively
// increases the player's buying power
$shrimp = $this->getLicenses($player_id, LICENSE_SHRIMP);
$coins += count($shrimp);
return $coins;
}
/*
* Returns all license cards a player owns
*/
function getLicenses($player_id, $type_arg=null)
{
return $this->cards->getCardsOfTypeInLocation(CARD_LICENSE, $type_arg, 'table', $player_id);
}
/*
* Returns number of processed fish a player has
*/
function getFishCrates($player_id)
{
return self::getUniqueValueFromDB("SELECT fish_crates FROM player WHERE player_id = $player_id");
}
/*
* Increments (pos/neg) the number of fish crates a player has
*/
function incFishCrates($player_id, $inc)
{
if ($inc == 0) {
return;
}
self::DbQuery("UPDATE player SET fish_crates = fish_crates + '$inc' WHERE player_id = $player_id");
}
/*
* Returns the current winning auction bid
*/
function getHighBid()
{
return self::getUniqueValueFromDB("SELECT MAX(auction_bid) AS high_bid FROM player");
}
/*
* Returns number of launches done by given player
*/
function getNumberOfLaunches( $player_id )
{
$result = 0;
if ( $this->optSimultaneousLaunchHire() ) {
$result = self::getUniqueValueFromDB( "SELECT nbr_launch_hire FROM player WHERE player_id = {$player_id}" );
} else {
$result = self::getGameStateValue('current_player_launches');
}
return $result;
}
/*
* Returns number of hires done by given player
*/
function getNumberOfHires( $player_id )
{
$result = 0;
if ( $this->optSimultaneousLaunchHire() ) {
$result = self::getUniqueValueFromDB( "SELECT nbr_launch_hire FROM player WHERE player_id = {$player_id}" );
} else {
$result = self::getGameStateValue('current_player_hires');
}
return $result;
}
/*
* Returns an array of all legal moves available to the player right now
* Mostly a list of card_ids that can be played, but some phases are more
* or less complicated and have different outputs
*/
function possibleMoves($player_id, $phase)
{
// Handle simultaneous launch/hire phases
if ($phase == PHASE_LAUNCH_HIRE) {
$state = self::getUniqueValueFromDB("SELECT launch_hire_phase FROM player WHERE player_id = {$player_id}");
if ($state == 0) {
$phase = PHASE_LAUNCH;
} else {
$phase = PHASE_HIRE;
}
}
$moves = array();
if ($phase == PHASE_AUCTION) {
// Auction: depends on current client state
if (self::getGameStateValue('auction_winner')) {
// Auction won
// All cards in hand can be used to pay
$moves = $this->cards->getPlayerHand($player_id);
} else if (!self::getGameStateValue('auction_card')) {
// Start new auction
// Only available licenses that the player can afford minimum bid
$coins = $this->getCoins($player_id);
$cards = $this->cards->getCardsInLocation('auction');
foreach ($cards as $card_id => $card) {
$card_info = $this->getCardInfo($card);
if ($coins >= $card_info['cost']) {
$moves[$card_id] = true;
}
}
} else {
if ($this->getCoins($player_id) > $this->getHighBid()) {
$moves[] = true;
}
}
} else if ($phase == PHASE_LAUNCH) {
// Launch Boats: any boat in hand the player can afford and owns same license
$coins = $this->getCoins($player_id);
$cards = $this->cards->getPlayerHand($player_id);
$licenses = array_column($this->getLicenses($player_id), 'type_arg');
foreach ($cards as $card_id => $card) {
$move = array('can_play' => false);
$card_info = $this->getCardInfo($card);
// Provide client as detailed error as possible
if ($card['type'] == CARD_BONUS) {
// Not boat
$move['error'] = clienttranslate("That's not a boat!");
} else if (!$this->isLicenseInList($card['type_arg'], $card_info['license'], $licenses)) {
// No license
$move['error'] = clienttranslate('You do not have the required license');
} else if (($coins - $card_info['coins']) < $card_info['cost']) {
// Not enough coins (after this boat removed)
$move['error'] = clienttranslate('You cannot afford this boat');
} else {
// All good
$move['can_play'] = true;
}
$moves[$card_id] = $move;
}
} else if ($phase == PHASE_HIRE) {
// Hire Captains: any boat card in hand and any
// boat played that does not already have a captain
// Hand
$cards = $this->cards->getPlayerHand($player_id);
foreach ($cards as $card_id => $card) {
if ($card['type'] == CARD_BOAT) {
$moves[$card_id] = true;
$moves['has_boat'] = true;
}
}
// Table
$boats = $this->getBoats($player_id);
foreach ($boats as $card_id => $boat) {
if (!$boat['has_captain']) {
$moves[$card_id] = true;
$moves['has_captain'] = true;
}
}
} else if ($phase == PHASE_PROCESSING) {
if ($this->hasPassed($player_id)) {
// Trading, client tracks this directly
$moves[] = true;
} else {
// Process Fish: any launched boat with fish on it
$boats = $this->getBoats($player_id);
foreach ($boats as $card_id => $boat) {
if ($boat['fish'] > 0) {
$moves[$card_id] = true;
}
}
}
} else if ($phase == PHASE_TRADING) {
$moves[] = true; // Client will track this directly
} else if ($phase == PHASE_DRAW) {
// Draw: either one of two cards just drawn, or any card with Tuna bonus
$cards = $this->cards->getCardsInLocation('draw', $player_id);
if (count($cards) == 0) {
// No draw means they're already in hand from Tuna bonus
$cards = $this->cards->getPlayerHand($player_id);
}
foreach ($cards as $card_id => $card) {
$moves[$card_id] = true;
}
}
return $moves;
}
/*
* Returns true if given boat license is in the list of licenses
* Convenience method to simplify flow control in possibleMoves above
*/
function isLicenseInList($card_type, $license_type, $licenses)
{
if ($card_type == BOAT_CRAB) {
// Crab has three unique licenses that all launch the same boat
foreach ($license_type as $crab_type) {
if (in_array($crab_type, $licenses)) {
return true;
}
}
return false;
} else {
return in_array($license_type, $licenses);
}
}
/*
* Increments (pos/neg) the player's score
*/
function incScore($player_id, $inc)
{
if ($inc == 0) {
return;
}
self::DbQuery("UPDATE player SET player_score = player_score + '$inc' WHERE player_id = $player_id");
}
/*
* Returns true if Gone Fishin' option is enabled
*/
function optGoneFishing()
{
return $this->gamestate->table_globals[100] == 1;
}
/*
* Returns true if Fast Passing option is enabled
*/
function optFastPassing()
{
return $this->gamestate->table_globals[101] == 2;
}
/*
* Returns true if Simultaneous launch boat & hire captain is enabled
*/
function optSimultaneousLaunchHire()
{
return array_key_exists(102, $this->gamestate->table_globals) // ensure option added
&& $this->gamestate->table_globals[102] == 2;
}
//////////////////////////////////////////////////////////////////////////////
//////////// Player actions
////////////
/*
* Player passes turn during any phase
*/
function pass()
{
self::checkAction('pass');
$player_id = $this->getPlayerIdForAction();
$this->passCheckAndNotify($player_id);
$state = $this->gamestate->state();
if ($state['name'] == PHASE_LAUNCH_HIRE) {
$state = self::getUniqueValueFromDB("SELECT launch_hire_phase FROM player WHERE player_id = {$player_id}");
if ($state == 0) {
$this->nextLaunch($player_id); // draw bonus
}
if ($state == 0 && !$this->skipPlayer($player_id, PHASE_HIRE)) {
self::DbQuery("UPDATE player SET nbr_launch_hire = 0, launch_hire_phase = 1, passed = 0 WHERE player_id = {$player_id}");
self::notifyPlayer($player_id, 'clientState', '', array(
'moves' => $this->possibleMoves($player_id, PHASE_HIRE),
'state' => 'client_' . PHASE_HIRE,
'desc' => clienttranslate('${you} may hire a captain'),
));
} else {
$this->nextHire($player_id); // draw bonus
$this->gamestate->setPlayerNonMultiactive($player_id, '');
}
} else if ($state['type'] == 'multipleactiveplayer') {
$this->gamestate->setPlayerNonMultiactive($player_id, '');
} else {
$this->gamestate->nextState();
}
}
/*
* Check any pass conditions for auction and notify players
* (main logic for passing so auto pass can execute from game state)
*/
function passCheckAndNotify($player_id)
{
$in_auction = false;
$auction_done = false;
$msg = clienttranslate('${player_name} passes');
$card = null;
if ($this->getCurrentPhase() == PHASE_AUCTION) {
// Special handling for auction phase
$in_auction = true;
// Keep track of which players pass during auction
$sql = 'UPDATE player SET auction_bid = 0, auction_pass = 1';
if (self::getGameStateValue('auction_card') == 0) {
// No active auction, player chooses not to buy
$sql .= ', passed = 1';
$auction_done = true; // tell client to remove player
if ($this->optGoneFishing()) {
// Player gets a Gone Fishin' card
$card = $this->cards->pickCard('gonefishing', $player_id);
if ($card != null) {
//TODO: what if none left?
$msg = clienttranslate('${player_name} skips the auction to go fishing');
}
}
self::incStat(1, 'auctions_passed', $player_id);
}
$sql .= " WHERE player_id = $player_id";
self::DbQuery($sql);
} else {
// All other phases simply record that player passed
// in case there are multiple actions possible
self::DbQuery("UPDATE player SET passed = 1 WHERE player_id = $player_id");
}
$players = self::loadPlayersBasicInfos(); // for player name since may be non-active state
self::notifyAllPlayers('pass', $msg, array(
'player_name' => $players[$player_id]['player_name'],
'player_id' => $player_id,
'in_auction' => $in_auction,
'auction_done' => $auction_done,
'card' => $card,
));
}
/*
* Player bids on auction license, and maybe chooses card to auction
*/
function bid($current_bid, $card_id=-1)
{
self::checkAction('bid');
$player_id = self::getActivePlayerId();
// Verify player is still active in auction
if (!$this->canBid($player_id)) {
throw new feException("Player is not active in this auction");
}
if ($card_id > 0) {
// Initial card selection for auction round
// Verify some card not already selected
if (self::getGameStateValue('auction_card') > 0) {
throw new feException("Impossible bid state");
}
// Verify card exists in auction
$card = $this->cards->getCard($card_id);
if ($card == null || $card['location'] != 'auction') {
throw new feException("Impossible bid action");
}
// Verify bid meets minimum
$card_info = $this->getCardInfo($card);
if ($current_bid < $card_info['cost']) {
$cost = $card_info['cost'];
throw new BgaUserException(self::_("You must bid at least {$cost}"));
}
// Store selected card for current auction round
self::setGameStateValue('auction_card', $card_id);
} else {
// Continue current auction
// Verify license already selected
if (self::getGameStateValue('auction_card') == 0) {
throw new feException("Impossible bid without license");
}
}
// Verify bid is higher than previous
$high_bid = $this->getHighBid();
if ($current_bid <= $high_bid) {
$min_bid = $high_bid + 1;
throw new BgaUserException(self::_("You must bid at least {$min_bid}"));
}
// Verify player can pay bid
$coins = $this->getCoins($player_id);
if ($coins < $current_bid) {
throw new BgaUserException(self::_("You cannot afford that bid"));
}
// Record player bid
$sql = "UPDATE player SET auction_bid = $current_bid WHERE player_id = $player_id";
self::DbQuery($sql);
// Notify
if ($card_id > 0) {
// Notify client which card will be auctioned
$msg = clienttranslate('${player_name} selects ${card_name} for auction');
self::notifyAllPlayers('auctionSelect', $msg, array(
'i18n' => array('card_name'),
'player_name' => self::getActivePlayerName(),
'card_name' => $this->getCardName($card),
'card_id' => $card_id,
));
}
$msg = clienttranslate('${player_name} bids ${bid}');
self::notifyAllPlayers('auctionBid', $msg, array(
'player_name' => self::getActivePlayerName(),
'bid' => $current_bid,
'player_id' => $player_id,
));
$this->gamestate->nextState();
}
/*
* Player wins auction and pays cards and/or fish to buy license
*/
function buyLicense($card_ids, $fish=0)
{
self::checkAction('buyLicense');
$player_id = self::getActivePlayerId();
$coins = $fish; // $1 per fish crate
// Validate game state and transaction
// Verify player is current auction winner
if ($player_id != self::getGameStateValue('auction_winner')) {
throw new feException("Impossible buy: not winner");
}
// Verify license selected in auction
$license_id = self::getGameStateValue('auction_card');
if ($license_id == 0) {
throw new feException("Impossible buy: no license");
}
$license = $this->cards->getCard($license_id);
if ($license == null || $license['location'] != 'auction') {
throw new feException("Impossible buy: non-auction license");
}
$license_info = $this->getCardInfo($license);
// Verify all cards in player hand and tally coins
foreach ($card_ids as $card_id) {
$card = $this->cards->getCard($card_id);
if ($card == null || $card['location'] != 'hand' || $card['location_arg'] != $player_id) {
throw new feException("Invalid card id for purchase: $card_id");
}
$card_info = $this->getCardInfo($card);
$coins += $card_info['coins'];
}
if ($fish > 0) {
// Verify player has fish to sell
if ($this->getFishCrates($player_id) < $fish) {
throw new feException("Impossible buy: too many fish");
}
}
// Each Shrimp License reduces the cost by one
$discount = count($this->getLicenses($player_id, LICENSE_SHRIMP));
// Verify bid
$bid = $this->getHighBid();
if ($bid < $license_info['cost']) {
throw new feException("Impossible buy: low bid");
}
// Verify player paid enough
if ($coins < ($bid - $discount)) {
throw new feException("Impossible buy: not enough");
}
// Purchase is valid
// Discard cards and fish crates
foreach ($card_ids as $card_id) {
$card = $this->cards->getCard($card_id);
if ($card['type'] == CARD_BONUS) {
$this->cards->moveCard($card_id, 'gonefishing');
} else {
$this->cards->playCard($card_id);
}
}
if ($fish > 0) {
$this->incFishCrates($player_id, -$fish);
}
// Take license and score VP
$this->cards->moveCard($license_id, 'table', $player_id);
$this->incScore($player_id, $license_info['points']);
// Stats
self::incStat(1, 'licenses_bought', $player_id);
self::incStat($license_info['points'], 'vp_licenses', $player_id);
self::incStat($license_info['points'], 'vp_total', $player_id);
$overpay = $coins + $discount - $bid;