Use DNS for joining server

This commit is contained in:
Sofia 2026-07-28 21:36:09 +03:00
parent 7e6672fd0c
commit 3e1dd1eb2f
6 changed files with 69 additions and 28 deletions

View File

@ -4,7 +4,7 @@
[ext_resource type="PackedScene" uid="uid://dsxu0y8pjbw3k" path="res://scenes/ui/credits.tscn" id="1_sugp2"] [ext_resource type="PackedScene" uid="uid://dsxu0y8pjbw3k" path="res://scenes/ui/credits.tscn" id="1_sugp2"]
[ext_resource type="PackedScene" uid="uid://n3u0jr4wiib1" path="res://scenes/ui/settings.tscn" id="2_0wfyh"] [ext_resource type="PackedScene" uid="uid://n3u0jr4wiib1" path="res://scenes/ui/settings.tscn" id="2_0wfyh"]
[node name="menu" type="MainMenu" unique_id=2036763876 node_paths=PackedStringArray("camera", "main_position", "host_position", "main_panel", "host_panel", "join_panel", "host_port", "join_addr", "recent_servers_button", "popup_panel", "popup_title", "popup_text", "popup_button", "open_replay_dialog")] [node name="menu" type="MainMenu" unique_id=2036763876 node_paths=PackedStringArray("camera", "main_position", "host_position", "main_panel", "host_panel", "join_panel", "host_port", "join_addr", "join_port", "recent_servers_button", "popup_panel", "popup_title", "popup_text", "popup_button", "open_replay_dialog")]
camera = NodePath("camera_3d") camera = NodePath("camera_3d")
main_position = NodePath("main_pos") main_position = NodePath("main_pos")
host_position = NodePath("host_pos") host_position = NodePath("host_pos")
@ -13,16 +13,18 @@ host_panel = NodePath("host_panel")
join_panel = NodePath("join_panel") join_panel = NodePath("join_panel")
host_port = NodePath("host_panel/v_box_container/host_port") host_port = NodePath("host_panel/v_box_container/host_port")
join_addr = NodePath("join_panel/v_box_container/address_text") join_addr = NodePath("join_panel/v_box_container/address_text")
join_port = NodePath("join_panel/v_box_container/port_text")
recent_servers_button = NodePath("join_panel/v_box_container/recent_servers") recent_servers_button = NodePath("join_panel/v_box_container/recent_servers")
popup_panel = NodePath("popup_panel") popup_panel = NodePath("popup_panel")
popup_title = NodePath("popup_panel/v_box_container/title") popup_title = NodePath("popup_panel/v_box_container/title")
popup_text = NodePath("popup_panel/v_box_container/text") popup_text = NodePath("popup_panel/v_box_container/text")
popup_button = NodePath("popup_panel/v_box_container/ok") popup_button = NodePath("popup_panel/v_box_container/ok")
current_menu = 2
credits = ExtResource("1_sugp2") credits = ExtResource("1_sugp2")
open_replay_dialog = NodePath("open_replay_dialog") open_replay_dialog = NodePath("open_replay_dialog")
[node name="camera_3d" type="Camera3D" parent="." unique_id=1513321931] [node name="camera_3d" type="Camera3D" parent="." unique_id=1513321931]
transform = Transform3D(0.98629165, -0.031186381, 0.16203775, -1.4054481e-08, 0.981978, 0.1889952, -0.16501158, -0.18640438, 0.9685167, 1.0974052, 1.7166122, 1.4909106) transform = Transform3D(0.39605948, -0.17354012, 0.9016766, -1.9851193e-08, 0.981978, 0.18899524, -0.9182249, -0.074853376, 0.3889217, -1.0602231, 1.7166122, 3.7597804)
current = true current = true
[node name="directional_light_3d" type="DirectionalLight3D" parent="." unique_id=75522463] [node name="directional_light_3d" type="DirectionalLight3D" parent="." unique_id=75522463]
@ -38,6 +40,7 @@ transform = Transform3D(0.9823497, 0, 0.18705386, 0, 1, 0, -0.18705386, 0, 0.982
transform = Transform3D(-0.26696783, 0, 0.9637054, 0, 1, 0, -0.9637054, 0, -0.26696783, -3.5550833, -2.3841858e-07, 2.7302406) transform = Transform3D(-0.26696783, 0, 0.9637054, 0, 1, 0, -0.9637054, 0, -0.26696783, -3.5550833, -2.3841858e-07, 2.7302406)
[node name="main_panel" type="Panel" parent="." unique_id=2078435804] [node name="main_panel" type="Panel" parent="." unique_id=2078435804]
visible = false
offset_left = 139.0 offset_left = 139.0
offset_top = 167.0 offset_top = 167.0
offset_right = 336.0 offset_right = 336.0
@ -144,7 +147,6 @@ layout_mode = 2
text = "Back" text = "Back"
[node name="join_panel" type="Panel" parent="." unique_id=1630672771] [node name="join_panel" type="Panel" parent="." unique_id=1630672771]
visible = false
offset_left = 139.0 offset_left = 139.0
offset_top = 167.0 offset_top = 167.0
offset_right = 336.0 offset_right = 336.0
@ -172,18 +174,35 @@ text = "Join Game"
horizontal_alignment = 1 horizontal_alignment = 1
metadata/_edit_lock_ = true metadata/_edit_lock_ = true
[node name="address_label" type="Label" parent="join_panel/v_box_container" unique_id=425811384] [node name="recent_servers_label" type="Label" parent="join_panel/v_box_container" unique_id=683871668]
layout_mode = 2 layout_mode = 2
text = "Address" text = "Recent Servers"
horizontal_alignment = 1 horizontal_alignment = 1
[node name="recent_servers" type="OptionButton" parent="join_panel/v_box_container" unique_id=486398131] [node name="recent_servers" type="OptionButton" parent="join_panel/v_box_container" unique_id=486398131]
layout_mode = 2 layout_mode = 2
[node name="address_label" type="Label" parent="join_panel/v_box_container" unique_id=425811384]
layout_mode = 2
text = "Address"
horizontal_alignment = 1
[node name="address_text" type="LineEdit" parent="join_panel/v_box_container" unique_id=672176728] [node name="address_text" type="LineEdit" parent="join_panel/v_box_container" unique_id=672176728]
custom_minimum_size = Vector2(150, 30) custom_minimum_size = Vector2(150, 30)
layout_mode = 2 layout_mode = 2
text = "127.0.0.1:8080" text = "127.0.0.1"
[node name="port_label" type="Label" parent="join_panel/v_box_container" unique_id=1606834624]
layout_mode = 2
text = "Port"
horizontal_alignment = 1
[node name="port_text" type="SpinBox" parent="join_panel/v_box_container" unique_id=418812818]
custom_minimum_size = Vector2(150, 30)
layout_mode = 2
min_value = 1.0
max_value = 16000.0
value = 8080.0
[node name="join" type="Button" parent="join_panel/v_box_container" unique_id=1953386108] [node name="join" type="Button" parent="join_panel/v_box_container" unique_id=1953386108]
layout_mode = 2 layout_mode = 2

View File

@ -90,7 +90,7 @@ impl INode3D for CliParser {
if let Some(port) = options.host { if let Some(port) = options.host {
NetworkManager::singleton().bind_mut().host(port); NetworkManager::singleton().bind_mut().host(port);
} else if let Some(addr) = options.join { } else if let Some(addr) = options.join {
NetworkManager::singleton().bind_mut().join(addr); NetworkManager::singleton().bind_mut().join_directly(addr);
} }
} }
} }

