-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into add-team-attribute-to…
…-entities
- Loading branch information
Showing
19 changed files
with
840 additions
and
1,143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
elixir 1.16.3-otp-26 | ||
erlang 26.2.5.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
defmodule Arena.Matchmaking.DeathmatchMode do | ||
@moduledoc false | ||
alias Arena.Utils | ||
alias Ecto.UUID | ||
|
||
use GenServer | ||
|
||
# 3 Mins | ||
# TODO: add this to the configurator https://github.com/lambdaclass/mirra_backend/issues/985 | ||
@match_duration 180_000 | ||
@respawn_time 5000 | ||
|
||
# API | ||
def start_link(_) do | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
def join(client_id, character_name, player_name) do | ||
GenServer.call(__MODULE__, {:join, client_id, character_name, player_name}) | ||
end | ||
|
||
def leave(client_id) do | ||
GenServer.call(__MODULE__, {:leave, client_id}) | ||
end | ||
|
||
# Callbacks | ||
@impl true | ||
def init(_) do | ||
Process.send_after(self(), :launch_game?, 300) | ||
{:ok, %{clients: [], batch_start_at: 0}} | ||
end | ||
|
||
@impl true | ||
def handle_call({:join, client_id, character_name, player_name}, {from_pid, _}, %{clients: clients} = state) do | ||
batch_start_at = maybe_make_batch_start_at(state.clients, state.batch_start_at) | ||
|
||
client = %{ | ||
client_id: client_id, | ||
character_name: character_name, | ||
name: player_name, | ||
from_pid: from_pid, | ||
type: :human | ||
} | ||
|
||
{:reply, :ok, | ||
%{ | ||
state | ||
| batch_start_at: batch_start_at, | ||
clients: clients ++ [client] | ||
}} | ||
end | ||
|
||
def handle_call({:leave, client_id}, _, state) do | ||
clients = Enum.reject(state.clients, fn %{client_id: id} -> id == client_id end) | ||
{:reply, :ok, %{state | clients: clients}} | ||
end | ||
|
||
@impl true | ||
def handle_info(:launch_game?, %{clients: clients} = state) do | ||
Process.send_after(self(), :launch_game?, 300) | ||
diff = System.monotonic_time(:millisecond) - state.batch_start_at | ||
|
||
if length(clients) >= Application.get_env(:arena, :players_needed_in_match) or | ||
(diff >= Utils.start_timeout_ms() and length(clients) > 0) do | ||
send(self(), :start_game) | ||
end | ||
|
||
{:noreply, state} | ||
end | ||
|
||
def handle_info(:start_game, state) do | ||
{game_clients, remaining_clients} = Enum.split(state.clients, Application.get_env(:arena, :players_needed_in_match)) | ||
create_game_for_clients(game_clients) | ||
|
||
{:noreply, %{state | clients: remaining_clients}} | ||
end | ||
|
||
def handle_info({:spawn_bot_for_player, bot_client, game_id}, state) do | ||
spawn(fn -> | ||
Finch.build(:get, Utils.get_bot_connection_url(game_id, bot_client)) | ||
|> Finch.request(Arena.Finch) | ||
end) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
defp maybe_make_batch_start_at([], _) do | ||
System.monotonic_time(:millisecond) | ||
end | ||
|
||
defp maybe_make_batch_start_at([_ | _], batch_start_at) do | ||
batch_start_at | ||
end | ||
|
||
defp spawn_bot_for_player(bot_clients, game_id) do | ||
Enum.each(bot_clients, fn %{client_id: bot_client_id} -> | ||
send(self(), {:spawn_bot_for_player, bot_client_id, game_id}) | ||
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) | ||
|
||
bot_names = Utils.list_bot_names(missing_clients) | ||
|
||
Enum.map(1..missing_clients//1, fn i -> | ||
client_id = UUID.generate() | ||
|
||
%{client_id: client_id, character_name: Enum.random(characters).name, name: Enum.at(bot_names, i - 1), type: :bot} | ||
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 | ||
game_params = | ||
Map.merge(game_params, %{ | ||
game_mode: :DEATHMATCH, | ||
match_duration: @match_duration, | ||
respawn_time: @respawn_time | ||
}) | ||
|
||
# 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 | ||
|
||
players = Utils.assign_teams_to_players(clients ++ bot_clients, :solo) | ||
|
||
{:ok, game_pid} = GenServer.start(Arena.GameUpdater, %{players: players, game_params: game_params}) | ||
|
||
game_id = game_pid |> :erlang.term_to_binary() |> Base58.encode() | ||
|
||
spawn_bot_for_player(bot_clients, game_id) | ||
|
||
Enum.each(clients, fn %{from_pid: from_pid} -> | ||
Process.send(from_pid, {:join_game, game_id}, []) | ||
Process.send(from_pid, :leave_waiting_game, []) | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.