Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurator versioning #908

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/arena/lib/arena_web/controllers/health_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful, this controller is duplicated in both arena and gateway apps.
We should distinct them somehow or see if both are needed.


conn
|> put_status(:ok)
|> text(Application.spec(:arena, :vsn))
|> text(arena_version <> "." <> configurator_version)
end
end
2 changes: 1 addition & 1 deletion apps/arena/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<.header>
Welcome to Champions of Mirra Configurator
</.header>
Current version: <%= GameBackend.Configuration.get_configuration_hash_version() %>

<.list>
<:item title="Versions"><.link href={~p"/versions"}>Link</.link></:item>
Expand Down
50 changes: 50 additions & 0 deletions apps/game_backend/lib/game_backend/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not :erlang.term_to_binary/1 the map configuration (or whatever subfields we actually want)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the query order of everything changes the hash everytime, and we only want to change the hash when the desired values change.
We can discuss a better approach in Slack.

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
5 changes: 4 additions & 1 deletion apps/gateway/lib/gateway/controllers/health_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading