Skip to content

Commit

Permalink
feat: passing tests for turn and round changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Nov 20, 2024
1 parent d62359b commit 915ae63
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 55 deletions.
2 changes: 2 additions & 0 deletions lib/viral_spiral/entity/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ defmodule ViralSpiral.Entity.Player do
end

defimpl ViralSpiral.Entity.Change, for: ViralSpiral.Entity.Player do
alias ViralSpiral.Affinity
alias ViralSpiral.Bias
alias ViralSpiral.Entity.Player
import ViralSpiral.Room.EngineConfig.Guards

Expand Down
1 change: 1 addition & 0 deletions lib/viral_spiral/entity/round.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ defmodule ViralSpiral.Entity.Round do
def apply_change(state, change_desc) do
case change_desc[:type] do
:next -> Round.next(state)
:skip -> Round.add_skip(state, change_desc[:player_id])
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/viral_spiral/room/change_descriptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule ViralSpiral.Room.ChangeDescriptions do
def change_chaos(offset), do: [type: :chaos_countdown, offset: offset]

def new_round(), do: []
def next_round(), do: []
def next_round(), do: [type: :next]
def skip_player(player_id), do: [type: :skip, player_id: player_id]
def new_turn(), do: []
def pass_turn_to(player) when is_binary(player), do: []
def pass_turn_to(players) when is_list(players), do: []
Expand Down
40 changes: 9 additions & 31 deletions test/support/fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,21 @@ defmodule Fixtures do
alias ViralSpiral.Room.State

def initialized_game() do
room = Room.new(4)

player_list = [
Player.new(room) |> Player.set_name("adhiraj"),
Player.new(room) |> Player.set_name("aman"),
Player.new(room) |> Player.set_name("krys"),
Player.new(room) |> Player.set_name("farah")
]

players = Enum.reduce(player_list, %{}, fn player, acc -> Map.put(acc, player.id, player) end)

round = Round.new(player_list)
turn = Turn.new(round)

%State{
room: room,
players: players,
round: round,
turn: turn
}
room = Room.reserve("test-room") |> Room.start(4)
State.new(room, ["adhiraj", "krys", "aman", "farah"])
end

def new_game() do
room = Room.reserve("test-room") |> Room.start(4)
State.new(room, ["adhiraj", "krys", "aman", "farah"])
end

def players() do
room_config = %Room{}

player_list = [
Player.new(room_config) |> Player.set_name("adhiraj"),
Player.new(room_config) |> Player.set_name("aman"),
Player.new(room_config) |> Player.set_name("krys"),
Player.new(room_config) |> Player.set_name("farah")
]

Enum.reduce(player_list, %{}, fn player, acc -> Map.put(acc, player.id, player) end)
def new_round() do
%Round{
order: ["player_abc", "player_def", "player_ghi", "player_jkl"],
count: 4,
current: 0,
skip: nil
}
end
end
18 changes: 9 additions & 9 deletions test/viral_spiral/entity/player_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule ViralSpiral.Game.PlayerTest do
use ExUnit.Case

test "create player from room config" do
room = Room.new(4)
room = Room.reserve("hello") |> Room.start(4)

player =
Factory.new_player_for_room(room)
Expand Down Expand Up @@ -76,42 +76,42 @@ defmodule ViralSpiral.Game.PlayerTest do
end

test "change clout", %{player: player} do
player = Change.apply_change(player, Options.change_clout(4))
player = Change.apply_change(player, ChangeDescriptions.change_clout(4))
assert player.clout == 4
end

test "change affinity", %{player: player} do
player = Change.apply_change(player, Options.change_affinity(:cat, 2))
player = Change.apply_change(player, ChangeDescriptions.change_affinity(:cat, 2))
assert player.affinities.cat == 2
end

test "change bias", %{player: player} do
player = Change.apply_change(player, Options.change_bias(:yellow, -1))
player = Change.apply_change(player, ChangeDescriptions.change_bias(:yellow, -1))
assert player.biases.yellow == 1
end

test "add card to hand", %{player: player} do
player = Change.apply_change(player, Options.add_to_hand("card_23b2323"))
player = Change.apply_change(player, ChangeDescriptions.add_to_hand("card_23b2323"))
assert length(player.hand) == 1
assert hd(player.hand) == "card_23b2323"
end

test "add_active_card", %{player: player} do
player = Change.apply_change(player, Options.add_to_active("card_29323"))
player = Change.apply_change(player, ChangeDescriptions.add_to_active("card_29323"))
assert player.active_cards == ["card_29323"]

player = Change.apply_change(player, Options.add_to_active("card_84843"))
player = Change.apply_change(player, ChangeDescriptions.add_to_active("card_84843"))
assert player.active_cards == ["card_29323", "card_84843"]
end

test "remove_active_card", %{player: player} do
player =
%{player | active_cards: ["card_29323", "card_84843"]}
|> Change.apply_change(Options.remove_active("card_29323"))
|> Change.apply_change(ChangeDescriptions.remove_active("card_29323"))

assert player.active_cards == ["card_84843"]

player = Change.apply_change(player, Options.remove_active("card_84843"))
player = Change.apply_change(player, ChangeDescriptions.remove_active("card_84843"))

assert player.active_cards == []
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule ViralSpiral.Entity.RoundTest do
alias ViralSpiral.Room.ChangeDescriptions
alias ViralSpiral.Entity.Change
alias ViralSpiral.Entity.Round
use ExUnit.Case
Expand Down Expand Up @@ -74,14 +75,24 @@ defmodule ViralSpiral.Entity.RoundTest do

describe "changes" do
setup do
players = Fixtures.players()
round = Round.new(players)
round = Fixtures.new_round()
%{round: round}
end

test "move to next round", %{round: round} do
new_round = Change.apply_change(round, type: :next)
new_round = Change.apply_change(round, ChangeDescriptions.next_round())
assert new_round.current == 1

new_round = Change.apply_change(new_round, ChangeDescriptions.next_round())
assert new_round.current == 2
end

test "skip a player's round", %{round: round} do
new_round =
Change.apply_change(round, ChangeDescriptions.skip_player("player_def"))
|> Change.apply_change(ChangeDescriptions.next_round())

assert new_round.current == 2
end
end
end
File renamed without changes.
11 changes: 0 additions & 11 deletions test/viral_spiral/room/state/deck_test.exs

This file was deleted.

0 comments on commit 915ae63

Please sign in to comment.