From 4c5f0370a09a6f341ce93a9bddba61c0a8035116 Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Wed, 4 Sep 2024 11:27:45 +0200 Subject: [PATCH] Add function for removing game from GameCoordinator --- apps/amethyst/lib/apps/game_coordinator.ex | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/apps/amethyst/lib/apps/game_coordinator.ex b/apps/amethyst/lib/apps/game_coordinator.ex index d9b3ad6..44a6004 100644 --- a/apps/amethyst/lib/apps/game_coordinator.ex +++ b/apps/amethyst/lib/apps/game_coordinator.ex @@ -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