-
Notifications
You must be signed in to change notification settings - Fork 1
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 4 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,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).() | ||
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 base16? 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. Just selected one at random. Any suggestions? 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. Nope, was just curious. Also, don't think is worth discussing for this use case |
||
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.
Why not
:erlang.term_to_binary/1
the map configuration (or whatever subfields we actually want)?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.
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.