Skip to content

Commit

Permalink
feat: add reducer for view and hide source
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Dec 8, 2024
1 parent 024aaa0 commit 34ac373
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
16 changes: 15 additions & 1 deletion lib/viral_spiral/room/actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,24 @@ defmodule ViralSpiral.Room.Actions do
discard_card(card, from)
end

def view_source(card_id, card_veracity) do
def view_source(player_id, card_id, card_veracity) do
%Action{
type: :view_source,
payload: %{
player_id: player_id,
card: %{
id: card_id,
veracity: card_veracity
}
}
}
end

def hide_source(player_id, card_id, card_veracity) do
%Action{
type: :hide_source,
payload: %{
player_id: player_id,
card: %{
id: card_id,
veracity: card_veracity
Expand Down
43 changes: 20 additions & 23 deletions lib/viral_spiral/room/reducer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ defmodule ViralSpiral.Room.Reducer do
def reduce(%State{} = state, %{type: :keep_card} = action) do
%{player: from, card: card} = action.payload

state =
State.apply_changes(state, [
{state.players[from], ChangeDescriptions.remove_active(card.id, card.veracity)},
{state.round, [type: :next]},
{state.players[from], [type: :add_to_hand, card: card]}
])

State.apply_changes(state, [
{state.players[from], ChangeDescriptions.remove_active(card.id, card.veracity)},
{state.round, [type: :next]},
{state.players[from], [type: :add_to_hand, card: card]}
])
|> State.apply_changes([
{state.turn, [type: :new, round: state.round]}
])

# &{&1.turn, [type: :new, round: &1.round]}
end

def reduce(%State{} = state, %{type: :draw_card} = action) do
Expand All @@ -77,29 +77,26 @@ defmodule ViralSpiral.Room.Reducer do
def reduce(%State{} = state, %{type: :start_game}) do
end

def reduce(%State{} = state, %{type: :check_source} = action) do
card = action.payload.card
player = action.payload.player
def reduce(%State{} = state, %{type: :view_source} = action) do
%{card: card, player_id: player_id} = action.payload

article = state.deck.article_store
article_store = state.deck.article_store
article = Encyclopedia.get_article_by_card(article_store, card)
article_entity = Factory.make_entity_article(article)

changes = [
{state.articles[player.id], ChangeDescriptions.set_article(card, article_entity)}
]

State.apply_changes(state, changes)
%{
state
| articles: Map.put(state.articles, {player_id, card}, article_entity)
}
end

def reduce(%State{} = state, %{type: :hide_source} = action) do
# card = action.payload.card
# player = action.payload.player

# changes = [
# {state.articles[player.id], ChangeDescriptions.reset_article(card)}
# ]
%{card: card, player_id: player_id} = action.payload

# State.apply_changes(state, changes)
%{
state
| articles: Map.delete(state.articles, {player_id, card})
}
end

def reduce(%State{} = state, %{type: :turn_to_fake}) do
Expand Down
10 changes: 10 additions & 0 deletions lib/viral_spiral/room/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule ViralSpiral.Room.State do
When a round begins, we also start a Turn. Within each Round there's a turn that includes everyone except the person who started the turn.
"""

alias ViralSpiral.Entity.Article
alias ViralSpiral.Entity.PowerViralSpiral
alias ViralSpiral.Room.Factory
alias ViralSpiral.Entity.Deck
Expand Down Expand Up @@ -115,6 +116,10 @@ defmodule ViralSpiral.Room.State do
defp get_target(%State{} = state, %PowerViralSpiral{} = power) do
end

defp get_target(%State{} = state, %Article{id: id} = article) do
state.articles[id]
end

@doc """
Generalized way to get a nested entity from state.
Expand All @@ -133,6 +138,11 @@ defmodule ViralSpiral.Room.State do
Map.put(state, :players, updated_player_map)
end

defp put_target(%State{} = state, %Article{id: id} = article) do
updated_article_map = Map.put(state.articles, id, article)
Map.put(state, :articles, updated_article_map)
end

defp put_target(%State{} = state, %Round{} = round) do
Map.put(state, :round, round)
end
Expand Down

0 comments on commit 34ac373

Please sign in to comment.