test "foo" do
:ok = Seelies.Router.dispatch(%Seelies.StartGame{game_id: "42", board: Seelies.Test.board(), teams: Seelies.Test.teams()})
end
➜ seelies git:(master) ✗ mix test test/seelies_test.exs:5
Excluding tags: [:test]
Including tags: [line: "5"]
"StartGame execute"
"GameStarted apply"
.
test "bar" do
:ok = Seelies.Router.dispatch(%Seelies.StartGame{game_id: "42", board: Seelies.Test.board(), teams: Seelies.Test.teams()})
assert_receive_event(Seelies.GameStarted, &(&1))
end
➜ seelies git:(master) ✗ mix test test/seelies_test.exs:10
Excluding tags: [:test]
Including tags: [line: "10"]
"StartGame execute"
"GameStarted apply"
{"Decode", Seelies.GameStarted}
{"Decode", Seelies.GameStarted}
{"Decode", Seelies.GameStarted}
.
defmodule Seelies.GameStarted do
@derive Jason.Encoder
defstruct [:game_id, :board, :teams]
def apply(_state, %Seelies.GameStarted{game_id: game_id, board: board, teams: teams}) do
IO.inspect("GameStarted apply")
dispatch = Seelies.Team.dispatch(Enum.map(teams, fn (team) -> team.id end), Map.keys(board.territories))
%Seelies.Game{
teams: teams,
players: Seelies.Team.players_from_teams(teams),
game_id: game_id,
board: board,
units: %{},
exploitations: %{},
convoys: %{},
territories: Enum.reduce(board.territories, %{}, fn ({territory_id, _territory}, acc) ->
Map.put(acc, territory_id, %{
team: dispatch[territory_id],
resources: Seelies.ResourcesQuantity.null,
baits: %{}
})
end)
}
end
end
defimpl Commanded.Serialization.JsonDecoder, for: Seelies.GameStarted do
def decode(%Seelies.GameStarted{} = event) do
IO.inspect({"Decode", event.__struct__})
%Seelies.GameStarted{event |
board: Seelies.Board.decode(event.board),
teams: Seelies.Team.decode(event.teams)}
end
end
defmodule Seelies.StartGame do
defstruct [:game_id, :board, :teams]
def execute(%Seelies.Game{game_id: nil}, %Seelies.StartGame{game_id: game_id, board: board, teams: teams}) do
IO.inspect("StartGame execute")
%Seelies.GameStarted{game_id: game_id, board: board, teams: teams}
end
def execute(%Seelies.Game{}, %Seelies.StartGame{}) do
{:error, :game_already_exists}
end
end