Skip to content
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 692 allow client to toggle bots #693

Merged
merged 15 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/arena/lib/arena/game_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ defmodule Arena.GameSocketHandler do
end
end

def websocket_info({:toggle_bots, message}, state) do
{:reply, {:binary, message}, state}
end

@impl true
def websocket_info(message, state) do
Logger.info("You should not be here: #{inspect(message)}")
Expand Down Expand Up @@ -185,6 +189,9 @@ defmodule Arena.GameSocketHandler do
defp handle_decoded_message(%{action_type: {:toggle_zone, _zone_params}}, state),
do: GameUpdater.toggle_zone(state.game_pid)

defp handle_decoded_message(%{action_type: {:toggle_bots, _bots_params}}, state),
do: GameUpdater.toggle_bots(state.game_pid)

defp handle_decoded_message(_action_type, %{enable: false} = _state), do: nil

defp handle_decoded_message(
Expand Down
17 changes: 16 additions & 1 deletion apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Arena.GameUpdater do
alias Arena.Game.Effect
alias Arena.{Configuration, Entities}
alias Arena.Game.{Player, Skill}
alias Arena.Serialization.{GameEvent, GameState, GameFinished}
alias Arena.Serialization.{GameEvent, GameState, GameFinished, ToggleBots}
alias Phoenix.PubSub
alias Arena.Utils
alias Arena.Game.Trap
Expand Down Expand Up @@ -43,6 +43,10 @@ defmodule Arena.GameUpdater do
GenServer.cast(game_pid, :toggle_zone)
end

def toggle_bots(game_pid) do
GenServer.cast(game_pid, :toggle_bots)
end

##########################
# END API
##########################
Expand Down Expand Up @@ -151,6 +155,17 @@ defmodule Arena.GameUpdater do
{:noreply, state}
end

def handle_cast(:toggle_bots, state) do
encoded_msg =
GameEvent.encode(%GameEvent{
event: {:toggle_bots, %ToggleBots{}}
})

PubSub.broadcast(Arena.PubSub, state.game_state.game_id, {:toggle_bots, encoded_msg})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this message broadcasted to the clients? The only difference turning on or off the bots makes to them is received through the GameState message, they don't handle this message in any way.

Client PR for reference: [GH-1831] Add toggle bots button

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bots are clients as well. Said so, the bots ARE handling that message to change its state.
You can check it in apps/bot_manager/lib/game_socket_handler.ex

Also, remember the bots are deployed in a different server than where the backend runs (arena), so we need to send the message somehow (I use this broadcast for that)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, that makes sense!


{:noreply, state}
end

##########################
# END API Callbacks
##########################
Expand Down
8 changes: 8 additions & 0 deletions apps/arena/lib/arena/serialization/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ defmodule Arena.Serialization.GameEvent do
field(:update, 2, type: Arena.Serialization.GameState, oneof: 0)
field(:finished, 3, type: Arena.Serialization.GameFinished, oneof: 0)
field(:ping, 4, type: Arena.Serialization.PingUpdate, oneof: 0)
field(:toggle_bots, 5, type: Arena.Serialization.ToggleBots, json_name: "toggleBots", oneof: 0)
end

defmodule Arena.Serialization.GameFinished.PlayersEntry do
Expand Down Expand Up @@ -634,6 +635,12 @@ defmodule Arena.Serialization.ToggleZone do
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule Arena.Serialization.ToggleBots do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule Arena.Serialization.GameAction do
@moduledoc false

Expand All @@ -652,6 +659,7 @@ defmodule Arena.Serialization.GameAction do
)

field(:toggle_zone, 6, type: Arena.Serialization.ToggleZone, json_name: "toggleZone", oneof: 0)
field(:toggle_bots, 7, type: Arena.Serialization.ToggleBots, json_name: "toggleBots", oneof: 0)
field(:timestamp, 3, type: :int64)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ defmodule ArenaLoadTest.Serialization.GameEvent do
field(:update, 2, type: ArenaLoadTest.Serialization.GameState, oneof: 0)
field(:finished, 3, type: ArenaLoadTest.Serialization.GameFinished, oneof: 0)
field(:ping, 4, type: ArenaLoadTest.Serialization.PingUpdate, oneof: 0)

field(:toggle_bots, 5,
type: ArenaLoadTest.Serialization.ToggleBots,
json_name: "toggleBots",
oneof: 0
)
end

defmodule ArenaLoadTest.Serialization.GameFinished.PlayersEntry do
Expand Down Expand Up @@ -676,6 +682,12 @@ defmodule ArenaLoadTest.Serialization.ToggleZone do
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule ArenaLoadTest.Serialization.ToggleBots do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule ArenaLoadTest.Serialization.GameAction do
@moduledoc false

Expand All @@ -699,6 +711,12 @@ defmodule ArenaLoadTest.Serialization.GameAction do
oneof: 0
)

field(:toggle_bots, 7,
type: ArenaLoadTest.Serialization.ToggleBots,
json_name: "toggleBots",
oneof: 0
)

field(:timestamp, 3, type: :int64)
end

Expand Down
4 changes: 4 additions & 0 deletions apps/bot_manager/lib/bot_state_machine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ defmodule BotManager.BotStateMachine do
alias BotManager.Utils
alias BotManager.Math.Vector

def decide_action(%{bots_enabled?: false}) do
{:move, %{x: 0, y: 0}}
end

def decide_action(%{game_state: game_state, bot_player: bot_player}) do
closest_player =
map_directions_to_players(game_state, bot_player)
Expand Down
5 changes: 4 additions & 1 deletion apps/bot_manager/lib/game_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule BotManager.GameSocketHandler do
def handle_connect(_conn, state) do
send(self(), :decide_action)
send(self(), :perform_action)
{:ok, state}
{:ok, Map.put(state, :bots_enabled?, true)}
end

def handle_frame({:binary, frame}, state) do
Expand All @@ -49,6 +49,9 @@ defmodule BotManager.GameSocketHandler do
%{event: {:finished, _}} ->
exit(:shutdown)

%{event: {:toggle_bots, _}} ->
{:ok, Map.put(state, :bots_enabled?, not state.bots_enabled?)}

_ ->
{:ok, state}
end
Expand Down
8 changes: 8 additions & 0 deletions apps/bot_manager/lib/protobuf/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ defmodule BotManager.Protobuf.GameEvent do
field(:update, 2, type: BotManager.Protobuf.GameState, oneof: 0)
field(:finished, 3, type: BotManager.Protobuf.GameFinished, oneof: 0)
field(:ping, 4, type: BotManager.Protobuf.PingUpdate, oneof: 0)
field(:toggle_bots, 5, type: BotManager.Protobuf.ToggleBots, json_name: "toggleBots", oneof: 0)
end

defmodule BotManager.Protobuf.GameFinished.PlayersEntry do
Expand Down Expand Up @@ -634,6 +635,12 @@ defmodule BotManager.Protobuf.ToggleZone do
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule BotManager.Protobuf.ToggleBots do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule BotManager.Protobuf.GameAction do
@moduledoc false

Expand All @@ -652,6 +659,7 @@ defmodule BotManager.Protobuf.GameAction do
)

field(:toggle_zone, 6, type: BotManager.Protobuf.ToggleZone, json_name: "toggleZone", oneof: 0)
field(:toggle_bots, 7, type: BotManager.Protobuf.ToggleBots, json_name: "toggleBots", oneof: 0)
field(:timestamp, 3, type: :int64)
end

Expand Down
Loading
Loading