Skip to content

Commit

Permalink
[GH-852] Configurator: Migrate power up config (#853)
Browse files Browse the repository at this point in the history
* Delete already migrated items config

* Add power up fields to game config

* Show and edit new power up fields from game configurator

* Use new power up fields from config instead of json

* Add power ups fields to game seeds

* Remove unnecessary power up fields from config.json

* Fix game configuration creation

* Remove missing power_up from json config

* Update apps/configurator/lib/configurator_web/controllers/game_configuration_html/game_configuration_form.html.heex

Co-authored-by: Tomás Villegas <[email protected]>

---------

Co-authored-by: Tomás Villegas <[email protected]>
  • Loading branch information
agustinesco and tvillegas98 authored Aug 22, 2024
1 parent e6f6b4a commit e580917
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 73 deletions.
12 changes: 12 additions & 0 deletions apps/arena/lib/arena/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ defmodule Arena.Configuration do
|> Map.update!(:characters, fn characters ->
parse_characters_config(characters)
end)
|> Map.update!(:game, fn game ->
parse_game_config(game)
end)
end

defp get_client_config() do
Expand Down Expand Up @@ -213,6 +216,15 @@ defmodule Arena.Configuration do
%{x: maybe_to_float(x), y: maybe_to_float(y)}
end

defp parse_game_config(game_config) do
%{
game_config
| power_up_damage_modifier: maybe_to_float(game_config.power_up_damage_modifier),
power_up_health_modifier: maybe_to_float(game_config.power_up_health_modifier),
power_up_radius: maybe_to_float(game_config.power_up_radius)
}
end

defp maybe_to_float(nil), do: nil

defp maybe_to_float(float_integer) when is_integer(float_integer) do
Expand Down
10 changes: 5 additions & 5 deletions apps/arena/lib/arena/entities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule Arena.Entities do
character_name: character.name,
forced_movement: false,
power_ups: 0,
power_up_damage_modifier: config.power_ups.power_up.power_up_damage_modifier,
power_up_damage_modifier: config.game.power_up_damage_modifier,
inventory: nil,
damage_immunity: false,
pull_immunity: false,
Expand Down Expand Up @@ -104,14 +104,14 @@ defmodule Arena.Entities do
}
end

def new_power_up(id, position, direction, owner_id, power_up) do
def new_power_up(id, position, direction, owner_id, game_config) do
%{
id: id,
category: :power_up,
shape: :circle,
name: "Power Up" <> Integer.to_string(id),
position: position,
radius: power_up.radius,
radius: game_config.power_up_radius,
vertices: [],
speed: 0.0,
direction: direction,
Expand All @@ -121,8 +121,8 @@ defmodule Arena.Entities do
status: :UNAVAILABLE,
remove_on_collision: true,
pull_immunity: true,
power_up_damage_modifier: power_up.power_up_damage_modifier,
power_up_health_modifier: power_up.power_up_health_modifier
power_up_damage_modifier: game_config.power_up_damage_modifier,
power_up_health_modifier: game_config.power_up_health_modifier
}
}
end
Expand Down
16 changes: 8 additions & 8 deletions apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ defmodule Arena.GameUpdater do
) do
entry = %{killer_id: killer_id, victim_id: victim_id}
victim = Map.get(game_state.players, victim_id)
amount_of_power_ups = get_amount_of_power_ups(victim, game_config.power_ups.power_ups_per_kill)
amount_of_power_ups = get_amount_of_power_ups(victim, game_config.game.power_ups_per_kill)

game_state =
game_state
Expand Down Expand Up @@ -1549,12 +1549,12 @@ defmodule Arena.GameUpdater do
victim,
amount
) do
distance_to_power_up = game_config.power_ups.power_up.distance_to_power_up
distance_to_power_up = game_config.game.distance_to_power_up

