Skip to content

Commit

Permalink
feat: reducer test draw_card
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Nov 19, 2024
1 parent d0d7d40 commit 1aee2e6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 73 deletions.
2 changes: 2 additions & 0 deletions lib/viral_spiral/entity/deck.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ defmodule ViralSpiral.Entity.Deck do
"""
# @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
:remove_card ->
new_sets =
Expand Down
4 changes: 2 additions & 2 deletions lib/viral_spiral/room/actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule ViralSpiral.Room.Actions do
"""
alias ViralSpiral.Room.Action

def draw_card() do
%Action{type: :draw_card}
def draw_card(draw_type) do
%Action{type: :draw_card, payload: %{draw_type: draw_type}}
end

@spec pass_card(String.t(), String.t(), String.t() | list(String.t())) :: Action.t()
Expand Down
17 changes: 17 additions & 0 deletions lib/viral_spiral/room/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule ViralSpiral.Gameplay.Factory do
@moduledoc """
Create entities for a Game Room
"""
alias ViralSpiral.Entity.Deck
alias ViralSpiral.Canon.Deck, as: CanonDeck
alias ViralSpiral.Canon.DrawTypeRequirements
alias ViralSpiral.Room.State
alias ViralSpiral.Room.EngineConfig
Expand Down Expand Up @@ -53,4 +55,19 @@ defmodule ViralSpiral.Gameplay.Factory do
}
}
end

def new_deck(%Room{} = room) do
cards = CanonDeck.load_cards()

set_opts = [
affinities: room.affinities,
biases: room.communities
]

%Deck{
available_cards: CanonDeck.create_sets(cards, set_opts),
dealt_cards: %{},
store: CanonDeck.create_store(cards)
}
end
end
40 changes: 8 additions & 32 deletions lib/viral_spiral/room/reducer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ViralSpiral.Room.Reducer do
@moduledoc """
"""
alias ViralSpiral.Gameplay.Factory
alias ViralSpiral.Playable
alias ViralSpiral.Room.State
alias ViralSpiral.Room.ChangeOptions
Expand All @@ -10,46 +11,21 @@ defmodule ViralSpiral.Room.Reducer do
alias ViralSpiral.Room.Action

@spec reduce(State.t(), Action.t()) :: State.t()
def reduce(%State{} = state, %{type: :draw_card}) do
# todo : this is a hardcoded value, should be fixed later
# this should be derivable from state.room
def reduce(%State{} = state, %{type: :draw_card} = action) do
draw_type = action.payload.draw_type

requirements = %DrawTypeRequirements{
tgb: 4,
total_tgb: 10,
biases: [:red, :yellow, :blue],
affinities: [:skub, :highfive],
current_player: %{
identity: :blue
}
}

# requirements = Factory.draw_type_requirements(state.room)
current_player = State.current_player(state)
current_player = State.current_round_player(state)

sets = state.deck.available_cards
draw_type = Deck.draw_type(requirements)
card_in_set = Deck.draw_card(sets, draw_type)

# IO.inspect(current_player)
# IO.inspect(type)
# IO.inspect(card_id)
# IO.inspect("hello")
draw_result = Deck.draw_card(sets, draw_type)

changes =
[
{state.deck, nil, ChangeOptions.remove_card(draw_type, card_in_set)}
{state.deck, nil, ChangeOptions.remove_card(draw_type, draw_result)},
{state.players[current_player.id], nil, ChangeOptions.add_to_active(draw_result.id)}
]
|> IO.inspect()

# State.apply_changes(state, changes) |> IO.inspect()
# [
# {state.deck, ChangeOptions.remove_card(type, card_id)},
# {state.players[current_player], ChangeOptions.add_to_active(card_id)}
# ]
# |> State.apply_changes()

state
State.apply_changes(state, changes)
end

def reduce(%State{} = state, %{type: pass_card} = action) do
Expand Down
10 changes: 7 additions & 3 deletions lib/viral_spiral/room/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule ViralSpiral.Room.State do

round = Round.new(players)
turn = Turn.new(round)
deck = Deck.new()
deck = Factory.new_deck(room)

%State{
room: room,
Expand Down Expand Up @@ -77,7 +77,7 @@ 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, 1)
change_inst = elem(change, 2)
new_value = apply_change(data, state, change_inst)
put_target(state, new_value)
end)
Expand All @@ -97,7 +97,7 @@ defmodule ViralSpiral.Room.State do
state.round
end

defp get_target(%State{} = state, %Deck{} = round) do
defp get_target(%State{} = state, %Deck{} = deck) do
state.deck
end

Expand Down Expand Up @@ -131,6 +131,10 @@ defmodule ViralSpiral.Room.State do
Map.put(state, :turn, turn)
end

