-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbang.game.php
235 lines (217 loc) · 7.73 KB
/
bang.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
<?php
/**
*------
* BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
* Bang implementation : © <Your name here> <Your email address here>
*
* 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.
* -----
*
* Bang.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.
*
*/
$swdNamespaceAutoload = function ($class) {
$classParts = explode('\\', $class);
if ($classParts[0] == 'BANG') {
array_shift($classParts);
$file = dirname(__FILE__) . '/modules/php/' . implode(DIRECTORY_SEPARATOR, $classParts) . '.php';
if (file_exists($file)) {
require_once $file;
} else {
var_dump("Impossible to load bang class : $class");
}
}
};
spl_autoload_register($swdNamespaceAutoload, true, true);
require_once APP_GAMEMODULE_PATH . 'module/table/table.game.php';
use BANG\Core\Globals;
use BANG\Core\Log;
use BANG\Helpers\GameOptions;
use BANG\Managers\Players;
use BANG\Managers\Cards;
use BANG\Managers\EventCards;
use BANG\Core\Stack;
use BANG\Managers\Rules;
class bang extends Table
{
use BANG\States\TurnTrait;
use BANG\States\DrawCardsTrait;
use BANG\States\PlayCardTrait;
use BANG\States\ReactTrait;
use BANG\States\SelectCardTrait;
use BANG\States\ResolveFlippedTrait;
use BANG\States\EndOfLifeTrait;
use BANG\States\EndOfGameTrait;
use BANG\States\TriggerAbilityTrait;
use BANG\States\PreferencesTrait;
use BANG\States\ChooseCharacterTrait;
use BANG\States\PhaseOneTrait;
use BANG\States\EventTrait;
use BANG\States\DiscardBlueCardTrait;
use BANG\States\BloodBrothersTrait;
use BANG\States\HardLiquorTrait;
use BANG\States\RanchTrait;
use BANG\States\PeyoteTrait;
use BANG\States\EndOfTurnEventEffectsTrait;
use BANG\States\BeforePlayCardTrait;
public static $instance = null;
public function __construct()
{
parent::__construct();
self::$instance = $this;
$this->bSelectGlobalsForUpdate = true;
self::initGameStateLabels([
'optionCharacters' => OPTION_CHOOSE_CHARACTERS,
'optionExpansions' => OPTION_EXPANSIONS,
'optionHighNoon' => OPTION_HIGH_NOON_EXPANSION,
'optionFistful' => OPTION_FISTFUL_OF_CARDS_EXPANSION,
'optionHighNoonAndFistful' => OPTION_HIGH_NOON_AND_FOC_EXPANSION,
]);
}
public static function get()
{
return self::$instance;
}
protected function getGameName()
{
return 'bang';
}
/*
* setupNewGame:
* This method is called only once, when a new game is launched.
* params:
* - array $bplayers
* - mixed $options
*/
protected function setupNewGame($bplayers, $options = [])
{
// Initialize board and cards
$expansions = array_merge([BASE_GAME], GameOptions::getExpansions());
Cards::setupNewGame($expansions);
if (GameOptions::isEvents()) {
EventCards::setupNewGame($expansions);
}
// Initialize players
$sheriff = Players::setupNewGame($bplayers, $expansions, $options);
// Initialize round counter
Globals::setRoundNumber(0);
$this->gamestate->changeActivePlayer($sheriff);
}
/*
* getAllDatas:
* Gather all information 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 and when a player refreshes the game page (F5)
*/
protected function getAllDatas()
{
$pId = self::getCurrentPlayerId();
$result = [];
$cards = Cards::getUIData();
if (GameOptions::isEvents()) {
$result = array_merge($result, [
'eventsDeckCount' => EventCards::getDeckCount(),
'eventActive' => EventCards::getActive(),
'eventNext' => EventCards::getNext(),
'notAgreedToDisclaimer' => EventCards::isResurrectionPossible() ? Players::getNotAgreedToDisclaimerList() : null,
]);
$cards = array_merge($cards, EventCards::getUiData());
}
$this->updateDBTableCustom();
return array_merge($result,[
'players' => Players::getUiData($pId),
'deckCount' => Cards::getDeckCount(),
'discard' => Cards::getLastDiscarded(),
'playerTurn' => Rules::getCurrentPlayerId(),
'cards' => $cards,
'distances' => Players::getDistances(),
'roundNumber' => Globals::getRoundNumber()
]);
}
/*
* getGameProgression:
* Compute and return the current game progression approximation
* This method is called each time we are in a game state with the "updateGameProgression" property set to true
*/
public function getGameProgression()
{
$someCharactersNeedToBeChosen = !empty(self::getObjectListFromDb('SELECT `player_character_chosen` FROM `player` WHERE `player_character_chosen` = 0'));
if ($someCharactersNeedToBeChosen) {
return 0;
} else {
$bulletsSum = (int)self::getUniqueValueFromDb('SELECT SUM(player_bullets) FROM player');
$currentHpSum = (int)self::getUniqueValueFromDb('SELECT SUM(player_hp) FROM player');
$lostBullets = $bulletsSum - $currentHpSum;
return $lostBullets / $bulletsSum * 100;
}
}
////////////////////////////////////
//////////// Zombie ////////////
////////////////////////////////////
/*
* zombieTurn:
* This method is called each time it is the turn of a player who has quit the game (= "zombie" player).
* You can do whatever you want in order to make sure the turn of this player ends appropriately
*/
public function zombieTurn($state, $activePlayer)
{
$player = Players::get($activePlayer);
if (!$player->isEliminated()) {
$atom = Stack::newAtom(ST_ELIMINATE, [
'type' => 'eliminate',
'src' => '',
'attacker' => $activePlayer,
'pId' => $activePlayer,
]);
Stack::insertOnTop($atom);
Stack::resolve();
}
// throw new BgaVisibleSystemException(
// 'Zombie player ' . $activePlayer . ' stuck in unexpected state ' . $state['name']
// );
}
/////////////////////////////////////
////////// DB upgrade ///////////
/////////////////////////////////////
// You don't have to care about this until your game has been published on BGA.
// Once your game is on BGA, this method is called everytime the system detects a game running with your old Database scheme.
// In this case, if you change your Database scheme, you just have to apply the needed changes in order to
// update the game database and allow the game to continue to run with your new version.
/////////////////////////////////////
/*
* upgradeTableDb
* - int $from_version : current version of this game database, in numerical form.
* For example, if the game was running with a release of your game named "140430-1345", $from_version is equal to 1404301345
*/
function upgradeTableDb($from_version)
{
if( $from_version <= 2406250036 ) {
$this->updateDBTableCustom();
}
}
function updateDBTableCustom()
{
$newSchema = self::DbQuery('SHOW COLUMNS FROM `rules` LIKE \'phase_one_event_draw\'')->num_rows === 1;
if (!$newSchema) {
$sql = "ALTER TABLE `rules` ADD `phase_one_event_draw` int(1) NOT NULL DEFAULT 0;";
self::applyDbUpgradeToAllDB($sql);
}
}
/////////////////////////////////////////////////////////////
// Exposing protected methods, please use at your own risk //
/////////////////////////////////////////////////////////////
// Exposing protected method getCurrentPlayerId
public static function getCurrentPId()
{
return self::get()->getCurrentPlayerId();
}
// Exposing protected method translation
public static function totranslate($text)
{
return self::get()->_($text);
}
}