Skip to content

Commit

Permalink
feat: mark as fake power
Browse files Browse the repository at this point in the history
- added reducer and a test for it
- modified Turn struct
- created necessary Action
  • Loading branch information
dennyabrain committed Jan 18, 2025
1 parent a6df1ce commit 5a8dfd0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/viral_spiral/entity/turn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ defmodule ViralSpiral.Entity.Turn do

defstruct card: nil,
current: nil,
pass_to: []
pass_to: [],
# track the order in which this card has been passed around
path: []

@type t :: %__MODULE__{
card: Sparse.t(),
current: String.t() | nil,
pass_to: list(String.t())
pass_to: list(String.t()),
path: list(String.t())
}

@spec new() :: Turn.t()
Expand All @@ -44,6 +47,8 @@ defmodule ViralSpiral.Entity.Turn do

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

def set_path(%Turn{} = turn, path), do: %{turn | path: path}

@doc """
todo : add check to ensure that it only runs next if
to is in the the current pass_to
Expand All @@ -53,6 +58,7 @@ defmodule ViralSpiral.Entity.Turn do
from
|> set_current(to)
|> set_pass_to(List.delete(from.pass_to, to))
|> set_path(List.insert_at(from.path, -1, from.current))
end

@spec next(Turn.t(), list(String.t())) :: list(Turn.t())
Expand All @@ -74,3 +80,7 @@ defmodule ViralSpiral.Entity.Turn do
end
end
end

defmodule ViralSpiral.Entity.Turn.IllegalPass do
defexception message: "This player can't be passed to in this Turn"
end
13 changes: 13 additions & 0 deletions lib/viral_spiral/room/actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ defmodule ViralSpiral.Room.Actions do
@moduledoc """
Instances of Action triggered by a Player or Game Engine .
"""
alias ViralSpiral.Canon.Card.Sparse
alias ViralSpiral.Room.Action
alias ViralSpiral.Entity.Turn

def draw_card(draw_type) do
%Action{type: :draw_card, payload: %{draw_type: draw_type}}
Expand Down Expand Up @@ -84,4 +86,15 @@ defmodule ViralSpiral.Room.Actions do
}
}
end

def mark_card_as_fake(from, %Sparse{} = card, %Turn{} = turn) do
%Action{
type: :mark_card_as_fake,
payload: %{
from: from,
card: card,
turn: turn
}
}
end
end
13 changes: 13 additions & 0 deletions lib/viral_spiral/room/reducer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ defmodule ViralSpiral.Room.Reducer do
])
end

def reduce(%State{} = state, %Action{type: :mark_card_as_fake} = action) do
%{from: from, card: card, turn: turn} = action.payload

clout_penalty_change =
if card.veracity == false,
do: {state.players[Enum.at(turn.path, 1)], ChangeDescriptions.change_clout(-1)},
else: {state.players[from.id], ChangeDescriptions.change_clout(-1)}

State.apply_changes(state, [clout_penalty_change])

# todo : will the card be kept/discarded at this point?
end

def reduce(%State{} = state, %{type: :draw_card} = action) do
end

Expand Down
102 changes: 102 additions & 0 deletions test/viral_spiral/room/reducer_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule ViralSpiral.Room.ReducerTest do
alias ViralSpiral.Entity.Turn
alias ViralSpiral.Entity.Round
alias ViralSpiral.Entity.Player
alias ViralSpiral.Canon.Card.Sparse
alias ViralSpiral.Canon.Article
alias ViralSpiral.Entity.PlayerMap
alias ViralSpiral.Room.Factory
Expand Down Expand Up @@ -182,4 +186,102 @@ defmodule ViralSpiral.Room.ReducerTest do
# # IO.inspect(store.players)
# # IO.inspect(store.room)
# end

describe "mark as fake" do
setup do
state = %State{
room: %Room{
id: "room_abc",
name: "crazy-house-3213",
state: :running,
unjoined_players: [],
affinities: [:skub, :houseboat],
communities: [:red, :yellow, :blue],
chaos: 0,
chaos_counter: 10,
volatality: :medium
},
players: %{
"player_abc" => %Player{
id: "player_abc",
name: "farah",
biases: %{yellow: 0, blue: 0},
affinities: %{skub: 0, houseboat: 0},
clout: 0,
identity: :red,
hand: [],
active_cards: []
},
"player_def" => %Player{
id: "player_def",
name: "aman",
biases: %{red: 0, blue: 0},
affinities: %{skub: 0, houseboat: 0},
clout: 0,
identity: :yellow,
hand: [],
active_cards: []
},
"player_ghi" => %Player{
id: "player_ghi",
name: "krys",
biases: %{yellow: 0, blue: 0},
affinities: %{skub: 0, houseboat: 0},
clout: 0,
identity: :red,
hand: [],
active_cards: []
},
"player_jkl" => %Player{
id: "player_jkl",
biases: %{yellow: 0, blue: 0},
affinities: %{skub: 0, houseboat: 0},
clout: 0,
name: "adhiraj",
identity: :red,
hand: [],
active_cards: []
}
},
round: %Round{
order: ["player_jkl", "player_ghi", "player_def", "player_abc"],
count: 4,
current: 1,
skip: nil
},
turn: %Turn{
current: "player_ghi",
pass_to: ["player_def"],
path: ["player_abc", "player_jkl"]
}
}

state = %{state | deck: Factory.new_deck(state.room)}

%{state: state}
end

# player_ghi has received a card from player_jkl
# We will force this card to be a true card and then have player_ghi mark it as fake
@tag timeout: :infinity
test "mark true card as fake", %{state: state} do
from = State.current_turn_player(state)
card = %Sparse{id: "card_121565043", veracity: true}
turn = state.turn

state = Reducer.reduce(state, Actions.mark_card_as_fake(from, card, turn))
assert state.players["player_ghi"].clout == -1
assert state.players["player_jkl"].clout == 0
end

test "mark false card as fake", %{state: state} do
from = State.current_turn_player(state)
card = %Sparse{id: "card_121565043", veracity: false}
turn = state.turn

state = Reducer.reduce(state, Actions.mark_card_as_fake(from, card, turn))
assert state.players["player_ghi"].clout == 0
assert state.players["player_jkl"].clout == -1
end
end
end

0 comments on commit 5a8dfd0

Please sign in to comment.