Skip to content

Commit

Permalink
Correctly (de-)serialize floats in dictionaries to fix "numeric_prope…
Browse files Browse the repository at this point in the history
…rties" (#92)
  • Loading branch information
dsnopek authored Mar 7, 2022
1 parent 6a8d703 commit cdbcf2c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p

### Fixed

- Fix Dictionary serialization (e.g. "NakamaSocket.add_matchmaker_async" "p_string_props").
- Fix Dictionary serialization (e.g. "NakamaSocket.add_matchmaker_async" "p_string_props" and "p_numeric_props").
- Pass join metadata onwards into match join message.
- Don't stop processing messages when the game is paused.
- Fix "rpc_async", "rpc_async_with_key". Now uses GET request only if no payload is passed.
Expand Down
15 changes: 13 additions & 2 deletions addons/com.heroiclabs.nakama/utils/NakamaSerializer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ static func serialize(p_obj : Object) -> Dictionary:
dict[l] = serialize(val[l])
else: # Map of simple types
for l in val:
if typeof(val[l]) != content:
var e = val[l]
if content == TYPE_REAL:
e = float(e)
elif content == TYPE_INT:
e = int(e)
elif content == TYPE_BOOL:
e = bool(e)
if typeof(e) != content:
continue
dict[l] = val[l]
dict[l] = e
out[k] = dict
_:
out[k] = val
Expand Down Expand Up @@ -89,6 +96,8 @@ static func deserialize(p_ns : GDScript, p_cls_name : String, p_dict : Dictionar
for l in val:
if typeof(content) == TYPE_STRING:
v[l] = deserialize(p_ns, content, val[l])
elif content == TYPE_REAL:
v[l] = float(val[l])
elif content == TYPE_INT:
v[l] = int(val[l])
elif content == TYPE_BOOL:
Expand All @@ -105,6 +114,8 @@ static func deserialize(p_ns : GDScript, p_cls_name : String, p_dict : Dictionar
for e in val:
if typeof(content) == TYPE_STRING:
v.append(deserialize(p_ns, content, e))
elif content == TYPE_REAL:
v.append(float(e))
elif content == TYPE_INT:
v.append(int(e))
elif content == TYPE_BOOL:
Expand Down
15 changes: 10 additions & 5 deletions test_suite/tests/socket_multi_test.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extends "res://base_test.gd"

var content = {"My": "message"}
var match_props = {"region": "europe"}
var match_string_props = {"region": "europe"}
var match_numeric_props = {"rank": 8}
var got_msg = false
var got_match = false
var socket1 = null
Expand Down Expand Up @@ -45,10 +46,10 @@ func setup():
if assert_false(msg_ack.is_exception()):
return

var ticket1 = yield(socket1.add_matchmaker_async("+properties.region:europe", 2, 8, match_props), "completed")
var ticket1 = yield(socket1.add_matchmaker_async("+properties.region:europe +properties.rank:>=7 +properties.rank:<=9", 2, 8, match_string_props, match_numeric_props), "completed")
if assert_false(ticket1.is_exception()):
return
var ticket2 = yield(socket2.add_matchmaker_async("+properties.region:europe", 2, 8, match_props), "completed")
var ticket2 = yield(socket2.add_matchmaker_async("+properties.region:europe +properties.rank:>=7 +properties.rank:<=9", 2, 8, match_string_props, match_numeric_props), "completed")
if assert_false(ticket2.is_exception()):
return

Expand All @@ -59,9 +60,13 @@ func _on_socket1_message(msg):
check_end()

func _on_socket1_matchmaker_matched(p_matchmaker_matched):
if assert_equal(JSON.print(p_matchmaker_matched.users[0].string_properties), JSON.print(match_props)):
if assert_equal(JSON.print(p_matchmaker_matched.users[0].string_properties), JSON.print(match_string_props)):
return
if assert_equal(JSON.print(p_matchmaker_matched.users[1].string_properties), JSON.print(match_props)):
if assert_equal(JSON.print(p_matchmaker_matched.users[0].numeric_properties), JSON.print(match_numeric_props)):
return
if assert_equal(JSON.print(p_matchmaker_matched.users[1].string_properties), JSON.print(match_string_props)):
return
if assert_equal(JSON.print(p_matchmaker_matched.users[1].numeric_properties), JSON.print(match_numeric_props)):
return
got_match = true
check_end()
Expand Down

0 comments on commit cdbcf2c

Please sign in to comment.