mirror of
https://port.numenaute.org/aleajactaest/bazar_alea.git
synced 2024-11-10 01:09:53 +00:00
300 lines
8.3 KiB
GDScript3
300 lines
8.3 KiB
GDScript3
|
extends Node3D
|
||
|
|
||
|
# The player name.
|
||
|
var player_name: String
|
||
|
var connected:bool = false
|
||
|
var enet:ENetConnection
|
||
|
var dataEnet:ENetPacketPeer
|
||
|
var errorEnet:Error
|
||
|
var id = 0
|
||
|
var maxplayer = 0
|
||
|
|
||
|
# Server confirm our account
|
||
|
var account_confirmed:bool = false
|
||
|
|
||
|
const PLAYER = preload("res://scenes/player.tscn")
|
||
|
|
||
|
const PORT = 33333
|
||
|
const ADDR = "127.0.0.1"
|
||
|
|
||
|
var dtls := PacketPeerDTLS.new()
|
||
|
var udp := PacketPeerUDP.new()
|
||
|
|
||
|
|
||
|
func test_cert(filename):
|
||
|
print("------", filename)
|
||
|
enet = ENetConnection.new()
|
||
|
print(enet)
|
||
|
#var cert: X509Certificate = X509Certificate.new()
|
||
|
#errorEnet = cert.load(filename)
|
||
|
#print("load cert:", errorEnet)
|
||
|
#print(cert)
|
||
|
#var client_tls_options = TLSOptions.client_unsafe(cert)
|
||
|
#print(client_tls_options)
|
||
|
errorEnet = enet.create_host(10)
|
||
|
if errorEnet != OK:
|
||
|
print("ERROR ENET.create_host: ", errorEnet)
|
||
|
return
|
||
|
#enet.connect_to_host(ADDR, PORT)
|
||
|
#errorEnet = enet.dtls_client_setup(ADDR, client_tls_options)
|
||
|
#print("dtls_client_setup:", errorEnet)
|
||
|
#if errorEnet != OK:
|
||
|
# print("ERROR ENET: ", errorEnet)
|
||
|
# return
|
||
|
dataEnet = enet.connect_to_host(ADDR, PORT, 10)
|
||
|
print(dataEnet)
|
||
|
if not dataEnet.is_active():
|
||
|
print("ERROR enet.connect_to_host: ", dataEnet.is_active())
|
||
|
return
|
||
|
|
||
|
|
||
|
func create_server_enet():
|
||
|
print("------ create_server_enet")
|
||
|
test_cert("res://cert.crt")
|
||
|
|
||
|
|
||
|
# Called when the node enters the scene tree for the first time.
|
||
|
func _ready():
|
||
|
#test_cert("res://cert.crt")
|
||
|
create_server_enet()
|
||
|
pass # Replace with function body.
|
||
|
|
||
|
|
||
|
func decode_msg(data:PackedByteArray):
|
||
|
print("==========================")
|
||
|
var cmd = data.decode_u8(0)
|
||
|
print("cmd:", cmd)
|
||
|
var taille = data.decode_u8(1)
|
||
|
print("taille:", taille)
|
||
|
var str:String = ""
|
||
|
var tmp = data.slice(2,taille+2)
|
||
|
print("tmp:", tmp)
|
||
|
|
||
|
str = tmp.get_string_from_utf8()
|
||
|
|
||
|
print("str:", str)
|
||
|
print("len str:", len(str))
|
||
|
|
||
|
print("==========================")
|
||
|
|
||
|
func send_account():
|
||
|
var data:PackedByteArray = PackedByteArray()
|
||
|
|
||
|
#data.encode_u8(1,0)
|
||
|
data.append(1)
|
||
|
data.append(len(player_name))
|
||
|
var packed_array = player_name.to_ascii_buffer()
|
||
|
# var str:PackedStringArray = PackedStringArray()
|
||
|
# str.push_back(player_name)
|
||
|
data += packed_array
|
||
|
#data.append_array(str.to_byte_array())
|
||
|
|
||
|
# print("-------------------------")
|
||
|
# print(len(packed_array))
|
||
|
# print("player_name: ", player_name)
|
||
|
# print("packed_array: ", packed_array)
|
||
|
# print("str: ", str)
|
||
|
# print("str -> bytes: ", str.to_byte_array())
|
||
|
# print("data: ", data)
|
||
|
# print("-------------------------")
|
||
|
|
||
|
print(player_name, " -> size:", data.size(), " / " , len(player_name) )
|
||
|
errorEnet = dataEnet.send(1, data, 1)
|
||
|
if errorEnet != OK:
|
||
|
print("ERROR ENET: ", errorEnet)
|
||
|
return
|
||
|
#decode_msg(data)
|
||
|
|
||
|
|
||
|
func set_player_position(pos: Vector3):
|
||
|
self.get_node("CharacterBody3D").set_global_position(pos)
|
||
|
|
||
|
|
||
|
func get_player_position():
|
||
|
if not account_confirmed:
|
||
|
return
|
||
|
print("perso: ", id, " ", self.get_node("CharacterBody3D").get_position())
|
||
|
var pos:PackedFloat64Array = PackedFloat64Array()
|
||
|
var posRaw:Vector3 = self.get_node("CharacterBody3D").get_position()
|
||
|
#posRaw.x = 123456789182729270e15
|
||
|
#posRaw.y = 2.0
|
||
|
#posRaw.z = 3.0
|
||
|
pos.append(posRaw.x)
|
||
|
#print("x:", pos.to_byte_array())
|
||
|
pos.append(posRaw.y)
|
||
|
#print("x+y:", pos.to_byte_array())
|
||
|
pos.append(posRaw.z)
|
||
|
#print("x+y+z:", pos.to_byte_array())
|
||
|
var data:PackedByteArray = PackedByteArray()
|
||
|
data += pos.to_byte_array()
|
||
|
#data.append(len(player_name))
|
||
|
#var packed_array = player_name.to_ascii_buffer()
|
||
|
#data += packed_array
|
||
|
#print(player_name, " -> size:", data.size(), " / " , len(player_name) )
|
||
|
errorEnet = dataEnet.send(2, data, 1)
|
||
|
if errorEnet != OK:
|
||
|
print("ERROR ENET: ", errorEnet)
|
||
|
return
|
||
|
|
||
|
|
||
|
func bad_acocunt():
|
||
|
player_name = ""
|
||
|
self.get_node("Window").open_windows()
|
||
|
|
||
|
|
||
|
func get_event_received():
|
||
|
var data = dataEnet.get_packet()
|
||
|
print("get: ", data)
|
||
|
if data[0] == 1: # Return connexion
|
||
|
if data[1] == 0: # OK
|
||
|
id = data.decode_u64(2)
|
||
|
var x = data.decode_double(2+8)
|
||
|
var y = data.decode_double(2+16)
|
||
|
var z = data.decode_double(2+24)
|
||
|
print("id:", id, " x:", x, " y:", y, " z:", z)
|
||
|
self.set_player_position(Vector3(x, y, z))
|
||
|
account_confirmed = true
|
||
|
self.get_node("CharacterBody3D").set_enable_event(true)
|
||
|
else: # KO
|
||
|
bad_acocunt()
|
||
|
elif data[0] == 3: # Get position
|
||
|
if id == 0:
|
||
|
return
|
||
|
var nb = data[1]
|
||
|
var pos = 2
|
||
|
for i in nb:
|
||
|
var mid = data.decode_u64(pos)
|
||
|
var mx = data.decode_double(pos+8)
|
||
|
var my = data.decode_double(pos+16)
|
||
|
var mz = data.decode_double(pos+24)
|
||
|
pos += 32
|
||
|
if mid == id:
|
||
|
print("Me id:", mid, " x:", mx, " y:", my, " z:", mz)
|
||
|
continue
|
||
|
print("-- id:", mid, " x:", mx, " y:", my, " z:", mz)
|
||
|
var child = $Players.find_child(str(mid), false, false)
|
||
|
if child == null:
|
||
|
print("Not found")
|
||
|
if maxplayer > 3:
|
||
|
continue
|
||
|
var scene = preload("res://scenes/player.tscn")
|
||
|
var instance = scene.instantiate()
|
||
|
#var current_scene = scene.instance()
|
||
|
#var GrabbedInstance = PLAYER.instance()
|
||
|
instance.set_name(str(mid))
|
||
|
$Players.add_child(instance)
|
||
|
#$Players.add_child(scene)
|
||
|
maxplayer += 1
|
||
|
for child3 in $Players.get_children():
|
||
|
print(" -> ", child3.get_name())
|
||
|
var child2 = $Players.find_child(str(mid), false, false)
|
||
|
child2.set_global_position(Vector3(mx, my, mz))
|
||
|
|
||
|
else:
|
||
|
child.set_global_position(Vector3(mx, my, mz))
|
||
|
print("Found:", $Players.get_child_count())
|
||
|
|
||
|
|
||
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||
|
func _process(_delta):
|
||
|
#print("state:", dataEnet.get_state())
|
||
|
get_player_position()
|
||
|
if dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTED:
|
||
|
var event = enet.service()
|
||
|
if event[0] == ENetConnection.EVENT_RECEIVE:
|
||
|
#print("get: ", dataEnet.get_packet())
|
||
|
get_event_received()
|
||
|
if player_name != "" and not account_confirmed:
|
||
|
send_account()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTING:
|
||
|
var event = enet.service()
|
||
|
if event[0] == ENetConnection.EVENT_RECEIVE:
|
||
|
pass
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_DISCONNECTED:
|
||
|
var _event = enet.service()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_ACKNOWLEDGING_CONNECT:
|
||
|
var _event = enet.service()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTION_PENDING:
|
||
|
var _event = enet.service()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTION_SUCCEEDED:
|
||
|
var _event = enet.service()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_DISCONNECT_LATER:
|
||
|
var _event = enet.service()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_DISCONNECTING:
|
||
|
var _event = enet.service()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_ACKNOWLEDGING_DISCONNECT:
|
||
|
var _event = enet.service()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_ZOMBIE:
|
||
|
var _event = enet.service()
|
||
|
else:
|
||
|
var _event = enet.service()
|
||
|
|
||
|
|
||
|
func _process_ter(_delta):
|
||
|
var res = enet.service(1)
|
||
|
#print(res)
|
||
|
if res[0] == ENetConnection.EVENT_RECEIVE:
|
||
|
#print("EVENT_RECEIVE")
|
||
|
print(dataEnet.get_packet())
|
||
|
#if res[1]
|
||
|
var data:PackedByteArray = PackedByteArray()
|
||
|
data.append(len(player_name))
|
||
|
data.append(123)
|
||
|
errorEnet = dataEnet.send(1, data, 1)
|
||
|
if errorEnet != OK:
|
||
|
print("ERROR ENET: ", errorEnet)
|
||
|
return
|
||
|
|
||
|
|
||
|
func _process_bis(_delta):
|
||
|
if not dataEnet.is_active():
|
||
|
create_server_enet()
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTING:
|
||
|
var res = enet.service(1)
|
||
|
print(res)
|
||
|
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTED:
|
||
|
var res = enet.service(1)
|
||
|
print(res)
|
||
|
var data:PackedByteArray = PackedByteArray()
|
||
|
data.append(len(player_name))
|
||
|
data.append(123)
|
||
|
errorEnet = dataEnet.send(1, data, 1)
|
||
|
if errorEnet != OK:
|
||
|
print("ERROR ENET: ", errorEnet)
|
||
|
return
|
||
|
#print("<> get_state: ", dataEnet.get_state())
|
||
|
else:
|
||
|
print("get_state: ", dataEnet.get_state())
|
||
|
return
|
||
|
|
||
|
if not connected:
|
||
|
if player_name != "":
|
||
|
create_server_enet()
|
||
|
return
|
||
|
var res = enet.service(1)
|
||
|
print(res)
|
||
|
return
|
||
|
if not connected:
|
||
|
if player_name != "":
|
||
|
create_server_enet()
|
||
|
else:
|
||
|
var data:PackedByteArray = PackedByteArray()
|
||
|
data.append(len(player_name))
|
||
|
data.append(123)
|
||
|
#print(len(data))
|
||
|
print("active:", dataEnet.is_active())
|
||
|
print("state:", dataEnet.get_state())
|
||
|
print("address:", dataEnet.get_remote_address())
|
||
|
print("port:", dataEnet.get_remote_port())
|
||
|
errorEnet = dataEnet.send(1, data, 1)
|
||
|
if errorEnet != OK:
|
||
|
print("ERROR ENET: ", errorEnet)
|
||
|
return
|
||
|
#enet.socket_send(ADDR, PORT, data)
|
||
|
|
||
|
|
||
|
func connect_enet(_name:String):
|
||
|
player_name = _name
|
||
|
print("My name:" + player_name)
|
||
|
|