diff options
Diffstat (limited to 'client/multiplayer.gd')
-rw-r--r-- | client/multiplayer.gd | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/client/multiplayer.gd b/client/multiplayer.gd index 6b642d90..fa429514 100644 --- a/client/multiplayer.gd +++ b/client/multiplayer.gd @@ -52,10 +52,30 @@ func _process(_delta): connection_closed.emit(tr("c.error.websocket") % [code, reason, code != -1]) self.queue_free() +func fix_packet_types(val): + match typeof(val): + TYPE_FLOAT: return val + TYPE_STRING: return val + TYPE_BOOL: return val + TYPE_ARRAY: return val.map(fix_packet_types) + TYPE_DICTIONARY: + var newval = {} + for k in val.keys(): + if typeof(val[k]) == TYPE_ARRAY and val[k].size() == 2 and typeof(val[k][0]) == TYPE_FLOAT and typeof(val[k][1]) == TYPE_FLOAT: + if k in ["tile"]: newval[k] = Vector2i(val[k][0], val[k][1]) + elif k in ["pos", "position"]: newval[k] = Vector2(val[k][0], val[k][1]) + elif k in ["player", "id"] and typeof(val[k]) == TYPE_FLOAT: + newval[k] = int(val[k]) + else: + newval[k] = fix_packet_types(val[k]) + return newval + func handle_packet(coded): var p = decode_packet(coded) if p == null: return + + p = fix_packet_types(p) match p["type"]: "version": @@ -116,8 +136,8 @@ func send_leave(player: int): "player": player, }) -func send_packet(packet): - var json = JSON.stringify(packet) +func send_packet(p): + var json = JSON.stringify(p) socket.send_text(json) func decode_packet(bytes: PackedByteArray): @@ -129,9 +149,3 @@ func decode_packet(bytes: PackedByteArray): else: print("Decode of packet failed: %s in %s" % [json.get_error_message(), in_str]) return null - -func pos_to_vec2(pos: Array) -> Vector2: - return Vector2(pos[0], pos[1]) - -func pos_to_vec2i(pos: Array) -> Vector2i: - return Vector2i(pos[0], pos[1]) |