Skip to content

Commit

Permalink
Merge pull request #562 from geekingfrog/autohost-to-generic-bots
Browse files Browse the repository at this point in the history
Autohost to generic bots
  • Loading branch information
L-e-x-o-n authored Jan 31, 2025
2 parents 4d0ec98 + 9f777ad commit e1efded
Show file tree
Hide file tree
Showing 48 changed files with 637 additions and 464 deletions.
2 changes: 1 addition & 1 deletion coveralls.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"coverage_options": {
"treat_no_relevant_lines_as_covered": true,
"output_dir": "cover/",
"minimum_coverage": 33
"minimum_coverage": 30
}
}

45 changes: 17 additions & 28 deletions lib/teiserver/autohost.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Teiserver.Autohost do
alias Teiserver.Autohost.{Autohost, Registry}
alias Teiserver.AutohostQueries
alias Teiserver.Repo
alias Teiserver.Autohost.Registry
alias Teiserver.Bot.Bot
alias Teiserver.BotQueries

@type id :: Teiserver.Autohost.Autohost.id()
@type id :: Teiserver.Bot.Bot.id()
@type reg_value :: Registry.reg_value()

@type start_script :: %{
Expand Down Expand Up @@ -31,43 +31,32 @@ defmodule Teiserver.Autohost do

@type start_response :: Teiserver.Autohost.TachyonHandler.start_response()

def create_autohost(attrs \\ %{}) do
%Autohost{}
|> Autohost.changeset(attrs)
|> Repo.insert()
end
defdelegate create_autohost(attrs \\ %{}), to: Teiserver.Bot, as: :create_bot

def change_autohost(%Autohost{} = autohost, attrs \\ %{}) do
Autohost.changeset(autohost, attrs)
end
defdelegate change_autohost(autohost, attrs \\ %{}),
to: Teiserver.Bot,
as: :change_bot

def update_autohost(%Autohost{} = autohost, attrs) do
autohost |> change_autohost(attrs) |> Repo.update()
end
defdelegate update_autohost(autohost, attrs), to: Teiserver.Bot, as: :update_bot

@spec delete(Autohost.t()) :: :ok | {:error, term()}
def delete(%Autohost{} = autohost) do
case Repo.delete(autohost) do
{:ok, _} -> :ok
{:error, err} -> {:error, err}
end
end
@spec delete(Bot.t()) :: :ok | {:error, term()}
defdelegate delete(autohost), to: Teiserver.Bot, as: :delete

defdelegate get_by_id(id), to: AutohostQueries
defdelegate get_by_id(id), to: BotQueries

@doc """
Returns the pid of the autohost registered with a given id
"""
@spec lookup_autohost(Autohost.id()) :: {pid(), reg_value()} | nil
def lookup_autohost(autohost_id) do
Teiserver.Autohost.Registry.lookup(autohost_id)
@spec lookup_autohost(Bot.id()) :: {pid(), reg_value()} | nil
def lookup_autohost(bot_id) do
Teiserver.Autohost.Registry.lookup(bot_id)
end

@spec list() :: [reg_value()]
defdelegate list(), to: Registry

@spec start_matchmaking(Autohost.id(), start_script()) ::
@spec start_matchmaking(Bot.id(), start_script()) ::
{:ok, start_response()} | {:error, term()}
defdelegate start_matchmaking(autohost_id, start_script),
defdelegate start_matchmaking(bot_id, start_script),
to: Teiserver.Autohost.TachyonHandler
end
7 changes: 4 additions & 3 deletions lib/teiserver/autohost/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ defmodule Teiserver.Autohost.Registry do
"""

alias Teiserver.Autohost.Autohost
alias Teiserver.Bot.Bot

@type reg_value :: %{
id: Autohost.id(),
id: Bot.id(),
max_battles: non_neg_integer(),
current_battles: non_neg_integer()
}
Expand All @@ -21,7 +22,7 @@ defmodule Teiserver.Autohost.Registry do
@doc """
how to reach a given autohost
"""
@spec via_tuple(Autohost.id()) :: GenServer.name()
@spec via_tuple(Bot.id()) :: GenServer.name()
def via_tuple(autohost_id) do
{:via, Horde.Registry, {__MODULE__, autohost_id}}
end
Expand All @@ -33,7 +34,7 @@ defmodule Teiserver.Autohost.Registry do
Horde.Registry.register(__MODULE__, via_tuple(autohost_id), val)
end

@spec lookup(Autohost.id()) :: {pid(), reg_value()} | nil
@spec lookup(Bot.id()) :: {pid(), reg_value()} | nil
def lookup(autohost_id) do
case Horde.Registry.lookup(__MODULE__, via_tuple(autohost_id)) do
[x] -> x
Expand Down
7 changes: 4 additions & 3 deletions lib/teiserver/autohost/tachyon_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ defmodule Teiserver.Autohost.TachyonHandler do
"""
alias Teiserver.Tachyon.{Handler, Schema}
alias Teiserver.Autohost.{Autohost, Registry}
alias Teiserver.Bot.Bot
require Logger
@behaviour Handler

@type connected_state :: %{max_battles: non_neg_integer(), current_battles: non_neg_integer()}
@type state :: %{
autohost: Autohost.t(),
autohost: Bot.t(),
state: :handshaking | {:connected, connected_state()},
pending_responses: Handler.pending_responses()
}
Expand All @@ -24,7 +25,7 @@ defmodule Teiserver.Autohost.TachyonHandler do

@impl Handler
def connect(conn) do
autohost = conn.assigns[:token].autohost
autohost = conn.assigns[:token].bot
{:ok, %{autohost: autohost, state: :handshaking, pending_responses: %{}}}
end

Expand Down Expand Up @@ -133,7 +134,7 @@ defmodule Teiserver.Autohost.TachyonHandler do
end

# TODO: there should be some kind of retry here
@spec start_matchmaking(Autohost.id(), Teiserver.Autohost.start_script()) ::
@spec start_matchmaking(Bot.id(), Teiserver.Autohost.start_script()) ::
{:ok, start_response()} | {:error, term()}
def start_matchmaking(autohost_id, start_script) do
case Registry.lookup(autohost_id) do
Expand Down
31 changes: 31 additions & 0 deletions lib/teiserver/bot.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Teiserver.Bot do
alias Teiserver.Bot.Bot
alias Teiserver.BotQueries
alias Teiserver.Repo

@type id :: Teiserver.Bot.Bot.id()

def create_bot(attrs \\ %{}) do
%Bot{}
|> Bot.changeset(attrs)
|> Repo.insert()
end

def change_bot(%Bot{} = bot, attrs \\ %{}) do
Bot.changeset(bot, attrs)
end

def update_bot(%Bot{} = bot, attrs) do
bot |> change_bot(attrs) |> Repo.update()
end

@spec delete(Bot.t()) :: :ok | {:error, term()}
def delete(%Bot{} = bot) do
case Repo.delete(bot) do
{:ok, _} -> :ok
{:error, err} -> {:error, err}
end
end

defdelegate get_by_id(id), to: BotQueries
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Teiserver.AutohostLib do
defmodule Teiserver.BotLib do
@spec icon :: String.t()
def icon, do: "fa-solid fa-robot"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
defmodule Teiserver.AutohostQueries do
defmodule Teiserver.BotQueries do
use TeiserverWeb, :queries
alias Teiserver.Autohost.Autohost
alias Teiserver.Bot.Bot

@doc """
Returns all autohosts.
Returns all bots.
That list may get big, so think about streaming and/or paginating
but for now this will do.
"""
@spec list_autohosts() :: [Autohost.t()]
def list_autohosts() do
@spec list_bots() :: [Bot.t()]
def list_bots() do
base_query() |> Repo.all()
end

@spec get_by_id(Autohost.id()) :: Autohost.t() | nil
@spec get_by_id(Bot.id()) :: Bot.t() | nil
def get_by_id(nil), do: nil

def get_by_id(id) do
base_query() |> where_id(id) |> Repo.one()
end

def base_query() do
from autohost in Autohost, as: :autohost
from bot in Bot, as: :bot
end

def where_id(query, id) do
from autohost in query,
where: autohost.id == ^id
from bot in query,
where: bot.id == ^id
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Teiserver.Autohost.Autohost do
defmodule Teiserver.Bot.Bot do
@moduledoc false
use TeiserverWeb, :schema

Expand All @@ -8,14 +8,14 @@ defmodule Teiserver.Autohost.Autohost do
name: String.t()
}

schema "teiserver_autohosts" do
schema "teiserver_bots" do
field :name, :string

timestamps(type: :utc_datetime)
end

def changeset(autohost, attrs) do
autohost
def changeset(bot, attrs) do
bot
|> cast(attrs, [:name])
|> Ecto.Changeset.validate_required([:name])
|> Ecto.Changeset.validate_length(:name, min: 3, max: 30)
Expand Down
Loading

0 comments on commit e1efded

Please sign in to comment.