aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/menu/main_menu.gd38
-rw-r--r--client/menu/main_menu.tscn17
-rw-r--r--client/project.godot1
-rw-r--r--client/server.gd90
4 files changed, 145 insertions, 1 deletions
diff --git a/client/menu/main_menu.gd b/client/menu/main_menu.gd
index 28629e0d..c96d6c13 100644
--- a/client/menu/main_menu.gd
+++ b/client/menu/main_menu.gd
@@ -20,10 +20,14 @@ extends Control
@onready var quit_button = $side/margin/options/quit
@onready var connect_uri = $side/margin/options/connect/uri
+@onready var server = $side/margin/options/server
+@onready var server_control = $side/margin/options/server/control
+@onready var server_connect = $side/margin/options/server/connect
func _ready():
if OS.has_feature("web"):
quit_button.hide()
+ server.hide()
connect_uri.text = Global.profile["last_server_url"]
func _on_quit_pressed():
@@ -58,3 +62,37 @@ func _on_change_character_pressed():
func _on_settings_pressed():
menu_manager.goto("settings")
+
+func _on_server_pressed():
+ match Server.state:
+ Server.State.RUNNING: Server.stop()
+ Server.State.STOPPED: Server.start()
+ Server.State.FAILED: Server.start()
+
+func _process(_delta):
+ server_control.disabled = false
+ server_connect.visible = Server.state == Server.State.RUNNING
+ server_control.modulate = Color.WHITE
+ match Server.state:
+ Server.State.RUNNING:
+ server_control.text = tr("Stop Server")
+ server_control.modulate = Color.AQUAMARINE
+ Server.State.TESTING:
+ server_control.text = tr("Server (Testing)")
+ server_control.disabled = true
+ Server.State.STARTING:
+ server_control.text = tr("Server is starting...")
+ server_control.disabled = true
+ Server.State.STOPPED:
+ server_control.text = tr("Start Server")
+ Server.State.FAILED:
+ server_control.text = tr("Server (Failed)")
+ server_control.modulate = Color(1,0.4,0.5)
+ server_control.tooltip_text = tr("The server crashed or exited in some way or another.\nGodot's APIs are so bad however, that we really can't know why that happend.\nYou should try starting the server from the command-line.")
+ Server.State.UNAVAILABLE:
+ server_control.text = tr("Server (Unavailable)")
+ server_control.disabled = true
+ server_control.tooltip_text = tr("Server binary was not found. Please install the server seperately.")
+
+func _on_server_connect_pressed():
+ connect_to("ws://127.0.0.1:27032/")
diff --git a/client/menu/main_menu.tscn b/client/menu/main_menu.tscn
index 361be363..03f9cf46 100644
--- a/client/menu/main_menu.tscn
+++ b/client/menu/main_menu.tscn
@@ -2,7 +2,7 @@
[ext_resource type="Theme" uid="uid://b0qmvo504e457" path="res://menu/theme/theme.tres" id="1_nlcpo"]
[ext_resource type="Script" path="res://menu/main_menu.gd" id="2_qot2j"]
-[ext_resource type="Material" uid="uid://beea1pc5nt67r" path="res://menu/theme/dark_blur_material.tres" id="3_k58q5"]
+[ext_resource type="Material" path="res://menu/theme/dark_blur_material.tres" id="3_k58q5"]
[ext_resource type="FontFile" uid="uid://bo4vh5xkpvrh1" path="res://menu/theme/font-sansita-swashed.woff2" id="4_mfs30"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_ukani"]
@@ -81,6 +81,19 @@ layout_mode = 2
text = "Settings"
alignment = 0
+[node name="server" type="HBoxContainer" parent="side/margin/options"]
+layout_mode = 2
+
+[node name="control" type="Button" parent="side/margin/options/server"]
+layout_mode = 2
+size_flags_horizontal = 3
+text = "Server"
+alignment = 0
+
+[node name="connect" type="Button" parent="side/margin/options/server"]
+layout_mode = 2
+text = "Connect"
+
[node name="credits" type="Button" parent="side/margin/options"]
layout_mode = 2
text = "Credits"
@@ -95,5 +108,7 @@ alignment = 0
[connection signal="pressed" from="side/margin/options/connect/connect" to="." method="_on_connect_pressed"]
[connection signal="pressed" from="side/margin/options/change_character" to="." method="_on_change_character_pressed"]
[connection signal="pressed" from="side/margin/options/settings" to="." method="_on_settings_pressed"]
+[connection signal="pressed" from="side/margin/options/server/control" to="." method="_on_server_pressed"]
+[connection signal="pressed" from="side/margin/options/server/connect" to="." method="_on_server_connect_pressed"]
[connection signal="pressed" from="side/margin/options/credits" to="." method="_on_credits_pressed"]
[connection signal="pressed" from="side/margin/options/quit" to="." method="_on_quit_pressed"]
diff --git a/client/project.godot b/client/project.godot
index 19ad4911..ec7780f3 100644
--- a/client/project.godot
+++ b/client/project.godot
@@ -20,6 +20,7 @@ config/icon="res://icon.png"
[autoload]
Global="*res://global.gd"
+Server="*res://server.gd"
[input]
diff --git a/client/server.gd b/client/server.gd
new file mode 100644
index 00000000..11e8024c
--- /dev/null
+++ b/client/server.gd
@@ -0,0 +1,90 @@
+class_name GameServer
+extends Node
+
+var thread = null
+var pid = null
+
+var state = State.TESTING
+enum State {
+ TESTING,
+ UNAVAILABLE,
+ FAILED,
+ STOPPED,
+ STARTING,
+ RUNNING,
+}
+
+var sem = Semaphore.new()
+var thread_result = null;
+
+func _ready():
+ state = State.TESTING
+ thread = Thread.new()
+ thread.start(_test_server)
+
+
+func start():
+ if state != State.STOPPED and state != State.FAILED:
+ push_error("server cant be started")
+ return
+ state = State.STARTING
+ thread = Thread.new()
+ thread.start(_server_exec)
+
+func stop():
+ if state != State.RUNNING:
+ push_error("server cant be stopped")
+ return
+ OS.kill(pid)
+
+func _test_server():
+ var output = []
+ thread_result = OS.execute("undercooked-server", ["version"], output, true, false)
+ sem.post()
+
+func _server_exec():
+ thread_result = OS.create_process("undercooked-server", [], false)
+ if thread_result >= 0:
+ var ok = false
+ while not ok:
+ var conn = StreamPeerTCP.new()
+ if conn.connect_to_host("127.0.0.1", 27032) == OK:
+ while conn.poll() == OK:
+ if conn.get_status() == StreamPeerTCP.STATUS_ERROR: break
+ elif conn.get_status() == StreamPeerTCP.STATUS_CONNECTED: ok = true; break
+ OS.delay_msec(10)
+ OS.delay_msec(500 if not ok else 50)
+ if !OS.is_process_running(thread_result):
+ thread_result = -1
+ break
+ sem.post()
+
+func _process(_delta):
+ match state:
+ State.TESTING:
+ if sem.try_wait():
+ print("Server: Test result=", thread_result)
+ if thread_result == 0: state = State.STOPPED
+ else: state = State.UNAVAILABLE
+ thread.wait_to_finish()
+ thread = null
+ State.STARTING:
+ if sem.try_wait():
+ if thread_result >= 0:
+ state = State.RUNNING
+ pid = thread_result
+ print("Server: Started pid=", thread_result)
+ else:
+ state = State.FAILED
+ print("Server: Failed")
+ thread.wait_to_finish()
+ thread = null
+ State.RUNNING:
+ if not OS.is_process_running(pid):
+ print("Server: Stopped")
+ state = State.STOPPED
+ pid = null
+
+func _exit_tree():
+ if thread != null: thread.wait_to_finish()
+ if pid != null: OS.kill(pid)