Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace sub apps, add DB app seketon #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ erl_crash.dump
rel/artifacts

.ssh
.todo
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ The `ExDoc` documentation is available [here](https://solomonhawk.github.io/gifs

## Starting Points

* [GameApp.Game](https://solomonhawk.github.io/gifs-to-gifs-reborn/GameApp.Game.html) - Game state and logic
* [GameApp.Round](https://solomonhawk.github.io/gifs-to-gifs-reborn/GameApp.Round.html) - Round state and logic
* [GameApp.Server](https://solomonhawk.github.io/gifs-to-gifs-reborn/GameApp.Server.html) - Game Server logic (GenServer)
* [GameApp.ServerSupervisor](https://solomonhawk.github.io/gifs-to-gifs-reborn/GameApp.ServerSupervisor.html) - Supervisor for Game Servers
* [GifMe.Game.Game](https://solomonhawk.github.io/gifs-to-gifs-reborn/GifMe.Game.Game.html) - Game state and logic
* [GifMe.Game.Round](https://solomonhawk.github.io/gifs-to-gifs-reborn/GifMe.Game.Round.html) - Round state and logic
* [GifMe.Game.Server](https://solomonhawk.github.io/gifs-to-gifs-reborn/GifMe.Game.Server.html) - Game Server logic (GenServer)
* [GifMe.Game.ServerSupervisor](https://solomonhawk.github.io/gifs-to-gifs-reborn/GifMe.Game.ServerSupervisor.html) - Supervisor for Game Servers

## Set Up

Expand Down
4 changes: 4 additions & 0 deletions apps/db/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
24 changes: 24 additions & 0 deletions apps/db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
db-*.tar

21 changes: 21 additions & 0 deletions apps/db/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Game.DB

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `db` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:db, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/db](https://hexdocs.pm/db).

18 changes: 18 additions & 0 deletions apps/db/lib/db.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule GifMe.DB do
@moduledoc """
Documentation for GifMe.DB.
"""

@doc """
Hello world.

## Examples

iex> GifMe.DB.hello()
:world

"""
def hello do
:world
end
end
19 changes: 19 additions & 0 deletions apps/db/lib/db/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule GifMe.DB.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false

use Application

def start(_type, _args) do
children = [
# Starts a worker by calling: Game.DB.Worker.start_link(arg)
# {Game.DB.Worker, arg}
]

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: GifMe.DB.Supervisor]
Supervisor.start_link(children, opts)
end
end
34 changes: 34 additions & 0 deletions apps/db/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule GifMe.DB.MixProject do
use Mix.Project

def project do
[
app: :db,
version: "0.1.0",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
mod: {GifMe.DB.Application, []}
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
# {:sibling_app_in_umbrella, in_umbrella: true}
]
end
end
8 changes: 8 additions & 0 deletions apps/db/test/game/db_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule GifMe.DBTest do
use ExUnit.Case
doctest GifMe.DB

test "greets the world" do
assert GifMe.DB.hello() == :world
end
end
1 change: 1 addition & 0 deletions apps/db/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
16 changes: 8 additions & 8 deletions apps/game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ $ iex -S mix
2. Create a Game Server, providing a shortcode and player (only id required for now)

```elixir
$ iex> GameApp.ServerSupervisor.start_game("ABCD", %GameApp.Player{ name: "fatal1ty", id: "Gamer" })
$ iex> GifMe.Game.ServerSupervisor.start_game("ABCD", %GifMe.Game.Player{ name: "fatal1ty", id: "Gamer" })
```

3. Get a summary of the game state (shortcode required to locate correct game server process to message)

```elixir
$ iex> GameApp.Server.summary("ABCD")
$ iex> GifMe.Game.Server.summary("ABCD")

%{
creator: %GameApp.Player{id: "gamer", name: "fatal1ity"},
creator: %GifMe.Game.Player{id: "gamer", name: "fatal1ity"},
funmaster: nil,
phase: :lobby,
players: %{"gamer" => %GameApp.Player{id: "gamer", name: "fatal1ity"}},
players: %{"gamer" => %GifMe.Game.Player{id: "gamer", name: "fatal1ity"}},
prompt: nil,
reactions: nil,
round_number: nil,
Expand All @@ -40,25 +40,25 @@ $ iex> GameApp.Server.summary("ABCD")
4. Join another player to the game

```elixir
$ iex> GameApp.Server.join("ABCD", %GameApp.Player{ id: "Noob", name: "dylan" })
$ iex> GifMe.Game.Server.join("ABCD", %GifMe.Game.Player{ id: "Noob", name: "dylan" })
```

5. Remove a player from the game

```elixir
$ iex> GameApp.Server.leave("ABCD", %GameApp.Player{ id: "Noob", name: "dylan"})
$ iex> GifMe.Game.Server.leave("ABCD", %GifMe.Game.Player{ id: "Noob", name: "dylan"})
```

6. Start the game

```elixir
$ iex> GameApp.Server.start_game("ABCD")
$ iex> GifMe.Game.Server.start_game("ABCD")
```

7. Start the next round

```elixir
$ iex> GameApp.Server.start_round("ABCD")
$ iex> GifMe.Game.Server.start_round("ABCD")
```


Expand Down
6 changes: 3 additions & 3 deletions apps/game/lib/game/application.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule GameApp.Application do
defmodule GifMe.Game.Application do
@moduledoc false

use Application

def start(_type, _args) do
children = [
{Registry, keys: :unique, name: GameApp.Registry}
{Registry, keys: :unique, name: GifMe.Game.Registry}
| Application.get_env(:game, :children)
]

_ = :ets.new(:games_table, [:public, :named_table])

opts = [strategy: :one_for_one, name: GameApp.Supervisor]
opts = [strategy: :one_for_one, name: GifMe.Game.Supervisor]
Supervisor.start_link(children, opts)
end
end
2 changes: 1 addition & 1 deletion apps/game/lib/game/config.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GameApp.Config do
defmodule GifMe.Game.Config do
alias __MODULE__, as: GameConfig

@derive Jason.Encoder
Expand Down
12 changes: 6 additions & 6 deletions apps/game/lib/game/server.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule GameApp.Server do
defmodule GifMe.Game.Server do
@moduledoc """
`GameApp.Server` provides a stateful process that maintains an internal game
`GifMe.Game.Server` provides a stateful process that maintains an internal game
state and provides a public API for interacting with the game.
"""

use GenServer

alias __MODULE__, as: Server
alias GameApp.{Game, Player}
alias GameApp.Config, as: GameConfig
alias GifMe.Game.{Game, Player}
alias GifMe.Game.Config, as: GameConfig

require Logger

Expand Down Expand Up @@ -270,9 +270,9 @@ defmodule GameApp.Server do
@doc """
Returns a tuple used to register and lookup a game server process by name.
"""
@spec via_tuple(String.t()) :: {:via, Registry, {GameApp.Registry, String.t()}}
@spec via_tuple(String.t()) :: {:via, Registry, {GifMe.Game.Registry, String.t()}}
def via_tuple(shortcode) do
{:via, Registry, {GameApp.Registry, "game:" <> shortcode}}
{:via, Registry, {GifMe.Game.Registry, "game:" <> shortcode}}
end

@doc """
Expand Down
19 changes: 10 additions & 9 deletions apps/game/lib/game/server_supervisor.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
defmodule GameApp.ServerSupervisor do
defmodule GifMe.Game.ServerSupervisor do
@moduledoc """
A dynamic supervisor that supervises child `GameApp.Server` processes.
A dynamic supervisor that supervises child `GifMe.Game.Server` processes.
"""

alias __MODULE__, as: ServerSupervisor
alias GameApp.Player
alias GameApp.Config, as: GameConfig
alias GifMe.Game
alias Game.Player
alias Game.Config, as: GameConfig

use DynamicSupervisor

Expand All @@ -25,15 +26,15 @@ defmodule GameApp.ServerSupervisor do

## Examples

iex> GameApp.ServerSupervisor.start_game("ABCD", %{id: "Gamer"})
iex> Game.ServerSupervisor.start_game("ABCD", %{id: "Gamer"})
{:ok, pid}

"""
@spec start_game(String.t(), Player.t(), GameConfig.t()) :: {:ok, pid()} | {:error, any()}
def start_game(shortcode, player, config \\ %GameConfig{}) do
child_spec = %{
id: GameApp.Server,
start: {GameApp.Server, :start_link, [shortcode, player, config]},
id: Game.Server,
start: {Game.Server, :start_link, [shortcode, player, config]},
# don't restart game server processes that exit normally
restart: :transient
}
Expand All @@ -48,7 +49,7 @@ defmodule GameApp.ServerSupervisor do
"""
@spec stop_game(String.t()) :: :ok | {:error, :not_found}
def stop_game(shortcode) do
child_pid = GameApp.Server.game_pid(shortcode)
child_pid = Game.Server.game_pid(shortcode)
DynamicSupervisor.terminate_child(ServerSupervisor, child_pid)
end

Expand All @@ -59,7 +60,7 @@ defmodule GameApp.ServerSupervisor do
"""
@spec find_game(String.t()) :: {:ok, pid()} | {:error, :not_found}
def find_game(shortcode) do
case GameApp.Server.game_pid(shortcode) do
case Game.Server.game_pid(shortcode) do
pid when is_pid(pid) ->
{:ok, pid}

Expand Down
8 changes: 4 additions & 4 deletions apps/game/lib/game/state/game.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule GameApp.Game do
defmodule GifMe.Game.Game do
@moduledoc """
`GameApp.Game` defines a struct that encapsulates game state as well as many
`GifMe.Game.Game` defines a struct that encapsulates game state as well as many
functions that advance the game state based on actions that players can take.
"""

alias __MODULE__, as: Game
alias GameApp.{Player, Round}
alias GameApp.Config, as: GameConfig
alias GifMe.Game.{Player, Round}
alias GifMe.Game.Config, as: GameConfig

require Logger

Expand Down
2 changes: 1 addition & 1 deletion apps/game/lib/game/state/player.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GameApp.Player do
defmodule GifMe.Game.Player do
@moduledoc """
A player in the game.
"""
Expand Down
6 changes: 3 additions & 3 deletions apps/game/lib/game/state/round.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule GameApp.Round do
defmodule GifMe.Game.Round do
@moduledoc """
`GameApp.Round` defines a struct that encapsulates Round state as well as
`GifMe.Game.Round` defines a struct that encapsulates Round state as well as
functions that update the round state.
"""

alias __MODULE__, as: Round
alias GameApp.Player
alias GifMe.Game.Player

@enforce_keys [:number]
defstruct number: nil,
Expand Down
4 changes: 2 additions & 2 deletions apps/game/mix.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GameApp.MixProject do
defmodule GifMe.Game.MixProject do
use Mix.Project

def project do
Expand All @@ -20,7 +20,7 @@ defmodule GameApp.MixProject do
def application do
[
extra_applications: [:logger],
mod: {GameApp.Application, []}
mod: {GifMe.Game.Application, []}
]
end

Expand Down
6 changes: 3 additions & 3 deletions apps/game/test/game_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule GameTest do
use ExUnit.Case, async: true
alias GameApp.{Round, Game, Player}
alias GameApp.Config, as: GameConfig
alias GifMe.Game.{Round, Game, Player}
alias GifMe.Game.Config, as: GameConfig

doctest GameApp.Game, import: true
doctest GifMe.Game.Game, import: true

@shortcode "ABCD"
@player1 Player.create(id: "1", name: "Gamer")
Expand Down
4 changes: 2 additions & 2 deletions apps/game/test/round_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule RoundTest do
use ExUnit.Case, async: true
alias GameApp.{Player, Round}
alias GifMe.Game.{Player, Round}

doctest GameApp.Round, import: true
doctest GifMe.Game.Round, import: true

@player Player.create(id: "1", name: "Gamer")

Expand Down
Loading