Skip to content

Commit

Permalink
feat: add multiple simultaneous turns
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Aug 14, 2024
1 parent 9cdc6a5 commit 943295d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/viral_spiral/game/turn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ defmodule ViralSpiral.Game.Turn do
defstruct current: nil,
pass_to: []

@type t :: %__MODULE__{
current: String.t(),
pass_to: list(String.t())
}

@spec new() :: Turn.t()
def new() do
%Turn{}
end

@spec new(Round.t()) :: Turn.t()
def new(%Round{} = round) do
current = Enum.at(round.order, round.current)

Expand All @@ -25,20 +36,31 @@ defmodule ViralSpiral.Game.Turn do
def new(%Round{} = _round, %Turn{} = _turn) do
end

def set_current(%Turn{} = round, current), do: %{round | current: current}
def set_current(%Turn{} = turn, current), do: %{turn | current: current}

def set_pass_to(%Turn{} = round, pass_to), do: %{round | pass_to: pass_to}
def set_pass_to(%Turn{} = turn, pass_to), do: %{turn | pass_to: pass_to}

@doc """
todo : add check to ensure that it only runs next if
to is in the the current pass_to
"""
def next(%Turn{} = from, to) do
@spec next(Turn.t(), String.t()) :: Turn.t()
def next(%Turn{} = from, to) when is_bitstring(to) do
from
|> set_current(to)
|> set_pass_to(List.delete(from.pass_to, to))
end

@spec next(Turn.t(), list(String.t())) :: list(Turn.t())
def next(%Turn{} = from, to) when is_list(to) do
new_pass_to = from.pass_to -- to

Enum.map(
to,
&%Turn{current: &1, pass_to: new_pass_to}
)
end

def change(%Turn{} = _turn) do
end
end
42 changes: 42 additions & 0 deletions test/viral_spiral/turn_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule ViralSpiral.TurnTest do
alias ViralSpiral.Game.Turn
alias ViralSpiral.Game.Round
use ExUnit.Case

describe "Turn Management" do
test "pass card" do
game = Fixtures.initialized_game()
player_list = game.player_list
round = Round.new(player_list)
turn = Turn.new(round)
assert length(turn.pass_to) == 3

current_player = Enum.at(round.order, round.current)

pass_to =
Enum.filter(player_list, &(&1.id != current_player))
|> Enum.shuffle()
|> Enum.take(1)
|> Enum.at(0)

turn = Turn.next(turn, pass_to.id)
assert length(turn.pass_to) == 2
end

@tag timeout: :infinity
test "pass card to multiple people during viral spiral special power" do
game = Fixtures.initialized_game()
player_list = game.player_list
round = Round.new(player_list)
turn = Turn.new(round)
assert length(turn.pass_to) == 3

current_player = Enum.at(round.order, 0)
to_pass_players = Enum.slice(round.order, 1..2)
turn = Turn.next(turn, to_pass_players)

assert length(turn) == 2
assert length(Enum.at(turn, 0).pass_to) == 1
end
end
end

0 comments on commit 943295d

Please sign in to comment.