Enum.reduce(1..amount//1, game_state, fn _, game_state ->
random_position =
random_position_in_map(
game_config.power_ups.power_up.radius,
game_config.game.power_up_radius,
game_state.external_wall,
game_state.obstacles,
victim.position,
Expand All @@ -1569,10 +1569,10 @@ defmodule Arena.GameUpdater do
random_position,
victim.direction,
victim.id,
game_config.power_ups.power_up
game_config.game
)

Process.send_after(self(), {:activate_power_up, last_id}, game_config.power_ups.power_up.activation_delay_ms)
Process.send_after(self(), {:activate_power_up, last_id}, game_config.game.power_up_activation_delay_ms)

game_state
|> put_in([:power_ups, last_id], power_up)
Expand All @@ -1581,12 +1581,12 @@ defmodule Arena.GameUpdater do
end

defp get_amount_of_power_ups(%{aditional_info: %{power_ups: power_ups}}, power_ups_per_kill) do
Enum.sort_by(power_ups_per_kill, fn %{minimum_amount: minimum} -> minimum end, :desc)
|> Enum.find(fn %{minimum_amount: minimum} ->
Enum.sort_by(power_ups_per_kill, fn %{minimum_amount_of_power_ups: minimum} -> minimum end, :desc)
|> Enum.find(fn %{minimum_amount_of_power_ups: minimum} ->
minimum <= power_ups
end)
|> case do
%{amount_of_drops: amount} -> amount
%{amount_of_power_ups_to_drop: amount} -> amount
_ -> 0
end
end
Expand Down
57 changes: 0 additions & 57 deletions apps/arena/priv/config.json
Original file line number Diff line number Diff line change
@@ -1,61 +1,4 @@
{
"items": [
{
"name": "golden_clock",
"radius": 200.0,
"effects": [
"golden_clock_effect"
],
"mechanics": {}
},
{
"name": "magic_boots",
"radius": 200.0,
"effects": [
"magic_boots_effect"
],
"mechanics": {}
},
{
"name": "mirra_blessing",
"radius": 200.0,
"effects": [
"mirra_blessing_effect"
],
"mechanics": {}
},
{
"name": "giant",
"radius": 200.0,
"effects": [
"giant_effect"
],
"mechanics": {}
}
],
"power_ups": {
"power_ups_per_kill": [
{
"minimum_amount": 0,
"amount_of_drops": 1
},
{
"minimum_amount": 2,
"amount_of_drops": 2
},
{
"minimum_amount": 6,
"amount_of_drops": 3
}
],
"power_up": {
"distance_to_power_up": 400,
"power_up_damage_modifier": 0.08,
"power_up_health_modifier": 0.08,
"radius": 200.0,
"activation_delay_ms": 500
}
},
"effects": [
{
"name": "invisible",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@
<.input field={f[:field_of_view_inside_bush]} type="number" label="Field of view inside bush" />
<.input field={f[:time_visible_in_bush_after_skill]} type="number" label="Field of view inside bush" />

<h2>Power Ups</h2>
<.input field={f[:distance_to_power_up]} type="number" label="Power Up spawn range" step="any" />
<.input field={f[:power_up_damage_modifier]} type="number" label="Power Up damage modifier" step="any" />
<.input field={f[:power_up_health_modifier]} type="number" label="Power Up health modifier" step="any" />
<.input field={f[:power_up_radius]} type="number" label="Power Up radius" step="any" />
<.input field={f[:power_up_activation_delay_ms]} type="number" label="Power Up activation delay" step="any" />
<.inputs_for :let={power_ups_per_kill_f} field={f[:power_ups_per_kill]}>
<div class=" w-full rounded overflow-hidden shadow-lg">
<div class="px-6 py-4">
<.input
field={power_ups_per_kill_f[:minimum_amount_of_power_ups]}
type="number"
label="Minimum amount of power ups"
step="any"
/>
</div>
<div class="px-6 py-4">
<.input
field={power_ups_per_kill_f[:amount_of_power_ups_to_drop]}
type="number"
label="Amount of power ups to drop"
step="any"
/>
</div>
</div>
</.inputs_for>

<:actions>
<.button>Save Game configuration</.button>
</:actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@
<:item title="Time Visible After performing skill inside of bush">
<%= @game_configuration.time_visible_in_bush_after_skill %>
</:item>
<:item title="Power Up spawn range"><%= @game_configuration.distance_to_power_up %></:item>
<:item title="Power Up damage modifier"><%= @game_configuration.power_up_damage_modifier %></:item>
<:item title="Power Up health modifier"><%= @game_configuration.power_up_health_modifier %></:item>
<:item title="Power Up radius"><%= @game_configuration.power_up_radius %></:item>
<:item title="Power Up activation delay"><%= @game_configuration.power_up_activation_delay_ms %></:item>
</.list>
<.button type="button" phx-click={show_modal("show_power_ups_per_kill_config")}>Power up per kill config</.button>

<.modal id="show_power_ups_per_kill_config">
<.header>
Power Up per kill config
</.header>
<%= for power_ups_per_kill <- @game_configuration.power_ups_per_kill do %>
<.list>
<:item title="Minimum amount of power ups"><%= power_ups_per_kill.minimum_amount_of_power_ups %></:item>
<:item title="Power ups to drop"><%= power_ups_per_kill.amount_of_power_ups_to_drop %></:item>
</.list>
<% end %>
</.modal>

<.back navigate={~p"/game_configurations"}>Back to game_configurations</.back>
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ defmodule GameBackend.CurseOfMirra.GameConfiguration do
:match_timeout_ms,
:field_of_view_inside_bush,
:time_visible_in_bush_after_skill,
:version_id
:version_id,
:distance_to_power_up,
:power_up_damage_modifier,
:power_up_health_modifier,
:power_up_radius,
:power_up_activation_delay_ms
]

@derive {Jason.Encoder, only: @required}
@derive {Jason.Encoder, only: @required ++ [:power_ups_per_kill]}
schema "game_configurations" do
field(:tick_rate_ms, :integer)
field(:bounty_pick_time_ms, :integer)
Expand All @@ -53,6 +58,13 @@ defmodule GameBackend.CurseOfMirra.GameConfiguration do
field(:match_timeout_ms, :integer)
field(:field_of_view_inside_bush, :integer)
field(:time_visible_in_bush_after_skill, :integer)
field(:distance_to_power_up, :integer)
field(:power_up_damage_modifier, :float)
field(:power_up_health_modifier, :float)
field(:power_up_radius, :float)
field(:power_up_activation_delay_ms, :integer)

embeds_many(:power_ups_per_kill, __MODULE__.PowerUpPerKillAmount)

belongs_to(:version, Version)

Expand All @@ -64,5 +76,25 @@ defmodule GameBackend.CurseOfMirra.GameConfiguration do
game_configuration
|> cast(attrs, @required)
|> validate_required(@required)
|> cast_embed(:power_ups_per_kill)
end

defmodule PowerUpPerKillAmount do
@moduledoc """
Position embedded schema to be used by MapConfiguration
"""
use GameBackend.Schema

@derive {Jason.Encoder, only: [:minimum_amount_of_power_ups, :amount_of_power_ups_to_drop]}
embedded_schema do
field(:minimum_amount_of_power_ups, :integer)
field(:amount_of_power_ups_to_drop, :integer)
end

def changeset(position, attrs) do
position
|> cast(attrs, [:minimum_amount_of_power_ups, :amount_of_power_ups_to_drop])
|> validate_required([:minimum_amount_of_power_ups, :amount_of_power_ups_to_drop])
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule GameBackend.Repo.Migrations.AddPowerUpConfigToGameConfiguration do
use Ecto.Migration

def change do
alter table(:game_configurations) do
add :distance_to_power_up, :integer
add :power_up_damage_modifier, :float
add :power_up_health_modifier, :float
add :power_up_radius, :float
add :power_up_activation_delay_ms, :integer
add :power_ups_per_kill, :map
end

end
end
21 changes: 20 additions & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,26 @@ game_configuration_1 = %{
match_timeout_ms: 300_000,
field_of_view_inside_bush: 400,
version_id: version.id,
time_visible_in_bush_after_skill: 2000
time_visible_in_bush_after_skill: 2000,
distance_to_power_up: 400,
power_up_damage_modifier: 0.08,
power_up_health_modifier: 0.08,
power_up_radius: 200.0,
power_up_activation_delay_ms: 500,
power_ups_per_kill: [
%{
minimum_amount_of_power_ups: 0,
amount_of_power_ups_to_drop: 1
},
%{
minimum_amount_of_power_ups: 2,
amount_of_power_ups_to_drop: 2
},
%{
minimum_amount_of_power_ups: 6,
amount_of_power_ups_to_drop: 3
}
]
}

{:ok, _game_configuration_1} =
Expand Down

0 comments on commit e580917

Please sign in to comment.