-
Notifications
You must be signed in to change notification settings - Fork 1
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
[GH-917] - Deathmatch mode #980
Open
tvillegas98
wants to merge
18
commits into
main
Choose a base branch
from
gh-917-deathmatch-iteration-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3cf6561
feat: enable deathmatch matchmaking
tvillegas98 4414be9
feat: add end game check for deathmatch
tvillegas98 6967bbc
Merge branch 'main' into gh-917-deathmatch-iteration-1
tvillegas98 c46d9fe
feat: add deathmatch logic into the game updater
tvillegas98 7724a21
feat: show deathmatch mode in the browser
tvillegas98 bf45d09
fix: input when respawning was not working
tvillegas98 08a26ac
fix: win condition
tvillegas98 c646b11
fix: battle royale was broken
tvillegas98 7bd02c4
fix: add mising parameter in pair mode matchmaking
tvillegas98 5522eb0
feat: increase deathmatch game time
tvillegas98 b93e2b1
feat: send the game mode to the client
tvillegas98 3eb0d77
fix: players were not respawning
tvillegas98 ab95ea6
chore: requested changes
tvillegas98 703ab15
fix: typo in deathmatch
tvillegas98 664702b
Merge branch 'main' into gh-917-deathmatch-iteration-1
tvillegas98 bd37988
feat: centralize bot names
tvillegas98 7112f16
fix: another wrong check and fix player positions
tvillegas98 004f3b8
chore: mix format
tvillegas98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,139 @@ | ||
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 | ||
|
||
# Time to wait to start game with any amount of clients | ||
@start_timeout_ms 4_000 | ||
|
||
# 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) | ||
|
||
{:reply, :ok, | ||
%{ | ||
state | ||
| batch_start_at: batch_start_at, | ||
clients: clients ++ [{client_id, character_name, player_name, from_pid}] | ||
}} | ||
end | ||
|
||
def handle_call({:leave, client_id}, _, state) do | ||
clients = Enum.reject(state.clients, fn {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 >= @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 {bot_client, _, _, _} -> | ||
send(self(), {:spawn_bot_for_player, bot_client, 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) | ||
|
||
Enum.map(1..missing_clients//1, fn i -> | ||
client_id = UUID.generate() | ||
|
||
{client_id, Enum.random(characters).name, Enum.at(Arena.Utils.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 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, %{ | ||
clients: clients, | ||
bot_clients: bot_clients, | ||
game_params: | ||
game_params | ||
|> Map.put(:game_mode, :DEATHMATCH) | ||
|> Map.put(:match_duration, @match_duration) | ||
|> Map.put(:respawn_time, @respawn_time) | ||
}) | ||
|
||
game_id = game_pid |> :erlang.term_to_binary() |> Base58.encode() | ||
|
||
spawn_bot_for_player(bot_clients, game_id) | ||
|
||
Enum.each(clients, fn {_client_id, _character_name, _player_name, 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a matchmaking file for each game mode