-
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 test for dynamic_card and canon.
- Loading branch information
1 parent
4227c27
commit 696e218
Showing
10 changed files
with
271 additions
and
26 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 |
---|---|---|
@@ -1,8 +1,27 @@ | ||
defmodule ViralSpiral.Affinity do | ||
alias ViralSpiral.Affinity | ||
import ViralSpiral.Game.EngineConfig.Guards | ||
|
||
defstruct target: nil | ||
|
||
@type target :: :cat | :sock | :highfive | :houseboat | :skub | ||
@labels %{ | ||
cat: "Cat", | ||
sock: "Sock", | ||
high_five: "High Five", | ||
houseboat: "Houseboat", | ||
skub: "Skub" | ||
} | ||
|
||
@type target :: :cat | :sock | :high_five | :houseboat | :skub | ||
@type t :: %__MODULE__{ | ||
target: target() | ||
} | ||
|
||
def label(target) when is_affinity(target) do | ||
@labels[target] | ||
end | ||
|
||
def label(%Affinity{} = affinity) do | ||
@labels[affinity.target] | ||
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 |
---|---|---|
@@ -1,8 +1,24 @@ | ||
defmodule ViralSpiral.Bias do | ||
alias ViralSpiral.Bias | ||
import ViralSpiral.Game.EngineConfig.Guards | ||
defstruct target: nil | ||
|
||
@type target :: :red | :yellow | :blue | ||
@type t :: %__MODULE__{ | ||
target: target() | ||
} | ||
|
||
@labels %{ | ||
red: "Red", | ||
yellow: "Yellow", | ||
blue: "Blue" | ||
} | ||
|
||
def label(target) when is_community(target) do | ||
@labels[target] | ||
end | ||
|
||
def label(%Bias{} = affinity) do | ||
@labels[affinity.target] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
defmodule ViralSpiral.Canon.DynamicCard do | ||
@moduledoc """ | ||
Changes card text dynamically. | ||
Viral Spiral card texts have placeholders that get replaced at runtime by the game engine. We call these cards to be having dynamic text. Some examples of dynamic texts in cards are : | ||
- City revokes docking privileges for (other community) HoBos - "If they like the water so much they can stay there! | ||
- (oppressed community) ghetto vandalized, burned down during hilarious (popular affinity) day parade hooliganism | ||
Supported placeholders are : (other community), (dominant community), (oppressed community), (unpopular affinity), (popular affinity). | ||
## Example Usage | ||
headline = "People who like (unpopular affinity) are usually (dominant community)" | ||
matches = DynamicCard.find_placeholders(headline) | ||
replacements = %{ | ||
unpopular_affinity: :skub, | ||
dominant_community: :red | ||
} | ||
new_headline = DynamicCard.replace_text(headline, matches, replacements) | ||
In practice you'd require visibility into the game state to create the replacements map show above. This falls under the responsibility of `ViralSpiral.Room.State.Analytics` | ||
""" | ||
alias ViralSpiral.Bias | ||
alias ViralSpiral.Affinity | ||
|
||
@mappings [ | ||
{"(other community)", :other_community}, | ||
{"(dominant community)", :dominant_community}, | ||
{"(oppressed community)", :oppressed_community}, | ||
{"(unpopular affinity)", :unpopular_affinity}, | ||
{"(popular affinity)", :popular_affinity} | ||
] | ||
@string_to_atom_map Enum.reduce(@mappings, %{}, fn x, acc -> | ||
Map.put(acc, elem(x, 0), elem(x, 1)) | ||
end) | ||
@atom_to_string_map Enum.reduce(@mappings, %{}, fn x, acc -> | ||
Map.put(acc, elem(x, 1), elem(x, 0)) | ||
end) | ||
|
||
def find_placeholders(headline) do | ||
results = | ||
Regex.scan( | ||
~r/(\(oppressed community\)|\(popular affinity\)|\(unpopular affinity\)|\(other community\)|\(dominant community\))/, | ||
headline | ||
) | ||
|
||
results | ||
|> Enum.map(&Enum.at(&1, 0)) | ||
|> Enum.map(&@string_to_atom_map[&1]) | ||
end | ||
|
||
def replace_text(headline, matches, replacements) do | ||
Enum.reduce( | ||
matches, | ||
headline, | ||
fn el, acc -> | ||
String.replace(acc, @atom_to_string_map[el], label(replacements[el])) | ||
end | ||
) | ||
end | ||
|
||
defp label(atom) do | ||
case atom do | ||
x when x in [:cat, :skub, :high_five, :houseboat, :sock] -> Affinity.label(atom) | ||
y when y in [:red, :yellow, :blue] -> Bias.label(atom) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defmodule ViralSpiral.Room.State.Analytics do | ||
@moduledoc """ | ||
Find insights into the room durin gameplay. | ||
As part of the gameplay we often need to know information like who is winning the game or which community is performing well etc. This module provides helpers for that | ||
""" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule ViralSpiral.Canon.DynamicCardTest do | ||
use ExUnit.Case | ||
alias ViralSpiral.Canon.DynamicCard | ||
|
||
test "find placeholder text for (other community)" do | ||
headline = "(other community) club shover crime spree continues - another rave in disarray" | ||
matches = DynamicCard.find_placeholders(headline) | ||
assert Enum.at(matches, 0) == :other_community | ||
end | ||
|
||
test "find placeholder text for (dominant community)" do | ||
headline = "(dominant community) club shover crime spree continues - another rave in disarray" | ||
matches = DynamicCard.find_placeholders(headline) | ||
assert Enum.at(matches, 0) == :dominant_community | ||
end | ||
|
||
test "find placeholder text for (oppressed community)" do | ||
headline = | ||
"City announces discriminatory rating system to filter 'productive' members of (oppressed community) community from others" | ||
|
||
matches = DynamicCard.find_placeholders(headline) | ||
assert Enum.at(matches, 0) == :oppressed_community | ||
end | ||
|
||
test "find placeholder text for (popuar affinity)" do | ||
headline = "People who like (popular affinity) are good" | ||
|
||
matches = DynamicCard.find_placeholders(headline) | ||
assert Enum.at(matches, 0) == :popular_affinity | ||
end | ||
|
||
test "find placeholder text for (unpopuar affinity)" do | ||
headline = "People who like (unpopular affinity) are good" | ||
|
||
matches = DynamicCard.find_placeholders(headline) | ||
assert Enum.at(matches, 0) == :unpopular_affinity | ||
end | ||
|
||
test "replace multiple placeholder text" do | ||
headline = "People who like (unpopular affinity) are usually (dominant community)" | ||
|
||
matches = DynamicCard.find_placeholders(headline) | ||
|
||
replacements = %{ | ||
unpopular_affinity: :skub, | ||
dominant_community: :red | ||
} | ||
|
||
new_headline = DynamicCard.replace_text(headline, matches, replacements) | ||
assert new_headline == "People who like Skub are usually Red" | ||
end | ||
end |
Oops, something went wrong.