-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplanetunknown.game.php
More file actions
330 lines (296 loc) · 10.5 KB
/
Copy pathplanetunknown.game.php
File metadata and controls
330 lines (296 loc) · 10.5 KB
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
<?php
// SLOT 2 : right before the last turn
// SLOT 1 : end of the last turn
// SLOT 3 : before end of game
/**
*------
* BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
* Planet Unknown implementation : © Timothée Pecatte <tim.pecatte@gmail.com>, Emmanuel Albisser <emmanuel.albisser@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.
* -----
*
* planetunknown.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] == 'PU') {
array_shift($classParts);
$file = dirname(__FILE__) . '/modules/php/' . implode(DIRECTORY_SEPARATOR, $classParts) . '.php';
if (file_exists($file)) {
require_once $file;
} else {
var_dump('Cannot find file : ' . $file);
}
}
};
spl_autoload_register($swdNamespaceAutoload, true, true);
require_once APP_GAMEMODULE_PATH . 'module/table/table.game.php';
use PU\Managers\Meeples;
use PU\Managers\Tiles;
use PU\Managers\Players;
use PU\Managers\Scores;
use PU\Core\Globals;
use PU\Helpers\Log;
use PU\Core\Preferences;
use PU\Core\Stats;
use PU\Core\Engine;
use PU\Managers\Cards;
use PU\Managers\Susan;
class planetunknown extends Table
{
use PU\DebugTrait;
use PU\States\SetupTrait;
use PU\States\EngineTrait;
use PU\States\TurnTrait;
use PU\States\ChooseSetupTrait;
use PU\States\EndGameTrait;
public static $instance = null;
function __construct()
{
parent::__construct();
self::$instance = $this;
$this->bSelectGlobalsForUpdate = true;
self::initGameStateLabels([
'mode' => 10, // DO NOT TOUCH, USED FOR SIMULATING DB MODIFICATION
'option_planet' => 105,
'option_corporation' => 106,
'option_event_card' => 107,
]);
// Stats::checkExistence();
}
protected function initTable()
{
Engine::boot();
}
public static function get()
{
return self::$instance;
}
protected function getGameName()
{
return 'planetunknown';
}
/*
* getAllDatas:
*/
public function getAllDatas()
{
$pId = (int) self::getCurrentPId();
$scores = Players::scores($pId);
$players = Players::getUiData($pId);
foreach ($players as $pId => &$infos) {
$infos['score'] = $scores[$pId]['total'];
}
return [
'prefs' => Preferences::getUiData($pId),
'players' => $players,
'tiles' => Tiles::getUiData(),
'meeples' => Meeples::getUiData(),
'cards' => Cards::getUiData(),
'susan' => Susan::getUiData(),
'scores' => $scores,
'firstPlayer' => Globals::getFirstPlayer(),
'endOfGameTriggered' => Globals::isGameEndTriggered(),
'eventGame' => Globals::getEventCardsGame(),
];
}
/*
* getGameProgression:
*/
function getGameProgression()
{
if (Globals::getEventCardsGame() == EVENT_CARD_GAME) {
return ((20 - Cards::countInLocation('deck_event')) * 100) / 20;
} else {
//estimate % of remaining tile in the most advanced depot
$tilesCounter = ((24 - Susan::getTilesNumberInMinDeck()) * 100) / 24;
//compute average % of emptyspace in all players planets
$cellsCounter = 0;
$players = Players::getAll();
foreach ($players as $pId => $player) {
$nbCells = count($player->planet()->getListOfCells());
$cellsCounter += (($nbCells - $player->planet()->countEmptySpaces()) * 100) / $nbCells;
}
$cellsCounter = $cellsCounter / count($players);
return max($cellsCounter, $tilesCounter);
}
}
function actChangePreference($pref, $value)
{
Preferences::set($this->getCurrentPId(), $pref, $value);
}
///////////////////////////////////////////////
///////////////////////////////////////////////
//////////// Custom Turn Order ////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
public function initCustomTurnOrder($key, $order, $callback, $endCallback, $loop = false, $autoNext = true, $args = [])
{
$turnOrders = Globals::getCustomTurnOrders();
$turnOrders[$key] = [
'order' => $order ?? Players::getTurnOrder(),
'index' => -1,
'callback' => $callback,
'args' => $args, // Useful mostly for auto card listeners
'endCallback' => $endCallback,
'loop' => $loop,
];
Globals::setCustomTurnOrders($turnOrders);
if ($autoNext) {
$this->nextPlayerCustomOrder($key);
}
}
public function initCustomDefaultTurnOrder($key, $callback, $endCallback, $loop = false, $autoNext = true)
{
$this->initCustomTurnOrder($key, null, $callback, $endCallback, $loop, $autoNext);
}
public function nextPlayerCustomOrder($key)
{
$turnOrders = Globals::getCustomTurnOrders();
if (!isset($turnOrders[$key])) {
throw new BgaVisibleSystemException('Asking for the next player of a custom turn order not initialized : ' . $key);
}
// Increase index and save
$o = $turnOrders[$key];
$i = $o['index'] + 1;
if ($i == count($o['order']) && $o['loop']) {
$i = 0;
}
$turnOrders[$key]['index'] = $i;
Globals::setCustomTurnOrders($turnOrders);
if ($i < count($o['order'])) {
$this->gamestate->jumpToState(ST_GENERIC_NEXT_PLAYER);
$this->gamestate->changeActivePlayer($o['order'][$i]);
$this->jumpToOrCall($o['callback'], $o['args']);
} else {
$this->endCustomOrder($key);
}
}
public function endCustomOrder($key)
{
$turnOrders = Globals::getCustomTurnOrders();
if (!isset($turnOrders[$key])) {
throw new BgaVisibleSystemException('Asking for ending a custom turn order not initialized : ' . $key);
}
$o = $turnOrders[$key];
$turnOrders[$key]['index'] = count($o['order']);
Globals::setCustomTurnOrders($turnOrders);
$callback = $o['endCallback'];
$this->jumpToOrCall($callback);
}
public function jumpToOrCall($mixed, $args = [])
{
if (is_int($mixed) && array_key_exists($mixed, $this->gamestate->states)) {
$this->gamestate->jumpToState($mixed);
} elseif (method_exists($this, $mixed)) {
$method = $mixed;
$this->$method($args);
} else {
throw new BgaVisibleSystemException('Failing to jumpToOrCall : ' . $mixed);
}
}
/********************************************
******* GENERIC CARD LISTENERS CHECK ********
********************************************/
/*
* A lot of time you want to loop through all the player to see if a card react or not
* => this is achieved using custom turn order with an arg containing the eventType
* => the custom order will call the genericPlayerCheckListeners that will getReaction from cards if any
*/
// public function checkCardListeners($typeEvent, $endCallback, $event = [], $order = null)
// {
// $event['type'] = $typeEvent;
// $event['method'] = $typeEvent;
// $this->initCustomTurnOrder($typeEvent, $order, 'genericPlayerCheckListeners', $endCallback, false, true, $event);
// }
// function genericPlayerCheckListeners($event)
// {
// $pId = Players::getActiveId();
// $event['pId'] = $pId;
// $reaction = ZooCards::getReaction($event);
// if (is_null($reaction)) {
// // No reaction => just go to next player
// $this->nextPlayerCustomOrder($event['type']);
// } else {
// // Reaction => boot up the Engine
// Engine::setup($reaction, ['order' => $event['type']]);
// Engine::proceed();
// }
// }
////////////////////////////////////
//////////// 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)
{
// $skipped = Globals::getSkippedPlayers();
// if (!in_array((int) $activePlayer, $skipped)) {
// $skipped[] = (int) $activePlayer;
// Globals::setSkippedPlayers($skipped);
// }
$stateName = $state['name'];
if ($state['type'] == 'activeplayer') {
if ($stateName == 'confirmTurn') {
$this->actConfirmTurn(true);
} elseif ($stateName == 'chooseRotation') {
$this->actChooseRotation(bga_rand(0, 5));
}
// Clear all node of player
elseif (Engine::getNextUnresolved($activePlayer) != null) {
Engine::clearZombieNodes($activePlayer);
} else {
$this->gamestate->nextState('zombiePass');
}
} elseif ($state['type'] == 'multipleactiveplayer') {
// Make sure player is in a non blocking status for role turn
$this->gamestate->setPlayerNonMultiactive($activePlayer, 'zombiePass');
}
}
/////////////////////////////////////
////////// 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
*/
public function upgradeTableDb($from_version)
{
// if ($from_version <= 2107011810) {
// $sql = 'ALTER TABLE `DBPREFIX_player` ADD `new_score` INT(10) NOT NULL DEFAULT 0';
// self::applyDbUpgradeToAllDB($sql);
// }
if ($from_version <= 2607061617) {
$this->debug_unblock();
}
}
/////////////////////////////////////////////////////////////
// Exposing protected methods, please use at your own risk //
/////////////////////////////////////////////////////////////
// Exposing protected method getCurrentPlayerId
public function getCurrentPId($bReturnNullIfNotLogged = false)
{
return self::get()->getCurrentPlayerId($bReturnNullIfNotLogged);
}
// Exposing protected method translation
public function translate($text)
{
return self::get()->_($text);
}
}