-
Notifications
You must be signed in to change notification settings - Fork 0
/
veggiegarden.game.php
1317 lines (1089 loc) · 48 KB
/
veggiegarden.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: (c) Gregory Isabelli <[email protected]> & Emmanuel Colin <[email protected]>
* veggiegarden implementation : (c) Antonio Soler [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.
* -----
*
* veggiegarden.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 veggiegarden 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(
"iterations" => 10,
"max_iterations" =>11,
"card_picked" => 12,
"card_target" => 13,
"token_target" => 14,
"groundhog_pos" => 15
// ...
// "my_first_game_variant" => 100,
// "my_second_game_variant" => 101,
// ...
) );
$this->cards = self::getNew( "module.common.deck" );
$this->cards->init( "cards" );
$this->tokens = self::getNew( "module.common.deck" );
$this->tokens->init( "tokens" );
}
protected function getGameName( )
{
// Used for translations and stuff. Please do not modify.
return "veggiegarden";
}
/*
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() )
{
try {
// 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", "008000", "0000ff", "ffa500", "773300" );
// 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, array( "ff0000", "008000", "0000ff", "ffa500", "773300" ) );
self::reloadPlayersBasicInfos();
/************ Start the game initialization *****/
// Init global values with their initial values
//self::setGameStateInitialValue( 'my_first_global_variable', 0 );
// Init game statistics
// (note: statistics used in this file must be defined in your stats.inc.php file)
self::initStat( 'table', 'turns_number' , 0 ); // Init a table statistics
self::initStat( 'table', 'carrots_value', 0 );
self::initStat( 'table', 'cabbage_value', 0 );
self::initStat( 'table', 'peas_value' , 0 );
self::initStat( 'table', 'peppers_value', 0 );
self::initStat( 'table', 'potato_value' , 0 );
self::initStat( 'table', 'tomato_value' , 0 );
self::initStat( 'player', 'turns_number' , 0 ); // Init a player statistics (for all players)
self::initStat( 'player', 'carrots_picked' , 0 );
self::initStat( 'player', 'cabbages_picked', 0 );
self::initStat( 'player', 'peas_picked' , 0 );
self::initStat( 'player', 'peppers_picked' , 0 );
self::initStat( 'player', 'potatos_picked' , 0 );
self::initStat( 'player', 'tomatos_picked' , 0 );
// TODO: setup the initial game situation here
$removed=mt_rand (1,6); //remove 1 card type from the game
$cards = array();
for ( $i=1 ; $i <=6 ; $i++ )
{
$card = array( 'type' => $i , 'type_arg' => 0 , 'nbr' => 14 );
if ( $i != $removed )
{
array_push($cards, $card);
}
}
$this->cards->createCards( $cards, 'deck' );
$cards = array();
for ( $i=1 ; $i <=3 ; $i++ )
{
$card = array( 'type' => $i , 'type_arg' => 0 , 'nbr' => 4 );
array_push($cards, $card);
}
$this->tokens->createCards( $cards, 'deck' );
if ( $removed != 1 ) // SHOULD WE PLACE THE BUNNY ?
{
$sql = "UPDATE tokens set card_type=0 where card_id=" . mt_rand (1,12) ;
self::DbQuery( $sql );
}
//shuffle
$this->cards->shuffle( 'deck' );
$this->tokens->shuffle( 'deck' );
for ($x=0 ; $x<=3 ; $x++)
{
for ($y=0 ; $y<=3 ; $y++)
{
$PlayedCard = $this->cards->pickCardForLocation( 'deck', 'field', $x * 10 + $y );
}
}
for ($x=0 ; $x<=3 ; $x++)
{
$this->tokens->pickCardsForLocation( 1, 'deck' , 'fence', $x*10 , true );
$this->tokens->pickCardsForLocation( 1, 'deck' , 'fence', $x*10+3 , true );
}
for ($x=1 ; $x<=2 ; $x++)
{
$this->tokens->pickCardsForLocation( 1, 'deck' , 'fence', $x , true );
$this->tokens->pickCardsForLocation( 1, 'deck' , 'fence', 30+$x , true );
}
for ($x=1 ; $x<=3 ; $x++)
{
$PlayedCard = $this->cards->pickCardForLocation( 'deck', 'table', $x );
}
if ( $removed != 3 ) // SHOULD WE PLACE THE groundhog ?
{
$random = mt_rand (0,3);
$groundhog_pos = array ( 11 ,12 , 21 ,22 ) ;
self::setGameStateInitialValue( 'groundhog_pos', $groundhog_pos[$random] );
}
else
{
self::setGameStateInitialValue( 'groundhog_pos', -55 ); //NO Groundhog
}
self::setGameStateInitialValue( 'iterations', 0 );
self::setGameStateInitialValue( 'max_iterations', sizeof( $players ) * 7 );
self::setGameStateInitialValue( 'card_picked', 0 );
self::setGameStateInitialValue( 'card_target', 0 );
self::setGameStateInitialValue( 'token_target', 0 );
foreach( $players as $player_id => $player )
{
$this->cards->pickCardsForLocation( 2, 'deck' , 'hand', $player_id );
}
} catch ( Exception $e ) {
$this->dump('err', $e);
$this->error("Error during game initialization: $e");
}
// Activate first player (which is in general a good idea :) )
$this->activeNextPlayer();
/************ 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( 'players' => 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 FROM player ";
$result['players'] = self::getCollectionFromDb( $sql );
$result['table'] = $this->cards->getCardsInLocation( 'table' );
$result['field'] = $this->cards->getCardsInLocation( 'field' );
$result['fence'] = $this->tokens->getCardsInLocation( 'fence' );
$result['groundhog'] = self::getGameStateValue('groundhog_pos');
$result['hand'] = $this->cards->getCardsInLocation( 'hand', $current_player_id );
$sql = "SELECT card_location_arg, COUNT(*) amount FROM cards WHERE card_location='hand' GROUP BY card_location_arg ";
$result['cardcount'] = self::getCollectionFromDb( $sql );
// TODO: Gather all information about current game situation (visible by player $current_player_id).
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()
{
$players = self::loadPlayersBasicInfos();
$cardcount=$this->cards->countCardInLocation( 'hand' );
$result = ( $cardcount * 100 ) / ( sizeof($players)*9 );
return $result ;
}
//////////////////////////////////////////////////////////////////////////////
//////////// Utility functions
////////////
/*
In this space, you can put any utility methods useful for your game logic
*/
//////////////////////////////////////////////////////////////////////////////
//////////// 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 veggiegarden.action.php)
*/
/*
Example:
function playCard( $card_id )
{
// Check that this is the player's turn and that it is a "possible action" at this game state (see states.inc.php)
self::checkAction( 'playCard' );
$player_id = self::getActivePlayerId();
// Add your game logic to play a card there
...
// Notify all players about the card played
self::notifyAllPlayers( "cardPlayed", clienttranslate( '${player_name} played ${card_name}' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_name' => $card_name,
'card_id' => $card_id
) );
}
*/
function pickcard( $card_id)
{
self::checkAction( 'pickcard' );
$player_id = self::getActivePlayerId();
$thiscard= $this->cards->getCard( $card_id );
self::setGameStateValue( 'card_picked', $card_id );
$thiscardtype=$thiscard['type'];
self::notifyAllPlayers( "selectitem", "" , array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'item' => 'card_'.$card_id ,
'thiscard_name' => $this->card_types[$thiscardtype]['name']
) );
$cardcount=$this->cards->countCardInLocation ('hand', $player_id);
if ($cardcount>=8)
{
self::DbQuery( "UPDATE cards SET card_location_arg=".$player_id.", card_location='hand' WHERE card_id=".$card_id );
self::notifyAllPlayers( "cardtohand", clienttranslate( '${player_name} this is your last card so the effect is not applied' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $card_id
) );
$this->gamestate->nextState( 'endTurn' );
}
else
{
$this->gamestate->nextState( 'selectTarget' );
}
}
function selectTarget( $target)
{
self::checkAction( 'selectTarget' );
$player_id = self::getActivePlayerId();
$targettype=substr( $target, 0, 5 ) ; // card_ field fence
switch ($targettype){
case "card_":
$card_id = substr( $target, 5, 2 ) ; // card_id
self::setGameStateValue( 'card_target', $card_id );
self::setGameStateValue( 'token_target', 0 );
self::notifyAllPlayers( "selectitem","" , array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'item' => 'card_'.$card_id
) );
$cardpicked=self::getGameStateValue( 'card_picked');
$card=$this->cards->getCard( $cardpicked );
$target=$this->cards->getCard( $card_id );
if ($card['type'] == 3) // move the groundhog
{
self::setGameStateValue( 'groundhog_pos', $target['location_arg'] );
$moveitems= array( 'groundhog' => 'field'.$target['location_arg'] );
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} moves the groundhog' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems
) );
}
break;
case "token":
$card_id = substr( $target, 6, 2 ) ; // token_id
self::setGameStateValue( 'card_target', 0 );
self::setGameStateValue( 'token_target', $card_id );
self::notifyAllPlayers( "selectitem", "" , array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'item' => 'token_'.$card_id
) );
break;
}
$this->gamestate->nextState( 'selectDestination' );
}
function cancel( )
{
self::checkAction( 'cancel' );
$player_id = self::getActivePlayerId();
$groundhog_pos=self::getGameStateValue( 'groundhog_pos');
if ( self::getGameStateValue( 'groundhog_pos') <> self::getGameStateValue( 'iterations'))
{
self::setGameStateValue( 'groundhog_pos', self::getGameStateValue( 'iterations') );
$moveitems= array( 'groundhog' => 'field'.self::getGameStateValue( 'iterations') );
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} returns the groundhog to its original position' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems
) );
}
$this->gamestate->nextState( 'playerpick' );
}
function selectDestination( $destination)
{
self::checkAction( 'selectDestination' );
$player_id = self::getActivePlayerId();
$targettype=substr( $destination, 0, 5 ) ; // card_ field fence
self::notifyAllPlayers( "selectitem", "" , array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'item' => $destination
) );
$cardpicked=self::getGameStateValue( 'card_picked');
$cardtarget=self::getGameStateValue( 'card_target');
$tokentarget=self::getGameStateValue( 'token_target');
$groundhog_pos=self::getGameStateValue( 'groundhog_pos');
$Ygroundhog_pos= $groundhog_pos % 10 ;
$Xgroundhog_pos= ($groundhog_pos - $groundhog_pos % 10) / 10;
$card=$this->cards->getCard( $cardpicked );
switch ($card['type']){
case "1": // CARROTS Move BUNNY
$sql = "select card_location_arg from tokens where card_type=0";
$bunnypos=self::getUniqueValueFromDB( $sql );
$sql = "select card_id from tokens where card_type=0";
$bunny_id=self::getUniqueValueFromDB( $sql );
$token_id = substr( $destination, 6, 2 ) ; // token_id
$sql = "select card_location_arg from tokens where card_id=".$token_id;
$tokenpos=self::getUniqueValueFromDB( $sql );
self::DbQuery( "UPDATE tokens SET card_location_arg=".$tokenpos." WHERE card_type=0" );
self::DbQuery( "UPDATE tokens SET card_location_arg=".$bunnypos." WHERE card_id=".$token_id );
$moveitems= array( 'token_'.$token_id => 'fence'.$bunnypos ,
'token_'.$bunny_id => 'fence'.$tokenpos
);
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} moves the bunny to other fence post' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems
) );
break;
case "2": //CABBAGE Shift any column or row of cards or fence (groundhog blocks row and column)
$targettype=substr( $destination, 0, 5 ) ; // card_ or token?
switch ($targettype){
case "card_":
$card_id = substr( $destination, 5, 2 ) ; // card_id
$sql = "select card_location_arg from cards where card_id=".$cardtarget;
$targetpos=self::getUniqueValueFromDB( $sql );
$sql = "select card_location_arg from cards where card_id=".$card_id;
$cardpos=self::getUniqueValueFromDB( $sql );
$delta=$cardpos-$targetpos;
$Y= $targetpos % 10 ;
$X= ($targetpos - $targetpos % 10) / 10;
$shiftcards= array ();
switch($delta)
{
case "1":
case "-3":
$shiftcards= array ($X*10+0,$X*10+1,$X*10+2,$X*10+3 );
break;
case "-1":
case "3":
$shiftcards= array ($X*10+3,$X*10+2,$X*10+1,$X*10+0 );
break;
case "10":
case "-30":
$shiftcards= array (0+$Y,10+$Y,20+$Y,30+$Y );
break;
case "-10":
case "30":
$shiftcards= array (30+$Y,20+$Y,10+$Y,0+$Y );
break;
}
$shiftcards_ids= array ();
for ($c=0;$c<=3;$c++)
{
$sql = "select card_id from cards where (card_location='field') and (card_location_arg=".$shiftcards[$c].")";
$shiftcards_ids[$c]=self::getUniqueValueFromDB( $sql );
}
$moveitems= array();
for ($c=1;$c<=4;$c++)
{
self::DbQuery( "UPDATE cards SET card_location_arg=".$shiftcards[$c%4]." WHERE card_id=".$shiftcards_ids[$c-1] );
$moveitems[ 'card_'.$shiftcards_ids[$c-1]] = 'field'.$shiftcards[$c%4] ;
}
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} shifts a row or column of cards ' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems
) );
break;
case "token":
$token_id = substr( $destination, 6, 2 ) ; // token_id
$sql = "select card_location_arg from tokens where card_id=".$tokentarget;
$targetpos=self::getUniqueValueFromDB( $sql );
$sql = "select card_location_arg from tokens where card_id=".$token_id;
$tokenpos=self::getUniqueValueFromDB( $sql );
$delta=$tokenpos-$targetpos;
$Y= $targetpos % 10 ;
$X= ($targetpos - $targetpos % 10) / 10;
$shiftcards= array ();
switch($delta)
{
case "1":
$shiftcards= array ($X*10+1,$X*10+2 );
break;
case "-1":
$shiftcards= array ($X*10+2,$X*10+1 );
break;
case "10":
$shiftcards= array (0+$Y,10+$Y,20+$Y,30+$Y );
break;
case "-10":
$shiftcards= array (30+$Y,20+$Y,10+$Y,0+$Y );
break;
}
$shiftcards_ids= array ();
for ($c=0;$c<sizeof($shiftcards);$c++)
{
$sql = "select card_id from tokens where (card_location='fence') and (card_location_arg=".$shiftcards[$c].")";
$shiftcards_ids[$c]=self::getUniqueValueFromDB( $sql );
}
$moveitems= array();
for ($c=1;$c<=sizeof($shiftcards);$c++)
{
self::DbQuery( "UPDATE tokens SET card_location_arg=".$shiftcards[($c%sizeof($shiftcards))]." WHERE card_id=".$shiftcards_ids[$c-1] );
$moveitems[ 'token_'.$shiftcards_ids[$c-1]] = 'fence'.$shiftcards[($c%sizeof($shiftcards))] ;
}
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} shifts a row or column of tokens ' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems,
) );
break;
}
break;
case "3": //PEAS move the groundhog to other compost, select one card and it shifts position over the groundhog
$card_id = substr( $destination, 5, 2 ) ; // card_id
$sql = "select card_location_arg from cards where card_id=".$cardtarget;
$targetpos=self::getUniqueValueFromDB( $sql );
$sql = "select card_location_arg from cards where card_id=".$card_id;
$cardpos=self::getUniqueValueFromDB( $sql );
$opositepos=2*$targetpos - $cardpos;
$sql = "select card_id from cards where (card_location='field') and (card_location_arg=".$opositepos.")";
$opositeid=self::getUniqueValueFromDB( $sql );
self::DbQuery( "UPDATE cards SET card_location_arg=".$opositepos." WHERE card_id=".$card_id );
self::DbQuery( "UPDATE cards SET card_location_arg=".$cardpos." WHERE card_id=".$opositeid );
$moveitems= array( 'card_'.$card_id => 'field'.$opositepos ,
'card_'.$opositeid => 'field'.$cardpos
);
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} shifts two cards over the groundhog' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems
) );
break;
case "4": //PEPPERS Swaps two cards or tokens (the groundhog blocks one card)
$targettype=substr( $destination, 0, 5 ) ; // card_ or token?
switch ($targettype){
case "card_":
$card_id = substr( $destination, 5, 2 ) ; // card_id
$sql = "select card_location_arg from cards where card_id=".$cardtarget;
$targetpos=self::getUniqueValueFromDB( $sql );
$sql = "select card_location_arg from cards where card_id=".$card_id;
$cardpos=self::getUniqueValueFromDB( $sql );
self::DbQuery( "UPDATE cards SET card_location_arg=".$targetpos." WHERE card_id=".$card_id );
self::DbQuery( "UPDATE cards SET card_location_arg=".$cardpos." WHERE card_id=".$cardtarget );
$moveitems= array( 'card_'.$card_id => 'field'.$targetpos ,
'card_'.$cardtarget => 'field'.$cardpos
);
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} swaps two cards' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems
) );
break;
case "token":
$token_id = substr( $destination, 6, 2 ) ; // token_id
$sql = "select card_location_arg from tokens where card_id=".$tokentarget;
$targetpos=self::getUniqueValueFromDB( $sql );
$sql = "select card_location_arg from tokens where card_id=".$token_id;
$tokenpos=self::getUniqueValueFromDB( $sql );
self::DbQuery( "UPDATE tokens SET card_location_arg=".$targetpos." WHERE card_id=".$token_id );
self::DbQuery( "UPDATE tokens SET card_location_arg=".$tokenpos." WHERE card_id=".$tokentarget );
$moveitems= array( 'token_'.$token_id => 'fence'.$targetpos ,
'token_'.$tokentarget => 'fence'.$tokenpos
);
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} swaps two fence tokens' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems
) );
break;
}
break;
case "5": // POTATO Exchange one card from your hand with one on the table (the groundhog blocks one card)
$card_id = substr( $destination, 5, 2 ) ; // card_id
$sql = "select card_location_arg from cards where card_id=".$cardtarget;
$targetpos=self::getUniqueValueFromDB( $sql );
$sql = "select card_location_arg from cards where card_id=".$card_id;
$cardpos=self::getUniqueValueFromDB( $sql );
self::DbQuery( "UPDATE cards SET card_location_arg=".$player_id.", card_location='hand' WHERE card_id=".$card_id );
self::DbQuery( "UPDATE cards SET card_location_arg=".$cardpos." , card_location='field' WHERE card_id=".$cardtarget );
self::notifyAllPlayers( "cardtohand", clienttranslate( '${player_name} picks a card from the garden' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $card_id
) );
$sql = "select card_type from cards where card_id=".$cardtarget;
$target_type=self::getUniqueValueFromDB( $sql );
self::notifyAllPlayers( "cardfromhand", clienttranslate( '${player_name} replaces the card with one from the hand' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $cardtarget,
'card_pos' => $cardpos,
'card_type' => $target_type
) );
break;
case "6": //TOMATO Discard a veggie from the garden and replace it with one from the harvest (the groundhog blocks)
$card_id = substr( $destination, 5, 2 ) ; // card_id
$sql = "select card_location_arg from cards where card_id=".$cardtarget;
$targetpos=self::getUniqueValueFromDB( $sql );
$sql = "select card_location_arg from cards where card_id=".$card_id;
$cardpos=self::getUniqueValueFromDB( $sql );
self::DbQuery( "UPDATE cards SET card_location_arg=".$targetpos.", card_location='field' WHERE card_id=".$card_id );
self::DbQuery( "UPDATE cards SET card_location_arg=".$player_id." , card_location='removed' WHERE card_id=".$cardtarget );
self::notifyAllPlayers( "discard", clienttranslate( '${player_name} discards a card from the garden' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $cardtarget
) );
$moveitems= array( 'card_'.$card_id => 'field'.$targetpos );
self::notifyAllPlayers( "moveitems", clienttranslate( '${player_name} replaces the card wiht one from harvest' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'moveitems' => $moveitems
) );
break;
}
self::DbQuery( "UPDATE cards SET card_location_arg=".$player_id.", card_location='hand' WHERE card_id=".$cardpicked );
self::notifyAllPlayers( "cardtohand", clienttranslate( '${player_name} picks the selected card from the harvest' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $cardpicked
) );
$this->gamestate->nextState( 'endTurn' );
}
//////////////////////////////////////////////////////////////////////////////
//////////// 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.
*/
/*
Example for game state "MyGameState":
*/
function argPossibleDestinations()
{
$player_id = self::getActivePlayerId();
$cardpicked=self::getGameStateValue( 'card_picked');
$cardtarget=self::getGameStateValue( 'card_target');
$tokentarget=self::getGameStateValue( 'token_target');
$groundhog_pos=self::getGameStateValue( 'groundhog_pos');
$Ygroundhog_pos= $groundhog_pos % 10 ;
$Xgroundhog_pos= ($groundhog_pos - $groundhog_pos % 10) / 10;
$card=$this->cards->getCard( $cardpicked );
$result= array( 'possibledestinations' => array() );
$result['cardpicked']= $card['type'] ;
switch ($card['type'])
{
case "1": // CARROTS Move BUNNY
$sql = "select card_location_arg pos from tokens where card_type<>0";
$notbunnypos=self::getCollectionFromDb( $sql );
foreach( $notbunnypos as $thispos )
{
array_push($result["possibledestinations"],"fence".$thispos['pos']);
}
break;
case "2": //CABBAGE Shift any column or row of cards or fence (groundhog blocks row and column)
if ($cardtarget >= 1 )
{
$target=$this->cards->getCard( $cardtarget );
$Xtarget = ( $target['location_arg'] - $target['location_arg'] % 10) / 10; ;
$Ytarget = $target['location_arg'] % 10 ;
/*for ($x=-1 ; $x<=1 ; $x+=2)
{
if ( (($Xtarget +$x) >= 0 ) AND ( ( $Xtarget + $x ) < 4 ) )
{
array_push($result["possibledestinations"],"field".(($Xtarget+$x)*10+$Ytarget));
}
}
for ($y=-1 ; $y<=1 ; $y+=2)
{
if ( (($Ytarget +$y) >= 0 ) AND ( ( $Ytarget + $y ) < 4 ) )
{
array_push($result["possibledestinations"],"field".(($Xtarget)*10+$Ytarget+$y));
}
}*/
for ($x=-1 ; $x<=1 ; $x+=2)
{
array_push($result["possibledestinations"],"field".((($Xtarget+$x+4)%4 )*10+$Ytarget));
}
for ($y=-1 ; $y<=1 ; $y+=2)
{
array_push($result["possibledestinations"],"field".(($Xtarget)*10+($Ytarget+$y+4)%4 ));
}
}
if ($tokentarget >= 1 )
{
$target=$this->tokens->getCard( $tokentarget );
$Xtarget = ( $target['location_arg'] - $target['location_arg'] % 10) / 10; ;
$Ytarget = $target['location_arg'] % 10 ;
if ( $Ytarget == 0 OR $Ytarget == 3)
{
for ($x=-1 ; $x<=1 ; $x+=2)
{
if ( (($Xtarget +$x) >= 0 ) AND ( ( $Xtarget + $x ) < 4 ) )
{
array_push($result["possibledestinations"],"fence".(($Xtarget+$x)*10+$Ytarget));
}
}
}
if ( $Ytarget == 1 OR $Ytarget == 2)
{
for ($y=-1 ; $y<=1 ; $y+=2)
{
if ( (($Ytarget +$y) >= 1 ) AND ( ( $Ytarget + $y ) < 3 ) )
{
array_push($result["possibledestinations"],"fence".(($Xtarget)*10+$Ytarget+$y));
}
}
}
}
break;
case "3": //PEAS move the groundhog to other compost, select one card and it shifts position over the groundhog
for ($x=1 ; $x<= 2; $x++)
{
for ($y=1 ; $y<= 2 ;$y++)
{
if ( $groundhog_pos != ($x*10 + $y ))
{
array_push($result["possibledestinations"],"field".($x*10+$y));
}
}
}
break;
case "4": //PEPPERS Swaps two cards (the groundhog blocks one card)
if ($cardtarget >= 1 )
{
$target=$this->cards->getCard( $cardtarget );
$Xtarget = ( $target['location_arg'] - $target['location_arg'] % 10) / 10; ;
$Ytarget = $target['location_arg'] % 10 ;
$groundhog_pos=self::getGameStateValue( 'groundhog_pos');
for ($x=-1 ; $x<=1 ; $x+=1)
{
for ($y=-1 ; $y<=1 ; $y+=1)
{
if ( (($Ytarget +$y) >= 0 ) AND ( ( $Ytarget + $y ) < 4 ) AND (($Xtarget +$x) >= 0 ) AND ( ( $Xtarget + $x ) < 4 ) AND ( $groundhog_pos <> (($Xtarget+$x)*10+$Ytarget+$y) ) AND ( $target['location_arg'] != (($Xtarget+$x)*10+$Ytarget+$y)))
{
array_push($result["possibledestinations"],"field".(($Xtarget+$x)*10+$Ytarget+$y));
}
}
}
}
if ($tokentarget >= 1 )
{
$target=$this->tokens->getCard( $tokentarget );
$Xtarget = ( $target['location_arg'] - $target['location_arg'] % 10) / 10; ;
$Ytarget = $target['location_arg'] % 10 ;
$sql = "select card_location_arg from tokens where card_type=0";
$bunnypos=self::getUniqueValueFromDB( $sql );
if ( $Ytarget == 0 OR $Ytarget == 3)
{
for ($x=-1 ; $x<=1 ; $x+=2)
{
if ( (($Xtarget +$x) >= 0 ) AND ( ( $Xtarget + $x ) < 4 ) )
{
if ($bunnypos<>(($Xtarget+$x)*10+$Ytarget))
{
array_push($result["possibledestinations"],"fence".(($Xtarget+$x)*10+$Ytarget));
}
}
}
}
if ( $Ytarget == 1 OR $Ytarget == 2)
{
/* for ($y=-1 ; $y<=1 ; $y+=2)
{
if ( (($Ytarget +$y) >= 1 ) AND ( ( $Ytarget + $y ) < 3 ) )
{
array_push($result["possibledestinations"],"fence".(($Xtarget)*10+$Ytarget+$y));
}
}*/
if ($bunnypos<>(($Xtarget)*10+$Ytarget+1))
{
array_push($result["possibledestinations"],"fence".(($Xtarget)*10+$Ytarget+1));
}
if ($bunnypos<>(($Xtarget)*10+$Ytarget-1))
{
array_push($result["possibledestinations"],"fence".(($Xtarget)*10+$Ytarget-1));
}
}
}
break;
break;
case "5": // POTATO Exchange one card from your hand with one on the table (the groundhog blocks one card)
for ($x=0 ; $x<= 3 ;$x++)
{
for ($y=0 ; $y<= 3; $y++)
{
if ( $groundhog_pos != ($x*10 + $y ))
{
array_push($result["possibledestinations"],"field".($x*10+$y));
}
}
}
break;
case "6": //TOMATO Discard a veggie from the garden and replace it with one from the harvest (the groundhog blocks)
$sql = "select card_id from cards where card_location='table' and card_id<>".$card['id'];
$cardsintable=self::getCollectionFromDb( $sql );
foreach( $cardsintable as $thiscard )
{
array_push($result["possibledestinations"],"card_".$thiscard['card_id']);
}
break;
}
// return values:
return $result ;
}
function argPossibleTargets()
{
$player_id = self::getActivePlayerId();
$cardpicked=self::getGameStateValue( 'card_picked');
$groundhog_pos=self::getGameStateValue( 'groundhog_pos');
$Ygroundhog_pos= $groundhog_pos % 10 ;
$Xgroundhog_pos= ($groundhog_pos - $groundhog_pos % 10) / 10;
$card=$this->cards->getCard( $cardpicked );
$result= array( 'possiblemoves' => array() );
$result['cardpicked']= $card['type'] ;
switch ($card['type'])
{
case "1": // CARROTS Move BUNNY
$sql = "select card_location_arg from tokens where card_type=0";
$bunnypos=self::getUniqueValueFromDB( $sql );
array_push($result["possiblemoves"],"fence".$bunnypos);
break;
case "2": //CABBAGE Shift any column or row of cards or fence (groundhog blocks row and column)
for ($x=0 ; $x<= 3 ; $x++)
{
for ($y=0 ; $y<= 3 ;$y++)
{
if (( $Xgroundhog_pos != $x ) and ( $Ygroundhog_pos != $y ))
{
array_push($result["possiblemoves"],"field".($x*10+$y));
}
}
}
for ($x=0 ; $x<=3 ; $x++)
{
array_push($result["possiblemoves"],"fence".($x*10 ) );
array_push($result["possiblemoves"],"fence".($x*10+3) );
}
for ($y=1 ; $y<=2 ; $y++)
{
array_push($result["possiblemoves"],"fence".($y) );
array_push($result["possiblemoves"],"fence".(30+$y) );
}
break;
case "3": //PEAS move the groundhog to other compost, select one card and it shifts position over the groundhog
for ($x=1 ; $x<= 2; $x++)
{
for ($y=1 ; $y<= 2 ;$y++)
{
if ( $groundhog_pos != ($x*10 + $y ))
{
array_push($result["possiblemoves"],"field".($x*10+$y));
}
}
}
break;
case "4": //PEPPERS Swaps two cards (the groundhog blocks one card)
for ($x=0 ; $x<= 3 ;$x++)
{
for ($y=0 ; $y<= 3; $y++)
{
if ( $groundhog_pos != ($x*10 + $y ))
{
array_push($result["possiblemoves"],"field".($x*10+$y));
}
}
}
$sql = "select card_location_arg pos from tokens where card_type<>0";
$notbunnypos=self::getCollectionFromDb( $sql );
foreach( $notbunnypos as $thispos )
{
array_push($result["possiblemoves"],"fence".$thispos['pos']);
}
break;
break;
case "5": // POTATO Exchange one card from your hand with one on the table (the groundhog blocks one card)
$hand = $this->cards->getCardsInLocation( 'hand', $player_id );
foreach( $hand as $thiscard => $hand )
{
array_push($result["possiblemoves"],"card_".$thiscard);
}