Skip to content

Commit

Permalink
[GH-852] Configurator: Migrate crates config (#854)
Browse files Browse the repository at this point in the history
* Migrate crates config to configurator

* Add crates to configurator app

* Refactor configuration code

---------

Co-authored-by: tvillegas98 <[email protected]>
  • Loading branch information
agustinesco and tvillegas98 authored Aug 27, 2024
1 parent 656640b commit e49121b
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 261 deletions.
36 changes: 13 additions & 23 deletions apps/arena/lib/arena/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,26 @@ defmodule Arena.Configuration do
| radius: maybe_to_float(map_config.radius),
initial_positions: Enum.map(map_config.initial_positions, &parse_position/1),
obstacles: Enum.map(map_config.obstacles, &parse_obstacle/1),
pools: Enum.map(map_config.pools, &parse_pool/1),
bushes: Enum.map(map_config.bushes, &parse_bush/1)
pools: Enum.map(map_config.pools, &parse_entity_values/1),
bushes: Enum.map(map_config.bushes, &parse_entity_values/1),
crates: Enum.map(map_config.crates, &parse_entity_values/1)
}
end

defp parse_obstacle(obstacle) do
%{
obstacle
| position: parse_position(obstacle.position),
vertices: Enum.map(obstacle.vertices, &parse_position/1),
radius: maybe_to_float(obstacle.radius),
statuses_cycle: parse_status_cycle(obstacle.statuses_cycle)
}
obstacle
|> parse_entity_values()
|> Map.merge(%{
statuses_cycle: parse_status_cycle(obstacle.statuses_cycle)
})
end

defp parse_bush(bush) do
defp parse_entity_values(entity) do
%{
bush
| position: parse_position(bush.position),
vertices: Enum.map(bush.vertices, &parse_position/1),
radius: maybe_to_float(bush.radius)
entity
| position: parse_position(entity.position),
vertices: Enum.map(entity.vertices, &parse_position/1),
radius: maybe_to_float(entity.radius)
}
end

Expand All @@ -207,15 +206,6 @@ defmodule Arena.Configuration do
%{mechanics | polygon_hit: %{polygon_hit | vertices: Enum.map(polygon_hit.vertices, &parse_position/1)}}
end

defp parse_pool(pool) do
%{
pool
| position: parse_position(pool.position),
vertices: Enum.map(pool.vertices, &parse_position/1),
radius: maybe_to_float(pool.radius)
}
end

defp parse_position(%{x: x, y: y}) do
%{x: maybe_to_float(x), y: maybe_to_float(y)}
end
Expand Down
2 changes: 1 addition & 1 deletion apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ defmodule Arena.GameUpdater do
end)

{obstacles, last_id} = initialize_obstacles(config.map.obstacles, game.last_id)
{crates, last_id} = initialize_crates(config.crates, last_id)
{crates, last_id} = initialize_crates(config.map.crates, last_id)
{bushes, last_id} = initialize_bushes(config.map.bushes, last_id)
{pools, last_id} = initialize_pools(config.map.pools, last_id)

