Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico-Sanchez committed Dec 19, 2024
1 parent 460dd4a commit ba7394d
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 68 deletions.
16 changes: 15 additions & 1 deletion apps/arena/lib/arena/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Arena.Configuration do
|> Map.put(:client_config, client_config)
end

def get_current_game_configuration do
defp get_current_game_configuration do
gateway_url = Application.get_env(:arena, :gateway_url)

{:ok, payload} =
Expand All @@ -41,6 +41,20 @@ defmodule Arena.Configuration do
end)
end

# TODO: This is a placeholder because the above function needs to retrieve only 1 map params
def get_current_map_configuration_for_configurator do
gateway_url = Application.get_env(:arena, :gateway_url)

{:ok, payload} =
Finch.build(:get, "#{gateway_url}/curse/configuration/current", [{"content-type", "application/json"}])
|> Finch.request(Arena.Finch)

Jason.decode!(payload.body, [{:keys, :atoms}])
|> Map.update!(:map, fn maps -> Enum.map(maps, fn map -> parse_map_config(map) end)
end)
|> Map.get(:map)
end

defp get_client_config() do
{:ok, config_json} =
Application.app_dir(:arena, "priv/client_config.json")
Expand Down
8 changes: 4 additions & 4 deletions apps/configurator/assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
}

.input-item {
border-bottom: 1px solid #ccc; /* Adjust the border style and color as needed */
margin-bottom: 10px; /* Add some bottom margin for spacing */
border-bottom: 1px solid #ccc;
margin-bottom: 10px;
align-items: center;
flex-wrap: wrap; /* Allow items to wrap to the next line if needed */
flex-wrap: wrap;
}

