## freeftf
## Copyright (C) 2024  Patrick_Pluto
##
## This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along with this program.  If not, see <https://www.gnu.org/licenses/>.

extends Node

var offline

func _ready():
	offline = multiplayer.multiplayer_peer
	multiplayer.server_disconnected.connect(_on_server_disconnected)

func join_game():
	var peer = ENetMultiplayerPeer.new()
	peer.create_client(Game.settings["ip"], int(Game.settings["port"]))
	multiplayer.multiplayer_peer = peer
	
func is_valid(sender_id, must_be_from_owner):
	if must_be_from_owner:
		if sender_id == Game.player_list.keys()[0] or Game.is_server:
			return true
	for id in Game.player_list:
		if sender_id == id:
			return true
	if sender_id == 0 or sender_id == 1:
		return true
	return false

func _on_server_disconnected():
	Game.reset()
	get_tree().change_scene_to_file("res://menus/main_menu.tscn")

@rpc("authority","call_remote","reliable")
func _on_player_disconnected(id):
	var x = 0
	for i in Game.player_list:
		if i == id:
			var current_character = get_tree().root.get_node("./"+Game.map_name+"/player"+str(x))
			if current_character.hp == 0:
				Game.dead -= 1
			elif current_character.is_frozen:
				Game.frozen -= 1
			elif current_character.beast:
				Game.player_escaped = true
				Game.players = 0
			else:
				Game.players -= 1
			await get_tree().create_timer(0.5).timeout
			current_character.free()
			if current_character.captured_by != null:
				current_character.captured_by.lost_one()
		x += 1
	Game.player_list.erase(id)

@rpc("authority","call_remote","reliable")
func room_closed():
	_on_server_disconnected()

@rpc("authority","call_remote","reliable")
func send_playerinfo():
	Matchmaking.rpc_id(1,"recieve_playerinfo",Game.player_name,Game.room_name)

@rpc("authority","call_remote","reliable")
func get_player_list(server_player_list):
	Game.player_list = server_player_list

@rpc("any_peer", "call_local", "reliable")
func start_game():
	if is_valid(multiplayer.get_remote_sender_id(), true):
		Game.start_game()

@rpc("any_peer", "call_local", "reliable")
func player_hit(target, beast):
	if is_valid(multiplayer.get_remote_sender_id(), false):
		Game.player_hit(target, beast)

@rpc("any_peer", "call_local", "unreliable")
func sync_player(node_path, position, rotation):
	if is_valid(multiplayer.get_remote_sender_id(), false):
		Game.sync_player(node_path, position, rotation)

@rpc("any_peer", "call_local", "unreliable")
func sync_hammer(node_path, rotation):
	if is_valid(multiplayer.get_remote_sender_id(), false):
		Game.sync_hammer(node_path, rotation)

@rpc("any_peer", "call_local", "reliable")
func sync_computers(node_path, current):
	if is_valid(multiplayer.get_remote_sender_id(), true):
		Game.sync_computers(node_path, current)

@rpc("any_peer", "call_local", "reliable")
func sync_beast(player):
	if is_valid(multiplayer.get_remote_sender_id(), true):
		Game.sync_beast(player)

@rpc("any_peer", "call_local", "reliable")
func sync_level(map_name):
	if is_valid(multiplayer.get_remote_sender_id(), true):
		Game.map_name = map_name