-
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.
- add a Registry, Supervisor, DynamicSupervisor to manage game state in Engine.
- Loading branch information
1 parent
99580b7
commit b9f3fa5
Showing
6 changed files
with
124 additions
and
38 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,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 |
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,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
|
||
end | ||
|
||
@impl true | ||
def handle_cast({:pass, from, to}, state) do | ||
Check warning on line 26 in lib/viral_spiral/room/game_engine.ex
|
||
end | ||
|
||
@impl true | ||
def handle_cast({:keep, from}, state) do | ||
Check warning on line 30 in lib/viral_spiral/room/game_engine.ex
|
||
end | ||
|
||
@impl true | ||
def handle_cast({:discard, from}, state) do | ||
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 |
This file was deleted.
Oops, something went wrong.
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
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 |