Skip to content

Commit

Permalink
refactor change_descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Nov 19, 2024
1 parent 1aee2e6 commit d316424
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 61 deletions.
18 changes: 1 addition & 17 deletions lib/viral_spiral/entity/change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@ defprotocol ViralSpiral.Entity.Change do
- score: struct which implements the `Change` protocol
- change_description: a Keyword List with parameters defining the change
"""
alias ViralSpiral.Room.State

@spec apply_change(t(), State.t(), keyword()) :: t()
def apply_change(state, global_state, change_description)
end

defmodule ViralSpiral.Entity.ChangeOptions do
defstruct type: nil,
target: nil,
id: nil,
extra: nil

@type t :: %__MODULE__{
type: atom(),
target: atom(),
id: String.t(),
extra: any()
}
def apply_change(state, change_desc)
end
12 changes: 7 additions & 5 deletions lib/viral_spiral/entity/deck.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ defmodule ViralSpiral.Entity.Deck do
- Make card unavailable for drawing. Implemted by change type :remove
"""
# @spec apply_change(Deck.t(), Deck.change_opts()) :: Deck.t()
def apply_change(%Deck{} = deck, _global_state, opts) do
IO.inspect(opts)

case opts[:type] do
def apply_change(%Deck{} = deck, change_desc) do
case change_desc[:type] do
:remove_card ->
new_sets =
CanonDeck.remove_card(deck.available_cards, opts[:draw_type], opts[:card_in_set])
CanonDeck.remove_card(
deck.available_cards,
change_desc[:draw_type],
change_desc[:card_in_set]
)

Map.put(deck, :available_cards, new_sets)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/viral_spiral/entity/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ defimpl ViralSpiral.Entity.Change, for: ViralSpiral.Entity.Player do
%{player | clout: new_clout}
end

def apply_change(player, _game_state, change_desc) do
def apply_change(player, change_desc) do
case change_desc[:type] do
:clout -> change(player, :clout, change_desc[:offset])
:affinity -> change(player, :affinity, change_desc[:target], change_desc[:offset])
Expand Down
9 changes: 4 additions & 5 deletions lib/viral_spiral/entity/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,11 @@ defimpl ViralSpiral.Entity.Change, for: ViralSpiral.Entity.Room do
@doc """
Change state of a Room.
"""
@spec apply_change(Room.t(), State.t(), keyword()) :: Room.t()
def apply_change(%Room{} = score, _global_state, opts) do
opts = Keyword.validate!(opts, type: nil, offset: 0)
def apply_change(%Room{} = score, change_desc) do
change_desc = Keyword.validate!(change_desc, type: nil, offset: 0)

case opts[:type] do
:chaos_countdown -> Map.put(score, :chaos, score.chaos + opts[:offset])
case change_desc[:type] do
:chaos_countdown -> Map.put(score, :chaos, score.chaos + change_desc[:offset])
end
end
end
4 changes: 2 additions & 2 deletions lib/viral_spiral/entity/round.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ defmodule ViralSpiral.Entity.Round do
alias ViralSpiral.Entity.Round

# @spec apply_change(Round.t(), Round.cphange_opts()) :: Round.t()
def apply_change(state, global_state, opts) do
case opts[:type] do
def apply_change(state, change_desc) do
case change_desc[:type] do
:next -> Round.next(state)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/viral_spiral/entity/turn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ defmodule ViralSpiral.Entity.Turn do
end

