-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement Change for Room and Player
- Loading branch information
1 parent
9e39d80
commit 15640fb
Showing
17 changed files
with
314 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule ViralSpiral.Deck.Card do | ||
import ViralSpiral.Deck.CardGuards | ||
alias ViralSpiral.Deck.Card | ||
|
||
defstruct id: nil, | ||
type: nil | ||
|
||
@type card_types :: :affinity | :bias | :topical | :conflated | ||
|
||
@type t :: %__MODULE__{ | ||
id: String.t(), | ||
type: card_types() | ||
} | ||
|
||
def new(type) when is_card_type(type) do | ||
%Card{ | ||
id: UXID.generate!(prefix: "card", size: :small), | ||
type: type | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
defmodule ViralSpiral.Deck.CardContent do | ||
defstruct title: "", | ||
description: "", | ||
fake_title: "", | ||
fake_description: "" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule ViralSpiral.Deck.CardGuards do | ||
@card_types Application.compile_env(:viral_spiral, CardConfig)[:card_types] | ||
|
||
defguard is_card_type(value) when value in @card_types | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# defmodule ViralSpiral.Game.Change do | ||
# defstruct type: nil, | ||
# target: nil, | ||
# target_id: nil | ||
|
||
# @type change_type :: :inc | :dec | :value | ||
# @type target :: :chaos_counter | :clout | :affinity | :bias | ||
|
||
# @type t :: %__MODULE__{ | ||
# type: change_type(), | ||
# target: target(), | ||
# target_id: String.t() | ||
# } | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defprotocol ViralSpiral.Score.Change do | ||
@moduledoc """ | ||
Protocol to change scores used in Viral Spiral. | ||
## Fields | ||
- score: struct which implements the `Change` protocol | ||
- change_description: a Keyword List with parameters defining the change | ||
""" | ||
def apply_change(score, change_description) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
defmodule ViralSpiral.Score.Player do | ||
@moduledoc """ | ||
Create and update Player Score. | ||
## Example | ||
iex> player_score = %ViralSpiral.Game.Score.Player{ | ||
biases: %{red: 0, blue: 0}, | ||
affinities: %{cat: 0, sock: 0}, | ||
clout: 0 | ||
} | ||
""" | ||
alias ViralSpiral.Score.Player | ||
alias ViralSpiral.Game.RoomConfig | ||
alias ViralSpiral.Game.Player, as: PlayerData | ||
import ViralSpiral.Game.RoomConfig.Guards | ||
alias ViralSpiral.Score.Change | ||
|
||
defstruct biases: %{}, affinities: %{}, clout: 0 | ||
|
||
@type change_opts :: [type: :clout | :affinity | :bias, offset: integer(), target: atom()] | ||
@type t :: %__MODULE__{ | ||
biases: map(), | ||
affinities: map(), | ||
clout: integer() | ||
} | ||
|
||
def new(%PlayerData{} = player, %RoomConfig{} = room_config) do | ||
bias_list = Enum.filter(room_config.communities, &(&1 != player.identity)) | ||
bias_map = Enum.reduce(bias_list, %{}, fn x, acc -> Map.put(acc, x, 0) end) | ||
|
||
affinity_list = room_config.affinities | ||
affinity_map = Enum.reduce(affinity_list, %{}, fn x, acc -> Map.put(acc, x, 0) end) | ||
|
||
%Player{ | ||
biases: bias_map, | ||
affinities: affinity_map | ||
} | ||
end | ||
|
||
defimpl Change do | ||
@doc """ | ||
Implement change protocol for a Player's Score. | ||
""" | ||
@spec apply_change(Player.t(), Player.change_opts()) :: Player.t() | ||
def apply_change(player, opts) do | ||
case opts[:type] do | ||
:clout -> change(player, :clout, opts[:offset]) | ||
:affinity -> change(player, :affinity, opts[:target], opts[:offset]) | ||
:bias -> change(player, :bias, opts[:target], opts[:offset]) | ||
end | ||
end | ||
|
||
def change(%Player{} = player, :bias, target_bias, count) | ||
when is_community(target_bias) and is_integer(count) do | ||
new_biases = Map.put(player.biases, target_bias, player.biases[target_bias] + count) | ||
%{player | biases: new_biases} | ||
end | ||
|
||
def change(%Player{} = player, :affinity, target, count) | ||
when is_affinity(target) and is_integer(count) do | ||
new_affinities = Map.put(player.affinities, target, player.affinities[target] + count) | ||
%{player | affinities: new_affinities} | ||
end | ||
|
||
def change(%Player{} = player, :clout, count) when is_integer(count) do | ||
new_clout = player.clout + count | ||
%{player | clout: new_clout} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule ViralSpiral.Score.Room do | ||
@moduledoc """ | ||
## Example | ||
""" | ||
alias ViralSpiral.Score.Change | ||
alias ViralSpiral.Score.Room | ||
defstruct chaos_countdown: 10 | ||
|
||
@type t :: %__MODULE__{ | ||
chaos_countdown: integer() | ||
} | ||
|
||
@spec new() :: ViralSpiral.Score.Room.t() | ||
def new() do | ||
%Room{} | ||
end | ||
|
||
@spec countdown(ViralSpiral.Score.Room.t()) :: ViralSpiral.Score.Room.t() | ||
def countdown(%Room{} = room) do | ||
%{room | chaos_countdown: room.chaos_countdown - 1} | ||
end | ||
|
||
defimpl Change do | ||
@spec apply_change(ViralSpiral.Score.Room.t(), keyword()) :: ViralSpiral.Score.Room.t() | ||
def apply_change(%Room{} = score, opts) do | ||
opts = Keyword.validate!(opts, offset: 0) | ||
|
||
case opts[:offset] do | ||
x when is_integer(x) -> | ||
Map.put(score, :chaos_countdown, score.chaos_countdown + opts[:offset]) | ||
|
||
y when is_bitstring(y) -> | ||
Map.put(score, :chaos_countdown, score.chaos_countdown) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.