Skip to content

Commit

Permalink
[GH-696] Allow clients to modify tickrate (#704)
Browse files Browse the repository at this point in the history
* Add new ChangeTickrate protobuf message

* Add new handle_decoded_message matches functions to print unexpected messages in terminal

* Add new change_tickrate function and cast handler to modify the game tickrate when called

* Add new handle_decoded_message to catch change_tickrate message and call the corresponding function
  • Loading branch information
Nico-Sanchez authored Jun 24, 2024
1 parent ec349fb commit 0ef8952
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 4 deletions.
10 changes: 9 additions & 1 deletion apps/arena/lib/arena/game_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ defmodule Arena.GameSocketHandler do
defp handle_decoded_message(%{action_type: {:toggle_bots, _bots_params}}, state),
do: GameUpdater.toggle_bots(state.game_pid)

defp handle_decoded_message(%{action_type: {:change_tickrate, tickrate_params}}, state),
do: GameUpdater.change_tickrate(state.game_pid, tickrate_params.tickrate)

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

defp handle_decoded_message(
Expand Down Expand Up @@ -244,5 +247,10 @@ defmodule Arena.GameSocketHandler do
end
end

defp handle_decoded_message(_, _), do: nil
# We don't do anything in these messages, we already handle these actions when we have to in previous functions.
defp handle_decoded_message(%{action_type: {action, _}}, _state) when action in [:move, :attack, :use_item], do: nil

defp handle_decoded_message(message, _) do
Logger.info("Unexpected message: #{inspect(message)}")
end
end
8 changes: 8 additions & 0 deletions apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ defmodule Arena.GameUpdater do
GenServer.cast(game_pid, :toggle_bots)
end

def change_tickrate(game_pid, tickrate) do
GenServer.cast(game_pid, {:change_tickrate, tickrate})
end

##########################
# END API
##########################
Expand Down Expand Up @@ -168,6 +172,10 @@ defmodule Arena.GameUpdater do
{:noreply, state}
end

def handle_cast({:change_tickrate, tickrate}, state) do
{:noreply, put_in(state, [:game_config, :game, :tick_rate_ms], tickrate)}
end

##########################
# END API Callbacks
##########################
Expand Down
15 changes: 15 additions & 0 deletions apps/arena/lib/arena/serialization/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,14 @@ defmodule Arena.Serialization.ToggleBots do
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule Arena.Serialization.ChangeTickrate do
@moduledoc false

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

field(:tickrate, 1, type: :int64)
end

defmodule Arena.Serialization.GameAction do
@moduledoc false

Expand All @@ -660,6 +668,13 @@ 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(:change_tickrate, 8,
type: Arena.Serialization.ChangeTickrate,
json_name: "changeTickrate",
oneof: 0
)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,14 @@ defmodule ArenaLoadTest.Serialization.ToggleBots do
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule ArenaLoadTest.Serialization.ChangeTickrate do
@moduledoc false

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

field(:tickrate, 1, type: :int64)
end

defmodule ArenaLoadTest.Serialization.GameAction do
@moduledoc false

Expand Down Expand Up @@ -717,6 +725,12 @@ defmodule ArenaLoadTest.Serialization.GameAction do
oneof: 0
)

field(:change_tickrate, 8,
type: ArenaLoadTest.Serialization.ChangeTickrate,
json_name: "changeTickrate",
oneof: 0
)

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

Expand Down
15 changes: 15 additions & 0 deletions apps/bot_manager/lib/protobuf/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,14 @@ defmodule BotManager.Protobuf.ToggleBots do
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule BotManager.Protobuf.ChangeTickrate do
@moduledoc false

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

field(:tickrate, 1, type: :int64)
end

defmodule BotManager.Protobuf.GameAction do
@moduledoc false

Expand All @@ -660,6 +668,13 @@ 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(:change_tickrate, 8,
type: BotManager.Protobuf.ChangeTickrate,
json_name: "changeTickrate",
oneof: 0
)

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

Expand Down
Loading

0 comments on commit 0ef8952

Please sign in to comment.