extends Node

var net = NetworkedMultiplayerENet.new()
var api = MultiplayerAPI.new()
var port = 8081
var maxplayer = 4
export(NodePath) var database

func _ready():
	print('listening to port: ' + str(port))
	net.create_server(port, maxplayer)
	
	self.set_custom_multiplayer(api)
	custom_multiplayer.set_root_node(self)
	custom_multiplayer.set_network_peer(net)
	
	net.connect("peer_disconnected",self,"disc")
	net.connect("peer_connected",self,"conn")
	pass # Replace with function body.

func _process(_delta):
	custom_multiplayer.poll()

var playerphsyics = preload('res://playerphysics.tscn')

func conn(id):
	print('client connected, id: ' + str(id))
	yield(get_tree(),"physics_frame")
	rpc_id(0,'ping')
	rpc_id(0,'tokerequest')

export var db_validation = true

remote func tokenreply(token, username):
	var db = get_parent().get_node('PostgreSQL')
	var data = db.Read('validtokens_database','username',"'"+str(username)+"'")
	var message = 'token validated'
	if db_validation:
		if data == [] or data == null:
			message = "no token found"
			return
		if data[0][2] != token:
			message = "token is invalid"
		print('check tokens....')
	print(token)
	print(data)
	print(message)
	if message == 'token validated':
		print('clients connected: ' + str(custom_multiplayer.get_network_connected_peers().size()))
		var instance = playerphsyics.instance()
		instance.name = str(api.get_rpc_sender_id())
		$players.add_child(instance)
		if username == null or username == "":
			username = 'player' +  str(custom_multiplayer.get_network_connected_peers().size())
		usernames_dict[api.get_rpc_sender_id()] =  str(username)
		send_list_of_usernames(api.get_rpc_sender_id(), username)

func send_list_of_usernames(user, username):
	rpc_id(user,'_on_usernames', usernames_dict)
	rpc_id(0,'_on_usernames', { user : username } )

var messages_array = []
var usernames_dict = {}

remote func chat_messages(message):
	if $players.has_node(str(api.get_rpc_sender_id())):
		var buff = usernames_dict[api.get_rpc_sender_id()] + ': ' + str(message)
		messages_array.append( buff )
		rpc_id(0,'_On_chat_Message', buff)

remote func connected_peer():
	print('syncing clients')
	rpc_id(0,'_On_Peer_Connected')

func makedummmy():
	var node = api.get_rpc_sender_id()
	$players.get_node(str(node)).add_to_group('dummy')

remote func send_clients():
	rpc_id(api.get_rpc_sender_id(), 'client_data', custom_multiplayer.get_network_connected_peers())
# extra

func disc(arg):
	print('client disconnected, id: ' + str(arg))
	rpc_id(0,'_On_Player_Disconnect',arg)

remote func latency(arg):
	rpc_id(api.get_rpc_sender_id(), 'returnping', arg)

remote func input_handler(arg):
	var id = api.get_rpc_sender_id()
	if $players.has_node(str(id)):
		$players.get_node(str(id)).input = arg

var tick = 0
export var tickrate = 3

func _physics_process(delta):
	var position_datachunk = {}
	for i in $players.get_children():
		position_datachunk[i.name] = i.global_position
	rpc_unreliable_id(0, 'player_position', position_datachunk)
	
	var animation_datachunk = {}
	for i in $players.get_children():
		animation_datachunk[i.name] = i.animation
	rpc_unreliable_id(0, 'player_animation', animation_datachunk)
	
#	tick += 1
#	if fmod(tick,tickrate) == 0:
#		var enemies_datachunk = PoolByteArray()
#		for i in $enemies.dict.arraypos.size():
#			if $enemies.dict.arrayvida[i] > 0:
#				var pos = $enemies.dict.arraypos[i]
#				enemies_datachunk.append_array( return_int16(pos.x) )
#				enemies_datachunk.append_array( return_int16(pos.y) )
#				var bytetest : PoolByteArray
#				bytetest.append($enemies.dict.type[i] )
#				enemies_datachunk.append_array( bytetest )
#		rpc_unreliable_id(0, 'enemies_position_2', enemies_datachunk)

func return_int16(number):
	var int16 = number + 32768
	var bytetest : PoolByteArray
	var byte1:int = int(int16/255)
	var byte2:int = int(fmod(int16,255))
	bytetest.append(byte1)
	bytetest.append(byte2)
	return bytetest

func player_anim(attack,arg):
	rpc_id( 0,'attack_anim', attack, int(arg) )

func hp_update(vida, id):
	rpc_id( 0,'atr_update', {id:{'hp':vida}})

func kill_update(kill, xp, lv, nxt, id):
	rpc_id( 0,'atr_update', {id:{'k':kill, 'xp':xp, 'lv':lv, 'nxt':nxt}})

func level_up(lv, maxhp, gain, dmgup, id):
	rpc_id( 0,'level_up', lv, id)
	$players.get_node(str(id)).regenhp(gain)
	$players.get_node(str(id)).dmg += dmgup
	rpc_id( 0,'atr_update', {id:{'maxhp':maxhp}})
	
#	gen_power_queue(str(id))

#powerup code

var queue_powermenu_request = {
#	'id' : {'power':['a','b','c'],'client':0}
}

func gen_power_queue(client):
	var time = str(OS.get_system_time_msecs())+str(client)
	queue_powermenu_request[time] = {}
	queue_powermenu_request[time]['power'] = gen_power()
	queue_powermenu_request[time]['client'] = client
	rpc_id( int(client), 'powerpick', queue_powermenu_request[time]['power'] , time )
	_pauser_gen(false)

remote func optionpick(id, choice):
	var client = api.get_rpc_sender_id()
	if queue_powermenu_request.has(id):
		var power = queue_powermenu_request[id]['power'][choice]
		print(power)
		if $players.has_node(str(client)):
			$players.get_node(str(client)).add_power(power)
		queue_powermenu_request.erase(id)
	print(queue_powermenu_request)
	_pauser_gen(true)

func _pauser_gen(arg):
	set_physics_process(arg)
	$enemies.set_physics_process(arg)
	for i in $players.get_children():
		i.set_physics_process(arg)

func gen_power():
	return ['a','b','c']
