-
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.
feat: add entity and tests for cancel player
- Loading branch information
1 parent
1dee895
commit 4308ccb
Showing
3 changed files
with
113 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule ViralSpiral.Entity.PowerCancelPlayer do | ||
@moduledoc """ | ||
The Cancel Super Power. | ||
When a Player meets certain criteria, they are allowed to cancel another Player. This takes the following form : | ||
A player initiates cancellation by choosing which player they want to cancel and which of their affinity they want to use for it. The affinity they choose determines who can vote to cancel this player. If the majority votes to cancel the targetted Player, that player will skip a turn. | ||
""" | ||
alias ViralSpiral.Entity.PowerCancelPlayer | ||
alias ViralSpiral.Affinity | ||
import ViralSpiral.Room.EngineConfig.Guards | ||
|
||
defstruct state: :idle, | ||
target: nil, | ||
affinity: nil, | ||
votes: [], | ||
result: nil | ||
|
||
@type states :: :idle | :waiting | :done | ||
|
||
@typedoc "A player's vote for/against the target" | ||
@type vote :: %{id: String.t(), vote: boolean()} | ||
|
||
@type t :: %__MODULE__{ | ||
state: states(), | ||
target: String.t(), | ||
affinity: Affinity.t(), | ||
votes: list(vote()), | ||
result: boolean() | ||
} | ||
|
||
@spec start_vote(t(), String.t(), Affinity.target()) :: t() | ||
def start_vote(%PowerCancelPlayer{} = power, target, affinity) when is_affinity(affinity) do | ||
%{power | target: target, affinity: affinity, state: :waiting} | ||
end | ||
|
||
@spec vote(t(), String.t(), boolean()) :: t() | ||
def vote(%PowerCancelPlayer{} = power, player, vote, opts \\ []) do | ||
done = Keyword.get(opts, :done, false) | ||
state = if done, do: :done, else: power.state | ||
%{power | votes: power.votes ++ [%{id: player, vote: vote}], state: state} | ||
end | ||
|
||
@spec put_result(t()) :: t() | ||
def put_result(%PowerCancelPlayer{} = power) do | ||
total_votes = length(power.votes) | ||
true_votes = power.votes |> Enum.filter(& &1.vote) |> length() | ||
result = if true_votes / total_votes > 0.5, do: true, else: false | ||
%{power | result: result, state: :done} | ||
end | ||
|
||
def reset(%PowerCancelPlayer{} = _power) do | ||
%PowerCancelPlayer{} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule ViralSpiral.Entity.PowerCancelPlayerTest do | ||
alias ViralSpiral.Entity.PowerCancelPlayer | ||
import ViralSpiral.Entity.PowerCancelPlayer | ||
use ExUnit.Case | ||
|
||
describe "entity" do | ||
test "happy path" do | ||
power = %PowerCancelPlayer{} | ||
assert power.state == :idle | ||
|
||
power = power |> start_vote("player_abc", :cat) | ||
assert power.state == :waiting | ||
|
||
power = | ||
power | ||
|> vote("player_def", true) | ||
|> vote("player_jkl", true) | ||
|
||
assert power.votes |> length() == 2 | ||
|
||
power = power |> vote("player_lmn", false, done: true) | ||
assert power.votes |> length() == 3 | ||
assert power.state == :done | ||
|
||
power = power |> put_result() | ||
assert power.result == true | ||
|
||
power = power |> reset() | ||
assert power.state == :idle | ||
assert length(power.votes) == 0 | ||
end | ||
end | ||
|
||
describe "changes" do | ||
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