Skip to content

Commit

Permalink
Merge branch 'main' into movement-depending-on-time-instead-of-ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
agustinesco committed Jun 19, 2024
2 parents 1467175 + b7f2f51 commit 2fdfa14
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 167 deletions.
75 changes: 0 additions & 75 deletions .github/workflows/central-brazil-testing-deploy.yml

This file was deleted.

10 changes: 7 additions & 3 deletions apps/arena/lib/arena/game_launcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ defmodule Arena.GameLauncher do

@impl true
def handle_call({:join_quick_game, client_id, character_name, player_name}, {from_pid, _}, state) do
create_game_for_clients([{client_id, character_name, player_name, from_pid}])
create_game_for_clients([{client_id, character_name, player_name, from_pid}], %{
bots_enabled: false,
zone_enabled: false
})

{:reply, :ok, state}
end
Expand Down Expand Up @@ -128,13 +131,14 @@ defmodule Arena.GameLauncher do

# Receives a list of clients.
# Fills the given list with bots clients, creates a game and tells every client to join that game.
defp create_game_for_clients(clients) do
defp create_game_for_clients(clients, game_params \\ %{}) do
bot_clients = get_bot_clients(Application.get_env(:arena, :players_needed_in_match) - Enum.count(clients))

{:ok, game_pid} =
GenServer.start(Arena.GameUpdater, %{
clients: clients,
bot_clients: bot_clients
bot_clients: bot_clients,
game_params: game_params
})

game_id = game_pid |> :erlang.term_to_binary() |> Base58.encode()
Expand Down
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 @@ -150,6 +150,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 @@ -199,6 +203,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
38 changes: 30 additions & 8 deletions 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,13 +43,19 @@ 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
##########################

def init(%{clients: clients, bot_clients: bot_clients}) do
def init(%{clients: clients, bot_clients: bot_clients, game_params: game_params}) do
game_id = self() |> :erlang.term_to_binary() |> Base58.encode()
game_config = Configuration.get_game_config()
game_config = Map.put(game_config, :game, Map.merge(game_config.game, game_params))

game_state = new_game(game_id, clients ++ bot_clients, game_config)
match_id = Ecto.UUID.generate()

Expand Down Expand Up @@ -151,6 +157,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})

{:noreply, state}
end

##########################
# END API Callbacks
##########################
Expand All @@ -159,6 +176,12 @@ defmodule Arena.GameUpdater do
# Game Callbacks
##########################

def handle_info(:toggle_bots, state) do
GenServer.cast(self(), :toggle_bots)

{:noreply, state}
end

def handle_info(:update_game, %{game_state: game_state} = state) do
Process.send_after(self(), :update_game, state.game_config.game.tick_rate_ms)
now = DateTime.utc_now() |> DateTime.to_unix(:millisecond)
Expand Down Expand Up @@ -218,12 +241,11 @@ defmodule Arena.GameUpdater do
send(self(), :natural_healing)
send(self(), {:end_game_check, Map.keys(state.game_state.players)})

state =
state
|> put_in([:game_state, :zone, :enabled], true)
|> put_in([:game_state, :status], :RUNNING)
unless state.game_config.game.bots_enabled do
toggle_bots(self())
end

{:noreply, state}
{:noreply, put_in(state, [:game_state, :status], :RUNNING)}
end

def handle_info({:end_game_check, last_players_ids}, state) do
Expand Down Expand Up @@ -637,7 +659,7 @@ defmodule Arena.GameUpdater do
|> Map.put(:external_wall, Entities.new_external_wall(0, config.map.radius))
|> Map.put(:zone, %{
radius: config.map.radius,
enabled: false,
enabled: config.game.zone_enabled,
shrinking: false,
next_zone_change_timestamp:
initial_timestamp + config.game.zone_shrink_start_ms + config.game.start_game_time_ms
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
Loading

0 comments on commit 2fdfa14

Please sign in to comment.