From abd52d466a92b1a7539135bbca63d2ca0a39140c Mon Sep 17 00:00:00 2001 From: Manuel Camejo Date: Thu, 26 Sep 2024 09:30:06 -0300 Subject: [PATCH] If you are alone bots will appear in battle royale mode --- .../lib/arena/matchmaking/game_launcher.ex | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/apps/arena/lib/arena/matchmaking/game_launcher.ex b/apps/arena/lib/arena/matchmaking/game_launcher.ex index 3ad1d6221..fb92a9333 100644 --- a/apps/arena/lib/arena/matchmaking/game_launcher.ex +++ b/apps/arena/lib/arena/matchmaking/game_launcher.ex @@ -1,12 +1,27 @@ defmodule Arena.Matchmaking.GameLauncher do @moduledoc false alias Arena.Utils + alias Ecto.UUID use GenServer # Time to wait to start game with any amount of clients @start_timeout_ms 4_000 + @bot_names [ + "TheBlackSwordman", + "SlashJava", + "SteelBallRun", + "Jeff", + "Messi", + "Stone Ocean", + "Jeepers Creepers", + "Bob", + "El javo", + "Alberso", + "Thomas" + ] + # API def start_link(_) do GenServer.start_link(__MODULE__, [], name: __MODULE__) @@ -87,12 +102,28 @@ defmodule Arena.Matchmaking.GameLauncher do end) end + defp get_bot_clients(missing_clients) do + characters = + Arena.Configuration.get_game_config() + |> Map.get(:characters) + |> Enum.filter(fn character -> character.active end) + + Enum.map(1..missing_clients//1, fn i -> + client_id = UUID.generate() + + {client_id, Enum.random(characters).name, Enum.at(@bot_names, i), nil} + end) + end + # Receives a list of clients. # Fills the given list with bots clients, creates a game and tells every client to join that game. defp create_game_for_clients(clients, game_params \\ %{}) do - # We won't spawn bots in normal matches. - # Check https://github.com/lambdaclass/mirra_backend/pull/951 to know how to restore former behavior - bot_clients = [] + # We spawn bots only if there is one player + bot_clients = + case Enum.count(clients) do + 1 -> get_bot_clients(Application.get_env(:arena, :players_needed_in_match) - Enum.count(clients)) + _ -> [] + end {:ok, game_pid} = GenServer.start(Arena.GameUpdater, %{