Add master server dialog

This commit is contained in:
Hamcha 2020-07-14 01:59:45 +02:00
parent 9e6763d2bb
commit 40705ac661
Signed by: hamcha
GPG key ID: 41467804B19A3315
4 changed files with 175 additions and 31 deletions

View file

@ -31,7 +31,6 @@ master func send_map() -> void:
}) })
puppet func receive_data(data: Dictionary) -> void: puppet func receive_data(data: Dictionary) -> void:
print(data.systems)
world.map.deserialize(data["map"]) world.map.deserialize(data["map"])
systems.deserialize(data["systems"]) systems.deserialize(data["systems"])
print("Received map data from master") print("Received map data from master")

View file

@ -171,6 +171,17 @@ func _ms_request(endpoint: String, data):
if error != OK: if error != OK:
push_error("An error occurred in the HTTP request.") push_error("An error occurred in the HTTP request.")
func ms_get_entries():
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.connect("request_completed", self, "_ms_response", ["list_games"])
var error = http_request.request(
"https://" + MASTER_SERVER_ADDR + "/" + MS_GAME_CODE,
["Content-Type: application/json"],
true, HTTPClient.METHOD_GET)
if error != OK:
push_error("An error occurred in the HTTP request.")
func get_game_data(): func get_game_data():
return { return {
"name": server_name, "name": server_name,
@ -192,7 +203,15 @@ func update_ms_entry():
"key": ms_key, "key": ms_key,
"data": get_game_data() "data": get_game_data()
}) })
yield(get_tree().create_timer(20), "timeout")
var time_left = 30
func _process(delta):
if not ms_active:
return
time_left -= delta
if time_left < 0:
update_ms_entry()
time_left = 30
func _ms_response(_result: int, response_code: int, _headers: PoolStringArray, body: PoolByteArray, action: String): func _ms_response(_result: int, response_code: int, _headers: PoolStringArray, body: PoolByteArray, action: String):
print_debug("MS said " + str(response_code)) print_debug("MS said " + str(response_code))

View file

@ -8,12 +8,18 @@ export var delay = 1.0
onready var viewport = $Viewport onready var viewport = $Viewport
onready var background = $Background onready var background = $Background
onready var netgame = $"/root/Multiplayer"
onready var server_list = $Popup/MarginContainer/VBoxContainer/ItemList
func _ready(): var servers = []
func _ready() -> void:
set_scale(scale) set_scale(scale)
$"/root/Music/BGM".play() $"/root/Music/BGM".play()
netgame.connect("ms_updated", self, "_ms_update")
request_servers()
func _process(delta): func _process(delta: float) -> void:
# Give the system time to adjust # Give the system time to adjust
if delay > 0: if delay > 0:
delay -= delta delay -= delta
@ -25,25 +31,44 @@ func _process(delta):
print("GPU is taking it easy (", delta, "s < ", upThreshold, "s), adjusting shader scale") print("GPU is taking it easy (", delta, "s < ", upThreshold, "s), adjusting shader scale")
set_scale(scale-1) set_scale(scale-1)
func set_scale(val): func request_servers():
netgame.ms_get_entries()
func _ms_update(action, result):
if action == "list_games":
# Reset server list
server_list.clear()
servers = result
for server in servers:
server_list.add_item(server.data.name + " (" + server.address + ") - " + str(server.data.players) + "/" + str(server.data.max_players) + " players")
func set_scale(val) -> void:
scale = val scale = val
if viewport: if viewport:
viewport.size = rect_size / scale viewport.size = rect_size / scale
background.update() background.update()
func _on_resized(): func _on_resized() -> void:
if viewport: if viewport:
viewport.size = rect_size / scale viewport.size = rect_size / scale
background.update() background.update()
func _host_pressed(): func _host_pressed() -> void:
$"/root/Music/BGM".stop() $"/root/Music/BGM".stop()
$"/root/Multiplayer".host() $"/root/Multiplayer".host()
func _join_pressed(): func _join_pressed() -> void:
$"/root/Music/BGM".stop() $Popup.popup_centered_ratio()
var addr = $CenterContainer/PanelContainer/HBoxContainer/ServerAddr
$"/root/Multiplayer".join(addr.text)
func _server_addr_changed(new_text: String): func join_server(addr: String) -> void:
$CenterContainer/PanelContainer/HBoxContainer/JoinButton.disabled = new_text.length() < 1 $"/root/Music/BGM".stop()
$"/root/Multiplayer".join(addr)
func _server_addr_changed(new_text: String) -> void:
$Popup/MarginContainer/VBoxContainer/HBoxContainer/Button.disabled = new_text.length() < 1
func _manual_join_pressed():
join_server($Popup/MarginContainer/VBoxContainer/HBoxContainer/LineEdit.text)
func _server_item_clicked(index):
join_server(servers[index].address)

