Renamed globals to autoloads for clarity
This commit is contained in:
7
autoloads/globals.gd
Normal file
7
autoloads/globals.gd
Normal file
@@ -0,0 +1,7 @@
|
||||
extends Node
|
||||
|
||||
|
||||
var game_data: GameData
|
||||
var game_state: GameState
|
||||
var lobby_data: LobbyData
|
||||
var multiplayer_data: MultiplayerData
|
||||
1
autoloads/globals.gd.uid
Normal file
1
autoloads/globals.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://biu64hvcxb1su
|
||||
72
autoloads/instance_launcher.gd
Normal file
72
autoloads/instance_launcher.gd
Normal file
@@ -0,0 +1,72 @@
|
||||
extends Node
|
||||
|
||||
|
||||
var port_range_start: int = 5000
|
||||
var port_range_end: int = 5010
|
||||
var threads: Dictionary = Dictionary()
|
||||
|
||||
|
||||
func _wait_for_child(port: String, mode: String, id: String) -> void:
|
||||
if port == "-1":
|
||||
call_deferred("_child_finished", id, -1)
|
||||
return
|
||||
|
||||
var output := []
|
||||
var exit_code := OS.execute(
|
||||
"./game.sh",
|
||||
[
|
||||
"--headless", "--", "server_" + port, "game", mode
|
||||
],
|
||||
output,
|
||||
true
|
||||
)
|
||||
|
||||
call_deferred("_child_finished", id, exit_code)
|
||||
|
||||
func _child_finished(id: String, exit_code: int) -> void:
|
||||
var thread: Thread = threads.get(id)
|
||||
if not thread: return
|
||||
|
||||
push_warning("Game Server [ %s ] Exited: %d" % [id, exit_code])
|
||||
|
||||
thread.wait_to_finish()
|
||||
threads.erase(id)
|
||||
|
||||
func is_port_free(port: int) -> bool:
|
||||
var server := TCPServer.new()
|
||||
var state := server.listen(port)
|
||||
|
||||
match state:
|
||||
OK:
|
||||
server.stop()
|
||||
return true
|
||||
_:
|
||||
return false
|
||||
|
||||
func find_free_port(start_port: int = 1024, end_port: int = 65535) -> int:
|
||||
for port in range(start_port, end_port + 1):
|
||||
if is_port_free(port):
|
||||
return port
|
||||
|
||||
return -1
|
||||
|
||||
func launch_game_server(match_data: Dictionary) -> String:
|
||||
var thread := Thread.new()
|
||||
var _thread_id := str( thread.get_instance_id() )
|
||||
var port := str(
|
||||
find_free_port(
|
||||
port_range_start,
|
||||
port_range_end
|
||||
)
|
||||
)
|
||||
|
||||
thread.start(
|
||||
_wait_for_child.bind(
|
||||
port,
|
||||
match_data.type,
|
||||
match_data.match_id
|
||||
)
|
||||
)
|
||||
|
||||
threads.set(match_data.match_id, thread)
|
||||
return port
|
||||
1
autoloads/instance_launcher.gd.uid
Normal file
1
autoloads/instance_launcher.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cru35twp27pa4
|
||||
60
autoloads/message_bus.gd
Normal file
60
autoloads/message_bus.gd
Normal file
@@ -0,0 +1,60 @@
|
||||
extends Node
|
||||
|
||||
|
||||
var message_types: Dictionary[String, Array] = {}
|
||||
|
||||
|
||||
func subscribe(id: String, callback: Callable) -> void:
|
||||
if not message_types.has(id):
|
||||
message_types.set(id, Array())
|
||||
|
||||
message_types.get(id).append(callback)
|
||||
|
||||
|
||||
func emit_local(id: String, data: Variant, expand: bool = false) -> void:
|
||||
emit_propagation(id, data, expand)
|
||||
|
||||
|
||||
# TODO: Need to actually validate client request. Also, message bus is more
|
||||
# about server talking to clients than clients to server. I.E, reducing
|
||||
# the number of needed @rpc method decorator bindings.
|
||||
@rpc("any_peer", "call_remote", "reliable")
|
||||
func emit_request(id: String, data: Variant, expand: bool = false) -> void:
|
||||
if not multiplayer.is_server(): return
|
||||
if not message_types.has(id):
|
||||
push_error("'%s' not an existing message type to handle...", [id])
|
||||
return
|
||||
|
||||
emit_propagation(id, data, expand)
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func emit_propagation(id: String, data: Variant, expand: bool = false) -> void:
|
||||
if not message_types.has(id):
|
||||
push_error("'%s' not an existing message type to handle...", [id])
|
||||
return
|
||||
|
||||
for callback: Callable in message_types.get(id):
|
||||
if data == null:
|
||||
callback.call()
|
||||
continue
|
||||
|
||||
if not expand:
|
||||
callback.call(data)
|
||||
else:
|
||||
callback.callv(data)
|
||||
|
||||
@rpc("authority", "call_remote", "reliable")
|
||||
func emit_propagation_clients_only(id: String, data: Variant, expand: bool = false) -> void:
|
||||
if not message_types.has(id):
|
||||
push_error("'%s' not an existing message type to handle...", [id])
|
||||
return
|
||||
|
||||
for callback: Callable in message_types.get(id):
|
||||
if data == null:
|
||||
callback.call()
|
||||
continue
|
||||
|
||||
if not expand:
|
||||
callback.call(data)
|
||||
else:
|
||||
callback.callv(data)
|
||||
1
autoloads/message_bus.gd.uid
Normal file
1
autoloads/message_bus.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://oatwf3crdtpt
|
||||
Reference in New Issue
Block a user