Begin implementing logging player into a game
All checks were successful
Build & Test / nix-build (push) Successful in 1m9s

This commit is contained in:
Kodi Craft 2024-08-18 13:44:09 +02:00
parent 11db275db2
commit 78442a94af
Signed by: kodi
GPG Key ID: 69D9EED60B242822
6 changed files with 85 additions and 17 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
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

View File

@ -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 <https://www.gnu.org/licenses/>.
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

View File

@ -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 <https://www.gnu.org/licenses/>.
defmodule Amethyst.GameRegistry do
use GenServer

View File

@ -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)

View File

@ -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

View File

@ -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: []