-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from all commits
8c187e4
1d75b8d
32a71f8
f02d8f6
f4dc310
8cc09ad
651b5e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct
There was a problem hiding this comment.
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.