rework of server loading

This commit is contained in:
leca 2024-03-05 00:18:05 +03:00
parent eee4baf79a
commit 151b68d6d8
2 changed files with 60 additions and 29 deletions

View File

@ -21,7 +21,7 @@ var Weapons = {
var client_settings var client_settings
var server_settings var server_settings
func _ready(): func read_settings():
var user_dir = DirAccess.open(".") var user_dir = DirAccess.open(".")
if(!user_dir.dir_exists("./settings")): if(!user_dir.dir_exists("./settings")):
user_dir.make_dir("settings") user_dir.make_dir("settings")

View File

@ -17,6 +17,7 @@ var spawnpoints_cs = []
var cs_score = 0 var cs_score = 0
var os_score = 0 var os_score = 0
var round_number = 0 var round_number = 0
var gamemode
func parse_arguments(): func parse_arguments():
var arguments = {} var arguments = {}
@ -32,56 +33,88 @@ func parse_arguments():
func check_map_availability(path): func check_map_availability(path):
var maps = DirAccess.open("res://scenes/maps").get_files() var maps = DirAccess.open("res://scenes/maps").get_files()
var map_name = (path.split("/")[-1]).split(".")[0]
for map in maps: for map in maps:
var map_name = str(map.split(".")[0]) var checking_map_name = str(map.split(".")[0])
print(map_name + " " + map_root_name) if(checking_map_name == map_name):
if(map_root_name == map_name): return true
return false
func check_gamemode_availability(gamemode):
print(str(settings["game"]))
var gamemodes = settings["game"]["gamemodes"].keys()
for gm in gamemodes:
if gm == gamemode:
return true return true
return false return false
func _ready(): func _ready():
var arguments = parse_arguments() var arguments = parse_arguments()
if "--server" in OS.get_cmdline_args(): if "--server" in OS.get_cmdline_args():
var path GameData.read_settings()
if (arguments.has("map")): settings = GameData.server_settings
map_root_name = str(arguments["map"])
path = "res://scenes/maps/" + str(arguments["map"]) + ".tscn"
else:
map_root_name = "training"
path = "res://scenes/maps/training.tscn"
################ prepairing map
var path = "res://scenes/maps/%s.tscn" % arguments["map"] if arguments.has("map") else "res://scenes/maps/%s.tscn" % settings["defaults"]["map"]
if (check_map_availability(path)): if (check_map_availability(path)):
map_path = path map_path = path
else: else:
print("Unknown map. Available maps:") print("Unknown map %s Available maps:" % path)
for map in DirAccess.open("res://scenes/maps").get_files(): for map in DirAccess.open("res://scenes/maps").get_files():
print(str(map.split(".")[0])) print(str(map.split(".")[0]))
get_tree().quit() get_tree().quit()
return return
################ prepairing gamemode
StartServer(map_path) var gm = str(arguments["gamemode"]) if (arguments.has("gamemode")) else "TDM"
if(check_gamemode_availability(gm)):
print("Gamemode exists")
else:
print("No")
StartServer(map_path, gm)
#######################################SERVER#################################### #######################################SERVER####################################
func switch_map(new_map_path):
print("Switching map to %s" % new_map_path)
if (not check_map_availability(new_map_path)):
var default_map = settings["defaults"]["map"]
print("Error. No map found. Loading default map %s" % default_map)
new_map_path = "res://scenes/maps/%s.tscn" % default_map
if (not check_map_availability(new_map_path)):
print("Error. Default map is not valid. Please, specify a valid map in a config file.")
return
map_path = new_map_path
get_tree().change_scene_to_file(map_path)
await get_tree().create_timer(0.1).timeout #I know that this isn't a good practice, but I didn't find anything better
map_root_name = (map_path.split("/")[-1]).split(".")[0]
server_map = get_tree().root.get_node(map_root_name)
func switch_gamemode(new_gamemode):
if not check_gamemode_availability(new_gamemode):
print("No gamemode found")
return
gamemode = new_gamemode
func new_game(new_map_path, new_gamemode):
cs_score = 0
os_score = 0
round_number = 0
await switch_map(new_map_path)
await switch_gamemode(new_gamemode)
func new_round():
round_number += 1
func find_playermodel_by_internal_id(internal_id): func find_playermodel_by_internal_id(internal_id):
return server_map.get_node("player" + str(internal_id)) return server_map.get_node("player" + str(internal_id))
func StartServer(map_name): func StartServer(map_path, gm):
await get_tree().create_timer(0.01).timeout #костыль пиздец но мне похую как-то await new_game(map_path, gm)
if (map_name != null):
get_tree().change_scene_to_file(map_name)
await get_tree().create_timer(0.1).timeout #костыль пиздец но мне похую как-то
var root_node_name = (map_name.split("/")[-1]).split(".")[0]
server_map = get_tree().root.get_node(root_node_name)
await get_tree().create_timer(0.5).timeout #костыль
settings = GameData.server_settings
var port = int(settings["port"]) var port = int(settings["port"])
var maxclients = int(settings["maxclients"]) var maxclients = int(settings["maxclients"])
if (peer.create_server(port, maxclients) != OK): if (peer.create_server(port, maxclients) != OK):
print("Couldn't create server. Check if another proccess binds port " + str(port)) print("Couldn't create server. Check if another proccess binds port %s" % str(port))
return return
multiplayer.multiplayer_peer = peer multiplayer.multiplayer_peer = peer
@ -93,9 +126,7 @@ func StartServer(map_name):
var spectator = preload("res://scenes/models/spectator.tscn").instantiate() var spectator = preload("res://scenes/models/spectator.tscn").instantiate()
server_map.add_child(spectator) server_map.add_child(spectator)
var spawnpoints = server_map.find_children("spawnpoint*", "" ,true) var spawnpoints = server_map.find_children("spawnpoint*", "" ,true)
print(str(spawnpoints))
for spawnpoint in spawnpoints: for spawnpoint in spawnpoints:
print("Checking spawnpoint: " + str(spawnpoint) + ", team: " + str(spawnpoint.team))
if spawnpoint.team == 0: # cs if spawnpoint.team == 0: # cs
spawnpoints_cs.push_back(spawnpoint) spawnpoints_cs.push_back(spawnpoint)
elif spawnpoint.team > 0: # os elif spawnpoint.team > 0: # os