diff --git a/apps/amethyst/lib/api/game.ex b/apps/amethyst/lib/api/game.ex index 2723dfe..cdfa36d 100644 --- a/apps/amethyst/lib/api/game.ex +++ b/apps/amethyst/lib/api/game.ex @@ -1,9 +1,26 @@ +# Amethyst - An experimental Minecraft server written in Elixir. +# Copyright (C) 2024 KodiCraft +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + defmodule Amethyst.API.Game do @moduledoc """ This module includes the interface for defining and registering a game with Amethyst. """ @callback instantiate() :: {:ok, new_state :: term} | {:error, reason :: term} + @callback login(connection_process :: pid(), player_cfg :: keyword(), state :: term) :: {:ok, new_state :: term} | {:refuse, new_state :: term} defmacro __using__(opts) do meta = Keyword.get(opts, :meta, []) @@ -23,8 +40,26 @@ defmodule Amethyst.API.Game do loop(state) end defp loop(state) do - loop(state) + receive do + {:login, caller, cfg} -> + case login(caller, cfg, state) do + {:ok, state} -> + send(caller, :ok) + loop(state) + {:refuse, state} -> + send(caller, :refuse) + loop(state) + end + end end end end + + def login(pid, cfg) do + send(pid, {:login, self(), cfg}) + receive do + :ok -> :ok + :refuse -> :refuse + end + end end diff --git a/apps/amethyst/lib/apps/game_coordinator.ex b/apps/amethyst/lib/apps/game_coordinator.ex index c915402..b1dceb3 100644 --- a/apps/amethyst/lib/apps/game_coordinator.ex +++ b/apps/amethyst/lib/apps/game_coordinator.ex @@ -1,3 +1,19 @@ +# Amethyst - An experimental Minecraft server written in Elixir. +# Copyright (C) 2024 KodiCraft +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + defmodule Amethyst.GameCoordinator do use GenServer @@ -55,13 +71,13 @@ defmodule Amethyst.GameCoordinator do end end - def create(type) do + def create(type) when is_atom(type) do GenServer.call({:global, __MODULE__}, {:create, type}) end - def find(type) do + def find(type) when is_atom(type) do GenServer.call({:global, __MODULE__}, {:find, type}) end - def find_or_create(type) do + def find_or_create(type) when is_atom(type) do GenServer.call({:global, __MODULE__}, {:find_or_create, type}) end end diff --git a/apps/amethyst/lib/apps/game_registry.ex b/apps/amethyst/lib/apps/game_registry.ex index c1465b6..2b626ea 100644 --- a/apps/amethyst/lib/apps/game_registry.ex +++ b/apps/amethyst/lib/apps/game_registry.ex @@ -1,3 +1,19 @@ +# Amethyst - An experimental Minecraft server written in Elixir. +# Copyright (C) 2024 KodiCraft +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + defmodule Amethyst.GameRegistry do use GenServer diff --git a/apps/amethyst/lib/servers/configuration.ex b/apps/amethyst/lib/servers/configuration.ex index 71f2db2..36e56c2 100644 --- a/apps/amethyst/lib/servers/configuration.ex +++ b/apps/amethyst/lib/servers/configuration.ex @@ -327,16 +327,14 @@ defmodule Amethyst.Server.Configuration do end # Acknowledge Finish Configuration https://wiki.vg/Protocol#Acknowledge_Finish_Configuration def handle({:acknowledge_finish_configuration}, client, state) do + game = Amethyst.GameRegistry.get_default() |> elem(0) |> Amethyst.GameCoordinator.find_or_create() + state = Keyword.put(state, :game, game) + Amethyst.API.Game.login(game, state) # TODO: All of this stuff should obviously not be hardcoded here Amethyst.Server.Play.transmit({:login, 0, false, ["minecraft:overworld"], 0, 16, 16, false, true, true, 0, "minecraft:overworld", <<0::64>>, :spectator, nil, false, true, nil, 0, false }, client) - game = Amethyst.GameRegistry.get_default() - if game == nil do - raise RuntimeError, "No game is set as a default! A single game must be defined as default for players to be able to join." - end - state = Keyword.put(state, :game, game) Amethyst.Server.Play.serve(client, state) end # Serverbound Plugin Message https://wiki.vg/Protocol#Serverbound_Plugin_Message_(configuration) diff --git a/apps/example_game/lib/example/game.ex b/apps/example_game/lib/example/game.ex index 0ead295..e8163a3 100644 --- a/apps/example_game/lib/example/game.ex +++ b/apps/example_game/lib/example/game.ex @@ -1,8 +1,15 @@ defmodule Example.Game do + require Logger use Amethyst.API.Game, meta: [default: true] @impl true def instantiate() do {:ok, [:hello]} end + + @impl true + def login(from, cfg, state) do + Logger.debug("Player logged in from #{inspect(from)}: #{inspect(cfg)}") + {:ok, state} + end end diff --git a/config/config.exs b/config/config.exs index ab23e80..000c5f1 100644 --- a/config/config.exs +++ b/config/config.exs @@ -8,11 +8,7 @@ # configurations or dependencies per app, it is best to # move said applications out of the umbrella. import Config - -# Sample configuration: -# -# config :logger, :console, -# level: :info, -# format: "$date $time [$level] $metadata$message\n", -# metadata: [:user_id] -# + config :logger, :console, + level: :debug, + format: "$date $time [$level] $metadata$message", + metadata: []