defp put_target(%State{} = state, %Deck{} = deck) do
Map.put(state, :deck, deck)
end

def current_turn_player(%State{} = state), do: state.players[state.turn.current]

def current_round_player(%State{} = state),
Expand Down
2 changes: 1 addition & 1 deletion test/viral_spiral/entity/deck_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule ViralSpiral.Entity.DeckTest do
draw_result = CanonDeck.draw_card(deck.available_cards, draw_type)

new_deck = Change.apply_change(deck, nil, ChangeOptions.remove_card(draw_type, draw_result))
assert MapSet.size(new_deck.available_cards[{:affinity, true, :cat}]) == 59
assert CanonDeck.size(new_deck.available_cards, draw_type) == 59
end
end
end
75 changes: 40 additions & 35 deletions test/viral_spiral/reducers_test.exs
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
defmodule ViralSpiral.ReducersTest do
alias ViralSpiral.Gameplay.Factory
alias ViralSpiral.Room.Actions
alias ViralSpiral.Room.ChangeOptions
alias ViralSpiral.Entity.Change
alias ViralSpiral.Canon.Deck
alias ViralSpiral.Reducers
alias ViralSpiral.Room.Reducer
alias ViralSpiral.Room.State
alias ViralSpiral.Entity.Room
use ExUnit.Case

describe "" do
setup do
:rand.seed(:exsss, {12356, 123_534, 345_345})
:rand.seed(:exsss, {123, 135, 254})

room = Room.reserve("test-room") |> Room.start(4)
state = State.new(room, ["adhiraj", "krys", "aman", "farah"])

%{state: state}
end

@tag timeout: :infinity
test "draw_card", %{state: state} do
draw_type = [target: :yellow, type: :bias, veracity: false]
assert Deck.size(state.deck.available_cards, draw_type) == 30
requirements = Factory.draw_type(state)
draw_type = Deck.draw_type(requirements)
assert Deck.size(state.deck.available_cards, draw_type) == 60

new_state = Reducers.draw_card(state)
new_state = Reducer.reduce(state, Actions.draw_card(draw_type))

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

current_player = new_state.players[new_state.turn.current]
current_player = State.current_round_player(state)
assert length(current_player.active_cards) == 1
IO.inspect(current_player)
end

test "keep_card" do
Expand All @@ -48,41 +53,41 @@ defmodule ViralSpiral.ReducersTest do
end
end

describe "pass card" do
setup do
:rand.seed(:exsss, {12356, 123_534, 345_345})
# describe "pass card" do
# setup do
# :rand.seed(:exsss, {12356, 123_534, 345_345})

room = Room.reserve("test-room") |> Room.start(4)
state = State.new(room, ["adhiraj", "krys", "aman", "farah"])
# room = Room.reserve("test-room") |> Room.start(4)
# state = State.new(room, ["adhiraj", "krys", "aman", "farah"])

%{state: state}
end
# %{state: state}
# end

test "passing an affinity card should increase player's clout", %{state: state} do
sets = state.deck.available_cards
type = [type: :affinity, veracity: true, tgb: 0, target: :skub]
card_id = Deck.draw_card(sets, type)
# test "passing an affinity card should increase player's clout", %{state: state} do
# sets = state.deck.available_cards
# type = [type: :affinity, veracity: true, tgb: 0, target: :skub]
# card_id = Deck.draw_card(sets, type)

IO.inspect(card_id)
end
end
# IO.inspect(card_id)
# end
# end

@tag timeout: :infinity
test "temp" do
:rand.seed(:exsss, {12356, 123_534, 345_345})
store = StoreFixtures.new_store()
store = StoreFixtures.set_chaos(store, 4)
# @tag timeout: :infinity
# test "temp" do
# :rand.seed(:exsss, {12356, 123_534, 345_345})
# store = StoreFixtures.new_store()
# store = StoreFixtures.set_chaos(store, 4)

%{adhiraj: adhiraj, aman: aman, farah: farah, krys: krys} =
StoreFixtures.player_by_names(store)
# %{adhiraj: adhiraj, aman: aman, farah: farah, krys: krys} =
# StoreFixtures.player_by_names(store)

# draw card of a certain type(?)
# # draw card of a certain type(?)

Change.apply_change(store.players[aman.id], Options.change_clout(2))
# Change.apply_change(store.players[aman.id], Options.change_clout(2))

# IO.inspect(store.round)
# IO.inspect(store.turn)
# IO.inspect(store.players)
# IO.inspect(store.room)
end
# # IO.inspect(store.round)
# # IO.inspect(store.turn)
# # IO.inspect(store.players)
# # IO.inspect(store.room)
# end
end

0 comments on commit 1aee2e6

Please sign in to comment.