defimpl Change do
def apply_change(turn, _global_state, opts) do
case opts[:type] do
:next -> Turn.next(turn, opts[:target])
:new -> Turn.new(opts[:round])
def apply_change(turn, change_desc) do
case change_desc[:type] do
:next -> Turn.next(turn, change_desc[:target])
:new -> Turn.new(change_desc[:round])
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule ViralSpiral.Room.ChangeOptions do
defmodule ViralSpiral.Room.ChangeDescriptions do
@moduledoc """
Commonly used change options put behind user friendly names.
"""
Expand Down
6 changes: 3 additions & 3 deletions lib/viral_spiral/room/reducer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule ViralSpiral.Room.Reducer do
alias ViralSpiral.Gameplay.Factory
alias ViralSpiral.Playable
alias ViralSpiral.Room.State
alias ViralSpiral.Room.ChangeOptions
alias ViralSpiral.Room.ChangeDescriptions
alias ViralSpiral.Canon.DrawTypeRequirements
alias ViralSpiral.Canon.Deck
alias ViralSpiral.Room.Action
Expand All @@ -21,8 +21,8 @@ defmodule ViralSpiral.Room.Reducer do

changes =
[
{state.deck, nil, ChangeOptions.remove_card(draw_type, draw_result)},
{state.players[current_player.id], nil, ChangeOptions.add_to_active(draw_result.id)}
{state.deck, ChangeDescriptions.remove_card(draw_type, draw_result)},
{state.players[current_player.id], ChangeDescriptions.add_to_active(draw_result.id)}
]

State.apply_changes(state, changes)
Expand Down
6 changes: 3 additions & 3 deletions lib/viral_spiral/room/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ defmodule ViralSpiral.Room.State do
def apply_changes(state, changes) do
Enum.reduce(changes, state, fn change, state ->
data = get_target(state, elem(change, 0))
change_inst = elem(change, 2)
new_value = apply_change(data, state, change_inst)
change_desc = elem(change, 1)
new_value = apply_change(data, change_desc)
put_target(state, new_value)
end)
end

defdelegate apply_change(change, state, opts), to: Change
defdelegate apply_change(change, change_desc), to: Change

defp get_target(%State{} = state, %Player{id: id}) do
state.players[id]
Expand Down
6 changes: 4 additions & 2 deletions test/viral_spiral/entity/deck_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ViralSpiral.Entity.DeckTest do
alias ViralSpiral.Canon.Deck, as: CanonDeck
alias ViralSpiral.Room.ChangeOptions
alias ViralSpiral.Room.ChangeDescriptions
alias ViralSpiral.Entity.Change
alias ViralSpiral.Entity.Deck
use ExUnit.Case
Expand All @@ -16,7 +16,9 @@ defmodule ViralSpiral.Entity.DeckTest do
draw_type = [type: :affinity, veracity: true, tgb: 2, target: :cat]
draw_result = CanonDeck.draw_card(deck.available_cards, draw_type)

new_deck = Change.apply_change(deck, nil, ChangeOptions.remove_card(draw_type, draw_result))
new_deck =
Change.apply_change(deck, ChangeDescriptions.remove_card(draw_type, draw_result))

assert CanonDeck.size(new_deck.available_cards, draw_type) == 59
end
end
Expand Down
18 changes: 9 additions & 9 deletions test/viral_spiral/entity/player_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ViralSpiral.Game.PlayerTest do
alias ViralSpiral.Gameplay.Factory
alias ViralSpiral.Room.ChangeOptions
alias ViralSpiral.Room.ChangeDescriptions
alias ViralSpiral.Entity.Room
alias ViralSpiral.Entity.Player.ActiveCardDoesNotExist
alias ViralSpiral.Entity.Player.DuplicateActiveCardException
Expand Down Expand Up @@ -76,42 +76,42 @@ defmodule ViralSpiral.Game.PlayerTest do
end

test "change clout", %{player: player} do
player = Change.apply_change(player, nil, Options.change_clout(4))
player = Change.apply_change(player, Options.change_clout(4))
assert player.clout == 4
end

test "change affinity", %{player: player} do
player = Change.apply_change(player, nil, Options.change_affinity(:cat, 2))
player = Change.apply_change(player, Options.change_affinity(:cat, 2))
assert player.affinities.cat == 2
end

test "change bias", %{player: player} do
player = Change.apply_change(player, nil, Options.change_bias(:yellow, -1))
player = Change.apply_change(player, Options.change_bias(:yellow, -1))
assert player.biases.yellow == 1
end

