Skip to content

Commit

Permalink
Make vertices diffable
Browse files Browse the repository at this point in the history
  • Loading branch information
AminArria committed Aug 29, 2024
1 parent 28d62de commit e9f322d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
20 changes: 12 additions & 8 deletions apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ defmodule Arena.GameUpdater do
defp complete_entity(entity, category) do
Map.update(entity, :category, nil, &to_string/1)
|> Map.update(:shape, nil, &to_string/1)
|> Map.update(:vertices, nil, fn vertices -> %{positions: vertices} end)
|> Map.put(:aditional_info, Entities.maybe_add_custom_info(Map.put(entity, :category, category)))
end

Expand Down Expand Up @@ -1911,6 +1912,8 @@ defmodule Arena.GameUpdater do
## Following somen discussions we are going to have special handling for certain cases and essentially
## check the lists for exact matches (returning :no_diff) or for differences (returning the entire new list)
case {old, new} do
## TODO: to enable this we need a fix similar to the ListPositionPB in protobuf for the other fields
#
# ## Lists of the same scalars we can compare directly. One assumption we currently do here is that lists
# ## are homogeneous, all elements are of same type (they should, but FYI)
# {[elem | _], [elem | _]} when is_atom(elem) or is_binary(elem) or is_boolean(elem) or is_number(elem) ->
Expand All @@ -1919,14 +1922,15 @@ defmodule Arena.GameUpdater do
# false -> {:ok, new}
# end

# ## Lists containing %{x: _, y: _} are treated as points (vertices) and this case we know we can
# ## do ===/2 comparison and it will verify the exactness. At the moment we don't want to do this
# ## for all lists of maps cause the exactness of this comparison of maps hasn't been verified by us
# {[%{x: _, y: _} | _], [%{x: _, y: _} | _]} ->
# case old === new do
# true -> :no_diff
# false -> {:ok, new}
# end
## Lists containing %{x: _, y: _} are treated as points (vertices) and this case we know we can
## do ===/2 comparison and it will verify the exactness. At the moment we don't want to do this
## for all lists of maps cause the exactness of this comparison of maps hasn't been
## verified (is it a deep === comparison for all keys and values?) and we don't know the performance impact
{[%{x: _, y: _} | _], [%{x: _, y: _} | _]} ->
case old === new do
true -> :no_diff
false -> {:ok, new}
end

## Anything else we still consider to risky to try and compare, so we return the new list
_ ->
Expand Down
10 changes: 9 additions & 1 deletion apps/arena/lib/arena/serialization/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ defmodule Arena.Serialization.Position do
field :y, 2, proto3_optional: true, type: :float
end

defmodule Arena.Serialization.ListPositionPB do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"

field :positions, 1, repeated: true, type: Arena.Serialization.Position
end

defmodule Arena.Serialization.LobbyEvent do
@moduledoc false

Expand Down Expand Up @@ -478,7 +486,7 @@ defmodule Arena.Serialization.Entity do
field :name, 4, proto3_optional: true, type: :string
field :position, 5, type: Arena.Serialization.Position
field :radius, 6, proto3_optional: true, type: :float
field :vertices, 7, repeated: true, type: Arena.Serialization.Position
field :vertices, 7, proto3_optional: true, type: Arena.Serialization.ListPositionPB
field :collides_with, 8, repeated: true, type: :uint64, json_name: "collidesWith"
field :speed, 9, proto3_optional: true, type: :float
field :direction, 10, type: Arena.Serialization.Direction
Expand Down
6 changes: 5 additions & 1 deletion apps/serialization/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ message Position {
optional float y = 2;
}

message ListPositionPB {
repeated Position positions = 1;
}

message LobbyEvent {
oneof event {
LeaveLobby leave = 1;
Expand Down Expand Up @@ -178,7 +182,7 @@ message Entity {
optional string name = 4;
Position position = 5;
optional float radius = 6;
repeated Position vertices = 7;
optional ListPositionPB vertices = 7;
repeated uint64 collides_with = 8;
optional float speed = 9;
Direction direction = 10;
Expand Down

0 comments on commit e9f322d

Please sign in to comment.