.input-item > div {
padding: 10px; /* Add some bottom margin for spacing */
padding: 10px;
flex: 0 0 150px;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ defmodule ConfiguratorWeb.CoreComponents do
end

@doc """
Renders a css flex styled form.
Renders a css flex container styled simple form.
## Examples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule ConfiguratorWeb.CharacterController do
character = Characters.get_character(id)
changeset = Ecto.Changeset.change(character)
version = Configuration.get_version!(character.version_id)
skills = get_curse_skills_by_type(version.id)
skills = get_curse_skills_by_type(version.id) |> IO.inspect(label: :aver_skills)
render(conn, :edit, character: character, changeset: changeset, skills: skills, version: version)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
<% end %>
<.input field={fp[:shape]} type="select" label="Shape of entity to spawn" options={["circle", "polygon"]} />

<%!-- TODO: Fix this because it breaks if its null --%>
<.input field={fp[:vertices]} type="text" label="Vertices" value={[]} />
<%= fp.data.vertices %>
<%!-- END TODO --%>

<.button type="button" phx-click={show_modal("on-arrival-mechanic-modal")}>Edit on arrival mechanic</.button>
<.modal id="on-arrival-mechanic-modal">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule ConfiguratorWeb.VersionController do
use ConfiguratorWeb, :controller
# import Ecto.Query
alias ConfiguratorWeb.MapConfigurationController

alias GameBackend.Configuration
Expand All @@ -12,8 +11,7 @@ defmodule ConfiguratorWeb.VersionController do
end

def new(conn, _params) do
config = Arena.Configuration.get_game_config()
map_params = config.map
map_params = Arena.Configuration.get_current_map_configuration_for_configurator()
last_version = GameBackend.Configuration.get_current_version()
skills = GameBackend.Units.Skills.list_curse_skills_by_version(last_version.id) |> Enum.group_by(& &1.type)

Expand All @@ -22,13 +20,16 @@ defmodule ConfiguratorWeb.VersionController do
|> Map.put(:characters, schema_to_map(last_version.characters))
|> Map.put(:consumable_items, schema_to_map(last_version.consumable_items))
|> Map.put(:game_configuration, schema_to_map(last_version.game_configuration))
|> Map.put(:map_configurations, [map_params])
|> Map.put(:map_configurations, map_params)
|> Map.put(:skills, schema_to_map(last_version.skills))

changeset = Configuration.change_version(%Version{}, params)
render(conn, :new, changeset: changeset, last_version: last_version, skills: skills)
end

# TODO: We have a cycle in our Skill.Mechanic assocs so this is to end that loop.
# This replaces the unloaded mechanics assoc by an empty list.
# The key is that we preload the necessary steps of the mechanics loop associations.
def schema_to_map(%{
__cardinality__: :many,
__field__: _,
Expand All @@ -41,10 +42,8 @@ defmodule ConfiguratorWeb.VersionController do

def schema_to_map(%_struct{} = schema) do
schema
# Convert the struct to a map
|> Map.from_struct()
|> Enum.map(fn {key, value} -> {key, schema_to_map(value)} end)
# Convert back into a map
|> Enum.into(%{})
end

Expand All @@ -65,13 +64,9 @@ defmodule ConfiguratorWeb.VersionController do
Map.put(
version_params,
"map_configurations",
Enum.map(version_params["map_configurations"], fn {k, map} ->
%{k => MapConfigurationController.parse_json_params(map)}
# Enum.map(map, fn {k, v} ->
# %{k => MapConfigurationController.parse_json_params(v)}
# end)
Map.new(version_params["map_configurations"], fn {key, map_params} ->
{key, MapConfigurationController.parse_json_params(map_params)}
end)
|> hd()
)

case Configuration.create_version(version_params) do
Expand All @@ -81,6 +76,7 @@ defmodule ConfiguratorWeb.VersionController do
|> redirect(to: ~p"/versions/#{version}")

{:error, %Ecto.Changeset{} = changeset} ->
IO.inspect(changeset, label: :aver_changeset)
last_version = GameBackend.Configuration.get_current_version()
skills = GameBackend.Units.Skills.list_curse_skills_by_version(last_version.id) |> Enum.group_by(& &1.type)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ConfiguratorWeb.VersionHTML do
use ConfiguratorWeb, :html
import ConfiguratorWeb.SkillHTML
import ConfiguratorWeb.MapConfigurationHTML
import ConfiguratorWeb.CharacterHTML

embed_templates "version_html/*"

Expand All @@ -14,20 +15,4 @@ defmodule ConfiguratorWeb.VersionHTML do
attr :skills, :list

def version_form(assigns)

attr :field, Phoenix.HTML.FormField, required: true
attr :label, :string, required: true
attr :skills, :list, required: true

def skill_select(assigns) do
~H"""
<.input
field={@field}
type="select"
label={@label}
prompt="Select a skill"
options={Enum.map(@skills, &{&1.name, &1.id})}
/>
"""
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<:item title="Map settings"><.link href={~p"/versions/#{@version}/map_configurations"}>Link</.link></:item>
<:item title="Skill settings"><.link href={~p"/versions/#{@version}/skills"}>Link</.link></:item>
<:item title="Consumable Items"><.link href={~p"/versions/#{@version}/consumable_items"}>Link</.link></:item>
<:item title="Arena servers"><.link href={~p"/versions/#{@version}/arena_servers"}>Link</.link></:item>
</.list>

<.back navigate={~p"/versions"}>Back to versions</.back>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<:item title="Map settings"><.link href={~p"/versions/#{@version}/map_configurations"}>Link</.link></:item>
<:item title="Skill settings"><.link href={~p"/versions/#{@version}/skills"}>Link</.link></:item>
<:item title="Consumable Items"><.link href={~p"/versions/#{@version}/consumable_items"}>Link</.link></:item>
<:item title="Arena servers"><.link href={~p"/versions/#{@version}/arena_servers"}>Link</.link></:item>
</.list>

<.back navigate={~p"/versions"}>Back to versions</.back>
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<.input field={fs[:max_autoaim_range]} type="number" label="Max autoaim range" />
<.input field={fs[:stamina_cost]} type="number" label="Stamina cost" />
<.input field={fs[:mana_cost]} type="number" label="Mana cost" />
<.input type="hidden" field={fs[:game_id]} />

<.skill_mechanic_inputs skill_form={fs} />
<%= if fs.data.effect_to_apply do %>
Expand Down
1 change: 1 addition & 0 deletions apps/game_backend/lib/game_backend/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ defmodule GameBackend.Configuration do
%Version{}
|> Version.changeset(attrs)
|> Repo.insert()
|> dbg()
end

@doc """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule GameBackend.Configuration.Version do
|> cast(attrs, [:name, :current])
|> cast_assoc(:characters)
|> cast_assoc(:consumable_items)
|> cast_assoc(:skills)
|> cast_assoc(:skills, with: &Skill.assoc_changeset/2)
|> cast_assoc(:map_configurations, with: &MapConfiguration.assoc_changeset/2)
|> cast_assoc(:game_configuration, with: &GameConfiguration.assoc_changeset/2)

Expand Down
24 changes: 17 additions & 7 deletions apps/game_backend/lib/game_backend/units/skills.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,29 @@ defmodule GameBackend.Units.Skills do
|> Repo.update()
end

# TODO: These two functions are placeholders to fix the validations the normal changeset do in the future.
# e.g. the version_id constraint.
def autobattler_insert_skill(attrs) do
%Skill{}
|> Skill.assoc_changeset(attrs)
|> Repo.insert()
end

def autobattler_update_skill(skill, attrs \\ %{}) do
skill
|> Skill.assoc_changeset(attrs)
|> Repo.update()
end

@doc """
Inserts all skills into the database.
If another one already exists with the same name, it updates it instead.
"""
def upsert_skills(attrs_list) do
# TODO: Remove this after fixing Autobattler seeds. Did this to mark version_id as required for skills
autobattler_id = GameBackend.Utils.get_game_id(:champions_of_mirra)
autobattler_version_id = GameBackend.Utils.get_autobattler_version!()

Enum.reduce(attrs_list, Ecto.Multi.new(), fn attrs, multi ->
# Cannot use Multi.insert because of the embeds_many
Ecto.Multi.run(multi, attrs.name, fn _, _ ->
upsert_skill(Map.put(attrs, :version_id, autobattler_version_id) |> Map.put(:game_id, autobattler_id))
upsert_skill(attrs)
end)
end)
|> Repo.transaction()
Expand All @@ -47,8 +57,8 @@ defmodule GameBackend.Units.Skills do

def upsert_skill(attrs) do
case get_skill_by_name(attrs.name) do
nil -> insert_skill(attrs)
skill -> update_skill(skill, attrs)
nil -> autobattler_insert_skill(attrs)
skill -> autobattler_update_skill(skill, attrs)
end
end

Expand Down
36 changes: 36 additions & 0 deletions apps/game_backend/lib/game_backend/units/skills/skill.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,42 @@ defmodule GameBackend.Units.Skills.Skill do
|> cast_embed(:effect_to_apply)
end

@doc false
def assoc_changeset(skill, attrs \\ %{}) do
skill
|> cast(attrs, [
:name,
:game_id,
:cooldown,
:energy_regen,
:animation_duration,
:buff_id,
:next_skill_id,
:activation_delay_ms,
:autoaim,
:block_movement,
:can_pick_destination,
:cooldown_mechanism,
:cooldown_ms,
:reset_combo_ms,
:execution_duration_ms,
:inmune_while_executing,
:is_passive,
:is_combo?,
:max_autoaim_range,
:stamina_cost,
:mana_cost,
:type,
:version_id
])
|> cast_assoc(:mechanics)
|> unique_constraint([:game_id, :name, :version_id])
|> foreign_key_constraint(:characters, name: "characters_basic_skill_id_fkey")
|> cooldown_mechanism_validation()
|> validate_combo_fields()
|> cast_embed(:effect_to_apply)
end

defp cooldown_mechanism_validation(changeset) do
case get_field(changeset, :cooldown_mechanism) do
:stamina ->
Expand Down
13 changes: 0 additions & 13 deletions apps/game_backend/lib/game_backend/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,9 @@ defmodule GameBackend.Utils do
Helper module
"""

import Ecto.Query

def get_game_id(:curse_of_mirra), do: 1
def get_game_id(:champions_of_mirra), do: 2

# TODO: Remove this after fixing Autobattler seeds. Did this to mark version_id as required for skills
def get_autobattler_version!() do
GameBackend.Repo.one!(
from(
v in GameBackend.Configuration.Version,
where: v.name == "autobattler",
select: v.id
)
)
end

def get_daily_rewards_config(),
do: Application.get_env(:game_backend, :daily_rewards_config) |> Map.get("reward_per_day")
end
9 changes: 0 additions & 9 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ champions_of_mirra_id = Utils.get_game_id(:champions_of_mirra)

### Champions Currencies

# TODO: Remove this after fixing Autobattler seeds. Did this to mark version_id as required for skills
default_version_params = %{
name: "autobattler",
current: false
}

{:ok, version} =
GameBackend.Configuration.create_version(default_version_params)

{:ok, _skills} = Champions.Config.import_skill_config()

{:ok, _characters} = Champions.Config.import_character_config()
Expand Down

0 comments on commit ba7394d

Please sign in to comment.