Skip to content

Commit

Permalink
Update deathmatch endgame check to fit teams feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico-Sanchez committed Dec 6, 2024
1 parent c397023 commit 06fb10b
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -329,29 +329,38 @@ defmodule Arena.GameUpdater do
end

def handle_info(:deathmatch_end_game_check, state) do
players =
players_with_kills =
state.game_state.players
|> Enum.map(fn {player_id, player} ->
%{kills: kills} = GameTracker.get_player_result(player_id)
{player_id, player, kills}
Map.put(player, :kills, kills)
end)

{winner_team, _team_kills} =
players_with_kills
|> Enum.group_by(fn player -> player.aditional_info.team end)
|> Enum.map(fn {team, players} ->
team_kills = Enum.reduce(players, 0, fn player, acc -> player.kills + acc end)
{team, team_kills}
end)
|> Enum.sort_by(fn {_player_id, _player, kills} -> kills end, :desc)
|> Enum.max_by(fn {_team, team_kills} -> team_kills end)

{winner_id, winner, _kills} = Enum.at(players, 0)
winners =
Enum.filter(state.game_state.players, fn {_player_id, player} -> player.aditional_info.team == winner_team end)

state =
state
|> put_in([:game_state, :status], :ENDED)
|> update_in([:game_state], fn game_state ->
players
|> Enum.reduce(game_state, fn {player_id, _player, _kills}, game_state_acc ->
put_player_position(game_state_acc, player_id)
players_with_kills
|> Enum.reduce(game_state, fn player, game_state_acc ->
put_player_position(game_state_acc, player.id)
end)
end)

PubSub.broadcast(Arena.PubSub, state.game_state.game_id, :end_game_state)
broadcast_game_ended(winner, state.game_state)
GameTracker.finish_tracking(self(), winner_id)
broadcast_game_ended(winners, state.game_state)
GameTracker.finish_tracking(self(), winner_team)

Process.send_after(self(), :game_ended, state.game_config.game.shutdown_game_wait_ms)

Expand Down

0 comments on commit 06fb10b

Please sign in to comment.