Initial push
This commit is contained in:
+169
@@ -0,0 +1,169 @@
|
||||
class_name WebClient
|
||||
extends Node
|
||||
|
||||
@export var name_input: LineEdit
|
||||
@export var session_code_input: LineEdit
|
||||
@export var output: Label
|
||||
@export var create_lobby_button: Button
|
||||
@export var join_lobby_button: Button
|
||||
@export var disconnect_lobby_button: Button
|
||||
|
||||
var _peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
||||
var _address: String = "127.0.0.1"
|
||||
var _port: int = 9974
|
||||
|
||||
var _udp: PacketPeerUDP = PacketPeerUDP.new()
|
||||
var _tcp = PacketPeerDTLS
|
||||
|
||||
var _session_id: String
|
||||
var _client_id: String
|
||||
var _client_name: String
|
||||
|
||||
var _is_in_lobby: bool = false
|
||||
|
||||
const MSG_CREATE_LOBBY: String = "cr"
|
||||
const MSG_JOIN_LOBBY: String = "jn"
|
||||
const MSG_LEAVE_LOBBY: String = "lv"
|
||||
|
||||
const REMOTE_CREATE_SESSION_SUCCESS: String = "c0"
|
||||
const REMOTE_ERR_CREATE_SESSION: String = "c1"
|
||||
const REMOTE_JOIN_SESSION_SUCCESS: String = "j0"
|
||||
const REMOTE_JOIN_SESSION_DUPLICATE: String = "j1"
|
||||
const REMOTE_JOIN_SESSION_MISSING: String = "j2"
|
||||
const REMOTE_JOIN_SESSION_FULL: String = "j3"
|
||||
const REMOTE_EXIT_SESSION: String = "ex"
|
||||
|
||||
func _ready() -> void:
|
||||
_write_output("me client, you jane.")
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if _udp.get_available_packet_count() > 0:
|
||||
var packet_str = _udp.get_packet().get_string_from_ascii()
|
||||
print(packet_str)
|
||||
match packet_str.substr(0, 2):
|
||||
# Session creation
|
||||
REMOTE_CREATE_SESSION_SUCCESS:
|
||||
_write_output("Created and joined lobby successfully.")
|
||||
_write_output("Lobby code: {session_id}".format({
|
||||
"session_id": _session_id
|
||||
}))
|
||||
_write_output("Your player code: {client_id}".format({
|
||||
"client_id": _client_id
|
||||
}))
|
||||
_on_lobby_connected()
|
||||
|
||||
REMOTE_ERR_CREATE_SESSION:
|
||||
_write_output("Could not create lobby.")
|
||||
_write_output("A lobby with the code {session_id} already exists.".format({
|
||||
"session_id": _session_id
|
||||
}))
|
||||
|
||||
REMOTE_JOIN_SESSION_SUCCESS:
|
||||
_write_output("Joined lobby successfully.")
|
||||
_write_output("Your player code: {client_id}".format({"client_id": _client_id}))
|
||||
_on_lobby_connected()
|
||||
|
||||
# Session joining
|
||||
REMOTE_JOIN_SESSION_DUPLICATE:
|
||||
_write_output("Could not join lobby {session_id}.".format({
|
||||
"session_id": _session_id
|
||||
}))
|
||||
_write_output("A player with the code {client_id} is already registered in the lobby.".format({
|
||||
"client_id": _client_id,
|
||||
}))
|
||||
|
||||
REMOTE_JOIN_SESSION_MISSING:
|
||||
_write_output("No lobby exists with the code {session_id}.".format({
|
||||
"session_id": _session_id
|
||||
}))
|
||||
|
||||
REMOTE_JOIN_SESSION_FULL:
|
||||
_write_output("Could not join lobby {session_id}.".format({
|
||||
"session_id": _session_id
|
||||
}))
|
||||
_write_output("The lobby has reached the maximum player count.")
|
||||
|
||||
# Session exit
|
||||
REMOTE_EXIT_SESSION:
|
||||
_on_lobby_disconnected()
|
||||
|
||||
func _exit_tree() -> void:
|
||||
_disconnect_from_lobby()
|
||||
|
||||
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
|
||||
|
||||
func _on_peer_connected(id: int) -> void:
|
||||
$Label.text += "connected to id: " + str(id)
|
||||
|
||||
func _get_client_string(remote_code: String, session_id: String, client_id: String, client_name: String) -> PackedByteArray:
|
||||
return "{remote_code}:{session_id}:{client_id}:{client_name}".format({
|
||||
"remote_code": remote_code,
|
||||
"session_id": session_id,
|
||||
"client_id": client_id,
|
||||
"client_name": client_name,
|
||||
}).to_utf8_buffer()
|
||||
|
||||
func _disconnect_from_lobby() -> void:
|
||||
if _is_in_lobby:
|
||||
_udp.put_packet(_get_client_string(MSG_LEAVE_LOBBY, _session_id, _client_id, _client_name))
|
||||
_udp.close()
|
||||
|
||||
func _write_output(text: String) -> void:
|
||||
output.text += text + "\n"
|
||||
|
||||
func _on_lobby_connected() -> void:
|
||||
_is_in_lobby = true
|
||||
create_lobby_button.disabled = true
|
||||
join_lobby_button.disabled = true
|
||||
disconnect_lobby_button.disabled = false
|
||||
|
||||
func _on_lobby_disconnected() -> void:
|
||||
_write_output("Connection with lobby {session_id} ended.".format({
|
||||
"session_id": _session_id
|
||||
}))
|
||||
_udp.close()
|
||||
_is_in_lobby = false
|
||||
name_input.editable = true
|
||||
session_code_input.editable = true
|
||||
session_code_input.text = ""
|
||||
_session_id = ""
|
||||
create_lobby_button.disabled = false
|
||||
join_lobby_button.disabled = false
|
||||
disconnect_lobby_button.disabled = true
|
||||
|
||||
# Create new session
|
||||
func _on_create_lobby_button_pressed() -> void:
|
||||
_udp.set_dest_address(_address, _port)
|
||||
_session_id = _generate_code()
|
||||
session_code_input.text = _session_id
|
||||
name_input.editable = false
|
||||
session_code_input.editable = false
|
||||
_client_id = _generate_code()
|
||||
_client_name = name_input.text.strip_edges()
|
||||
|
||||
_udp.put_packet(_get_client_string(MSG_CREATE_LOBBY, _session_id, _client_id, _client_name))
|
||||
|
||||
# Connect to existing session
|
||||
func _on_join_lobby_button_pressed() -> void:
|
||||
_udp.set_dest_address(_address, _port)
|
||||
name_input.editable = false
|
||||
session_code_input.editable = false
|
||||
|
||||
_session_id = session_code_input.text.strip_edges()
|
||||
_client_id = _generate_code()
|
||||
_client_name = name_input.text.strip_edges()
|
||||
|
||||
_udp.put_packet(_get_client_string(MSG_JOIN_LOBBY, _session_id, _client_id, _client_name))
|
||||
|
||||
func _on_disconnect_lobby_button_pressed() -> void:
|
||||
_disconnect_from_lobby()
|
||||
_on_lobby_disconnected()
|
||||
Reference in New Issue
Block a user