View File

@ -137,7 +137,8 @@ impl VolumeSettings {
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RecentServer { pub struct RecentServer {
pub addr: SocketAddr, pub host: String,
pub port: u16,
pub visited: u128, pub visited: u128,
} }
@ -240,12 +241,12 @@ impl GameSettingsManager {
self.run_deferred(|s| s.signals().on_settings_changed().emit()); self.run_deferred(|s| s.signals().on_settings_changed().emit());
} }
pub fn visit_server(&mut self, server: SocketAddr) { pub fn visit_server(&mut self, host: String, port: u16) {
if let Some(server) = self if let Some(server) = self
.settings .settings
.recent_servers .recent_servers
.iter_mut() .iter_mut()
.find(|s| s.addr == server) .find(|s| s.host == host && s.port == port)
{ {
server.visited = SystemTime::now() server.visited = SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
@ -253,7 +254,8 @@ impl GameSettingsManager {
.as_nanos(); .as_nanos();
} else { } else {
self.settings.recent_servers.push(RecentServer { self.settings.recent_servers.push(RecentServer {
addr: server, host,
port,
visited: SystemTime::now() visited: SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.unwrap_or(Duration::from_millis(0)) .unwrap_or(Duration::from_millis(0))

View File

@ -1,6 +1,6 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
net::SocketAddr, net::{SocketAddr, ToSocketAddrs},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -56,7 +56,7 @@ impl INode for NetworkManager {
cli.bind_mut() cli.bind_mut()
.signals() .signals()
.on_join() .on_join()
.connect_other(self, |s, addr| s.join(addr.parse().unwrap())); .connect_other(self, |s, addr| s.join_directly(addr.parse().unwrap()));
cli.bind_mut() cli.bind_mut()
.signals() .signals()
.on_disconnect() .on_disconnect()
@ -229,7 +229,24 @@ impl NetworkManager {
} }
} }
pub fn join(&mut self, addr: SocketAddr) { pub fn join(&mut self, host: String, port: u16) -> Result<(), ()> {
match format!("{}:{}", host, port).to_socket_addrs() {
Ok(mut addrs) => {
if let Some(addr) = addrs.next() {
GameSettingsManager::singleton()
.bind_mut()
.visit_server(host, port);
self.join_directly(addr);
Ok(())
} else {
Err(())
}
}
Err(_) => Err(()),
}
}
pub fn join_directly(&mut self, addr: SocketAddr) {
if self.peer.is_none() { if self.peer.is_none() {
let mut cli = CommandLinePanel::singleton(); let mut cli = CommandLinePanel::singleton();
cli.bind_mut() cli.bind_mut()

View File

@ -19,10 +19,6 @@ impl NetworkManager {
match peer { match peer {
PeerKind::Client(peer, addr) => match message { PeerKind::Client(peer, addr) => match message {
teanet::PeerMessage::NewConnection(connection) => { teanet::PeerMessage::NewConnection(connection) => {
GameSettingsManager::singleton()
.bind_mut()
.visit_server(addr.clone());
cli.bind_mut().publish_message( cli.bind_mut().publish_message(
format!("Connected to: {}", connection.address), format!("Connected to: {}", connection.address),
CliColor::Info, CliColor::Info,

View File

@ -47,6 +47,8 @@ pub struct MainMenu {
#[export] #[export]
join_addr: Option<Gd<LineEdit>>, join_addr: Option<Gd<LineEdit>>,
#[export] #[export]
join_port: Option<Gd<SpinBox>>,
#[export]
recent_servers_button: Option<Gd<OptionButton>>, recent_servers_button: Option<Gd<OptionButton>>,
#[export] #[export]
@ -83,7 +85,7 @@ impl INode3D for MainMenu {
.recent_servers .recent_servers
.clone(); .clone();
for server in &self.recent_servers { for server in &self.recent_servers {
recent_servers_button.add_item(&server.addr.to_string()); recent_servers_button.add_item(&format!("{}:{}", server.host, server.port));
} }
recent_servers_button.select(-1); recent_servers_button.select(-1);
@ -203,21 +205,24 @@ impl MainMenu {
#[func] #[func]
pub fn join_game(&mut self) { pub fn join_game(&mut self) {
if let Some(addr) = self.join_addr.clone() { if let Some(addr) = self.join_addr.clone()
&& let Some(port) = self.join_port.clone()
{
self.show_popup("Joining", "joining game", false); self.show_popup("Joining", "joining game", false);
PopupQueue::singleton().bind_mut().queue(Popup { PopupQueue::singleton().bind_mut().queue(Popup {
title: "Joining".to_owned(), title: "Joining".to_owned(),
message: "joining game".to_owned(), message: "joining game".to_owned(),
ok: false, ok: false,
}); });
match addr.get_text().to_string().parse() { match NetworkManager::singleton()
Ok(addr) => { .bind_mut()
NetworkManager::singleton().bind_mut().join(addr); .join(addr.get_text().to_string(), port.get_value() as u16)
} {
Err(err) => { Ok(_) => {}
Err(_) => {
PopupQueue::singleton().bind_mut().queue(Popup { PopupQueue::singleton().bind_mut().queue(Popup {
title: "Error".to_owned(), title: "Error".to_string(),
message: format!("Error joining: {}", err), message: "Address is invalid".to_string(),
ok: true, ok: true,
}); });
} }
@ -279,8 +284,10 @@ impl MainMenu {
if idx >= 0 { if idx >= 0 {
if let Some(server) = self.recent_servers.get(idx as usize) if let Some(server) = self.recent_servers.get(idx as usize)
&& let Some(addr) = &mut self.join_addr && let Some(addr) = &mut self.join_addr
&& let Some(port) = &mut self.join_port
{ {
addr.set_text(&server.addr.to_string()); addr.set_text(&server.host);
port.set_value(server.port as f64);
} }
if let Some(button) = &mut self.recent_servers_button { if let Some(button) = &mut self.recent_servers_button {
button.select(-1); button.select(-1);