forked from willcrichton/sevenwonders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.php
477 lines (414 loc) · 16.6 KB
/
player.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
<?php
require_once('scoring.php');
class Player {
const TRASHING = 'trashing';
const BUYING = 'buying';
const BUILDING = 'building';
const USINGFREE = 'usingfree';
const USINGDISCARD = 'usingdiscard';
// General player state
private $_id;
private $_name;
private $_conn;
private $_game;
// Game state
public $coins;
public $permResources;
public $wonder;
public $wonderName;
public $wonderStage;
public $wonderSide;
public $order;
public $hand;
public $selectedCard;
public $pendingCost; // cost of $selectedCard
public $cardsPlayed;
public $military;
public $militaryPoints;
public $points;
public $science;
public $leftPlayer;
public $rightPlayer;
public $discounts;
public $state; // one of the constants above (or unset)
public $canHaveFreeCard; // state for olympia's free card
public $hasFreeCard;
public $canPlayTwoBuilt; // state for babylon's play2 stage
public $secondPending; // second card being played
public $secondState; // what's happening to the second card
public $secondCost; // pending cost of the second card
public $canStealGuild; // olympia's guild steal (b side)
// Figuring out card costs
private $possibilities;
public function __construct($id, $unique) {
$this->_name = "Guest $unique";
$this->_id = $id;
}
public function id() {
return $this->_id;
}
public function name() {
return $this->_name;
}
public function setName($name) {
$this->_name = $name;
}
public function info() {
return "User {$this->name()} ({$this->id()})";
}
public function getPublicInfo(){
return array(
'id' => $this->_id,
'name' => $this->_name,
'cards' => array_map(function($c) { return $c->json(); }, $this->cardsPlayed),
'coins' => $this->coins,
'wonder' => array("name" => $this->wonderName,
"stage" => $this->wonderStage,
"side" => $this->wonderSide),
'military' => $this->military->json()
);
}
public function setGame($game) {
if ($this->_game != null)
throw new Exception("already in a game");
$this->_game = $game;
$this->coins = 0;
$this->permResources = array();
$this->wonder = null;
$this->order = -1;
$this->hand = null;
$this->selectedCard = null;
$this->cardsPlayed = array();
$this->points = 0;
$this->military = new Military();
$this->science = new Science();
$this->wonderStage = 0;
$this->leftPlayer = null;
$this->rightPlayer = null;
$this->discounts = array('left' => array(), 'right' => array());
$this->canHaveFreeCard = false;
$this->hasFreeCard = false;
$this->canPlayTwoBuilt = false;
}
public function quitGame(){
$this->_game = null;
}
public function game() {
return $this->_game;
}
public function setConnection(IWebSocketConnection $conn) {
$this->conn = $conn;
}
public function send($type, $msg) {
if (!isset($this->conn))
return;
$msg = is_array($msg) ? $msg : array('data' => $msg);
$msg['messageType'] = $type;
$this->conn->sendString(json_encode($msg));
}
public function canSellResource($resource){
if(!isset($this->permResources[$resource])) return false;
return isset($this->permResources[$resource]['buy']);
}
public function sendError($error){
$this->send('error', $error);
}
public function addCoins($coins){
if ($coins == 0)
return;
$this->coins += $coins;
$this->send('coins', $this->coins);
}
public function addResource(Resource $resource) {
$this->permResources[] = $resource;
}
public function evaluateMilitary($age){
$this->military->fight($this->leftPlayer->military, $age);
$this->military->fight($this->rightPlayer->military, $age);
$this->send('military', $this->military->json());
}
public function calcPoints(){
$points = array(
'coins' => floor($this->coins / 3),
'wonder' => 0,
Card::BLUE => 0,
Card::GREEN => $this->science->points(),
Card::RED => $this->military->points(),
Card::YELLOW => 0,
Card::PURPLE => 0,
Card::BROWN => 0,
Card::GREY => 0,
'total' => 0
);
foreach($this->cardsPlayed as $card){
$points[$card->getColor()] += $card->points($this);
}
for($i = 0; $i < $this->wonderStage; $i++){
$stage = $this->wonder['stages'][$i];
if (isset($stage['points']))
$points['wonder'] += $stage['points'];
}
// if player has guild card stealing wonder
if($this->canStealGuild){
// calculate maximum points
$cards = array_merge($this->leftPlayer->cardsPlayed,
$this->rightPlayer->cardsPlayed);
$max = 0;
// todo: include science guild in calculations
foreach($cards as $card){
if($card->getColor() == Card::PURPLE)
$max = max($card->points($this), $max);
}
$points['wonder'] += $max;
}
$points['total'] = array_sum($points);
return $points;
}
public function addDiscount($dir, Resource $res) {
$this->discounts[$dir][] = $res;
}
public function neighbor($dir) {
if ($dir == 'left')
return $this->leftPlayer;
else if ($dir == 'right')
return $this->rightPlayer;
return $this; // 'self'
}
public function sendHand() {
// called once per turn, reset calculated card possibilities
$this->possibilities = array();
if(isset($this->hand)){
$info = array_map(function($c) { return $c->json(); }, $this->hand);
$this->send('hand',
array('age' => $this->_game->age, 'cards' => $info));
}
}
public function sendStartInfo($isRejoin = false) {
$playerInfo = array();
foreach($this->_game->players as $player){
$playerInfo[] = $player->getPublicInfo();
}
$tojson = function($a) { return $a->json(); };
$wonderInfo = array("name" => $this->wonderName,
"stage" => $this->wonderStage);
if (isset($this->wonder['resource']))
$wonderInfo['resource'] = $this->wonder['resource']->json();
$startInfo = array(
"coins" => $this->coins,
"wonder" => $wonderInfo,
"wonderside" => $this->wonderSide,
"plinfo" => $playerInfo,
"military" => $this->military->json(),
"neighbors" => array('left' => array(
'name' => $this->leftPlayer->_name,
'id' => $this->leftPlayer->id(),
'resource' => isset($this->leftPlayer->wonder) ?
$this->leftPlayer->wonder['resource']->json() : '',
'stage' => $this->leftPlayer->wonderStage,
'wonder' => $this->leftPlayer->wonderName
),
'right' => array(
'name' => $this->rightPlayer->_name,
'id' => $this->rightPlayer->id(),
'resource' => isset($this->rightPlayer->wonder) ?
$this->rightPlayer->wonder['resource']->json() : '',
'stage' => $this->rightPlayer->wonderStage,
'wonder' => $this->rightPlayer->wonderName
)
),
'leftcards' => array_map($tojson, $this->leftPlayer->cardsPlayed),
'rightcards' => array_map($tojson, $this->rightPlayer->cardsPlayed),
'played' => array_map($tojson, $this->cardsPlayed),
'rejoin' => $isRejoin,
);
$this->send("startinfo", $startInfo);
if ($this->hasFreeCard)
$this->getFreeCard();
if ($this->canPlayTwoBuilt)
$this->send('canplay2', '');
}
public function rejoinGame() {
$this->sendStartInfo(true);
$this->sendHand();
}
public function rejoinWaitingRoom() {
$players = array();
foreach ($this->game()->players as $player) {
if ($player != $this)
$players[] = $player->name();
}
$this->send('joingame', array('players' => $players));
}
public function findCost(Card $card, $type) {
$possibilities = $this->calculateCost($card, $type);
// Save off what we just calculated so we can verify a cost strategy
// when one is provided when playing the card
$this->possibilities[$card->getName()] =
array('combs' => $possibilities, 'type' => $type);
// Send off everything we just found
$this->send('possibilities', array('combs' => $possibilities));
}
private function calculateCost(Card $card, $type) {
if ($type == 'play') {
// check for duplicates
foreach ($this->cardsPlayed as $cardPlayed)
if ($cardPlayed->getName() == $card->getName())
return array();
// check if it's a prerequisite for being free
foreach ($this->cardsPlayed as $cardPlayed)
if ($card->hasPrereq($cardPlayed))
return array(array());
$required = $card->getResourceCost();
} else { // $type == 'wonder'
// Can't over-build the wonder
if ($this->wonderStage == count($this->wonder['stages']))
return array();
$stage = $this->wonder['stages'][$this->wonderStage];
$required = $stage['requirements'];
}
// Otherwise, we're going to have to pay for this card somehow
$have = array();
// We get all our resources for free
foreach ($this->permResources as $resource)
$have[] = ResourceOption::me($resource);
// Add in all the left player's resources, factoring in discounts
foreach ($this->leftPlayer->permResources as $resource) {
if (!$resource->buyable())
continue;
$have[] = ResourceOption::left($resource,
$resource->discount($this->discounts['left']));
}
// Add in all the right player's resources, factoring discounts
foreach ($this->rightPlayer->permResources as $resource) {
if (!$resource->buyable())
continue;
$have[] = ResourceOption::right($resource,
$resource->discount($this->discounts['right']));
}
// Figure out how we can pay neighbors to satisfy our requirements
$possible = Resource::satisfy($required, $have,
$this->coins - $card->getMoneyCost());
return $possible;
}
public function cardCost(Card $card, $selection, $type){
// Make sure we've pre-calculated the cost of this card and that the
// specified selection is in bounds
if (!isset($this->possibilities[$card->getName()]))
return false;
$arr = $this->possibilities[$card->getName()];
if (!isset($arr['combs'][$selection]) || $arr['type'] != $type)
return false;
unset($this->possibilities[$card->getName()]);
return $arr['combs'][$selection];
}
public function playWonderStage() {
$stage = $this->wonder['stages'][$this->wonderStage];
$this->wonderStage++;
// Do all the easy things first
if (isset($stage['military']))
$this->military->add($stage['military']);
if (isset($stage['coins']))
$this->addCoins($stage['coins']);
if (isset($stage['science']))
$this->science->add(Science::ANY); // only babylon for now
if (isset($stage['resource']))
$this->addResource($stage['resource']);
if (!isset($stage['custom']))
return;
switch ($stage['custom']) {
case '1free': // olympia's 1 free card per age
$this->getFreeCard();
break;
case 'guild': // olympia's steal a guild at the end of the game
$this->canStealGuild = true;
break;
case 'discard': // halikarnassus's play from the discard pile
if(count($this->_game->discard) > 0){
$tojson = function($a){ return $a->json(); };
$this->state = Player::USINGDISCARD;
$this->send('discard', array('cards' => array_map($tojson, $this->_game->discard)));
}
break;
case 'play2': // babylon's play both cards at the end of a hand
$this->canPlayTwoBuilt = true;
$this->send('canplay2', '');
break;
case 'discount': // olympia's discount COWS for both L/R
$resource = new Resource(false, false);
$resource->add(Resource::CLAY);
$resource->add(Resource::ORE);
$resource->add(Resource::WOOD);
$resource->add(Resource::STONE);
$this->addDiscount('left', $resource);
$this->addDiscount('right', $resource);
break;
}
}
public function getFreeCard() {
$this->canHaveFreeCard = true;
$this->hasFreeCard = true;
$this->send('freecard', array('hasfree' => true));
}
public function useFreeCard() {
$this->hasFreeCard = false;
$this->send('freecard', array('hasfree' => false));
}
public function isPlayingCard() {
return isset($this->state) and ($this->state == self::BUYING ||
$this->state == self::USINGFREE ||
$this->state == self::USINGDISCARD);
}
public function playCard(SevenWonders $game, Card $card, $state,
$costarr) {
switch ($state) {
case Player::TRASHING:
$this->addCoins(3);
$game->discard[] = $card;
break;
case Player::BUYING:
$card->play($this);
$this->cardsPlayed[] = $card;
$this->addCoins(-1 * $card->getMoneyCost());
break;
case Player::BUILDING:
$this->playWonderStage();
$info = array(
'id' => $this->id(),
'stage' => $this->wonderStage
);
$this->leftPlayer->send('builtwonder', $info);
$this->rightPlayer->send('builtwonder', $info);
break;
case Player::USINGFREE:
$this->useFreeCard();
$card->play($this);
$this->cardsPlayed[] = $card;
break;
case Player::USINGDISCARD:
$card->play($this);
$this->cardsPlayed[] = $card;
break;
default:
throw new Error("unimplemented play state");
}
unset($this->hand[array_search($card, $this->hand)]);
// Consume money cost for this this and pay adjacent thiss
foreach ($costarr as $dir => $cost) {
if ($dir == 'left')
$this->leftPlayer->addCoins($cost);
else if ($dir == 'right')
$this->rightPlayer->addCoins($cost);
$this->addCoins(-1 * $cost);
}
}
public function canPlayTwo() {
// With babylon's play2 wonder stage, you've either built it in the past
// so you can play two, or you are building it currently, enabling
// yourself to play two cards.
return $this->canPlayTwoBuilt ||
(isset($this->state) &&
$this->state == Player::BUILDING &&
isset($this->wonder['stages'][$this->wonderStage]['custom']) &&
$this->wonder['stages'][$this->wonderStage]['custom'] == 'play2');
}
}