-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- create reducers and actions - create new entity and changes, with tests
- Loading branch information
1 parent
31c9a19
commit c2370e8
Showing
10 changed files
with
416 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
defmodule ViralSpiral.Entity.PowerViralSpiral do | ||
@moduledoc """ | ||
Struct used to conduct special power of viral spiral. | ||
When a user uses the power of viral spiral, they can pass a card from their hand to multiple players. This is a very special case of the game where a player can hold multiple cards and pass to other players. | ||
""" | ||
|
||
alias ViralSpiral.Canon.Card.Sparse | ||
alias ViralSpiral.Entity.Change | ||
alias ViralSpiral.Entity.Player | ||
alias ViralSpiral.Entity.PowerViralSpiral | ||
alias ViralSpiral.Entity.Turn | ||
defstruct turns: nil | ||
|
||
@type t :: %__MODULE__{ | ||
turns: list(Turn.t()) | ||
} | ||
|
||
def new(%Sparse{} = card, players) when is_list(players) do | ||
Enum.map( | ||
players, | ||
&%Turn{ | ||
card: %Sparse{id: card.id, veracity: card.veracity}, | ||
current: &1, | ||
pass_to: players | ||
} | ||
) | ||
end | ||
|
||
def get_turn(%PowerViralSpiral{} = power, player_id) do | ||
power.turns | ||
|> Enum.filter(&(&1.current == player_id)) | ||
|> hd | ||
end | ||
|
||
@doc """ | ||
Return turns other than the one of the passed player_id | ||
""" | ||
def other_turns(%PowerViralSpiral{} = power, player_id) do | ||
Enum.filter(power.turns, &(&1.current != player_id)) | ||
end | ||
|
||
def put_turn(%PowerViralSpiral{} = power, player_id, %Turn{} = turn) do | ||
ix = Enum.find_index(power.turns, &(&1.current == player_id)) | ||
|
||
new_turns = List.replace_at(power.turns, ix, turn) | ||
%{power | turns: new_turns} | ||
end | ||
|
||
def pass_to(%PowerViralSpiral{} = power, %Player{id: player_id}) do | ||
power.turns | ||
|> Enum.filter(&(&1.current == player_id)) | ||
|> hd | ||
|> Map.get(:pass_to) | ||
end | ||
|
||
def pass_to(%PowerViralSpiral{} = power, player_id) when is_bitstring(player_id) do | ||
power.turns | ||
|> Enum.filter(&(&1.current == player_id)) | ||
|> hd | ||
|> Map.get(:pass_to) | ||
end | ||
|
||
def reset(%PowerViralSpiral{}) do | ||
nil | ||
end | ||
|
||
defimpl Change do | ||
alias ViralSpiral.Canon.Card.Sparse | ||
alias ViralSpiral.Room.ChangeDescriptions | ||
|
||
def apply_change(state, change_desc) do | ||
case change_desc[:type] do | ||
:set -> | ||
players = change_desc[:players] | ||
card = change_desc[:card] | ||
|
||
PowerViralSpiral.new(card, players) | ||
|
||
:reset -> | ||
PowerViralSpiral.reset(state) | ||
|
||
:pass -> | ||
from = change_desc[:from] | ||
to = change_desc[:to] | ||
turn = PowerViralSpiral.get_turn(state, from) | ||
|
||
turn = Change.apply_change(turn, ChangeDescriptions.pass_turn_to(to)) | ||
|
||
# remove options from other player's turn | ||
other_turns = | ||
PowerViralSpiral.other_turns(state, from) | ||
|> Enum.map(&Map.put(&1, :pass_to, &1.pass_to -- [to])) | ||
|
||
new_turn = [turn] ++ other_turns | ||
|
||
# check if this no possible pass options remain | ||
case length(turn.pass_to) do | ||
0 -> nil | ||
_ -> %{state | turns: new_turn} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
test/support/store_fixtures.ex → test/support/state_fixtures.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
defmodule ViralSpiral.Entity.PowerViralSpiralTest do | ||
alias ViralSpiral.Room.ChangeDescriptions | ||
alias ViralSpiral.Entity.Change | ||
alias ViralSpiral.Canon.Card.Sparse | ||
alias ViralSpiral.Entity.Turn | ||
alias ViralSpiral.Entity.PowerViralSpiral | ||
use ExUnit.Case | ||
|
||
describe "entity" do | ||
setup do | ||
power = %PowerViralSpiral{ | ||
turns: [ | ||
%Turn{ | ||
card: %Sparse{id: "card_pqr", veracity: true}, | ||
current: "player_abc", | ||
pass_to: ["player_def", "player_ghi"] | ||
}, | ||
%Turn{ | ||
card: %Sparse{id: "card_pqr", veracity: true}, | ||
current: "player_jkl", | ||
pass_to: ["player_def", "player_ghi"] | ||
} | ||
] | ||
} | ||
|
||
%{power: power} | ||
end | ||
|
||
test "new", %{power: power} do | ||
assert power.turns |> length() == 2 | ||
assert PowerViralSpiral.get_turn(power, "player_jkl").current == "player_jkl" | ||
assert PowerViralSpiral.get_turn(power, "player_abc").current == "player_abc" | ||
|
||
assert PowerViralSpiral.pass_to(power, "player_jkl") == | ||
["player_def", "player_ghi"] | ||
end | ||
end | ||
|
||
describe "changes" do | ||
setup do | ||
power = %PowerViralSpiral{ | ||
turns: [ | ||
%Turn{ | ||
card: %Sparse{id: "card_pqr", veracity: true}, | ||
current: "player_abc", | ||
pass_to: ["player_def", "player_ghi"] | ||
}, | ||
%Turn{ | ||
card: %Sparse{id: "card_pqr", veracity: true}, | ||
current: "player_jkl", | ||
pass_to: ["player_def", "player_ghi"] | ||
} | ||
] | ||
} | ||
|
||
%{power: power} | ||
end | ||
|
||
test "reset", %{power: power} do | ||
power = Change.apply_change(power, ChangeDescriptions.PowerViralSpiral.reset()) | ||
assert power == nil | ||
end | ||
|
||
test "pass to someone", %{power: power} do | ||
power = | ||
Change.apply_change( | ||
power, | ||
ChangeDescriptions.PowerViralSpiral.pass("player_jkl", "player_ghi") | ||
) | ||
|
||
assert PowerViralSpiral.get_turn(power, "player_ghi").current == "player_ghi" | ||
assert PowerViralSpiral.pass_to(power, "player_ghi") == ["player_def"] | ||
assert PowerViralSpiral.pass_to(power, "player_abc") == ["player_def"] | ||
|
||
power = | ||
Change.apply_change( | ||
power, | ||
ChangeDescriptions.PowerViralSpiral.pass("player_abc", "player_def") | ||
) | ||
|
||
assert power == nil | ||
end | ||
end | ||
end |
Oops, something went wrong.