Skip to content

Commit

Permalink
feat: create Room context
Browse files Browse the repository at this point in the history
- add a Registry, Supervisor, DynamicSupervisor to manage game state in Engine.
  • Loading branch information
dennyabrain committed Sep 19, 2024
1 parent 99580b7 commit b9f3fa5
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 38 deletions.
1 change: 1 addition & 0 deletions lib/viral_spiral/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule ViralSpiral.Application do
ViralSpiral.Repo,
{DNSCluster, query: Application.get_env(:viral_spiral, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: ViralSpiral.PubSub},
ViralSpiral.Room,
# Start the Finch HTTP client for sending emails
{Finch, name: ViralSpiral.Finch},
# Start a worker by calling: ViralSpiral.Worker.start_link(arg)
Expand Down
64 changes: 64 additions & 0 deletions lib/viral_spiral/room.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
defmodule ViralSpiral.Room do
@moduledoc """
Context for activities within a Viral Spiral Room.
This architecture is heavily borrowed from [here](https://dashbit.co/blog/homemade-analytics-with-ecto-and-elixir).
Rooms in viral spiral need to manage mutable state of the game, hence they are wrapped in a `GenServer`. These genservers are then supervised by a Dynamic Supervisor, which is managed by this Supervisor. This supervisor is started by the application and is part of its supervision tree.
## Usage :
Room.new("vanilla-bean-23")
engine = ViralSpiral.Room.room_gen!("ok-pista-4")
send(engine, msg)
"""

use Supervisor
alias ViralSpiral.Room.NotFound

@room_gen ViralSpiral.Room.GameEngine
@registry ViralSpiral.Room.Registry
@supervisor ViralSpiral.Room.GameEngineSupervisor

def start_link(_opts) do
Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
end

@impl true
def init(:ok) do
children = [
{Registry, keys: :unique, name: @registry},
{DynamicSupervisor, name: @supervisor, strategy: :one_for_one}
]

Supervisor.init(children, strategy: :one_for_all)
end

def new(path) when is_binary(path) do
pid =
case Registry.lookup(@registry, path) do
[{pid, _}] ->
pid

[] ->
pid =
case DynamicSupervisor.start_child(@supervisor, {@room_gen, path}) do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end

pid
end

send(pid, :new_room)
end

def room_gen!(path) do
case Registry.lookup(@registry, path) do
[{pid, _}] -> pid
_ -> raise NotFound
end
end
end

defmodule ViralSpiral.Room.NotFound do
defexception message: "Unable to find room"
end
54 changes: 54 additions & 0 deletions lib/viral_spiral/room/game_engine.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule ViralSpiral.Room.GameEngine do
alias ViralSpiral.Room.State.Root
alias ViralSpiral.Room.State.Room
use GenServer

@registry ViralSpiral.Room.Registry

def start_link(path) do
GenServer.start_link(__MODULE__, path, name: {:via, Registry, {@registry, path}})
end

@impl true
def init(_init_arg) do
# room = Room.new() |> Room.start(4)
# # root = Root.new(room, init_arg.players)
# root = Root.new(room, ["adhiraj", "krys", "aman", "farah"])

{:ok, [1]}
end

@impl true
def handle_cast({:join, player_name}, state) do

Check warning on line 22 in lib/viral_spiral/room/game_engine.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "player_name" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 22 in lib/viral_spiral/room/game_engine.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "state" is unused (if the variable is not meant to be used, prefix it with an underscore)
end

@impl true
def handle_cast({:pass, from, to}, state) do

Check warning on line 26 in lib/viral_spiral/room/game_engine.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "from" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 26 in lib/viral_spiral/room/game_engine.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "state" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 26 in lib/viral_spiral/room/game_engine.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "to" is unused (if the variable is not meant to be used, prefix it with an underscore)
end

@impl true
def handle_cast({:keep, from}, state) do

Check warning on line 30 in lib/viral_spiral/room/game_engine.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "from" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 30 in lib/viral_spiral/room/game_engine.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "state" is unused (if the variable is not meant to be used, prefix it with an underscore)
end

@impl true
def handle_cast({:discard, from}, state) do

Check warning on line 34 in lib/viral_spiral/room/game_engine.ex

View workflow job for this annotation

GitHub Actions / build-and-deploy

variable "from" is unused (if the variable is not meant to be used, prefix it with an underscore)
end

@impl true
def handle_cast({:check_source, from}, state) do
end

@impl true
def handle_cast({:cancel, from, target}, state) do
end

@impl true
def handle_cast({:viral_spiral, from, targets}, state) do
end

@impl true
def handle_info(msg, state) do
IO.inspect("msg in genserver #{msg}")
{:noreply, state}
end
end
36 changes: 0 additions & 36 deletions lib/viral_spiral/room/room_genserver.ex

This file was deleted.

2 changes: 0 additions & 2 deletions lib/viral_spiral/room/storage.ex

This file was deleted.

5 changes: 5 additions & 0 deletions lib/viral_spiral/room/store.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule ViralSpiral.Game.Store do
@doc """
Persist game state to disk.
"""
end

0 comments on commit b9f3fa5

Please sign in to comment.