diff --git a/apps/arena/lib/arena_web/controllers/health_controller.ex b/apps/arena/lib/arena_web/controllers/health_controller.ex index 217811d9e..9def123ef 100644 --- a/apps/arena/lib/arena_web/controllers/health_controller.ex +++ b/apps/arena/lib/arena_web/controllers/health_controller.ex @@ -8,8 +8,11 @@ defmodule ArenaWeb.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 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", 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 diff --git a/apps/game_backend/lib/game_backend/configuration.ex b/apps/game_backend/lib/game_backend/configuration.ex index 8ad3a0c6b..53f0e7a8c 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,52 @@ 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_float() + |> :math.pow(2) + |> round() + |> Integer.to_string() + 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 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