-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragonkeeper.game.php
1129 lines (946 loc) · 46.1 KB
/
dragonkeeper.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]>
* dragonkeeper 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.
* -----
*
* dragonkeeper.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 dragonkeeper 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(
"stairs_used" => 10,
"level" => 11,
"drakepos" => 12,
"cardpicked" => 13,
"cardtypepicked" => 14,
"cardpower" => 15 ,
"playerpicked" => 16
// ...
// "my_first_game_variant" => 100,
// "my_second_game_variant" => 101,
// ...
) );
$this->cards = self::getNew( "module.common.deck" );
$this->cards->init( "cards" );
}
protected function getGameName( )
{
// Used for translations and stuff. Please do not modify.
return "dragonkeeper";
}
/*
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, player_gold , player_guild ) VALUES ";
$values = array();
$guilds=array(1,2,3,4);
shuffle($guilds);
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'] )."','".addslashes( 4 )."','". array_pop($guilds)."')";
}
$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( 'level', 3 );
self::setGameStateInitialValue( 'stairs_used', 0 );
self::setGameStateInitialValue( 'drakepos', 22 );
self::setGameStateInitialValue( 'playerpicked', 0 );
// Init game statistics
// (note: statistics used in this file must be defined in your stats.inc.php file)
self::initStat( 'table', 'turns_number', 1 ); // Init a table statistics
//self::initStat( 'player', 'player_teststat1', 0 ); // Init a player statistics (for all players)
// TODO: setup the initial game situation here
$cards = array();
$stairs = array();
foreach( $this->card_types as $cardType)
{
if ($cardType['type_id'] == 1)
{
$card = array( 'type' => $cardType["type_id"], 'type_arg' => $cardType["value"] , 'nbr' => $cardType["amount"]);
array_push($stairs, $card);
}
else
{
$card = array( 'type' => $cardType["type_id"], 'type_arg' => $cardType["value"] , 'nbr' => $cardType["amount"]);
array_push($cards, $card);
}
}
$this->cards->createCards( $cards, 'deck' );
$this->cards->createCards( $stairs, 'stairs' );
$this->cards->shuffle( 'deck' );
for ($j=0 ; $j<=4 ; $j++ )
{
for ($i=0 ; $i<=4 ; $i++ )
{
$this->cards->pickCardForLocation( "deck", "table1" , $j*10+$i );
}
}
for ($j=0 ; $j<=4 ; $j++ )
{
for ($i=0 ; $i<=4 ; $i++ )
{
if (($j*10+$i)== 22 )
{
$this->cards->pickCardForLocation( "stairs", "table2" , $j*10+$i );
}
else
{
$this->cards->pickCardForLocation( "deck", "table2" , $j*10+$i );
}
}
}
for ($j=0 ; $j<=4 ; $j++ )
{
for ($i=0 ; $i<=4 ; $i++ )
{
if (($j*10+$i)== 22 )
{
$this->cards->pickCardForLocation( "stairs", "table3" , $j*10+$i );
}
else
{
$this->cards->pickCardForLocation( "deck", "table3" , $j*10+$i );
}
}
}
// 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();
$current_player_id = self::getCurrentPlayerId(); // !! We must only return informations visible by this player !!
$sql = "SELECT player_guild from player WHERE player_id=". $current_player_id ;
$result['player_guild'] = self::getUniqueValueFromDb( $sql );
// Get information about players
// Note: you can retrieve some extra field you added for "player" table in "dbmodel.sql" if you need it.
$sql = "SELECT player_id id, player_score score , player_gold gold FROM player ";
$result['players'] = self::getCollectionFromDb( $sql );
$result['level'] = self::getGameStateValue('level');
$result['drakepos'] = self::getGameStateValue('drakepos');
$sql = "SELECT card_id id, card_location_arg location_arg, card_type type , card_type_arg type_arg , card_location location from cards WHERE card_location like 'table%' ORDER BY card_location_arg DESC";
$result['table'] = self::getCollectionFromDb( $sql );
$players = self::loadPlayersBasicInfos();
$sql = "SELECT card_id id, card_location_arg location_arg, card_type type , card_type_arg type_arg , card_location location from cards WHERE card_location like 'store%' ORDER BY card_location_arg DESC";
$result['playercards'] = 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()
{
// TODO: compute and return the game progression
$sql = "SELECT count(card_id) from cards WHERE card_location like 'table%' ";
$result = 100 - ( self::getUniqueValueFromDb( $sql ) * 100 ) / 75 ;
return $result;
}
//////////////////////////////////////////////////////////////////////////////
//////////// Utility functions
////////////
/*
In this space, you can put any utility methods useful for your game logic
*/
function getCardcolor ($cardType)
{
return ($this->card_types[$cardType]['color']);
}
function getCardvalue ($cardType)
{
return ($this->card_types[$cardType]['value']);
}
function getActivePlayers()
{
$playersIds = array();
$sql = "SELECT player_id id, player_name playerName , player_color playerColor , player_gold gold , player_guild guild FROM player WHERE player_eliminated=0";
//$playersIds = self::getObjectListFromDB( $sql );
$playersIds = self::getCollectionFromDB( $sql );
return $playersIds;
}
//////////////////////////////////////////////////////////////////////////////
//////////// 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 dragonkeeper.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} plays ${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 );
$thiscardtype=$thiscard['type'];
$thiscardcolor= $this->getCardcolor( $thiscardtype );
//clienttranslate( '${player_name} moves the dragon' )
self::notifyAllPlayers( "movedrake", "" , array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'drakepos' => 'table'.self::getGameStateValue('level').'field'.$thiscard['location_arg']
) );
self::setGameStateValue('drakepos', $thiscard['location_arg'] );
$this->cards->insertCardOnExtremePosition( $card_id , "store_".$player_id."_".$thiscardcolor , true );
self::setGameStateValue("cardpicked", $card_id );
self::setGameStateValue("playerpicked", $player_id );
self::setGameStateValue("cardtypepicked", $thiscardtype );
self::setGameStateValue("cardpower", $this->card_types[ $thiscardtype]['power'] );
self::notifyAllPlayers( "movecard", clienttranslate( '${player_name} takes a tile' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $card_id,
'destination' => "store_".$player_id."_".$thiscardcolor
) );
if ( self::getGameStateValue("cardpower") == 1 )
{
self::setGameStateValue("stairs_used", $player_id );
}
if ( ( self::getGameStateValue("cardpower") == 4 ) OR ( self::getGameStateValue("cardpower") == 5 ) )
{
$this->gamestate->nextState( "activatePower" );
}
else
{
$this->gamestate->nextState( "pickCard" );
}
}
function pickcardPower( $card_id)
{
self::checkAction( 'pickcardPower' ); // 1 => clienttranslate("Stairs" ),
$player_id = self::getActivePlayerId();
$thiscard= $this->cards->getCard( $card_id ); // 3 => clienttranslate("Freedom" ),
$thiscardtype=$thiscard['type'];
$thiscardcolor= $this->getCardcolor( $thiscardtype ); // * 5 => clienttranslate("Remote Trap" ),
$cardPower=self::getGameStateValue("cardpower");
switch ($cardPower) {
case 2: // * 2 => ("Secret Path" ),
$oldcard=self::getGameStateValue("cardpicked");
self::notifyAllPlayers( "movedrake", clienttranslate( '${player_name} moves the dragon using the Secret Path power' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'drakepos' => 'table'.self::getGameStateValue('level').'field'.$thiscard['location_arg']
) );
self::notifyAllPlayers( "discard", clienttranslate( '${player_name} discards the Secret Path tile' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $oldcard
) );
$this->cards->insertCardOnExtremePosition( $oldcard , "discard" , true );
self::setGameStateValue('drakepos', $thiscard['location_arg'] );
$this->cards->insertCardOnExtremePosition( $card_id , "store_".$player_id."_".$thiscardcolor , true );
self::notifyAllPlayers( "movecard", clienttranslate( '${player_name} takes a tile' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $card_id,
'destination' => "store_".$player_id."_".$thiscardcolor
) );
if ( $this->card_types[$thiscardtype]['power'] == 1 )
{
self::setGameStateValue("stairs_used", $player_id );
}
break;
case 4: // * 4 => clienttranslate("Exchange" ),
$targetplayer= explode('_', $thiscard['location'])[1];
$oldcard= self::getGameStateValue("cardpicked");
$oldcardcolor=$this->getCardcolor( self::getGameStateValue('cardtypepicked') );
self::notifyAllPlayers( "movecard", clienttranslate( '${player_name} gives the Prisoners Exchange tile to the other player' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $oldcard,
'destination' => "store_".$targetplayer."_".$oldcardcolor
) );
$this->cards->insertCardOnExtremePosition( $oldcard , "store_".$targetplayer."_".$oldcardcolor , true );
$this->cards->insertCardOnExtremePosition( $card_id , "store_".$player_id."_".$thiscardcolor , true );
self::notifyAllPlayers( "movecard", clienttranslate( '${player_name} takes a tile in exchange' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $card_id,
'destination' => "store_".$player_id."_".$thiscardcolor
) );
if ( $this->card_types[$thiscardtype]['power'] == 1 )
{
self::setGameStateValue("stairs_used", $player_id );
}
break;
case 5: // * 2 => ("Remote Trap" ),
$oldcard= self::getGameStateValue("cardpicked");
self::notifyAllPlayers( "discard", clienttranslate( '${player_name} discards the Remote Trap tile' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $oldcard
) );
$this->cards->insertCardOnExtremePosition( $oldcard , "discard" , true );
$this->cards->insertCardOnExtremePosition( $card_id , "store_".$player_id."_".$thiscardcolor , true );
self::notifyAllPlayers( "movecard", clienttranslate( '${player_name} takes a tile' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'card_id' => $card_id,
'destination' => "store_".$player_id."_".$thiscardcolor
) );
if ( $this->card_types[$thiscardtype]['power'] == 1 )
{
self::setGameStateValue("stairs_used", $player_id );
}
break;
}
if ( (self::getGameStateValue("cardpower") == 4) OR (self::getGameStateValue("cardpower") == 5) )
{
$this->gamestate->nextState( "playerDonate" );
}
else
{
$this->gamestate->nextState( "pickCard" );
}
}
function donateCard( $card_id)
{
self::checkAction( 'donateCard' );
$player_id = self::getActivePlayerId();
$nextplayer_id = $player_id;
do {
$nextplayer_id=self::getPlayerBefore( $nextplayer_id );
}while ( (self::getUniqueValueFromDb( "SELECT player_eliminated from player WHERE player_id=". $nextplayer_id ) == 1 ) );
$thiscard= $this->cards->getCard( $card_id );
$thiscardtype=$thiscard['type'];
$thiscardcolor= $this->getCardcolor( $thiscardtype );
$nextplayer_name=self::getUniqueValueFromDB( "SELECT player_name FROM player where player_id=".$nextplayer_id );
//clienttranslate( '${player_name} moves the dragon' )
self::notifyAllPlayers( "movedrake", "" , array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'drakepos' => 'table'.self::getGameStateValue('level').'field'.$thiscard['location_arg']
) );
self::setGamestateValue('drakepos', $thiscard['location_arg'] );
$this->cards->insertCardOnExtremePosition( $card_id , "store_".$nextplayer_id."_".$thiscardcolor , true );
self::notifyAllPlayers( "movecard", clienttranslate( '${player_name} gives a tile to ${nextplayer_name}' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'nextplayer_name' => $nextplayer_name,
'card_id' => $card_id,
'destination' => "store_".$nextplayer_id."_".$thiscardcolor
) );
if ( $this->card_types[$thiscardtype]['value'] >2 )
{
self::DbQuery( "UPDATE player set player_gold = player_gold + 1 WHERE Player_id = $player_id" );
self::notifyAllPlayers( "playergetgold", clienttranslate( '${player_name} gets ${amount} <div class="goldlog"></div> from the treasure as the value of the tile donated is 3 or more' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'amount' => 1 ,
'source' => "counter"
) );
}
if ( $this->card_types[$thiscardtype]['power'] == 1 )
{
self::setGameStateValue("stairs_used", $nextplayer_id );
}
if ( (self::getGameStateValue("cardpower") == 2) )
{
$this->gamestate->nextState( "activatePower" );
}
else
{
$this->gamestate->nextState( "nextPlayer" );
}
}
function playPower()
{
self::checkAction( 'playPower' );
$player_id = self::getActivePlayerId();
$cardPower=self::getGameStateValue("cardpower");
self::notifyAllPlayers( "playerplaypower", clienttranslate( '${player_name} chooses to play ${powername} <div class="power${power}"></div>' ), array(
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'power' =>$cardPower,
'powername' => $this->cardpowers[$cardPower]
) );
$this->gamestate->nextState( "playPower" );
}
function pass()
{
self::checkAction( 'pass' );
$player_id = self::getActivePlayerId();
if ( ( self::getGameStateValue("cardpower") == 4 ) OR ( self::getGameStateValue("cardpower") == 5 ) )
{
$this->gamestate->nextState( "playerDonate" );
}
else
{
$this->gamestate->nextState( "pass" );
}
}
//////////////////////////////////////////////////////////////////////////////
//////////// 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 argMyGameState()
{
// Get some values from the current game situation in database...
// return values:
return array(
'variable1' => $value1,
'variable2' => $value2,
...
);
}
*/
function argPossiblePicks()
{
$player_id = self::getActivePlayerId();
$level=self::getGameStateValue( 'level');
$drake_pos=self::getGameStateValue( 'drakepos');
$Xdrakepos= $drake_pos % 10 ;
$Ydrakepos= ($drake_pos - $drake_pos % 10) / 10;
$result= array( 'possibledestinations' => array() );
$sql="SELECT concat('card_',card_id) id from cards where ( card_location like 'table".$level."' ) and ( ". $Ydrakepos ." = floor( card_location_arg / 10 ))";
$result["possibledestinations"] = self::getObjectListFromDB( $sql );
/*if (sizeof($result["possibledestinations"]) < 1)
{
$this->gamestate->nextState( "nextLevel" );
}*/
return $result ;
}
function argPossibleDonations()
{
$player_id = self::getActivePlayerId();
$level=self::getGameStateValue( 'level');
$drake_pos=self::getGameStateValue( 'drakepos');
$Xdrakepos= $drake_pos % 10 ;
$Ydrakepos= ($drake_pos - $drake_pos % 10) / 10;
$result= array( 'possibledestinations' => array() );
$sql="SELECT concat('card_',card_id) id from cards where ( card_location like 'table".$level."' ) and ( ". $Xdrakepos ." = mod( card_location_arg , 10 ))";
$result["possibledestinations"] = self::getObjectListFromDB( $sql );
/* if (sizeof($result["possibledestinations"]) < 1)
{
$this->gamestate->nextState( "nextLevel" );
}*/
return $result ;
}
function argPossibleTargets()
{
$player_id = self::getActivePlayerId();
$level=self::getGameStateValue( 'level');
$drake_pos=self::getGameStateValue( 'drakepos');
$Xdrakepos= $drake_pos % 10 ;
$Ydrakepos= ($drake_pos - $drake_pos % 10) / 10; // 1 => clienttranslate("Stairs" ),
$result= array( 'possibledestinations' => array() ); // * 2 => clienttranslate("Secret Path" ),
switch ( self::getGameStateValue("cardpower") ) // 3 => clienttranslate("Freedom" ),
{ // * 4 => clienttranslate("Exchange" ),
case "2": // * 5 => clienttranslate("Remote Trap" ),
$sql="SELECT concat('card_',card_id) id from cards where ( card_location like 'table".$level."' ) and ( ". $Ydrakepos ." = floor( card_location_arg / 10 ))";
$result["possibledestinations"] = self::getObjectListFromDB( $sql );
break;
case "4":
$sql="SELECT concat('card_',card_id) id from cards where (card_location , card_location_arg) in ( SELECT card_location,max(card_location_arg) FROM cards WHERE card_location like 'store_%' and not (card_location LIKE 'store_".$player_id."_%' ) group by card_location)";
$result["possibledestinations"] = self::getObjectListFromDB( $sql );
break;
case "5":
$sql="SELECT concat('card_',card_id) id from cards where card_location like 'table".$level."' ";
$result["possibledestinations"] = self::getObjectListFromDB( $sql );
break;
}
/*if (sizeof($result["possibledestinations"]) < 1)
{
$this->gamestate->nextState( "nextLevel" );
}*/
return $result ;
}
function argPowertype()
{
$result=array ( 'cardpower' => self::getGameStateValue( 'cardpower'));
return $result ;
}
//////////////////////////////////////////////////////////////////////////////
//////////// Game state actions
////////////
/*
Here, you can create methods defined as "game state actions" (see "action" property in states.inc.php).
The action method of state X is called everytime the current game state is set to X.
*/
/*
Example for game state "MyGameState":
*/
function stNextPlayer()
{
// Do some stuff ...
self::setGameStateValue("cardpower",0);
self::setGameStateValue("cardpicked",0);
self::setGameStateValue("cardtypepicked",0);
// (very often) go to another gamestate
$this->activeNextPlayer();
$player_id = self::getActivePlayerId();
self::giveExtraTime($player_id);
self::incStat( 1 ,"turns_number" );
self::incStat( 1 ,"turns_number", $player_id );
$this->gamestate->nextState( "nextPlayer" );
}
function stNextLevel()
{
self::setGameStateValue("cardpower",0);
self::setGameStateValue("cardpicked",0);
self::setGameStateValue("cardtypepicked",0);
$lastplayer= self::getGameStateValue( 'playerpicked' );
$level=self::getGameStateValue( 'level' );
$sql = "SELECT count(card_id) from cards WHERE card_location like 'table".$level."' ";
$cardCount = self::getUniqueValueFromDb ($sql);
$stairsPlayer=self::getGameStateValue( 'stairs_used' );
self::setGameStateValue("stairs_used",0);
$players = self::loadPlayersBasicInfos();
if ($cardCount > 8 ){
$tribute=3;
} elseif ($cardCount > 5 ){
$tribute=2;
} elseif ($cardCount > 0 ){
$tribute=1;
} else {
$tribute=0;
}
$activePlayers=$this->getActivePlayers();
foreach($activePlayers as $playerId => $player )
{
$thisid = $player['id'] ;
$thisPlayerName = $players[$thisid]['player_name'];
$gold=$player['gold'];
if (( $lastplayer == $thisid ) )
{
$gold=$gold - 1 ;
self::notifyAllPlayers( "playerpaysgold", clienttranslate( '${player_name} pays ${amount} <div class="goldlog"></div> for taking the last tile of the level ' ), array(
'player_id' => $thisid,
'player_name' => $thisPlayerName,
'amount' => 1
) );
}
if ( $stairsPlayer == $thisid )
{
if ( self::getActivePlayerId() != $thisid )
{
$this->gamestate->changeActivePlayer( $thisid );
}
self::notifyAllPlayers( "message", clienttranslate( '${player_name} is now the first player as this player has the <div class="stairs"></div> tile from previous level' ), array(
'player_name' => $thisPlayerName
) );
}
if ($tribute > 0)
{
$gold=$gold - $tribute ;
self::notifyAllPlayers( "playerpaysgold", clienttranslate( '${player_name} pays ${amount} <div class="goldlog"></div> as tribute as there were ${cardcount} tiles not picked in this level' ), array(
'player_id' => $thisid,
'player_name' => $thisPlayerName,
'amount' => $tribute ,
'cardcount' => $cardCount
) );
}
self::DbQuery( "UPDATE player set player_gold = ".$gold." WHERE Player_id = $thisid" );
if ($gold < 0 )
{
if (self::getActivePlayerId() == $thisid) {
$this->activeNextPlayer();
}
if ( sizeof($this->getActivePlayers()) > 1 ) {
self::eliminatePlayer( $thisid );
}
else
{
self::DbQuery( "UPDATE player set player_eliminated = 1 WHERE Player_id = $thisid" );
}
self::notifyAllPlayers( "message", clienttranslate( '${player_name} <b>is now eliminated from the game as this player cannot pay the tribute!</b>' ), array(
'player_name' => $thisPlayerName
) );
}
}
if (( $level > 1 ) AND ( sizeof($this->getActivePlayers()) >1 ) )
{
self::setGameStateValue( 'drakepos', 22 );
self::DbQuery( "UPDATE cards set card_location = 'discard' WHERE card_location = 'table". $level ."'" );
self::notifyAllPlayers( "levelchange", clienttranslate( '<b> We move to the next level of the dungeon! </b>' ), array(
'level' => $level,
'drakepos' => 'table'. ( $level - 1 ) .'field'. 22
) );
$level=$level-1;
self::setGameStateValue( 'level', $level );
$this->gamestate->nextState( "nextPlayer" );
}
else
{
$this->gamestate->nextState( "endGameScoring" );
}
}
function stCheckpick()
{
// Do some stuff ...
$result=$this->argPossiblePicks();
//var_dump ( sizeof($result["possibledestinations"]) );
if (sizeof($result["possibledestinations"]) < 1)
{
$this->gamestate->nextState( "nextLevel" );
}
}
function stCheckdonate()
{
// Do some stuff ...
$result=$this->argPossibleDonations();
if (sizeof($result["possibledestinations"]) == 0)
{
$this->gamestate->nextState( "nextLevel" );
}
}
function stCheckpower()
{
// Do some stuff ...
$result=$this->argPossibleTargets();
if (sizeof($result["possibledestinations"]) == 0)
{
if ( (self::getGameStateValue("cardpower") == 4) OR (self::getGameStateValue("cardpower") == 5) )
{
$this->gamestate->nextState( "playerDonate" );
}
else
{
$this->gamestate->nextState( "pickCard" );
}
}
}
function displayScores()
{
$players = self::loadPlayersBasicInfos();
$table[] = array();
//left hand col
$table[0][0] = array( 'str' => 'Players:', 'args' => array(), 'type' => 'header');
$table[0][1] = array( 'str' => "<div class='header1'></div>" .clienttranslate($this->cardcolors[1]), 'args' => array(), 'type' => 'header');
$table[0][2] = array( 'str' => "<div class='header3'></div>" .clienttranslate($this->cardcolors[3]), 'args' => array(), 'type' => 'header');
$table[0][3] = array( 'str' => "<div class='header0'></div>" .clienttranslate($this->cardcolors[0]), 'args' => array(), 'type' => 'header');
$table[0][4] = array( 'str' => "<div class='header2'></div>" .clienttranslate($this->cardcolors[2]), 'args' => array(), 'type' => 'header');
$table[0][5] = array( 'str' => "<div class='header4'></div>" .clienttranslate($this->cardcolors[4]), 'args' => array(), 'type' => 'header');
$table[0][6] = array( 'str' => "<div class='coin'></div>".clienttranslate($this->resources["gold"]) , 'args' => array(), 'type' => 'header');
$table[0][] = clienttranslate($this->resources["score_window_title"]);
$i = 1 ;
foreach( $players as $player_id => $player )
{
$score=0;
$sql = "SELECT player_guild from player WHERE player_id=". $player_id ;
$player_guild = self::getUniqueValueFromDb( $sql );
$sql = "SELECT player_gold from player WHERE player_id=". $player_id ;
$player_gold = self::getUniqueValueFromDb( $sql );
$sql = "SELECT max(player_gold) from player WHERE player_eliminated=0 " ;
$max_player_gold = self::getUniqueValueFromDb( $sql );
$thisplayername=$player['player_name'];
$score_gold = 0 ;
if ( $player_gold == $max_player_gold )
{
$score_gold = 2 ;
}
$table[$i][] = array( 'str' => '${player_name} <div class="guildtile guild${player_guild}"></div>',
'args' => array( 'player_name' => $player['player_name'],
'player_guild' => $player_guild ),
'type' => 'header'
);
$cards_picked = array( );
$cards_picked[0]=self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'_0") ');
$cards_picked[1]=self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'_1") ');
$cards_picked[2]=self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'_2") ');
$cards_picked[3]=self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'_3") ');
$cards_picked[4]=self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'_4") ');
$biggest_stack= max( $cards_picked );
$r = array();
foreach ($cards_picked as $key => $value ) {
if ($cards_picked[$key] == $biggest_stack ) {
$r[] = $key;
}
}
$cards_values = array();
$cards_values[0]=self::getUniqueValueFromDB('SELECT coalesce( sum( card_type_arg ),0) FROM cards WHERE ( card_location like "store_'.$player_id.'_0") ');
$cards_values[1]=self::getUniqueValueFromDB('SELECT coalesce( sum( card_type_arg ),0) FROM cards WHERE ( card_location like "store_'.$player_id.'_1") ');
$cards_values[2]=self::getUniqueValueFromDB('SELECT coalesce( sum( card_type_arg ),0) FROM cards WHERE ( card_location like "store_'.$player_id.'_2") ');
$cards_values[3]=self::getUniqueValueFromDB('SELECT coalesce( sum( card_type_arg ),0) FROM cards WHERE ( card_location like "store_'.$player_id.'_3") ');
$cards_values[4]=self::getUniqueValueFromDB('SELECT coalesce( sum( card_type_arg ),0) FROM cards WHERE ( card_location like "store_'.$player_id.'_4") ');
self::setStat( $cards_picked[0], 'red_picked' , $player['player_id'] );
self::setStat( $cards_picked[1], 'blue_picked' , $player['player_id'] );
self::setStat( $cards_picked[2], 'yellow_picked', $player['player_id'] );
self::setStat( $cards_picked[3], 'green_picked' , $player['player_id'] );
self::setStat( $cards_picked[4], 'purple_picked', $player['player_id'] );
self::setStat( $player_guild , 'player_guild' , $player['player_id'] );
self::setStat( $player_gold , 'player_gold' , $player['player_id'] );
$biggest_stack= max( $cards_picked );
$r = array();
foreach ($cards_picked as $key => $value ) { //What are the biggest stacks now?
if ($cards_picked[$key] == $biggest_stack ) {
$r[] = $key;
}
}
if (in_array( $player_guild, $r)) /// Is the player guild the biggest stack?
{
$cards_picked[$player_guild]=0; // Remove the player guild cards
self::notifyAllPlayers( "discardstack" , clienttranslate( '${player_name} discards the highest stack <div class="header${color}"></div> and it matches this players guild : <div class="guildtile guild${color}"></div> ' ),
array(
'player_name' => $thisplayername,
'color' => $player_guild,
'store_id' => 'store_'.$player_id.'_'.$player_guild
) ) ;
$cards_values[$player_guild]=9999 ;
$biggest_stack= max( $cards_picked );
$r = array();
foreach ($cards_picked as $key => $value ) { //What are the biggest stacks now?
if ($cards_picked[$key] == $biggest_stack ) {
$r[] = $key;
}
}
$min_value = 9999;
for ( $j=0 ; $j < count($r); $j++ ) // What has the smaller value?
{
if ( $cards_values[$r[$j]] < $min_value) {
$min_value_stack = $r[$j];
$min_value = $cards_values[$r[$j]];
}
}
$cards_values[$player_guild]=0 ;
$cards_values[$min_value_stack]=0 ;
self::notifyAllPlayers( "discardstack" , clienttranslate( '${player_name} discards also a second stack: <div class="header${color}"></div>' ),
array(
'player_name' => $thisplayername,
'color' => $min_value_stack,
'store_id' => 'store_'.$player_id.'_'.$min_value_stack
) ) ;
self::setStat( $cards_values[0], 'red_score' , $player['player_id'] );
self::setStat( $cards_values[1], 'blue_score' , $player['player_id'] );
self::setStat( $cards_values[2], 'yellow_score', $player['player_id'] );
self::setStat( $cards_values[3], 'green_score' , $player['player_id'] );
self::setStat( $cards_values[4], 'purple_score', $player['player_id'] );
self::setStat( $score_gold , 'score_gold' , $player['player_id'] );
}
else
{
$min_value = 9999;
for ( $j=0 ; $j < count($r); $j++ ) // What has the smaller value?
{
// self::dump( ' *********** $cards_values[$r[$j]', $cards_values[$r[$j]] );
if ( $cards_values[$r[$j]] <= $min_value) {
$min_value_stack = $r[$j];
$min_value = $cards_values[$r[$j]];
}
}
self::notifyAllPlayers( "discardstack" , clienttranslate( '${player_name} discards the highest stack: <div class="header${color}"></div>' ),
array(
'player_name' => $thisplayername,
'color' => $min_value_stack,
'store_id' => 'store_'.$player_id.'_'.$min_value_stack
) ) ;
self::DbQuery( "UPDATE cards set card_location = 'discard' WHERE card_location like 'store_".$player_id."_".$min_value_stack."'");
// 10 19 28 37 RELEASE CARDS TYPES
// Release my own guild cards if beneficial
$Release_cards=self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'_%") AND (card_type in ( 10 ,19 , 28, 37))');
for ($j=0 ; $j < $Release_cards ; $j++ )
{
if ( self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'_'.$player_guild.'") AND (card_type_arg > 2 )') >= 1 )
{
$oldcard = self::getUniqueValueFromDB('SELECT card_id FROM cards WHERE ( card_location like "store_'.$player_id.'_'.$player_guild.'") AND (card_type_arg > 2 ) ORDER by card_type_arg DESC , card_id LIMIT 1 ') ;
self::notifyAllPlayers( "discard", clienttranslate( '${player_name} uses a Release power tile <div class="power3"></div> the highest valued tile from this player guild does not score negatively' ), array(
'player_id' => $player_id,
'player_name' => $thisplayername,
'card_id' => $oldcard
) );
$oldcard = self::getUniqueValueFromDB('SELECT card_id FROM cards WHERE ( card_location like "store_'.$player_id.'_%") AND (card_type in ( 10 ,19 , 28, 37)) ORDER by card_id LIMIT 1 ' ) ;
self::notifyAllPlayers( "discard", clienttranslate( '${player_name} discards a Release power tile <div class="power3"></div> ' ), array(
'player_id' => $player_id,
'player_name' => $thisplayername,
'card_id' => $oldcard
) );
self::DbQuery( "UPDATE cards set card_location = 'discard' WHERE ( card_location like 'store_".$player_id."_%') AND (card_type in ( 10 ,19 , 28, 37)) ORDER by card_id LIMIT 1");
self::DbQuery( "UPDATE cards set card_location = 'discard' WHERE ( card_location like 'store_".$player_id."_".$player_guild."' ) AND (card_type_arg > 2 ) ORDER by card_type_arg DESC , card_id LIMIT 1");
}
}
// Release other cards using my own guild color relase
$Release_cards=self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'_'.$player_guild.'") AND (card_type in ( 10 ,19 , 28, 37)) ');
for ($j=0 ; $j < $Release_cards ; $j++ )
{
if ( self::getUniqueValueFromDB('SELECT count(*) FROM cards WHERE ( card_location like "store_'.$player_id.'%") AND (card_type_arg < 2 )') >= 1 )
{
$oldcard = self::getUniqueValueFromDB('SELECT card_id FROM cards WHERE ( card_location like "store_'.$player_id.'_%" ) AND (card_type_arg < 2 ) ORDER by card_type_arg ASC , card_id LIMIT 1') ;
self::notifyAllPlayers( "discard", clienttranslate( '${player_name} uses a Release power tile <div class="power3"></div> of theirs own guild to release a low value card of other color' ), array(
'player_id' => $player_id,
'player_name' => $thisplayername,
'card_id' => $oldcard
) );
$oldcard = self::getUniqueValueFromDB('SELECT card_id FROM cards WHERE ( card_location like "store_'.$player_id.'_'.$player_guild.'") AND (card_type in ( 10 ,19 , 28, 37)) ORDER by card_id LIMIT 1' ) ;
self::notifyAllPlayers( "discard", clienttranslate( '${player_name} discards a Release power tile <div class="power3"></div> ' ), array(
'player_id' => $player_id,
'player_name' => $thisplayername,
'card_id' => $oldcard
) );
self::DbQuery( "UPDATE cards set card_location = 'discard' WHERE ( card_location like 'store_".$player_id."_".$player_guild."') AND (card_type in ( 10 ,19 , 28, 37)) ORDER by card_id LIMIT 1");
self::DbQuery( "UPDATE cards set card_location = 'discard' WHERE ( card_location like 'store_".$player_id."%' ) AND (card_type_arg < 2 ) ORDER by card_type_arg ASC , card_id LIMIT 1");
}