server: moved code generation from client to server to avoid code collision
This commit is contained in:
@@ -70,10 +70,13 @@ func _handle_response(response: String) -> void:
|
||||
match data["response"]:
|
||||
"create_lobby":
|
||||
if data["status"] == "ok":
|
||||
_lobby_id = data["lobby_id"]
|
||||
_client_id = data["client_id"]
|
||||
connected_to_lobby.emit.call_deferred(_lobby_id)
|
||||
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
||||
"join_lobby":
|
||||
if data["status"] == "ok":
|
||||
_client_id = data["client_id"]
|
||||
connected_to_lobby.emit.call_deferred(_lobby_id)
|
||||
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
||||
"client_joined_lobby":
|
||||
@@ -103,18 +106,15 @@ func disconnect_from_server() -> void:
|
||||
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
||||
|
||||
func create_lobby(client_name: String) -> void:
|
||||
_lobby_id = _generate_code()
|
||||
_client_id = _generate_code()
|
||||
_client_name = client_name
|
||||
|
||||
_send_message(TCPMessages.create_lobby(_lobby_id, _client_id, client_name))
|
||||
_send_message(TCPMessages.create_lobby(client_name))
|
||||
|
||||
func join_lobby(lobby_id: String, client_name: String) -> void:
|
||||
_lobby_id = lobby_id
|
||||
_client_id = _generate_code()
|
||||
_client_name = client_name
|
||||
|
||||
_send_message(TCPMessages.join_lobby(_lobby_id, _client_id, client_name))
|
||||
_send_message(TCPMessages.join_lobby(_lobby_id, client_name))
|
||||
|
||||
func leave_lobby() -> void:
|
||||
_send_message(TCPMessages.leave_lobby(_lobby_id, _client_id))
|
||||
@@ -128,14 +128,3 @@ func leave_lobby() -> void:
|
||||
|
||||
func _send_message(message: String) -> void:
|
||||
_tcp.put_data(message.to_utf8_buffer())
|
||||
|
||||
func _generate_code() -> String:
|
||||
# use a local randomized RNG to keep the global RNG reproducible
|
||||
var rng = RandomNumberGenerator.new()
|
||||
rng.randomize()
|
||||
var length = 6
|
||||
var result = ''
|
||||
for _n in range(length):
|
||||
var ascii = rng.randi_range(0, 25) + 65
|
||||
result += '%c' % ascii
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user