Verbs

Player Moved

1<?php ...
2 
3namespace Thunk\Verbs\Examples\Monopoly\Events\Gameplay;
4 
5use Thunk\Verbs\Attributes\Autodiscovery\AppliesToState;
6use Thunk\Verbs\Event;
7use Thunk\Verbs\Examples\Monopoly\Game\Phase;
8use Thunk\Verbs\Examples\Monopoly\Game\Spaces\Space;
9use Thunk\Verbs\Examples\Monopoly\States\GameState;
10use Thunk\Verbs\Examples\Monopoly\States\PlayerState;
11 
12#[AppliesToState(GameState::class)]
13#[AppliesToState(PlayerState::class)]
14class PlayerMoved extends Event
15{
16 use PlayerAction;
17 
18 public function __construct(
19 public int $game_id,
20 public int $player_id,
21 public Space $to,
22 ) {}
23 
24 public function validateGame(GameState $game)
25 {
26 $this->assert($game->last_roll, 'You must roll the dice first.');
27 $this->assert($game->phase === Phase::Move && ! $game->phase_complete, 'You are not allowed to move right now.');
28 $this->assert($this->to === $this->expectedSpace(), "You must move to '{$this->expectedSpace()->name()}'");
29 }
30 
31 public function applyToGame(GameState $game)
32 {
33 $game->phase = Phase::Move;
34 $game->phase_complete = true;
35 $game->last_roll = null;
36 }
37 
38 public function applyToPlayer(PlayerState $player)
39 {
40 $player->location = $this->to;
41 }
42 
43 protected function expectedSpace(): Space
44 {
45 $game = $this->state(GameState::class);
46 $player = $this->state(PlayerState::class);
47 
48 return $game->board->findNextSpace($player->location, array_sum($game->last_roll));
49 }
50}