Skip to content

Commit

Permalink
feat: view/hide source power
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Jan 14, 2025
1 parent 71e46dd commit c0539d0
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 99 deletions.
7 changes: 6 additions & 1 deletion lib/viral_spiral/canon/deck.ex
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,12 @@ defmodule ViralSpiral.Canon.Deck do
Returns a tuple that should be a valid key of a Store.
[
[type: :topical, veracity: false]
[type: :topical, veracity: true]
[type: :affinity, veracity: true, target: :skub]
[type: :bias, veracity: false, target: :yellow] and so on
deprecated : [
{:conflated, false},
{:topical, false},
{:topical, true},
Expand Down
32 changes: 32 additions & 0 deletions lib/viral_spiral/entity/check_source_map.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule ViralSpiral.Entity.CheckSource do
alias ViralSpiral.Entity.Change
alias ViralSpiral.Entity.CheckSource

defstruct [:map]

@type t :: %__MODULE__{
map: %{optional(String.t()) => CheckSource.t()}
}

def new() do
%CheckSource{map: %{}}
end

defimpl Change do
def apply_change(check_source, change_desc) do
case change_desc[:type] do
:put ->
%{
check_source
| map: Map.put(check_source.map, change_desc[:key], change_desc[:source])
}

:drop ->
%{
check_source
| map: Map.drop(check_source.map, [change_desc[:key]])
}
end
end
end
end
11 changes: 11 additions & 0 deletions lib/viral_spiral/entity/source.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule ViralSpiral.Entity.Source do
defstruct [:owner, :headline, :content, :author, :type]

@type t :: %__MODULE__{
owner: String.t(),
headline: String.t(),
content: String.t(),
author: String.t(),
type: String.t()
}
end
12 changes: 4 additions & 8 deletions lib/viral_spiral/room/actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ defmodule ViralSpiral.Room.Actions do
type: :view_source,
payload: %{
player_id: player_id,
card: %{
id: card_id,
veracity: card_veracity
}
card_id: card_id,
card_veracity: card_veracity
}
}
end
Expand All @@ -71,10 +69,8 @@ defmodule ViralSpiral.Room.Actions do
type: :hide_source,
payload: %{
player_id: player_id,
card: %{
id: card_id,
veracity: card_veracity
}
card_id: card_id,
card_veracity: card_veracity
}
}
end
Expand Down
27 changes: 17 additions & 10 deletions lib/viral_spiral/room/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ defmodule ViralSpiral.Room.Factory do
}
end

def make_entity_article(%Article{} = article) do
%EntityArticle{
headline: article.headline,
veracity: article.veracity,
type: article.type,
content: article.content,
author: article.author
}
end

