-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpapayoo.game.php
579 lines (477 loc) · 18.9 KB
/
papayoo.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
<?php
/**
*------
* BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
* papayoo implementation : © Guillaume NAVEL <guillaume.navel@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.
* -----
*
* papayoo.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' );
require_once("modules/constants.inc.php");
class papayoo extends Table
{
function __construct( )
{
parent::__construct();
self::initGameStateLabels([
"trickColor" => 10, // Color of the first card play
"remaining_to_play" => 11, // Number of remaining cards to play in that deal
"dealer_id" => 13, // Current dealer, dealer play first
"dice_color" => 14, // Current color of the dice
"number_of_deals" => 15, // Number of deal in the game
"nbr_deals_mode" => 100, // Number of deals mode
"shifting_discards" => 101, // Shifting discards variant
]);
// Init deck of cards
$this->cards = self::getNew( "module.common.deck" );
$this->cards->init( "card" );
}
protected function getGameName( )
{
return "papayoo";
}
/*
* setupNewGame:
*/
protected function setupNewGame( $players, $options = array() )
{
/************ Player init *****/
self::DbQuery("DELETE FROM player WHERE 1");
$gameinfos = self::getGameinfos();
$default_colors = $gameinfos['player_colors'];
$nbr_players = count($players);
$game_config = $this->game_configs[$nbr_players];
$sql = "INSERT INTO player (player_no, player_id, player_score, player_color, player_canal, player_name, player_avatar) VALUES ";
$values = [];
$default_score = 0;
$player_no = 1;
foreach(array_rand($players, $nbr_players) as $player_id){
$player = $players[$player_id];
$color = array_shift( $default_colors );
$values[] = "('$player_no','$player_id','$default_score','$color','".$player['player_canal']."','".addslashes( $player['player_name'] )."','".addslashes( $player['player_avatar'] )."')";
$player_no++;
}
self::DbQuery( $sql . implode( $values, ',' ));
self::reattributeColorsBasedOnPreferences($players, $gameinfos['player_colors']);
self::reloadPlayersBasicInfos();
/************ Start the game initialization *****/
/*
* Color of the first card played :
* 0 -> no card play
* 1 -> club
* 2 -> spade
* 3 -> heart
* 4 -> diamond
* 5 -> payoo
*/
self::setGameStateInitialValue( 'trickColor', 0 );
// Number of remaining cards to play in that deal
self::setGameStateInitialValue( 'remaining_to_play', 0 );
// Current dealer, dealer play first
self::setGameStateInitialValue( 'dealer_id', 0 );
// Init number of deals
$hands_option = self::getGameStateValue('nbr_deals_mode');
$nbrHands = 0;
if ($hands_option == JUST_ONE) $nbrHands = 1;
else if ($hands_option == ONE_PER_PLAYER) $nbrHands = $nbr_players;
else if ($hands_option == TWO_PER_PLAYER) $nbrHands = 2*$nbr_players;
else if ($hands_option == THREE_PER_PLAYER) $nbrHands = 3*$nbr_players;
self::setGameStateInitialValue( 'number_of_deals', $nbrHands);
/*
* Current color of the dice
* 0 -> dice not throw yet
* 1 -> club
* 2 -> spade
* 3 -> heart
* 4 -> diamond
*/
self::setGameStateInitialValue( 'dice_color', 0 );
// Init game statistics
// (note: statistics are defined in your stats.inc.php file)
self::initStat( "table", "handNbr", 0 );
self::initStat( "player", "nbrOfPayoo", 0 );
self::initStat( "player", "nbrOfPapayoo", 0 );
self::initStat( "player", "nbrOfTrick", 0 );
self::initStat( "player", "nbrNoPointCards", 0 );
// Create cards
$cards = array();
foreach( $this->cards_colors as $color_id => $cards_color ){ // club, spade, heart, diamond, payoo
if ($color_id != 5) { // No payoo, remove 1 cards for 7 and 8 players
$min_value = $game_config['min_color_value'];
} else {
$min_value = 1;
}
$max_value = $cards_color['valeur_max']; // Max value depand of card color
for( $value=$min_value; $value<=$max_value; $value++ ){ // min value to max value
$cards[] = array( 'type' => $color_id, 'type_arg' => $value, 'nbr' => 1);
}
}
$this->cards->createCards( $cards, 'deck' );
// Get the first dealer and the first player
$dealer_id = self::activeNextPlayer(); // This player will be the first dealer
self::setGameStateValue('dealer_id', $dealer_id);
/************ End of the game initialization *****/
}
/*
* getAllDatas:
*/
protected function getAllDatas()
{
$result = [];
$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, player_color, player_name, nbr_of_tricks_win FROM player ";
$result['players'] = self::getCollectionFromDb( $sql );
$result['current_player_id'] = self::getCurrentPlayerId();
$result['next_players_id'] = self::createNextPlayerTable(array_keys(self::loadPlayersBasicInfos()));
// Cards configuraiton
$result['cards_colors'] = $this->cards_colors;
// Cards in player hand
$result['hand'] = $this->cards->getCardsInLocation( 'hand', $current_player_id );
// Cards played on the table
$result['cards_on_table'] = $this->cards->getCardsInLocation( 'cards_on_table' );
// Color of the dice
$result['dice_color'] = self::getGameStateValue( 'dice_color' ) ;
$result['dice_colors'] = $this->dice_colors;
// Number of cards to give
$game_config = $this->game_configs[self::getPlayersNumber()];
$result['nbr_cards_to_give'] = $game_config['nbr_cards_to_pass'];
// Current hand and total number of hands
$result['handNbr'] = self::getStat('handNbr');
$result['handTotal'] = self::getGameStateValue('number_of_deals');
return $result;
}
/*
* getGameProgression:
*/
function getGameProgression()
{
// Game progression: number of the deal
return 100 * (self::getStat('handNbr') - 1)/ self::getGameStateValue('number_of_deals');
}
//////////////////////////////////////////
//////////// Utility functions ///////////
//////////////////////////////////////////
function throwDice()
{
$dice_value = bga_rand(1, 4);
self::setGameStateValue('dice_color', $dice_value);
return $dice_value;
}
function updateScores()
{
self::notifyAllPlayers( "newScores", '', [
'scores' => self::getObjectListFromDB("SELECT player_id as id, player_score as score FROM player")
]);
}
////////////////////////////////////
////////////////////////////////////
//////////// New Hand //////////////
////////////////////////////////////
////////////////////////////////////
function stNewHand()
{
self::incStat( 1, "handNbr" );
self::setGameStateValue('dice_color', 0);
self::notifyAllPlayers('throwDice', '', ['dice_value' => 0]);
// Take back all cards (from any location => null) to deck
$this->cards->moveAllCardsInLocation(null, "deck");
$this->cards->shuffle('deck');
// Reset number of tricks win/hand points
self::DbQuery("UPDATE player SET nbr_of_tricks_win = 0, hand_points = 0 WHERE 1");
// Deal cards to players : create deck, shuffle it and give 13 initial cards
$game_config = $this->game_configs[self::getPlayersNumber()];
$players = self::loadPlayersBasicInfos();
foreach( $game_config['deal'] as $id => $nbr_card_to_deal) {
foreach( $players as $player_id => $player ) {
$this->cards->pickCards($nbr_card_to_deal, 'deck', $player_id );
}
}
// Notify player about its cards
foreach($players as $player_id => $player) {
$cards = array_values($this->cards->getCardsInLocation( 'hand', $player_id));
self::notifyPlayer( $player_id, 'newHand', '', ['cards' => $cards]);
}
self::notifyAllPlayers('startingNewHand', '', ['handNbr' => self::getStat('handNbr') ]);
// Select first player
$dealer_id = self::getGameStateValue('dealer_id');
$next_player = self::getPlayerAfter($dealer_id);
$this->gamestate->changeActivePlayer($next_player);
self::setGameStateValue('dealer_id', $next_player);
$offset = $this->getShiftingOffset();
$this->gamestate->nextState($offset === 0? 'play' : 'give');
}
function getShiftingOffset()
{
$n = self::getStat("handNbr");
$variant = self::getGameStateValue('shifting_discards');
if($variant == SHIFTING_ON)
return ($n % self::getPlayersNumber());
else
return 1;
}
///////////////////////////////
///////// Give Cards //////////
///////////////////////////////
// The players all give {n} cards to their neighbour
///////////////////////////////
function stGiveCards()
{
$this->gamestate->setAllPlayersMultiactive();
}
function argGiveCards()
{
$game_config = $this->game_configs[self::getPlayersNumber()];
return [
"nbr_cards" => $game_config['nbr_cards_to_pass'],
"n" => $this->getShiftingOffset(),
"dealer" => self::getGameStateValue('dealer_id')
];
}
function giveCards($card_ids)
{
self::checkAction( "giveCards" );
$player_id = self::getCurrentPlayerId();
$game_config = $this->game_configs[self::getPlayersNumber()];
if( count($card_ids) != $game_config['nbr_cards_to_pass'])
throw new feException(sprintf(self::_("You must give exactly %s cards"), $game_config['nbr_cards_to_pass']));
// Check if these cards are in player hands
$cards = $this->cards->getCards($card_ids);
if(count($cards) != $game_config['nbr_cards_to_pass'])
throw new feException(self::_("Some of these cards don't exist"));
foreach( $cards as $card ) {
if($card['location'] != 'hand' || $card['location_arg'] != $player_id)
throw new feException(self::_("Some of these cards are not in your hand"));
}
// To which player should I give these cards ?
$players = self::loadPlayersBasicInfos();
$nextPlayerList = self::createNextPlayerTable(array_keys($players));
$nextPlayer_id = $player_id;
for($i = 0; $i < $this->getShiftingOffset(); $i++){
$nextPlayer_id = $nextPlayerList[$nextPlayer_id];
}
if( !isset($nextPlayer_id))
throw new feException(self::_("Next player doesn't existe"));
// Allright, these cards can be given to this player
// (note: we place the cards in some temporary location in order he can't see them before the hand starts)
$this->cards->moveCards( $card_ids, "temporary", $nextPlayer_id );
// Notify the player so we can make these cards disapear
self::notifyPlayer( $player_id, "giveCards", "", array(
"cards" => $card_ids
) );
// Make this player unactive now
// (and tell the machine state to use transtion "giveCards" if all players are now unactive
$this->gamestate->setPlayerNonMultiactive( $player_id, "giveCards" );
}
// Take cards given by the other player
function stTakeCards()
{
$players = self::loadPlayersBasicInfos();
foreach( $players as $player_id => $player ){
// Each player takes cards in the "temporary" location and place it in his hand
$cards = $this->cards->getCardsInLocation( "temporary", $player_id );
$this->cards->moveAllCardsInLocation( "temporary", "hand", $player_id, $player_id );
self::notifyPlayer( $player_id, "takeCards", "", ["cards" => $cards]);
}
$this->gamestate->nextState( "throwDice" );
}
function stThrowDice()
{
$dice_value = self::throwDice();
self::notifyAllPlayers( 'throwDice', clienttranslate('The dice value is ${dice_name} ${dice_symbole}'), [
'i18n' => ['dice_name'],
'dice_value' => $dice_value,
'dice_name' => $this->dice_colors[$dice_value]['name'],
'dice_symbole' => $this->dice_colors[$dice_value]['symbole'],
]);
$this->gamestate->changeActivePlayer(self::getGameStateValue('dealer_id'));
$this->gamestate->nextState( "startHand" );
}
///////////////////////////////
///////// Start trick /////////
///////////////////////////////
function stNewTrick()
{
self::setGameStateInitialValue( 'trickColor', 0 );
$this->gamestate->nextState();
}
function argPlayCard()
{
$pId = self:: getActivePlayerId();
$hand = array_values($this->cards->getCardsInLocation('hand', $pId));
$currentTrickColor = self::getGameStateValue('trickColor');
if($currentTrickColor != 0) {
$cards = array_values(array_filter($hand, function($card) use ($currentTrickColor){
return $card['type'] == $currentTrickColor;
}));
if(count($cards) > 0)
$hand = $cards;
}
return [
'_private' => [
'active' => ['cards' => $hand]
],
'pId' => $pId,
];
}
// Play a card from player hand
function playCard($card_id)
{
self::checkAction( "playCard" );
$arg = $this->argPlayCard();
$cards = array_values(array_filter($arg['_private']['active']['cards'], function($card) use ($card_id){
return $card['id'] == $card_id;
}));
if(count($cards) != 1)
throw new feException("You cannot play this card");
$currentCard = $cards[0];
// First card of the trick, save card color
if(self::getGameStateValue('trickColor') == 0)
self::setGameStateValue('trickColor', $currentCard['type']);
$player_id = self::getActivePlayerId();
$this->cards->moveCard( $card_id, 'cards_on_table', $player_id );
self::notifyAllPlayers( 'playCard', clienttranslate('${player_name} plays ${value_displayed} ${color_displayed}'), [
'i18n' => array( 'color_displayed', 'value_displayed' ),
'card_id' => $card_id,
'player_id' => $player_id,
'player_name' => self::getActivePlayerName(),
'value' => $currentCard['type_arg'],
'value_displayed' => $currentCard['type_arg'],
'color' => $currentCard['type'],
'color_displayed' => $this->cards_colors[$currentCard['type']]['name']
]);
$this->gamestate->nextState( 'cardPlayed' );
}
function stNextPlayer()
{
$players = self::loadPlayersBasicInfos();
$nbr_players = self::getPlayersNumber();
// Active next player OR end the trick
if($this->cards->countCardInLocation('cards_on_table') == $nbr_players){
$this->gamestate->nextState('endTrick');
return;
}
// Standard case (not the end of the trick) => just active the next player
$player_id = self::activeNextPlayer();
self::giveExtraTime($player_id);
$this->gamestate->nextState( 'nextPlayer' );
}
//////////////////////////////////
///////// End trick/hand /////////
//////////////////////////////////
function stEndOfTrick()
{
$players = self::loadPlayersBasicInfos();
// This is the end of the trick => Who wins ?
$cards_on_table = $this->cards->getCardsInLocation( 'cards_on_table' );
$best_value = 0;
$best_value_player_id = null;
$currentTrickColor = self::getGameStateValue('trickColor');
foreach($cards_on_table as $card) {
if(($card['type'] != $currentTrickColor) || ($card['type_arg'] <= $best_value))
continue;
$best_value_player_id = $card['location_arg'];
$best_value = $card['type_arg'];
}
if(is_null($best_value_player_id))
throw new feException( self::_("Error, nobody wins the trick") );
// Increase stat
self::incStat(1, "nbrOfTrick", $best_value_player_id);
// Compute score of trick
$diceColor = self::getGameStateValue('dice_color');
$score = 0;
foreach($cards_on_table as $card) {
if($card['type'] == $diceColor && $card['type_arg'] == 7) {
$score -= 40; // papayoo = 40pts
self::incStat(1, "nbrOfPapayoo", $best_value_player_id);
} else if($card['type'] == 5) {
$score -= $card['type_arg']; // Payoo value = pts
self::incStat(1, "nbrOfPayoo", $best_value_player_id);
} else {
// No point cards
self::incStat(1, "nbrNoPointCards", $best_value_player_id);
}
}
self::DbQuery("UPDATE player SET player_score=player_score+$score, hand_points=hand_points+$score WHERE player_id='$best_value_player_id' ");
// Move all cards to "cards_won" of the given player
$this->cards->moveAllCardsInLocation( 'cards_on_table', 'cards_won', null, $best_value_player_id );
// Notify
// Note: we use 2 notifications here in order we can pause the display during the first notification
// before we move all cards to the winner (during the second)
self::notifyAllPlayers( 'trickWin', clienttranslate('${player_name} wins the trick'), [
'player_name' => $players[ $best_value_player_id ]['player_name'],
]);
// Increase number of tricks win
self::DbQuery("UPDATE player SET nbr_of_tricks_win=nbr_of_tricks_win+1 WHERE player_id='$best_value_player_id' ");
$players = self::getCollectionFromDb("SELECT player_id, player_score, player_color, player_name, nbr_of_tricks_win FROM player ");
self::notifyAllPlayers( 'giveAllCardsToPlayer','', [
'player_id' => $best_value_player_id,
'nbr_of_tricks' => $players[$best_value_player_id]['nbr_of_tricks_win'],
'score' => $score,
]);
$this->updateScores();
// Active this player => he's the one who starts the next trick
$this->gamestate->changeActivePlayer($best_value_player_id);
$this->gamestate->nextState( ($this->cards->countCardInLocation( 'hand' ) == 0)? "endHand" : "nextTrick");
}
function stEndHand()
{
$players = self::getObjectListFromDB("SELECT * FROM player");
foreach($players as $player){
$score = $player['hand_points'];
$msg = $score == 0? clienttranslate( '${player_name} did not get any point' ) : clienttranslate( '${player_name} wins ${points} points' );
self::notifyAllPlayers( "points", $msg, [
'player_id' => $player['player_id'],
'player_name' => $player['player_name'],
'points' => $score
]);
}
/// Display table window with results ////
// Header line
$headers = [''];
$handPoints = [ ['str' => clienttranslate('Hand points'), 'args' => [] ] ];
$totalPoints = [ ['str' => clienttranslate('Total points'), 'args' => [] ] ];
foreach($players as $player){
$headers[] = [
'str' => '${player_name}',
'args' => ['player_name' => $player['player_name']],
'type' => 'header'
];
$handPoints[] = $player['hand_points'];
$totalPoints[] = $player['player_score'];
}
$table = [$headers, $handPoints, $totalPoints];
// Test if this is the end of the game
$end = self::getStat('handNbr') >= self::getGameStateValue('number_of_deals');
$this->notifyAllPlayers( "tableWindow", '', array(
"id" => 'finalScoring',
"title" => sprintf(clienttranslate('Result of hand %d/%d'), self::getStat('handNbr'), self::getGameStateValue('number_of_deals')),
"table" => $table,
"closing" => $end? clienttranslate("End of game") : clienttranslate("Next hand")
));
$this->gamestate->nextState($end? "endGame" : "nextHand");
}
////////////////////////////////
//////////// Zombie ////////////
////////////////////////////////
function zombieTurn( $state, $active_player )
{
throw new feException( "Zombie mode not supported for Papayoo" );
}
/////////////////////////////////
////////// DB upgrade ///////////
/////////////////////////////////
function upgradeTableDb( $from_version )
{
}
}