From 27133a3edb7acfea43ff31188ae21bf3dd1ff8d7 Mon Sep 17 00:00:00 2001 From: dennyabrain Date: Wed, 14 Aug 2024 09:42:57 +0530 Subject: [PATCH] chore: add type specs --- lib/viral_spiral/game.ex | 42 ++++++++++++++++++++++------------ lib/viral_spiral/game/room.ex | 10 ++++++++ lib/viral_spiral/game/round.ex | 10 +++++--- lib/viral_spiral/game/state.ex | 27 ++++++++++++++++++++++ test/support/fixtures.ex | 4 ++-- 5 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 lib/viral_spiral/game/state.ex diff --git a/lib/viral_spiral/game.ex b/lib/viral_spiral/game.ex index adba49e..2b3e860 100644 --- a/lib/viral_spiral/game.ex +++ b/lib/viral_spiral/game.ex @@ -1,16 +1,30 @@ defmodule ViralSpiral.Game do - defstruct room: nil, - player_list: nil, - player_map: nil, - scores: nil - - @moduledoc """ - Context for the game. - - Rounds and Turns - round = Round.new(players) - round_order = Round.order(round) - During a Round every player gets to draw a card and then take some actions. - 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.Game.Room + alias ViralSpiral.Game.State + + @spec create_room(String.t()) :: Room.t() + def create_room(name) do + end + + @spec join_room(String.t(), String.t()) :: Room.t() + def join_room(name, password) do + end + + def pass_card(state, player, to) do + end + + def keep_card(player) do + end + + def discard_card(player) do + end + + def turn_to_fake(player, card) do + end + + def cancel_player(player, target) do + end + + def viral_spiral(player, targets) do + end end diff --git a/lib/viral_spiral/game/room.ex b/lib/viral_spiral/game/room.ex index 4acb1a7..44e23b4 100644 --- a/lib/viral_spiral/game/room.ex +++ b/lib/viral_spiral/game/room.ex @@ -1,8 +1,18 @@ defmodule ViralSpiral.Game.Room do + alias ViralSpiral.Game.Room + defstruct id: "", name: "", state: :uninitialized + @type states :: :uninitialized | :waiting_for_players | :running | :paused + + @type t() :: %__MODULE__{ + id: String.t(), + name: String.t(), + state: states() + } + def new() do %__MODULE__{ id: UXID.generate!(prefix: "room", size: :small) diff --git a/lib/viral_spiral/game/round.ex b/lib/viral_spiral/game/round.ex index cca8892..e7e601c 100644 --- a/lib/viral_spiral/game/round.ex +++ b/lib/viral_spiral/game/round.ex @@ -32,6 +32,13 @@ defmodule ViralSpiral.Game.Round do current: 0, skip: nil + @type t :: %__MODULE__{ + order: list(String.t()), + count: integer(), + current: integer(), + skip: boolean() + } + @doc """ todo : this doesn't really need the players, merely a count of players, maybe? """ @@ -101,7 +108,4 @@ defmodule ViralSpiral.Game.Round do Map.merge(round, changes) end - - def current_id() do - end end diff --git a/lib/viral_spiral/game/state.ex b/lib/viral_spiral/game/state.ex new file mode 100644 index 0000000..132455a --- /dev/null +++ b/lib/viral_spiral/game/state.ex @@ -0,0 +1,27 @@ +defmodule ViralSpiral.Game.State do + defstruct room: nil, + player_list: nil, + player_map: nil, + scores: nil, + round: nil, + turn: nil + + @moduledoc """ + Context for the game. + + Rounds and Turns + round = Round.new(players) + round_order = Round.order(round) + During a Round every player gets to draw a card and then take some actions. + 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.Game.State + + def set_round(%State{} = game, round) do + %State{game | round: round} + end + + def set_turn(%State{} = game, turn) do + %State{game | turn: turn} + end +end diff --git a/test/support/fixtures.ex b/test/support/fixtures.ex index 6ba74da..815b52a 100644 --- a/test/support/fixtures.ex +++ b/test/support/fixtures.ex @@ -1,7 +1,7 @@ defmodule Fixtures do alias ViralSpiral.Game.Player alias ViralSpiral.Game.Room - alias ViralSpiral.Game + alias ViralSpiral.Game.State def initialized_game() do player_list = [ @@ -13,7 +13,7 @@ defmodule Fixtures do players = Enum.reduce(player_list, %{}, fn player, acc -> Map.put(acc, player.id, player) end) - %Game{ + %State{ room: Room.new(), player_map: players, player_list: player_list