def new_game() do
%State{
room: Room.new()
Expand All @@ -155,6 +145,15 @@ defmodule ViralSpiral.Room.Factory do
Reducer.reduce(state, Actions.draw_card(draw_type))
end

@doc """
draw_type is a tuple.
For more, visit `ViralSpiral.Canon.Deck.draw_type/1`
"""
def draw_card(%State{} = state, draw_type) do
Reducer.reduce(state, Actions.draw_card(draw_type))
end

def pass_card(%State{} = state, %Sparse{} = card, from, to) do
Reducer.reduce(state, Actions.pass_card(card.id, card.veracity, from, to))
end
Expand All @@ -165,4 +164,12 @@ defmodule ViralSpiral.Room.Factory do

def discard_card(%State{} = state, %Sparse{} = card, from) do
end

def view_source(%State{} = state, player_id, card_id, card_veracity) do
Reducer.reduce(state, Actions.view_source(player_id, card_id, card_veracity))
end

def close_source(%State{} = state, player_id, card_id, card_veracity) do
Reducer.reduce(state, Actions.hide_source(player_id, card_id, card_veracity))
end
end
41 changes: 27 additions & 14 deletions lib/viral_spiral/room/reducer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ defmodule ViralSpiral.Room.Reducer do
@moduledoc """
"""
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
Expand Down Expand Up @@ -78,25 +81,35 @@ defmodule ViralSpiral.Room.Reducer do
end

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

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

%{
state
| articles: Map.put(state.articles, {player_id, card}, article_entity)
%{card_id: card_id, card_veracity: card_veracity, player_id: player_id} = action.payload
card = Sparse.new({card_id, card_veracity})
article = Encyclopedia.get_article_by_card(state.deck.article_store, card)

key = "#{player_id}_#{card_id}_#{card_veracity}"

source = %Source{
owner: player_id,
headline: article.headline,
content: article.content,
author: article.author,
type: article.type
}

state
|> State.apply_changes([
{state.power_check_source, [type: :put, key: key, source: source]}
])
end

def reduce(%State{} = state, %{type: :hide_source} = action) do
%{card: card, player_id: player_id} = action.payload
%{card_id: card_id, card_veracity: card_veracity, player_id: player_id} = action.payload

%{
state
| articles: Map.delete(state.articles, {player_id, card})
}
key = "#{player_id}_#{card_id}_#{card_veracity}"

state
|> State.apply_changes([
{state.power_check_source, [type: :drop, key: key]}
])
end

def reduce(%State{} = state, %Action{type: :turn_card_to_fake} = action) do
Expand Down
146 changes: 80 additions & 66 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.CheckSource
alias ViralSpiral.Entity.Article
alias ViralSpiral.Entity.PowerViralSpiral
alias ViralSpiral.Room.Factory
Expand All @@ -23,14 +24,16 @@ defmodule ViralSpiral.Room.State do
alias ViralSpiral.Room.State
alias ViralSpiral.Entity.Change

@derive {Inspect, limit: 2}
defstruct room: nil,
players: %{},
round: nil,
turn: nil,
turns: %{},
deck: nil,
articles: %{},
power_viralspiral: nil
power_viralspiral: nil,
power_check_source: CheckSource.new()

@type t :: %__MODULE__{
room: Room.t(),
Expand All @@ -39,7 +42,8 @@ defmodule ViralSpiral.Room.State do
turn: Turn.t(),
deck: Deck.t(),
articles: map(),
power_viralspiral: PowerViralSpiral.t()
power_viralspiral: PowerViralSpiral.t(),
power_check_source: CheckSource.t()
}

def empty() do
Expand Down Expand Up @@ -84,6 +88,8 @@ defmodule ViralSpiral.Room.State do
# list({:ok, message :: String.t()} | {:error, reason :: String.t()})
def apply_changes(state, changes) do
Enum.reduce(changes, state, fn change, state ->
# require IEx
# IEx.pry()
data = get_target(state, elem(change, 0))
change_desc = elem(change, 1)
new_value = apply_change(data, change_desc)
Expand Down Expand Up @@ -116,6 +122,10 @@ defmodule ViralSpiral.Room.State do
defp get_target(%State{} = state, %PowerViralSpiral{} = power) do
end

defp get_target(%State{} = state, %CheckSource{} = _check_source) do
state.power_check_source
end

defp get_target(%State{} = state, %Article{id: id} = article) do
state.articles[id]
end
Expand Down Expand Up @@ -159,74 +169,78 @@ defmodule ViralSpiral.Room.State do
Map.put(state, :power_viralspiral, power)
end

defp put_target(%State{} = state, %CheckSource{} = check_source) do
Map.put(state, :power_check_source, check_source)
end

@spec current_round_player(State.t()) :: Player.t()
def current_turn_player(%State{} = state), do: state.players[state.turn.current]

def current_round_player(%State{} = state),
do: state.players[Round.current_player_id(state.round)]

defimpl Inspect do
import Inspect.Algebra
alias Inspect.Opts

def inspect(state, _opts) do
players =
Map.keys(state.players)
|> Enum.map(fn id ->
player = state.players[id]

current_round = if player == State.current_round_player(state), do: "CR", else: ""
current_turn = if player == State.current_turn_player(state), do: "CT", else: ""

header = "#{player.id} : #{player.name} : #{player.identity} : #{player.clout} "

affinities =
Map.keys(player.affinities)
|> Enum.map(&"#{&1} : #{player.affinities[&1]}")
|> Enum.join(" | ")

biases =
Map.keys(player.biases)
|> Enum.map(&"#{&1} : #{player.biases[&1]}")
|> Enum.join(" | ")

active_cards =
player.active_cards
|> Enum.map(&"#{elem(&1, 0)} : #{elem(&1, 1)}")
|> IO.iodata_to_binary()

# hand =
# player.hand
# |> Enum.map(&"#{elem(&1, 0)} : #{elem(&1, 1)}")
# |> IO.iodata_to_binary()

[current_round <> current_turn, header, affinities, biases, active_cards]
|> Enum.join("\n")
|> IO.iodata_to_binary()

# {player.id, player.name, player.clout, player.affinities, player.biases}
# |> IO.iodata_to_binary()
end)
|> Enum.intersperse(line())
|> Enum.intersperse(line())
|> concat()

concat([
nest(doc({state.room.id, state.room.name, state.room.chaos_counter}), 4),
line(),
players
])
end

def doc(entity) do
to_doc(entity, %Opts{pretty: true})
end

def linebr() do
concat([
String.duplicate("_", 50),
line()
])
end
end
# defimpl Inspect do
# import Inspect.Algebra
# alias Inspect.Opts

# def inspect(state, _opts) do
# players =
# Map.keys(state.players)
# |> Enum.map(fn id ->
# player = state.players[id]

# current_round = if player == State.current_round_player(state), do: "CR", else: ""
# current_turn = if player == State.current_turn_player(state), do: "CT", else: ""

# header = "#{player.id} : #{player.name} : #{player.identity} : #{player.clout} "

# affinities =
# Map.keys(player.affinities)
# |> Enum.map(&"#{&1} : #{player.affinities[&1]}")
# |> Enum.join(" | ")

# biases =
# Map.keys(player.biases)
# |> Enum.map(&"#{&1} : #{player.biases[&1]}")
# |> Enum.join(" | ")

# active_cards =
# player.active_cards
# |> Enum.map(&"#{elem(&1, 0)} : #{elem(&1, 1)}")
# |> IO.iodata_to_binary()

# # hand =
# # player.hand
# # |> Enum.map(&"#{elem(&1, 0)} : #{elem(&1, 1)}")
# # |> IO.iodata_to_binary()

# [current_round <> current_turn, header, affinities, biases, active_cards]
# |> Enum.join("\n")
# |> IO.iodata_to_binary()

# # {player.id, player.name, player.clout, player.affinities, player.biases}
# # |> IO.iodata_to_binary()
# end)
# |> Enum.intersperse(line())
# |> Enum.intersperse(line())
# |> concat()

# concat([
# nest(doc({state.room.id, state.room.name, state.room.chaos_counter}), 4),
# line(),
# players
# ])
# end

# def doc(entity) do
# to_doc(entity, %Opts{pretty: true})
# end

# def linebr() do
# concat([
# String.duplicate("_", 50),
# line()
# ])
# end
# end
end
Loading

0 comments on commit c0539d0

Please sign in to comment.