Expand Down
218 changes: 0 additions & 218 deletions apps/arena/priv/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,223 +181,5 @@
"effect_mechanics": {}
}
],
"crates": [
{
"position": {
"x": 5500.0,
"y": 200.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 3900.0,
"y": -2300.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 2400.0,
"y": -4800.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": -1241.0,
"y": -3554.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": -4200.0,
"y": -3500.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": -5400.0,
"y": -1000.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": -4200.0,
"y": 3200.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": -1400.0,
"y": 4600.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 1300.0,
"y": 4600.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 3500.0,
"y": 2600.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 1700.0,
"y": 2200.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 3000.0,
"y": 300.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 1200.0,
"y": -2600.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": -2500.0,
"y": -1200.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": -1900.0,
"y": 1700.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 100.0,
"y": 600.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": 700.0,
"y": -100.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
},
{
"position": {
"x": -500.0,
"y": -100.0
},
"shape": "circle",
"radius": 150.0,
"health": 250,
"vertices": [],
"amount_of_power_ups": 1,
"power_up_spawn_delay_ms": 300
}
],
"traps": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ defmodule ConfiguratorWeb.MapConfigurationController do
end
end

def edit_crates(conn, %{"id" => id}) do
map_configuration = Configuration.get_map_configuration!(id)

changeset = Configuration.change_map_configuration(map_configuration)

render(conn, :edit_crates,
map_configuration: map_configuration,
changeset: changeset,
action: ~p"/map_configurations/#{map_configuration}/update_crates"
)
end

def update_crates(conn, %{"id" => id, "map_configuration" => map_configuration_params}) do
map_configuration = Configuration.get_map_configuration!(id)

case Configuration.update_map_configuration(map_configuration, map_configuration_params) do
{:ok, map_configuration} ->
conn
|> put_flash(:info, "Map configuration updated successfully.")
|> redirect(to: ~p"/map_configurations/#{map_configuration}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit_crates,
map_configuration: map_configuration,
changeset: changeset,
action: ~p"/map_configurations/#{map_configuration}/update_obstacles"
)
end
end

defp parse_json_params(map_configuration_params) do
map_configuration_params
|> Map.update("initial_positions", "", &parse_json/1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<.button>Edit Map Pools</.button>
</.link>
<% end %>
<%= unless Enum.empty?(@map_configuration.crates) do %>
<.link href={~p"/map_configurations/#{@map_configuration}/edit_crates"}>
<.button>Edit Map Crates</.button>
</.link>
<% end %>
</:actions>
</.header>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<.simple_form :let={f} for={@changeset} action={@action}>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>

<.header>
Crates
</.header>
<.inputs_for :let={crate_f} field={f[:crates]}>
<.input field={crate_f[:radius]} type="number" label="Radius" step="any" />
<.input field={crate_f[:health]} type="number" label="Health" step="any" />
<.input field={crate_f[:amount_of_power_ups]} type="number" label="Amount of power ups to drop" step="any" />
<.input field={crate_f[:power_up_spawn_delay_ms]} type="number" label="Power up spawn delay" step="any" />

<.input
field={crate_f[:shape]}
type="select"
label="Shape"
options={Ecto.Enum.values(GameBackend.CurseOfMirra.MapConfiguration.Crate, :shape)}
/>
<.inputs_for :let={position_f} field={crate_f[:position]}>
<.input field={position_f[:x]} type="number" label="X" step="any" />
<.input field={position_f[:y]} type="number" label="Y" step="any" />
</.inputs_for>

<.button type="button" phx-click={show_modal("crate-vertices-#{crate_f.data.id}")}>Edit Vertices</.button>
<.modal id={"crate-vertices-#{crate_f.data.id}"}>
<.inputs_for :let={vertices_form} field={crate_f[:vertices]}>
<h3>Vertex</h3>
<.input field={vertices_form[:x]} type="number" label="X" step="any" />
<.input field={vertices_form[:y]} type="number" label="Y" step="any" />
</.inputs_for>
</.modal>
</.inputs_for>

<:actions>
<.button>Save Map Crates</.button>
</:actions>
</.simple_form>
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
</.modal>
<% end %>
</:col>
<:col :let={map_configuration} label="Crates">
<%= if (Enum.empty?(map_configuration.crates)) do %>
<p>N/A</p>
<% else %>
<.button type="button" phx-click={show_modal("crates-#{map_configuration.id}")}>Show</.button>
<.modal id={"crates-#{map_configuration.id}"}>
<p class="whitespace-pre"><%= embed_to_string(map_configuration.crates) %></p>
</.modal>
<% end %>
</:col>
<:action :let={map_configuration}>
<.link navigate={~p"/map_configurations/#{map_configuration}"}>View</.link>
</:action>
Expand Down
Loading

0 comments on commit e49121b

Please sign in to comment.