Skip to content

Commit

Permalink
feat: fix match positions in deathmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tvillegas98 committed Dec 5, 2024
1 parent ac701ad commit eb7ddec
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit eb7ddec

Please sign in to comment.