From 3db2d1cd614361e9684509398dc0f5dee1df0c49 Mon Sep 17 00:00:00 2001 From: Denny Date: Thu, 12 Sep 2024 21:12:24 +0530 Subject: [PATCH] feat: all tests passing --- lib/viral_spiral/card_share.ex | 11 ++--- lib/viral_spiral/room/state/player.ex | 12 +++-- lib/viral_spiral/room/state/root.ex | 17 ++----- lib/viral_spiral/room/state/round.ex | 9 ++-- test/support/fixtures.ex | 55 +++++++-------------- test/viral_spiral/card_share_test.exs | 19 ++++--- test/viral_spiral/gameplay_test.exs | 15 ------ test/viral_spiral/room/player_test.exs | 6 +-- test/viral_spiral/room/score_test.exs | 44 ++++++++--------- test/viral_spiral/room/state/room_test.exs | 2 +- test/viral_spiral/room/state/round_test.exs | 16 +++--- test/viral_spiral/room/state/turn_test.exs | 16 +++--- test/viral_spiral/rule_test.exs | 9 ---- test/viral_spiral/throwaway.exs | 15 ------ 14 files changed, 93 insertions(+), 153 deletions(-) delete mode 100644 test/viral_spiral/rule_test.exs delete mode 100644 test/viral_spiral/throwaway.exs diff --git a/lib/viral_spiral/card_share.ex b/lib/viral_spiral/card_share.ex index f6e5ab3..4eb8575 100644 --- a/lib/viral_spiral/card_share.ex +++ b/lib/viral_spiral/card_share.ex @@ -36,19 +36,18 @@ defimpl ViralSpiral.CardShare, for: ViralSpiral.Canon.Card.Affinity do end [ - {state.player_scores[from], - [type: :affinity, offset: affinity_offset, target: card.target]}, - {state.player_scores[from], [type: :clout, offset: 1]}, + {state.players[from], [type: :affinity, offset: affinity_offset, target: card.target]}, + {state.players[from], [type: :clout, offset: 1]}, {state.turn, [type: :next, target: to]} ] end # End the turn - def keep(_card, state, _from) do + def keep(_card, state, from) do [ {state.round, [type: :next]}, {state.turn, [type: :new, round: state.round]}, - {state.player_map, [type: :add_to_hand]} + {state.players[from], [type: :add_to_hand]} ] end @@ -68,7 +67,7 @@ defimpl ViralSpiral.CardShare, for: ViralSpiral.Canon.Card.Topical do # Update the turn def pass(_card, %Root{} = state, from, _to) do [ - {state.player_map[from], [type: :clout, offset: 1]}, + {state.players[from], [type: :clout, offset: 1]}, {state.turn, [type: :end]} ] end diff --git a/lib/viral_spiral/room/state/player.ex b/lib/viral_spiral/room/state/player.ex index 88bfff7..e4c9851 100644 --- a/lib/viral_spiral/room/state/player.ex +++ b/lib/viral_spiral/room/state/player.ex @@ -9,9 +9,10 @@ defmodule ViralSpiral.Room.State.Player do clout: 0 } """ + alias ViralSpiral.Room.State.Player.ActiveCardDoesNotExist + alias ViralSpiral.Room.State.Player.DuplicateActiveCardException alias ViralSpiral.Room.State.Player alias ViralSpiral.Bias - alias ViralSpiral.Deck.Card alias ViralSpiral.Game.EngineConfig import ViralSpiral.Game.EngineConfig.Guards @@ -32,7 +33,7 @@ defmodule ViralSpiral.Room.State.Player do clout: integer(), name: String.t(), identity: Bias.target(), - hand: list(Card.t()), + hand: list(), active_cards: list(String.t()) } @@ -50,7 +51,8 @@ defmodule ViralSpiral.Room.State.Player do id: UXID.generate!(prefix: "player", size: :small), identity: identity, biases: bias_map, - affinities: affinity_map + affinities: affinity_map, + clout: 0 } end @@ -158,10 +160,10 @@ defimpl ViralSpiral.Room.State.Change, for: ViralSpiral.Room.State.Player do end end -defmodule ViralSpiral.Room.Player.DuplicateActiveCardException do +defmodule ViralSpiral.Room.State.Player.DuplicateActiveCardException do defexception message: "This card is already held by the player" end -defmodule ViralSpiral.Room.Player.ActiveCardDoesNotExist do +defmodule ViralSpiral.Room.State.Player.ActiveCardDoesNotExist do defexception message: "This card is not an active card for this player " end diff --git a/lib/viral_spiral/room/state/root.ex b/lib/viral_spiral/room/state/root.ex index 3e717b7..135a824 100644 --- a/lib/viral_spiral/room/state/root.ex +++ b/lib/viral_spiral/room/state/root.ex @@ -14,17 +14,13 @@ defmodule ViralSpiral.Room.State.Root do alias ViralSpiral.Room.State.Round alias ViralSpiral.Room.State.Room alias ViralSpiral.Room.State.Player - alias ViralSpiral.Game.Room alias ViralSpiral.Game.EngineConfig alias ViralSpiral.Room.State.Root alias ViralSpiral.Room.State.Change defstruct engine_config: nil, room: nil, - player_list: nil, - player_map: nil, - room_score: nil, - player_scores: nil, + players: [], round: nil, turn: nil, deck: nil @@ -32,10 +28,7 @@ defmodule ViralSpiral.Room.State.Root do @type t :: %__MODULE__{ engine_config: EngineConfig.t(), room: Room.t(), - player_list: list(Player.t()), - player_map: map(), - room_score: Room.t(), - player_scores: map(), + players: list(Player.t()), round: Round.t() # turn: Turn.t(), # deck: Deck.t() @@ -65,7 +58,7 @@ defmodule ViralSpiral.Room.State.Root do defdelegate apply_change(change, state, opts), to: Change def get_target(%Root{} = state, %Player{id: id}) do - state.player_scores[id] + state.players[id] end def get_target(%Root{} = state, %Turn{} = turn) do @@ -81,8 +74,8 @@ defmodule ViralSpiral.Room.State.Root do end def put_target(%Root{} = state, %Player{id: id} = player) do - updated_player_map = Map.put(state.player_scores, id, player) - Map.put(state, :player_scores, updated_player_map) + updated_player_map = Map.put(state.players, id, player) + Map.put(state, :players, updated_player_map) end def put_target(%Root{} = state, %Round{} = round) do diff --git a/lib/viral_spiral/room/state/round.ex b/lib/viral_spiral/room/state/round.ex index 1d1c2dd..05e7bf3 100644 --- a/lib/viral_spiral/room/state/round.ex +++ b/lib/viral_spiral/room/state/round.ex @@ -24,10 +24,8 @@ defmodule ViralSpiral.Room.State.Round do # Doubt should it use game to initialize or players? """ + alias ViralSpiral.Room.State.Player alias ViralSpiral.Room.State.Change - alias ViralSpiral.Game.Player - - # alias ViralSpiral.Game.Player defstruct order: [], count: 0, @@ -59,6 +57,11 @@ defmodule ViralSpiral.Room.State.Round do } end + def new(players) when is_map(players) do + player_list = Map.keys(players) |> Enum.map(&players[&1]) + new(player_list) + end + @doc """ Skips a Player's `Turn`. diff --git a/test/support/fixtures.ex b/test/support/fixtures.ex index 0be1db6..c3294ae 100644 --- a/test/support/fixtures.ex +++ b/test/support/fixtures.ex @@ -1,23 +1,22 @@ defmodule Fixtures do + alias ViralSpiral.Room.State.Player alias ViralSpiral.Deck.Card - alias ViralSpiral.Game.Score.Player alias ViralSpiral.Room.State.Turn alias ViralSpiral.Room.State.Round alias ViralSpiral.Room.State.Room alias ViralSpiral.Game.EngineConfig - alias ViralSpiral.Room.State.Player, as: PlayerScore # alias ViralSpiral.Game.Score.Room, as: RoomScore - alias ViralSpiral.Game.Player + alias ViralSpiral.Room.State.Root def initialized_game() do - room_config = %EngineConfig{} + engine_config = %EngineConfig{} 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") + Player.new(engine_config) |> Player.set_name("adhiraj"), + Player.new(engine_config) |> Player.set_name("aman"), + Player.new(engine_config) |> Player.set_name("krys"), + Player.new(engine_config) |> Player.set_name("farah") ] players = Enum.reduce(player_list, %{}, fn player, acc -> Map.put(acc, player.id, player) end) @@ -25,41 +24,25 @@ defmodule Fixtures do round = Round.new(player_list) turn = Turn.new(round) - player_score_list = - Enum.map( - player_list, - &(Map.new() |> Map.put(:id, &1.id) |> Map.put(:score, PlayerScore.new(&1, room_config))) - ) - - player_score_map = - Enum.reduce(player_score_list, %{}, fn player, acc -> - Map.put(acc, player.id, player.score) - end) - %Root{ - room_config: room_config, + engine_config: engine_config, room: Room.new(), - player_map: players, - player_list: player_list, + players: players, round: round, - turn: turn, - # room_score: RoomScore.new(), - player_scores: player_score_map + turn: turn } end - def card_affinity() do - Card.new(:affinity) - end + def players() do + engine_config = %EngineConfig{} - def player_list() do - room_config = %EngineConfig{} - - [ - 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") + player_list = [ + Player.new(engine_config) |> Player.set_name("adhiraj"), + Player.new(engine_config) |> Player.set_name("aman"), + Player.new(engine_config) |> Player.set_name("krys"), + Player.new(engine_config) |> Player.set_name("farah") ] + + Enum.reduce(player_list, %{}, fn player, acc -> Map.put(acc, player.id, player) end) end end diff --git a/test/viral_spiral/card_share_test.exs b/test/viral_spiral/card_share_test.exs index d282e46..72d56b0 100644 --- a/test/viral_spiral/card_share_test.exs +++ b/test/viral_spiral/card_share_test.exs @@ -1,5 +1,5 @@ defmodule ViralSpiral.CardShareTest do - alias ViralSpiral.Game.State + alias ViralSpiral.Room.State.Root alias ViralSpiral.CardShare alias ViralSpiral.Affinity alias ViralSpiral.Canon.Deck @@ -19,20 +19,19 @@ defmodule ViralSpiral.CardShareTest do affinity_card = CardFixtures.affinity_card_true_anti_cat() changes = CardShare.pass(affinity_card, game_state, current_player_id, next_player_id) - new_state = State.apply_changes(game_state, changes) + new_state = Root.apply_changes(game_state, changes) - assert new_state.player_scores[current_player_id].affinities.cat == -1 - assert new_state.player_scores[current_player_id].clout == 1 + assert new_state.players[current_player_id].affinities.cat == -1 + assert new_state.players[current_player_id].clout == 1 affinity_card = CardFixtures.affinity_card_true_pro_cat() changes = CardShare.pass(affinity_card, game_state, current_player_id, next_player_id) - new_state = State.apply_changes(game_state, changes) + new_state = Root.apply_changes(game_state, changes) - assert new_state.player_scores[current_player_id].affinities.cat == 1 - assert new_state.player_scores[current_player_id].clout == 1 + assert new_state.players[current_player_id].affinities.cat == 1 + assert new_state.players[current_player_id].clout == 1 end - @tag timeout: :infinity test "keeping an affinity card does not change player's score", state do game_state = state.game round = game_state.round @@ -43,7 +42,7 @@ defmodule ViralSpiral.CardShareTest do affinity_card = CardFixtures.affinity_card_true_anti_cat() changes = CardShare.keep(affinity_card, game_state, current_player_id) - new_state = State.apply_changes(game_state, changes) + new_state = Root.apply_changes(game_state, changes) assert new_state.turn.current == next_turn_player_id assert new_state.round.current == 1 end @@ -58,7 +57,7 @@ defmodule ViralSpiral.CardShareTest do affinity_card = CardFixtures.affinity_card_true_anti_cat() changes = CardShare.keep(affinity_card, game_state, current_player_id) - new_state = State.apply_changes(game_state, changes) + new_state = Root.apply_changes(game_state, changes) assert new_state.turn.current == next_turn_player_id assert new_state.round.current == 1 diff --git a/test/viral_spiral/gameplay_test.exs b/test/viral_spiral/gameplay_test.exs index 7a367b4..61d4b07 100644 --- a/test/viral_spiral/gameplay_test.exs +++ b/test/viral_spiral/gameplay_test.exs @@ -9,21 +9,6 @@ defmodule ViralSpiral.GameTest do end test "passing an affinity card changes the player's clout and affinity", %{state: game_state} do - players = game_state.player_map - round = game_state.round - turn = game_state.turn - room_score = game_state.room_score - player_scores = game_state.player_scores - - card = Fixtures.card_affinity() - - current_player = players[turn.current] - target_player = players[Enum.at(turn.pass_to, 2)] - - Game.pass_card(game_state, card, current_player, target_player) - - # IO.inspect(game_state) - # IO.inspect(card) end end end diff --git a/test/viral_spiral/room/player_test.exs b/test/viral_spiral/room/player_test.exs index 42a2e30..e8a528e 100644 --- a/test/viral_spiral/room/player_test.exs +++ b/test/viral_spiral/room/player_test.exs @@ -1,8 +1,8 @@ defmodule ViralSpiral.Game.PlayerTest do + alias ViralSpiral.Room.State.Player.ActiveCardDoesNotExist + alias ViralSpiral.Room.State.Player.DuplicateActiveCardException + alias ViralSpiral.Room.State.Player alias ViralSpiral.Room.State.Change - alias ViralSpiral.Room.Player.ActiveCardDoesNotExist - alias ViralSpiral.Room.Player.DuplicateActiveCardException - alias ViralSpiral.Game.Player alias ViralSpiral.Game.EngineConfig use ExUnit.Case diff --git a/test/viral_spiral/room/score_test.exs b/test/viral_spiral/room/score_test.exs index ed4745e..d7663c2 100644 --- a/test/viral_spiral/room/score_test.exs +++ b/test/viral_spiral/room/score_test.exs @@ -1,50 +1,48 @@ defmodule ViralSpiral.Game.ScoreTest do - alias ViralSpiral.Game.Score.Player, as: PlayerScore + alias ViralSpiral.Room.State.Player alias ViralSpiral.Game.EngineConfig - alias ViralSpiral.Game.Player use ExUnit.Case setup_all do - room_config = %EngineConfig{} - player = Player.new(room_config) |> Player.set_identity("yellow") - player_score = PlayerScore.new(player, room_config) + engine_config = %EngineConfig{} + player = Player.new(engine_config) |> Player.set_identity("yellow") - %{player: player, player_score: player_score} + %{player: player} end test "player should not have a bias against their own identity", state do - player_score = state.player_score + player = state.player - assert Enum.find(player_score.biases, &(&1 == "yellow")) == nil + assert Enum.find(player.biases, &(&1 == "yellow")) == nil end test "change player bias", state do - player_score = state.player_score + player = state.player - player_score = PlayerScore.change(player_score, :bias, :yellow, 3) - assert player_score.biases.yellow == 3 + player = Player.change(player, :bias, :yellow, 3) + assert player.biases.yellow == 3 - player_score = PlayerScore.change(player_score, :bias, :yellow, -2) - assert player_score.biases.yellow == 1 + player = Player.change(player, :bias, :yellow, -2) + assert player.biases.yellow == 1 end test "change player affinity", state do - player_score = state.player_score + player = state.player - player_score = PlayerScore.change(player_score, :affinity, :cat, 5) - assert player_score.affinities.cat == 5 + player = Player.change(player, :affinity, :cat, 5) + assert player.affinities.cat == 5 - player_score = PlayerScore.change(player_score, :affinity, :cat, -2) - assert player_score.affinities.cat == 3 + player = Player.change(player, :affinity, :cat, -2) + assert player.affinities.cat == 3 end test "change player clout", state do - player_score = state.player_score + player = state.player - player_score = PlayerScore.change(player_score, :clout, 3) - assert player_score.clout == 3 + player = Player.change(player, :clout, 3) + assert player.clout == 3 - player_score = PlayerScore.change(player_score, :clout, -2) - assert player_score.clout == 1 + player = Player.change(player, :clout, -2) + assert player.clout == 1 end end diff --git a/test/viral_spiral/room/state/room_test.exs b/test/viral_spiral/room/state/room_test.exs index 24e0f33..612d3c4 100644 --- a/test/viral_spiral/room/state/room_test.exs +++ b/test/viral_spiral/room/state/room_test.exs @@ -21,7 +21,7 @@ defmodule ViralSpiral.Room.State.RoomTest do test "pass opts without required fields", %{room: room} do assert_raise ArgumentError, fn -> - Change.apply_change(room, invalid: "random") + Change.apply_change(room, nil, invalid: "random") end end end diff --git a/test/viral_spiral/room/state/round_test.exs b/test/viral_spiral/room/state/round_test.exs index d158052..e6c418f 100644 --- a/test/viral_spiral/room/state/round_test.exs +++ b/test/viral_spiral/room/state/round_test.exs @@ -5,8 +5,8 @@ defmodule ViralSpiral.Room.State.RoundTest do describe "round progression" do test "round progress" do - %{player_list: player_list} = Fixtures.initialized_game() - round = Round.new(player_list) + %{players: players} = Fixtures.initialized_game() + round = Round.new(players) assert round.current == 0 round = Round.next(round) @@ -25,8 +25,8 @@ defmodule ViralSpiral.Room.State.RoundTest do describe "skip player in a round" do test "if the player hasn't had their turn in this round, skip them in the current round itself" do - %{player_list: player_list} = Fixtures.initialized_game() - round = Round.new(player_list) + %{players: players} = Fixtures.initialized_game() + round = Round.new(players) player_order = round.order to_skip = Enum.at(player_order, 2) @@ -40,8 +40,8 @@ defmodule ViralSpiral.Room.State.RoundTest do end test "if the player has had their turn in the active round, skip their turn in the next round" do - %{player_list: player_list} = Fixtures.initialized_game() - round = Round.new(player_list) + %{players: players} = Fixtures.initialized_game() + round = Round.new(players) player_order = round.order assert round.current == 0 @@ -74,8 +74,8 @@ defmodule ViralSpiral.Room.State.RoundTest do describe "changes" do setup do - player_list = Fixtures.player_list() - round = Round.new(player_list) + players = Fixtures.players() + round = Round.new(players) %{round: round} end diff --git a/test/viral_spiral/room/state/turn_test.exs b/test/viral_spiral/room/state/turn_test.exs index 2c25c3f..0676c03 100644 --- a/test/viral_spiral/room/state/turn_test.exs +++ b/test/viral_spiral/room/state/turn_test.exs @@ -5,30 +5,32 @@ defmodule ViralSpiral.Room.State.TurnTest do use ExUnit.Case describe "turn progression" do + @tag timeout: :infinity test "pass card" do game = Fixtures.initialized_game() - player_list = game.player_list - round = Round.new(player_list) + players = game.players + round = Round.new(players) 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)) + Map.keys(players) + |> Enum.filter(&(&1 != current_player)) |> Enum.shuffle() |> Enum.take(1) |> Enum.at(0) - turn = Turn.next(turn, pass_to.id) + turn = Turn.next(turn, pass_to) 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) + players = game.players + round = Round.new(players) turn = Turn.new(round) assert length(turn.pass_to) == 3 @@ -43,7 +45,7 @@ defmodule ViralSpiral.Room.State.TurnTest do describe "changes" do setup do - players = Fixtures.player_list() + players = Fixtures.players() round = Round.new(players) turn = Turn.new(round) diff --git a/test/viral_spiral/rule_test.exs b/test/viral_spiral/rule_test.exs deleted file mode 100644 index 1cb421c..0000000 --- a/test/viral_spiral/rule_test.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule ViralSpiral.RuleTest do - use ExUnit.Case - - describe "card passing rules" do - test "you can only pass card to people after you in the turn order" do - _game = Fixtures.initialized_game() - end - end -end diff --git a/test/viral_spiral/throwaway.exs b/test/viral_spiral/throwaway.exs deleted file mode 100644 index 658b7c6..0000000 --- a/test/viral_spiral/throwaway.exs +++ /dev/null @@ -1,15 +0,0 @@ -defmodule ViralSpiral.Throwaway do - alias ViralSpiral.Game - - describe "draw cards" do - test "draw unused card" do - game = Game.new() - - assert game.id != nil - assert game.deck.status == :loading - # wait for deck to load - - # game = game |> Game.add_player() - end - end -end