test "add card to hand", %{player: player} do
player = Change.apply_change(player, nil, Options.add_to_hand("card_23b2323"))
player = Change.apply_change(player, Options.add_to_hand("card_23b2323"))
assert length(player.hand) == 1
assert hd(player.hand) == "card_23b2323"
end

test "add_active_card", %{player: player} do
player = Change.apply_change(player, nil, Options.add_to_active("card_29323"))
player = Change.apply_change(player, Options.add_to_active("card_29323"))
assert player.active_cards == ["card_29323"]

player = Change.apply_change(player, nil, Options.add_to_active("card_84843"))
player = Change.apply_change(player, Options.add_to_active("card_84843"))
assert player.active_cards == ["card_29323", "card_84843"]
end

test "remove_active_card", %{player: player} do
player =
%{player | active_cards: ["card_29323", "card_84843"]}
|> Change.apply_change(nil, Options.remove_active("card_29323"))
|> Change.apply_change(Options.remove_active("card_29323"))

assert player.active_cards == ["card_84843"]

player = Change.apply_change(player, nil, Options.remove_active("card_84843"))
player = Change.apply_change(player, Options.remove_active("card_84843"))

assert player.active_cards == []
end
Expand Down
2 changes: 1 addition & 1 deletion test/viral_spiral/reducers_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ViralSpiral.ReducersTest do
alias ViralSpiral.Gameplay.Factory
alias ViralSpiral.Room.Actions
alias ViralSpiral.Room.ChangeOptions
alias ViralSpiral.Room.ChangeDescriptions
alias ViralSpiral.Entity.Change
alias ViralSpiral.Canon.Deck
alias ViralSpiral.Room.Reducer
Expand Down
6 changes: 3 additions & 3 deletions test/viral_spiral/room/player_text.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ defmodule ViralSpiral.Entity.PlayerTest do
end

test "change player clout", %{player: player} do
new_player = Change.apply_change(player, nil, type: :clout, offset: 5)
new_player = Change.apply_change(player, type: :clout, offset: 5)
assert new_player.clout == 5
end

test "change player affinity", %{player: player} do
new_player = Change.apply_change(player, nil, type: :affinity, target: :cat, offset: -2)
new_player = Change.apply_change(player, type: :affinity, target: :cat, offset: -2)
assert new_player.affinities.cat == -2
end

test "change player bias", %{player: player} do
new_player = Change.apply_change(player, nil, type: :bias, target: :red, offset: 9)
new_player = Change.apply_change(player, type: :bias, target: :red, offset: 9)
assert new_player.biases.red == 9
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/viral_spiral/room/room_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ defmodule ViralSpiral.Room.RoomTest do
end

test "change chaos countdown", %{room: room} do
new_room = Change.apply_change(room, nil, offset: 5)
new_room = Change.apply_change(room, offset: 5)
assert new_room.chaos_counter == 9
end

test "pass invalid offset in change description", %{room: room} do
new_room = Change.apply_change(room, nil, offset: "hi")
new_room = Change.apply_change(room, offset: "hi")
assert new_room.chaos_counter == 4
end

test "pass opts without required fields", %{room: room} do
assert_raise ArgumentError, fn ->
Change.apply_change(room, nil, invalid: "random")
Change.apply_change(room, invalid: "random")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/viral_spiral/room/state/round_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule ViralSpiral.Entity.RoundTest do
end

test "move to next round", %{round: round} do
new_round = Change.apply_change(round, nil, type: :next)
new_round = Change.apply_change(round, type: :next)
assert new_round.current == 1
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/viral_spiral/room/state/turn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule ViralSpiral.Entity.TurnTest do
end

test "move to next turn", %{turn: turn, target: target} do
new_turn = Change.apply_change(turn, nil, type: :next, target: target)
new_turn = Change.apply_change(turn, type: :next, target: target)
assert new_turn.current == target
end
end
Expand Down

0 comments on commit d316424

Please sign in to comment.