Skip to content

Commit

Permalink
[GH-607] Only spawn bots if player is alive (#608)
Browse files Browse the repository at this point in the history
* Only spawn bots if players is alive

* Split enabled from player alive
  • Loading branch information
agustinesco authored May 14, 2024
1 parent 03211e1 commit 2d5684a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions apps/arena/lib/arena/game_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule Arena.GameSocketHandler do
|> Map.put(:enable, game_status == :RUNNING)
|> Map.put(:block_actions, false)
|> Map.put(:game_finished, game_status == :ENDED)
|> Map.put(:player_alive, true)

encoded_msg =
GameEvent.encode(%GameEvent{
Expand Down Expand Up @@ -131,7 +132,12 @@ defmodule Arena.GameSocketHandler do
@impl true
def websocket_info({:player_dead, player_id}, state) do
if state.player_id == player_id do
{:ok, Map.put(state, :enable, false)}
state =
state
|> Map.put(:enable, false)
|> Map.put(:player_alive, false)

{:ok, state}
else
{:ok, state}
end
Expand All @@ -153,7 +159,7 @@ defmodule Arena.GameSocketHandler do
end

@impl true
def terminate(_reason, _req, %{game_finished: false} = state) do
def terminate(_reason, _req, %{game_finished: false, player_alive: true} = state) do
spawn(fn ->
Finch.build(:get, Utils.get_bot_connection_url(state.game_id, state.client_id))
|> Finch.request(Arena.Finch)
Expand Down

0 comments on commit 2d5684a

Please sign in to comment.