From 8c187e4ba062c5bb016caec2cdf2caa79bbb240a Mon Sep 17 00:00:00 2001 From: Nicolas Sanchez Date: Tue, 10 Sep 2024 15:21:03 -0300 Subject: [PATCH 1/7] Add function to calculate map configurations hash version value --- .../lib/game_backend/configuration.ex | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/apps/game_backend/lib/game_backend/configuration.ex b/apps/game_backend/lib/game_backend/configuration.ex index 8ad3a0c6b..d0a743f15 100644 --- a/apps/game_backend/lib/game_backend/configuration.ex +++ b/apps/game_backend/lib/game_backend/configuration.ex @@ -3,8 +3,10 @@ defmodule GameBackend.Configuration do Configuration context for GameBackend """ import Ecto.Query + require Decimal alias Ecto.Multi alias GameBackend.CurseOfMirra.GameConfiguration + alias GameBackend.CurseOfMirra.MapConfiguration.Position alias GameBackend.Items.ConsumableItem alias GameBackend.Units.Characters.Character alias GameBackend.CurseOfMirra.MapConfiguration @@ -541,4 +543,50 @@ defmodule GameBackend.Configuration do def get_latest_game_configuration do Repo.one(from(g in GameConfiguration, order_by: [desc: g.inserted_at], limit: 1)) end + + @doc """ + Returns a 16 base encoded string representing the hashed value of the map configurations. + ## Examples + iex> get_configuration_hash_version() + "A6F0CC6917D195AB8A03129ACBE1FA48364845B8" + """ + def get_configuration_hash_version do + get_current_version() + |> Map.get(:map_configurations) + |> Enum.flat_map(fn map_config -> + Map.take(map_config, [:obstacles, :initial_positions, :bushes, :radius]) |> Map.values() + end) + |> List.flatten() + |> Enum.map(fn config -> sum_shape_coordinates(config) end) + |> Enum.reduce(fn coordinates_last, coordinates_current -> Decimal.add(coordinates_last, coordinates_current) end) + |> Decimal.to_string() + |> (fn coordinates -> :crypto.hash(:sha, coordinates) |> Base.encode16() end).() + end + + # The following function retrieves a number representing a shape's figure. + # If it's a circle, gets its radius. + # For everything else, gets its vertices' positions. + # And for everyone, gets their positions in map. + # Calculate the sum of every retrieved value. + defp sum_shape_coordinates(%Position{x: x, y: y}) do + Decimal.add(x, y) + end + + defp sum_shape_coordinates(%{shape: "circle"} = obstacle) do + Decimal.add(obstacle.radius, sum_shape_coordinates(obstacle.position)) + end + + defp sum_shape_coordinates(map_radius) when Decimal.is_decimal(map_radius) do + map_radius + end + + defp sum_shape_coordinates([]), do: Decimal.new(0) + + defp sum_shape_coordinates([vertex | vertices]) do + Decimal.add(sum_shape_coordinates(vertex), sum_shape_coordinates(vertices)) + end + + defp sum_shape_coordinates(%{position: position, vertices: vertices}) do + Decimal.add(sum_shape_coordinates(position), sum_shape_coordinates(vertices)) + end end From 1d75b8d60e61f1de75ecac8be8d1a3ee45e0a5a1 Mon Sep 17 00:00:00 2001 From: Nicolas Sanchez Date: Tue, 10 Sep 2024 15:21:49 -0300 Subject: [PATCH 2/7] Add configurator hashed version to /api/version endpoint payload --- apps/gateway/lib/gateway/controllers/health_controller.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/gateway/lib/gateway/controllers/health_controller.ex b/apps/gateway/lib/gateway/controllers/health_controller.ex index 423daaeb7..a7d235fc2 100644 --- a/apps/gateway/lib/gateway/controllers/health_controller.ex +++ b/apps/gateway/lib/gateway/controllers/health_controller.ex @@ -11,8 +11,11 @@ defmodule Gateway.Controllers.HealthController do end def version(conn, _params) do + arena_version = Application.spec(:arena, :vsn) |> to_string() + configurator_version = GameBackend.Configuration.get_configuration_hash_version() + conn |> put_status(:ok) - |> text(Application.spec(:arena, :vsn)) + |> text(arena_version <> "." <> configurator_version) end end From 32a71f8b2e66138e0bc9a06bddccb333924d5713 Mon Sep 17 00:00:00 2001 From: Nicolas Sanchez Date: Tue, 10 Sep 2024 15:14:44 -0300 Subject: [PATCH 3/7] Show current configurator hashed version in home page --- .../lib/configurator_web/controllers/home_html/home.html.heex | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/configurator/lib/configurator_web/controllers/home_html/home.html.heex b/apps/configurator/lib/configurator_web/controllers/home_html/home.html.heex index 02c255148..0f39d22ab 100644 --- a/apps/configurator/lib/configurator_web/controllers/home_html/home.html.heex +++ b/apps/configurator/lib/configurator_web/controllers/home_html/home.html.heex @@ -2,6 +2,7 @@ <.header> Welcome to Champions of Mirra Configurator + Current version: <%= GameBackend.Configuration.get_configuration_hash_version() %> <.list> <:item title="Versions"><.link href={~p"/versions"}>Link From f02d8f6153ba9bc9a8901e05fb9501fd0c0c3ca7 Mon Sep 17 00:00:00 2001 From: Nicolas Sanchez Date: Tue, 10 Sep 2024 15:14:09 -0300 Subject: [PATCH 4/7] Increase arena's version --- apps/arena/mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/arena/mix.exs b/apps/arena/mix.exs index 14aedd518..9c59fbb9a 100644 --- a/apps/arena/mix.exs +++ b/apps/arena/mix.exs @@ -4,7 +4,7 @@ defmodule Arena.MixProject do def project do [ app: :arena, - version: "0.7.1", + version: "0.8.0", build_path: "../../_build", config_path: "../../config/config.exs", deps_path: "../../deps", From f4dc31098ec89d4fc479f584f96046209568acac Mon Sep 17 00:00:00 2001 From: tvillegas98 Date: Thu, 12 Sep 2024 16:51:10 -0300 Subject: [PATCH 5/7] duplicate logic for arena version endpoint --- apps/arena/lib/arena_web/controllers/health_controller.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/arena/lib/arena_web/controllers/health_controller.ex b/apps/arena/lib/arena_web/controllers/health_controller.ex index 217811d9e..27bd1894d 100644 --- a/apps/arena/lib/arena_web/controllers/health_controller.ex +++ b/apps/arena/lib/arena_web/controllers/health_controller.ex @@ -6,10 +6,12 @@ defmodule ArenaWeb.HealthController do |> put_status(:ok) |> text("ok") end - def version(conn, _params) do + arena_version = Application.spec(:arena, :vsn) |> to_string() + configurator_version = GameBackend.Configuration.get_configuration_hash_version() + conn |> put_status(:ok) - |> text(Application.spec(:arena, :vsn)) + |> text(arena_version <> "." <> configurator_version) end end From 8cc09adf2d7e557a3cd8df8c5fb9e08f14f23cfb Mon Sep 17 00:00:00 2001 From: tvillegas98 Date: Thu, 12 Sep 2024 16:52:29 -0300 Subject: [PATCH 6/7] mix format --- apps/arena/lib/arena_web/controllers/health_controller.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/arena/lib/arena_web/controllers/health_controller.ex b/apps/arena/lib/arena_web/controllers/health_controller.ex index 27bd1894d..9def123ef 100644 --- a/apps/arena/lib/arena_web/controllers/health_controller.ex +++ b/apps/arena/lib/arena_web/controllers/health_controller.ex @@ -6,6 +6,7 @@ defmodule ArenaWeb.HealthController do |> put_status(:ok) |> text("ok") end + def version(conn, _params) do arena_version = Application.spec(:arena, :vsn) |> to_string() configurator_version = GameBackend.Configuration.get_configuration_hash_version() From 651b5e80a15b580dd70c1598b4b900b56b569358 Mon Sep 17 00:00:00 2001 From: Manuel Camejo Date: Fri, 13 Sep 2024 12:49:56 -0300 Subject: [PATCH 7/7] iphone only allows numbers and 18 or less --- apps/game_backend/lib/game_backend/configuration.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/game_backend/lib/game_backend/configuration.ex b/apps/game_backend/lib/game_backend/configuration.ex index d0a743f15..53f0e7a8c 100644 --- a/apps/game_backend/lib/game_backend/configuration.ex +++ b/apps/game_backend/lib/game_backend/configuration.ex @@ -559,8 +559,10 @@ defmodule GameBackend.Configuration do |> List.flatten() |> Enum.map(fn config -> sum_shape_coordinates(config) end) |> Enum.reduce(fn coordinates_last, coordinates_current -> Decimal.add(coordinates_last, coordinates_current) end) - |> Decimal.to_string() - |> (fn coordinates -> :crypto.hash(:sha, coordinates) |> Base.encode16() end).() + |> Decimal.to_float() + |> :math.pow(2) + |> round() + |> Integer.to_string() end # The following function retrieves a number representing a shape's figure.