40 lines
1.1 KiB
GDScript
40 lines
1.1 KiB
GDScript
extends PacketHandler
|
|
|
|
var auth_token: String
|
|
|
|
func _ready():
|
|
DisplayServer.window_set_title(Global.username)
|
|
send_login()
|
|
|
|
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
|
var buffer := StreamPeerBuffer.new()
|
|
buffer.data_array = data
|
|
|
|
var error := buffer.get_8()
|
|
if error != 0:
|
|
print("[%s] Login failed." % Global.username)
|
|
return
|
|
|
|
Global.account_id = buffer.get_string()
|
|
auth_token = buffer.get_string()
|
|
|
|
print("[%s] Login succeeded." % Global.username)
|
|
print("[%s] ID: %s" % [Global.username, Global.account_id])
|
|
print("[%s] Auth token: %s" % [Global.username, auth_token])
|
|
|
|
DisplayServer.window_set_title("%s - %s" % [Global.username, Global.account_id])
|
|
DisplayServer.window_set_position(Vector2((Global.instance_id % 2) * 960, (Global.instance_id / 2 % 2) * 540))
|
|
|
|
func send_login():
|
|
if is_logged_in():
|
|
return
|
|
|
|
var password := "password"
|
|
var buffer := StreamPeerBuffer.new()
|
|
buffer.put_8(Packet.LOGIN)
|
|
buffer.put_data(JSON.stringify([Global.username, password]).to_utf8_buffer())
|
|
%Client.send(buffer.data_array)
|
|
print("[%s] Connecting..." % Global.username)
|
|
|
|
func is_logged_in() -> bool:
|
|
return auth_token != "" |