Skip to content

Commit

Permalink
feat: all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Sep 12, 2024
1 parent 442da3a commit 3db2d1c
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 153 deletions.
11 changes: 5 additions & 6 deletions lib/viral_spiral/card_share.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions lib/viral_spiral/room/state/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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())
}

Expand All @@ -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

Expand Down Expand Up @@ -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
17 changes: 5 additions & 12 deletions lib/viral_spiral/room/state/root.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,21 @@ 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

@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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/viral_spiral/room/state/round.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`.
Expand Down
55 changes: 19 additions & 36 deletions test/support/fixtures.ex
Original file line number Diff line number Diff line change
@@ -1,65 +1,48 @@
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)

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
19 changes: 9 additions & 10 deletions test/viral_spiral/card_share_test.exs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 0 additions & 15 deletions test/viral_spiral/gameplay_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions test/viral_spiral/room/player_test.exs
Original file line number Diff line number Diff line change
@@ -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

Expand Down
44 changes: 21 additions & 23 deletions test/viral_spiral/room/score_test.exs
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 3db2d1c

Please sign in to comment.