Skip to content

Commit

Permalink
docs: improve typespecs and documentation for State structs
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Aug 20, 2024
1 parent cf22602 commit acab248
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/viral_spiral/score/change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ defprotocol ViralSpiral.Score.Change do
- score: struct which implements the `Change` protocol
- change_description: a Keyword List with parameters defining the change
"""
@spec apply_change(t(), keyword()) :: t()
def apply_change(score, change_description)
end
34 changes: 31 additions & 3 deletions lib/viral_spiral/score/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ defmodule ViralSpiral.Score.Player do
clout: integer()
}

@spec new(ViralSpiral.Game.Player.t(), %ViralSpiral.Game.RoomConfig{
:affinities => list(),
:communities => list()
}) :: ViralSpiral.Score.Player.t()
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)
Expand All @@ -50,18 +54,42 @@ defmodule ViralSpiral.Score.Player do
end
end

@doc """
Change a Player's Bias.
"""
@spec change(
ViralSpiral.Score.Player.t(),
:bias,
:blue | :red | :yellow,
integer()
) :: ViralSpiral.Score.Player.t()
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)
@doc """
Change a Player's Affinity.
"""
@spec change(
ViralSpiral.Score.Player.t(),
:affinity,
:cat | :highfive | :houseboat | :skub | :sock,
integer()
) :: ViralSpiral.Score.Player.t()
def change(%Player{} = player, :affinity, target_affinity, count)
when is_affinity(target_affinity) and is_integer(count) do
new_affinities =
Map.put(player.affinities, target_affinity, player.affinities[target_affinity] + count)

%{player | affinities: new_affinities}
end

@doc """
Change a Player's Clout.
"""
@spec change(ViralSpiral.Score.Player.t(), :clout, integer()) :: ViralSpiral.Score.Player.t()
def change(%Player{} = player, :clout, count) when is_integer(count) do
new_clout = player.clout + count
%{player | clout: new_clout}
Expand Down
9 changes: 9 additions & 0 deletions lib/viral_spiral/score/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ defmodule ViralSpiral.Score.Room do
chaos_countdown: integer()
}

@doc """
Create a new Room with default values.
"""
@spec new() :: ViralSpiral.Score.Room.t()
def new() do
%Room{}
end

@doc """
Reduce the chaos countdown by 1.
"""
@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
@doc """
Change state of a Room.
"""
@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)
Expand Down

0 comments on commit acab248

Please sign in to comment.