Skip to content

Commit

Permalink
feat: support dynamic card during draw_card
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Jan 16, 2025
1 parent 8b05201 commit a6df1ce
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 29 deletions.
34 changes: 32 additions & 2 deletions lib/viral_spiral/canon/dynamic_card.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ defmodule ViralSpiral.Canon.DynamicCard do
| :dominant_community
| :oppressed_community
| :unpopular_affinity
| :popular_community
| :popular_affinity
@type replacements :: %{
optional(:other_community) => Bias.target(),
optional(:dominant_community) => Bias.target(),
optional(:oppressed_community) => Bias.target(),
optional(:unpopular_affinity) => Affinity.target(),
optional(:popular_community) => Affinity.target()
optional(:popular_affinity) => Affinity.target()
}

@doc """
Expand Down Expand Up @@ -91,4 +91,34 @@ defmodule ViralSpiral.Canon.DynamicCard do
y when y in [:red, :yellow, :blue] -> Bias.label(atom)
end
end

@doc """
Returns `true` if the card headline contains placeholder for dynamic content.
"""
def valid?(headline) do
results =
Regex.scan(
~r/(\(oppressed community\)|\(popular affinity\)|\(unpopular affinity\)|\(other community\)|\(dominant community\))/,
headline
)

results
|> Enum.map(&Enum.at(&1, 0))

length(results) != 0
end

@doc """
TODO : return a Card. Because this will need to update bias, affinity etc as well.
"""
def patch(headline, gamestate_analytics) do

Check warning on line 114 in lib/viral_spiral/canon/dynamic_card.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "gamestate_analytics" is unused (if the variable is not meant to be used, prefix it with an underscore)
matches = find_placeholders(headline)

replacements = %{
unpopular_affinity: :skub,
dominant_community: :red
}

replace_text(headline, matches, replacements)
end
end
23 changes: 23 additions & 0 deletions lib/viral_spiral/room/analytics.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule ViralSpiral.Room.Analytics do
alias ViralSpiral.Room.State

@doc """
identity of the player with most clout
"""
def dominant_community(%State{} = state) do
end
end

defmodule ViralSpiral.Room.Analytics.GameState do
alias ViralSpiral.Room.State

def analytics(%State{} = _state) do
%{
unpopular_affinity: :skub,
popular_affinity: :houseboat,
dominant_community: :red,
other_community: :blue,
oppressed_community: :yellow
}
end
end
14 changes: 11 additions & 3 deletions lib/viral_spiral/room/reducer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ defmodule ViralSpiral.Room.Reducer do
@moduledoc """
"""
alias ViralSpiral.Room.Analytics.GameState
alias ViralSpiral.Canon.DynamicCard
alias ViralSpiral.Entity.Source
alias ViralSpiral.Entity.CheckSource
alias ViralSpiral.Canon.Card.Sparse
alias ViralSpiral.Entity.PowerViralSpiral
alias ViralSpiral.Canon.Encyclopedia
alias ViralSpiral.Room.Factory
alias ViralSpiral.Playable
alias ViralSpiral.Room.State
alias ViralSpiral.Room.ChangeDescriptions
alias ViralSpiral.Canon.DrawTypeRequirements
alias ViralSpiral.Canon.Deck
alias ViralSpiral.Room.Action

Expand All @@ -22,6 +21,15 @@ defmodule ViralSpiral.Room.Reducer do
draw_type = action.payload.draw_type
sets = state.deck.available_cards
draw_result = Deck.draw_card(sets, draw_type)
card = state.deck.store[{draw_result.id, draw_type[:veracity]}]

gamestate_analytics = GameState.analytics(state)

# headline =
# case DynamicCard.valid?(card.headline) do
# true -> DynamicCard.patch(card.headline, gamestate_analytics)
# false -> card.headline
# end

changes =
[
Expand Down
57 changes: 35 additions & 22 deletions lib/viral_spiral_web/live/game_room.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,56 @@
<div
:for={player <- @state.players}
class={[
"p-2 border-2 w-full rounded-xl m-2",
"border-2 w-full m-2 bg-clip-border",
if(player.is_active,
do: "border-red-400",
else: "border-red-100"
)
]}
>
<span><%= player.name %></span>
<div class={[
"w-full px-2 py-2",
if(player.is_active,
do: "bg-red-400",
else: "bg-red-100"
)
]}>
<p><%= player.name %></p>
</div>

<%!-- <span class="ml-6"><%= player.identity %></span> --%>
<div class="h-2"></div>
<div>
<h2 class="font-semibold">Clout</h2>
<span><%= player.clout %></span>
</div>
<div class="h-2"></div>
<div>
<h2 class="font-semibold">Affinities</h2>
<div :for={affinity <- player.affinities}>
<span><%= elem(affinity, 0) %></span>
<span><%= elem(affinity, 1) %></span>
</div>

<div class="h-2"></div>
<h2 class="font-semibold">Bias</h2>
<div :for={bias <- player.biases}>
<span><%= elem(bias, 0) %></span>
<span><%= elem(bias, 1) %></span>
<div class="p-2">
<div class="flex flex-row gap-8 flex-wrap">
<div>
<h2 class="font-semibold">Clout</h2>
<span><%= player.clout %></span>
</div>

<div>
<h2 class="font-semibold">Affinities</h2>
<div :for={affinity <- player.affinities}>
<span><%= elem(affinity, 0) %></span>
<span><%= elem(affinity, 1) %></span>
</div>
</div>

<div>
<h2 class="font-semibold">Biases</h2>
<div :for={bias <- player.biases}>
<span><%= elem(bias, 0) %></span>
<span><%= elem(bias, 1) %></span>
</div>
</div>
</div>

<div class="h-4"></div>
<div class="mt-4"></div>

<div :for={card <- player.cards} :if={player.is_active}>
<.card card={card} from={player.id}></.card>
</div>
</div>

<%!-- <div class="h-4"></div>
<%!-- <div class="h-4"></div>
<%= if current_player?(@state, player) do %>
<.card>
<p><%= card(@state, player).headline %></p>
Expand All @@ -62,6 +74,7 @@
<span class="mr-4"><%= pass_to %></span>
<% end %>
<% end %> --%>
</div>
</div>
</div>
</div>
3 changes: 1 addition & 2 deletions test/viral_spiral/room/reducer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ defmodule ViralSpiral.Room.ReducerTest do

assert Deck.size(new_state.deck.available_cards, draw_type) == 59

current_player = State.current_round_player(state)
current_player = State.current_round_player(new_state)
assert length(current_player.active_cards) == 1
IO.inspect(current_player)
end

test "keep_card" do
Expand Down
75 changes: 75 additions & 0 deletions test/viral_spiral/room/state_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,79 @@
defmodule ViralSpiral.Room.StateTest do
alias ViralSpiral.Game.State
use ExUnit.Case

describe "card" 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: 0,
skip: nil
},
turn: %Turn{
card: nil,
current: "player_jkl",
pass_to: ["player_abc", "player_def", "player_ghi"]
}
}

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

%{state: state}
end
end
end

0 comments on commit a6df1ce

Please sign in to comment.