Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Last active November 1, 2019 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sephi-Chan/197e28ca495373540978c1e69d957991 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/197e28ca495373540978c1e69d957991 to your computer and use it in GitHub Desktop.

Test 1

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 2

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}
.

Code

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment