Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If you are alone bots will appear in battle royale mode #965

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions apps/arena/lib/arena/matchmaking/game_launcher.ex
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down Expand Up @@ -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, %{
Expand Down
Loading