Verbs

Game

1<?php ...
2 
3namespace Thunk\Verbs\Examples\Monopoly\States;
4 
5use Carbon\CarbonImmutable;
6use Illuminate\Support\Collection;
7use Thunk\Verbs\Examples\Monopoly\Game\Bank;
8use Thunk\Verbs\Examples\Monopoly\Game\Board;
9use Thunk\Verbs\Examples\Monopoly\Game\Phase;
10use Thunk\Verbs\State;
11 
12class GameState extends State
13{
14 public bool $started = false;
15 
16 public Board $board;
17 
18 public Bank $bank;
19 
20 public array $player_ids = [];
21 
22 public ?int $active_player_id = null;
23 
24 public Phase $phase = Phase::Move;
25 
26 public bool $phase_complete = false;
27 
28 public ?array $last_roll = null;
29 
30 public CarbonImmutable $started_at;
31 
32 /** @return Collection<int, PlayerState> */
33 public function players(): Collection
34 {
35 return collect($this->player_ids)->map(fn (int $id) => PlayerState::load($id));
36 }
37 
38 public function activePlayer(): ?PlayerState
39 {
40 return $this->active_player_id ? PlayerState::load($this->active_player_id) : null;
41 }
42 
43 public function hasPlayer(PlayerState|int $player): bool
44 {
45 if ($player instanceof PlayerState) {
46 $player = $player->id;
47 }
48 
49 return in_array($player, $this->player_ids);
50 }
51 
52 public function moveToNextPlayer(): static
53 {
54 $active_index = array_search($this->active_player_id, $this->player_ids);
55 
56 $this->active_player_id = $this->player_ids[$active_index + 1] ?? $this->player_ids[0];
57 
58 return $this;
59 }
60}