View file

@ -1,9 +1,10 @@
[gd_scene load_steps=10 format=2] [gd_scene load_steps=13 format=2]
[ext_resource path="res://Scenes/Menu.gd" type="Script" id=1] [ext_resource path="res://Scenes/Menu.gd" type="Script" id=1]
[ext_resource path="res://Graphics/UI/Background.shader" type="Shader" id=2] [ext_resource path="res://Graphics/UI/Background.shader" type="Shader" id=2]
[ext_resource path="res://Graphics/UI/logo-temp-pixel.png" type="Texture" id=3] [ext_resource path="res://Graphics/UI/logo-temp-pixel.png" type="Texture" id=3]
[ext_resource path="res://Graphics/UI/ui_theme.tres" type="Theme" id=4] [ext_resource path="res://Graphics/UI/ui_theme.tres" type="Theme" id=4]
[ext_resource path="res://Graphics/UI/iosevka-aile-regular.ttf" type="DynamicFontData" id=5]
[sub_resource type="ViewportTexture" id=1] [sub_resource type="ViewportTexture" id=1]
viewport_path = NodePath("Viewport") viewport_path = NodePath("Viewport")
@ -51,6 +52,18 @@ corner_radius_top_right = 4
corner_radius_bottom_right = 4 corner_radius_bottom_right = 4
corner_radius_bottom_left = 4 corner_radius_bottom_left = 4
[sub_resource type="DynamicFont" id=6]
size = 20
extra_spacing_top = 5
extra_spacing_bottom = 5
font_data = ExtResource( 5 )
[sub_resource type="DynamicFont" id=7]
size = 20
extra_spacing_top = 10
extra_spacing_bottom = 10
font_data = ExtResource( 5 )
[node name="Control" type="Control"] [node name="Control" type="Control"]
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@ -125,9 +138,9 @@ __meta__ = {
[node name="PanelContainer" type="PanelContainer" parent="CenterContainer"] [node name="PanelContainer" type="PanelContainer" parent="CenterContainer"]
margin_left = 390.0 margin_left = 390.0
margin_top = 43.0 margin_top = 35.0
margin_right = 690.0 margin_right = 690.0
margin_bottom = 207.0 margin_bottom = 215.0
rect_min_size = Vector2( 300, 0 ) rect_min_size = Vector2( 300, 0 )
custom_styles/panel = SubResource( 5 ) custom_styles/panel = SubResource( 5 )
@ -135,41 +148,129 @@ custom_styles/panel = SubResource( 5 )
margin_left = 16.0 margin_left = 16.0
margin_top = 20.0 margin_top = 20.0
margin_right = 284.0 margin_right = 284.0
margin_bottom = 144.0 margin_bottom = 160.0
custom_constants/separation = 10 custom_constants/separation = 10
[node name="HostButton" type="Button" parent="CenterContainer/PanelContainer/HBoxContainer"] [node name="HostButton" type="Button" parent="CenterContainer/PanelContainer/HBoxContainer"]
margin_right = 268.0 margin_right = 268.0
margin_bottom = 24.0 margin_bottom = 42.0
theme = ExtResource( 4 ) theme = ExtResource( 4 )
custom_fonts/font = SubResource( 6 )
text = "Start a new game" text = "Start a new game"
[node name="Label" type="Label" parent="CenterContainer/PanelContainer/HBoxContainer"] [node name="Label" type="Label" parent="CenterContainer/PanelContainer/HBoxContainer"]
margin_top = 34.0 margin_top = 52.0
margin_right = 268.0 margin_right = 268.0
margin_bottom = 52.0 margin_bottom = 88.0
theme = ExtResource( 4 ) theme = ExtResource( 4 )
custom_fonts/font = SubResource( 6 )
text = "or" text = "or"
align = 1 align = 1
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="ServerAddr" type="LineEdit" parent="CenterContainer/PanelContainer/HBoxContainer"]
margin_top = 62.0
margin_right = 268.0
margin_bottom = 90.0
theme = ExtResource( 4 )
placeholder_text = "Server address or URL"
[node name="JoinButton" type="Button" parent="CenterContainer/PanelContainer/HBoxContainer"] [node name="JoinButton" type="Button" parent="CenterContainer/PanelContainer/HBoxContainer"]
margin_top = 100.0 margin_top = 98.0
margin_right = 268.0 margin_right = 268.0
margin_bottom = 124.0 margin_bottom = 140.0
theme = ExtResource( 4 ) theme = ExtResource( 4 )
disabled = true custom_fonts/font = SubResource( 6 )
text = "Join an existing game" text = "Join an existing game"
[node name="Popup" type="PopupDialog" parent="."]
visible = true
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 100.0
margin_top = 100.0
margin_right = -100.0
margin_bottom = -100.0
custom_styles/panel = SubResource( 5 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="MarginContainer" type="MarginContainer" parent="Popup"]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 20.0
margin_top = 20.0
margin_right = -20.0
margin_bottom = -20.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="VBoxContainer" type="VBoxContainer" parent="Popup/MarginContainer"]
margin_right = 1040.0
margin_bottom = 560.0
custom_constants/separation = 5
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Label" type="Label" parent="Popup/MarginContainer/VBoxContainer"]
margin_right = 1040.0
margin_bottom = 36.0
custom_fonts/font = SubResource( 6 )
text = "Server Browser"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="ItemList" type="ItemList" parent="Popup/MarginContainer/VBoxContainer"]
margin_top = 41.0
margin_right = 1040.0
margin_bottom = 468.0
size_flags_vertical = 3
custom_fonts/font = SubResource( 7 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Label2" type="Label" parent="Popup/MarginContainer/VBoxContainer"]
margin_top = 473.0
margin_right = 1040.0
margin_bottom = 509.0
custom_fonts/font = SubResource( 6 )
text = "Join by IP/address"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="HBoxContainer" type="HBoxContainer" parent="Popup/MarginContainer/VBoxContainer"]
margin_top = 514.0
margin_right = 1040.0
margin_bottom = 560.0
size_flags_horizontal = 3
custom_constants/separation = 10
[node name="LineEdit" type="LineEdit" parent="Popup/MarginContainer/VBoxContainer/HBoxContainer"]
margin_right = 300.0
margin_bottom = 46.0
rect_min_size = Vector2( 300, 0 )
custom_fonts/font = SubResource( 6 )
placeholder_text = "server.address"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Button" type="Button" parent="Popup/MarginContainer/VBoxContainer/HBoxContainer"]
margin_left = 310.0
margin_right = 410.0
margin_bottom = 46.0
rect_min_size = Vector2( 100, 0 )
theme = ExtResource( 4 )
custom_fonts/font = SubResource( 6 )
disabled = true
text = "Join"
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="resized" from="." to="." method="_on_resized"] [connection signal="resized" from="." to="." method="_on_resized"]
[connection signal="pressed" from="CenterContainer/PanelContainer/HBoxContainer/HostButton" to="." method="_host_pressed"] [connection signal="pressed" from="CenterContainer/PanelContainer/HBoxContainer/HostButton" to="." method="_host_pressed"]
[connection signal="text_changed" from="CenterContainer/PanelContainer/HBoxContainer/ServerAddr" to="." method="_server_addr_changed"]
[connection signal="pressed" from="CenterContainer/PanelContainer/HBoxContainer/JoinButton" to="." method="_join_pressed"] [connection signal="pressed" from="CenterContainer/PanelContainer/HBoxContainer/JoinButton" to="." method="_join_pressed"]
[connection signal="item_activated" from="Popup/MarginContainer/VBoxContainer/ItemList" to="." method="_server_item_clicked"]
[connection signal="text_changed" from="Popup/MarginContainer/VBoxContainer/HBoxContainer/LineEdit" to="." method="_server_addr_changed"]
[connection signal="pressed" from="Popup/MarginContainer/VBoxContainer/HBoxContainer/Button" to="." method="_manual_join_pressed"]