From eb7ddec858a36e5c0b3d9b3a7b1337feb071a84b Mon Sep 17 00:00:00 2001 From: tvillegas98 Date: Thu, 5 Dec 2024 18:59:56 -0300 Subject: [PATCH] feat: fix match positions in deathmatch --- apps/arena/lib/arena/game_updater.ex | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/arena/lib/arena/game_updater.ex b/apps/arena/lib/arena/game_updater.ex index 99d53e71a..065009934 100644 --- a/apps/arena/lib/arena/game_updater.ex +++ b/apps/arena/lib/arena/game_updater.ex @@ -333,16 +333,17 @@ defmodule Arena.GameUpdater do {player_id, player, kills} end) |> Enum.sort_by(fn {_player_id, _player, kills} -> kills end, :desc) + |> Enum.with_index(1) - {winner_id, winner, _kills} = Enum.at(players, 0) + {{winner_id, winner, _kills}, _position} = Enum.at(players, 0) 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) + |> Enum.reduce(game_state, fn {{player_id, _player, kills}, position}, game_state_acc -> + put_player_position(game_state_acc, player_id, position) end) end) @@ -1851,6 +1852,13 @@ defmodule Arena.GameUpdater do update_in(game_state, [:positions], fn positions -> Map.put(positions, client_id, "#{next_position}") end) end + defp put_player_position(game_state, player_id, next_position) do + {client_id, _player_id} = + Enum.find(game_state.client_to_player_map, fn {_, map_player_id} -> map_player_id == player_id end) + + update_in(game_state, [:positions], fn positions -> Map.put(positions, client_id, "#{next_position}") end) + end + defp maybe_add_kill_to_player(%{players: players} = game_state, player_id) do if Map.has_key?(players, player_id) do update_in(game_state, [:players, player_id, :aditional_info, :kill_count], fn count ->