Add function for removing game from GameCoordinator
Some checks failed
Build & Test / nix-build (push) Failing after 18s

This commit is contained in:
Kodi Craft 2024-09-04 11:27:45 +02:00
parent d63bd0a9b2
commit 4c5f0370a0
Signed by: kodi
GPG Key ID: 69D9EED60B242822

View File

@ -21,6 +21,7 @@ defmodule Amethyst.GameCoordinator do
The game coordinator is responsible for keeping track of active
instances of each game and can create new ones on demand.
"""
require Logger
defmodule State do
@moduledoc """
@ -73,6 +74,12 @@ defmodule Amethyst.GameCoordinator do
end
end
@impl true
def handle_cast({:remove, gid}, state) do
{games, state} = _remove(gid, state)
{:noreply, state}
end
@spec _create(atom(), State.t()) :: {Game.t(), State.t()}
defp _create(type, state) do
# Create a DynamicSupervisor for this game
@ -92,6 +99,7 @@ defmodule Amethyst.GameCoordinator do
mod: type, refs: refs, opts: [], gid: state.gid
}
games = state.games |> Map.put(state.gid, game)
Logger.info("Created new game #{inspect(game)}")
{game, %State{gid: state.gid + 1, games: games}}
end
@ -102,6 +110,11 @@ defmodule Amethyst.GameCoordinator do
end
end
defp _remove(gid, state) do
games = state.games |> Map.delete(gid)
{games, %State{state | games: games}}
end
def create(type) when is_atom(type) do
GenServer.call({:global, __MODULE__}, {:create, type})
end
@ -111,4 +124,7 @@ defmodule Amethyst.GameCoordinator do
def find_or_create(type) when is_atom(type) do
GenServer.call({:global, __MODULE__}, {:find_or_create, type})
end
def remove(gid) when is_integer(gid) do
GenServer.cast({:global, __MODULE__}, {:remove, gid})
end
end