-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwonders.php
396 lines (347 loc) · 14.1 KB
/
wonders.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
<?php
require_once("cards.php");
class SevenWonders {
public $debug = true;
public $name;
public $players = array();
public $maxplayers;
public $creator;
public $id;
public $deck;
public $server;
public $started = false;
public $age = 1;
public $turn = 1;
public $wonders;
public $wondersChosen = false;
public $playerInfo;
public $discard = array();
public function __construct(){
global $deck;
$this->deck = $deck;
$this->wonders = $this->loadWonders();
}
private function loadWonders() {
$wonders = json_decode(file_get_contents("cards/wonders.json"), true);
foreach ($wonders as &$wonder) {
foreach ($wonder as $side => &$value) {
if ($side == 'name')
continue;
if (isset($value['resource']))
$value['resource'] =
Card::csvResources($value['resource'], true)[0];
foreach ($value['stages'] as &$stage) {
if (isset($stage['resource']))
$stage['resource'] =
Card::csvResources($stage['resource'], false)[0];
$stage['requirements'] =
Card::csvResources($stage['requirements'], false);
}
}
}
return $wonders;
}
public function log($msg){
if($this->debug)
echo "Game {$this->id} (turn {$this->turn}, age {$this->age}): $msg\n";
}
public function addPlayer(Player $user){
foreach($this->players as $player){
$player->send('newplayer', array('id' => $user->id(),
'name' => $user->name()));
$user->send('newplayer', array('id' => $player->id(),
'name' => $player->name()));
}
if(count($this->players) == 0)
$this->creator = $user;
$this->players[] = $user;
$user->setGame($this);
if (count($this->players) == $this->maxplayers)
$this->startGame();
}
public function startGame() {
// tell the players we're starting the game
$this->started = true;
$this->server->broadcast('started', array('id' => $this->id));
// set up the start conditions
$wonderKeys = array_keys($this->wonders);
shuffle($wonderKeys);
foreach($this->players as $player){
// starting moneyz
$player->coins = 3;
// select a wonder
$wonder = $this->wonders[array_pop($wonderKeys)];
$player->wonderName = $wonder['name'];
}
// shuffle order of players
shuffle($this->players);
$playerInfo = array();
for($i = 0; $i < count($this->players); $i++){
$this->players[$i]->leftPlayer = $i == 0 ? $this->players[count($this->players) - 1] : $this->players[$i - 1];
$this->players[$i]->rightPlayer = $i == (count($this->players) - 1) ? $this->players[0] : $this->players[$i + 1];
$this->players[$i]->order = $i + 1;
$playerInfo[] = array(
'id' => $this->players[$i]->id(),
'name' => $this->players[$i]->name(),
'order' => $this->players[$i]->order,
);
}
$this->playerInfo = $playerInfo;
// send start information
foreach($this->players as $player){
$player->sendStartInfo();
}
}
public function deal() {
$this->deck->deal($this->age, $this->players);
foreach ($this->players as $player)
$player->sendHand();
}
public function removePlayer(Player $user){
// drop player from game
unset($this->players[array_search($user, $this->players)]);
}
public function broadcast($type, $msg){
foreach($this->players as $player)
$player->send($type, $msg);
}
public function rotateHands($moveLeft){
$neighbor = $this->players[0];
$last = $neighbor->hand;
do {
$player = $moveLeft ? $neighbor->leftPlayer : $neighbor->rightPlayer;
$tmp = $last;
$last = $player->hand;
$player->hand = $tmp;
$player->sendHand();
$neighbor = $player;
} while($neighbor != $this->players[0]);
}
private function playCards($isDiscard = false) {
$this->log("All cards found");
// execute effects of all played cards
foreach ($this->players as $player) {
$data = array();
if ($player->leftPlayer->isPlayingCard())
$data['left'] = $player->leftPlayer->selectedCard->json();
if ($player->rightPlayer->isPlayingCard())
$data['right'] = $player->rightPlayer->selectedCard->json();
$player->send('cardschosen', $data);
if($isDiscard and (!isset($player->state) or $player->state != Player::USINGDISCARD))
continue;
// play both cards (if we can)
$player->playCard($this, $player->selectedCard, $player->state,
$player->pendingCost);
if (isset($player->secondState))
$player->playCard($this, $player->secondPending,
$player->secondState, $player->secondCost);
}
$shouldPause = false;
foreach ($this->players as $player) {
if(!isset($player->state) or $player->state != Player::USINGDISCARD or $isDiscard)
unset($player->state);
else
$shouldPause = true;
unset($player->selectedCard);
unset($player->pendingCost);
unset($player->secondState);
unset($player->secondPending);
unset($player->secondCost);
if ($this->turn == 6 && count($player->hand) > 0)
$this->discard[] = array_pop($player->hand);
}
if(!$shouldPause){
if ($this->turn == 6) {
// go into a new age
$this->log("Ending age {$this->age}");
$this->log("Evaluating military");
foreach ($this->players as $player) {
$player->evaluateMilitary($this->age);
if ($player->canHaveFreeCard)
$player->getFreeCard();
}
$this->age++;
$this->turn = 1;
if($this->age == 4){
$this->endGame();
} else {
$this->deal();
}
} else {
// change hands and start a new turn
$this->log("Ending turn " . $this->turn);
$this->turn++;
$this->rotateHands($this->age != 2);
}
} else {
foreach($this->players as $player)
$player->send('hand', array('card' => array()));
}
foreach($this->players as $player){
$points = array_sum($player->calcPoints());
$this->log("{$player->info()} has $points points");
}
}
public function onMessage(Player $user, $args){
switch($args['messageType']){
case 'cardignore':
$card = $args['card'];
// First handle if you ignore the selected card (default)
if (isset($user->state) && isset($user->selectedCard) &&
$user->selectedCard->getName() == $card) {
unset($user->state);
unset($user->selectedCard);
unset($user->pendingCost);
}
// If you ignored the first card, then we have to force ignoring
// the second card (maybe first enabled second). Otherwise we
// only need to ignore the second card if it's the actual card
if (isset($user->secondState) &&
($user->secondPending->getName() == $card ||
!isset($user->state))) {
unset($user->secondState);
unset($user->secondPending);
unset($user->secondCost);
}
break;
case 'cardplay':
// TODO: this means that a refreshed game with a selected card
// won't be able to play any new cards because the card
// selection isn't pushed back to the user in the game info sent
if(isset($user->state) and $user->state == Player::USINGDISCARD){
foreach($this->discard as $card)
if($card->getName() == $args['value'][0])
$foundCard = $card;
if(!isset($foundCard)) break;
$user->selectedCard = $foundCard;
$user->pendingCost = array();
$this->playCards(true);
break;
}
if (isset($user->state) &&
($this->turn != 6 ||
!$user->canPlayTwo() ||
isset($user->secondState)))
break;
$cardName = $args['value'][0];
// match what they sent with a Card object
foreach ($user->hand as $card)
if($card->getName() == $cardName)
$foundCard = $card;
if (!isset($foundCard)) // don't have the specified card
break;
$cost = array();
$state = '';
if ($args['value'][1] == 'trash') {
$state = Player::TRASHING;
} else if ($args['value'][1] == 'free') {
if (!$user->hasFreeCard)
break;
$state = Player::USINGFREE;
} else {
$cost = $user->cardCost($foundCard, $args['value'][2],
$args['value'][1]);
if ($cost === false)
break;
if ($args['value'][1] == 'wonder')
$state = Player::BUILDING;
else
$state = Player::BUYING;
}
$this->log("{$user->info()} chose {$foundCard->getName()} in state $state");
if (!isset($user->state)) {
$user->state = $state;
$user->selectedCard = $foundCard;
$user->pendingCost = $cost;
} else {
$user->secondState = $state;
$user->secondPending = $foundCard;
$user->secondCost = $cost;
}
// if everyone's played a card, execute cards and redeal/new turn
if ($this->finishedChoosing())
$this->playCards();
break;
case 'checkresources':
$cardName = $args['value'];
foreach($user->hand as $card)
if($card->getName() == $cardName)
$toPlay = $card;
if(!isset($toPlay))
return;
$user->findCost($toPlay, $args['type']);
break;
case 'wonderside':
if($this->wondersChosen) break;
$side = $args['value'] == true ? 'a' : 'b';
$user->wonderSide = $side;
foreach($this->wonders as $wonder){
if($wonder['name'] == $user->wonderName){
$user->wonder = $wonder[$side];
if (isset($user->wonder['resource']))
$user->addResource($user->wonder['resource']);
break;
}
}
$allChosen = true;
foreach($this->players as $player){
if(!isset($player->wonder)){
$allChosen = false;
break;
}
}
if($allChosen){
$this->log("All wonders chosen, dealing hands");
$this->wondersChosen = true; //todo: incorporate this into some sort of state var
foreach($this->players as $player){
$player->send('neighborwonders', array(
'left' => array(
'wonder' => $player->leftPlayer->wonderName,
'resource' => $player->leftPlayer->wonder['resource']->json(),
),
'right' => array(
'wonder' => $player->rightPlayer->wonderName,
'resource' => $player->rightPlayer->wonder['resource']->json()
)
));
}
$this->deal();
}
break;
case 'playerinfo':
$token = isset($args['value']) ? $args['value'] : '';
foreach($this->players as $player){
if($player->id() == $token){
$user->send('playerinfo', $player->getPublicInfo());
break;
}
}
break;
default:
echo "Undefined message\n";
print_r($args);
break;
}
}
private function finishedChoosing() {
$count = 0;
foreach ($this->players as $player) {
if (!isset($player->state))
continue;
if ($this->turn == 6 && $player->canPlayTwo() &&
!isset($player->secondState))
continue;
$count++;
}
return $count == count($this->players);
}
private function endGame(){
$scores = array();
foreach($this->players as $player){
$scores[$player->id()] = $player->calcPoints();
$player->quitGame();
}
$this->server->broadcast('scores', $scores);
}
}
?>