Skip to content

Commit

Permalink
Update to use new server scanner without LAN/Internet selection. Just…
Browse files Browse the repository at this point in the history
… always list all of them. And prepare for steam servers being listed potentially in the future
  • Loading branch information
daid committed Aug 23, 2024
1 parent 0115b9b commit ebcb2c6
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 89 deletions.
38 changes: 14 additions & 24 deletions src/menus/joinServerMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
#include "gui/gui2_textentry.h"
#include "gui/gui2_button.h"

JoinServerScreen::JoinServerScreen(ServerBrowserMenu::SearchSource source)
JoinServerScreen::JoinServerScreen(const ServerScanner::ServerInfo& target)
: target(target)
{
this->source = source;

status_label = new GuiLabel(this, "STATUS", tr("connectserver", "Connecting..."), 30);
status_label->setPosition(0, 300, sp::Alignment::TopCenter)->setSize(0, 50);
(new GuiButton(this, "BTN_CANCEL", tr("button", "Cancel"), [this]() {
destroy();
disconnectFromServer();
new ServerBrowserMenu(this->source);
new ServerBrowserMenu();
}))->setPosition(50, -50, sp::Alignment::BottomLeft)->setSize(300, 50);

password_entry_box = new GuiPanel(this, "PASSWORD_ENTRY_BOX");
Expand All @@ -35,24 +34,15 @@ JoinServerScreen::JoinServerScreen(ServerBrowserMenu::SearchSource source)
password_focused = false;
game_client->sendPassword(password_entry->getText().upper());
}))->setPosition(420, 0, sp::Alignment::CenterLeft)->setSize(160, 50);
}

JoinServerScreen::JoinServerScreen(ServerBrowserMenu::SearchSource source, sp::io::network::Address ip, int port)
: JoinServerScreen(source)
{
this->ip = ip;
this->port = port;

new GameClient(VERSION_NUMBER, ip, port);
}

if (target.type == ServerScanner::ServerType::SteamFriend) {
#ifdef STEAMSDK
JoinServerScreen::JoinServerScreen(ServerBrowserMenu::SearchSource source, uint64_t steam_id)
: JoinServerScreen(source)
{
new GameClient(VERSION_NUMBER, steam_id);
}
new GameClient(VERSION_NUMBER, target.port);
#endif
} else {
new GameClient(VERSION_NUMBER, target.address, target.port);
}
}

void JoinServerScreen::update(float delta)
{
Expand All @@ -76,14 +66,14 @@ void JoinServerScreen::update(float delta)
destroy();
disconnectFromServer();

new ServerBrowserMenu(this->source, reason);
new ServerBrowserMenu(reason);
} break;
case GameClient::Connected:
if (!this->ip.getHumanReadable().empty())
if (!target.address.getHumanReadable().empty())
{
string last_server = this->ip.getHumanReadable()[0];
if (port != defaultServerPort)
last_server += ":" + string(port);
string last_server = target.address.getHumanReadable()[0];
if (target.port != defaultServerPort)
last_server += ":" + string(int(target.port));
PreferencesManager::set("last_server", last_server);
}
if (game_client->getClientId() > 0)
Expand Down
23 changes: 8 additions & 15 deletions src/menus/joinServerMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#define JOIN_SERVER_MENU_H

#include "gui/gui2_canvas.h"
#include "serverBrowseMenu.h"
#include "io/network/address.h"
#include "multiplayer_server_scanner.h"


class GuiLabel;
Expand All @@ -12,23 +11,17 @@ class GuiTextEntry;

class JoinServerScreen : public GuiCanvas, public Updatable
{
sp::io::network::Address ip;
int port = 0;
public:
JoinServerScreen(const ServerScanner::ServerInfo& target);

virtual void update(float delta) override;

private:
ServerScanner::ServerInfo target;
GuiLabel* status_label;
GuiPanel* password_entry_box;
GuiTextEntry* password_entry = nullptr;
bool password_focused = false;

ServerBrowserMenu::SearchSource source;

JoinServerScreen(ServerBrowserMenu::SearchSource source);
public:
JoinServerScreen(ServerBrowserMenu::SearchSource source, sp::io::network::Address ip, int port);
#ifdef STEAMSDK
JoinServerScreen(ServerBrowserMenu::SearchSource source, uint64_t steam_id);
#endif

virtual void update(float delta) override;
};

#endif//JOIN_SERVER_MENU_H
2 changes: 1 addition & 1 deletion src/menus/mainMenus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ MainMenu::MainMenu()
}))->setPosition({50, -230}, sp::Alignment::BottomLeft)->setSize(300, 50);

(new GuiButton(this, "START_CLIENT", tr("mainMenu", "Start client"), [this]() {
new ServerBrowserMenu(ServerBrowserMenu::Local);
new ServerBrowserMenu();
destroy();
}))->setPosition({50, -170}, sp::Alignment::BottomLeft)->setSize(300, 50);

Expand Down
117 changes: 78 additions & 39 deletions src/menus/serverBrowseMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ namespace
}
}

ServerBrowserMenu::ServerBrowserMenu(SearchSource source, std::optional<GameClient::DisconnectReason> last_attempt /* = {} */)
ServerBrowserMenu::ServerBrowserMenu(std::optional<GameClient::DisconnectReason> last_attempt /* = {} */)
{
scanner = new ServerScanner(VERSION_NUMBER);

if (source == Local)
scanner->scanLocalNetwork();
else
scanner->scanMasterServer(PreferencesManager::get("registry_list_url", "http://daid.eu/ee/list.php"));
scanner->scanLocalNetwork();
scanner->scanMasterServer(PreferencesManager::get("registry_list_url", "http://daid.eu/ee/list.php"));

new GuiOverlay(this, "", colorConfig.background);
(new GuiOverlay(this, "", glm::u8vec4{255,255,255,255}))->setTextureTiled("gui/background/crosses.png");
Expand All @@ -62,49 +59,90 @@ ServerBrowserMenu::ServerBrowserMenu(SearchSource source, std::optional<GameClie
error_info->setPosition(0, 25, sp::Alignment::TopCenter);
}

lan_internet_selector = new GuiSelector(this, "LAN_INTERNET_SELECT", [this](int index, string value) {
if (index == 0)
scanner->scanLocalNetwork();
else
scanner->scanMasterServer(PreferencesManager::get("registry_list_url", "http://daid.eu/ee/list.php"));
});
lan_internet_selector->setOptions({tr("LAN"), tr("Internet")})->setSelectionIndex(source == Local ? 0 : 1)->setPosition(0, -50, sp::Alignment::BottomCenter)->setSize(300, 50);

connect_button = new GuiButton(this, "CONNECT", tr("screenLan", "Connect"), [this]() {
connect(manual_ip->getText());
if (selected_server) {
connect(selected_server.value());
} else {
connect(manual_ip->getText());
}
});
connect_button->setPosition(-50, -50, sp::Alignment::BottomRight)->setSize(300, 50);

manual_ip = new GuiTextEntry(this, "IP", "");
manual_ip->setPosition(-50, -120, sp::Alignment::BottomRight)->setSize(300, 50);
manual_ip->callback([this](string text) {
selected_server.reset();
});
manual_ip->enterCallback([this](string text) {
connect(text);
});
server_list = new GuiListbox(this, "SERVERS", [this](int index, string value) {
manual_ip->setText(value);
server_list_box = new GuiListbox(this, "SERVERS", [this](int index, string value) {
if (value == "last_server") {
manual_ip->setText(PreferencesManager::get("last_server", ""));
selected_server.reset();
} else {
selected_server = server_list[value.toInt()];
manual_ip->setText(selected_server.value().address.getHumanReadable()[0]);
}
});
if (PreferencesManager::get("last_server", "") != "") {
server_list->addEntry(tr("Last Session ({last})").format({{"last", PreferencesManager::get("last_server", "")}}),
PreferencesManager::get("last_server", ""));
}
scanner->addCallbacks([this](const ServerScanner::ServerInfo& info) {
//New server found
if (info.address.getHumanReadable().empty()) return;
auto addr_str = info.address.getHumanReadable()[0];
server_list->addEntry(info.name + " (" + addr_str + ")", addr_str);

server_list.push_back(info);
updateServerList();
if (manual_ip->getText() == "")
manual_ip->setText(addr_str);

manual_ip->setText(info.address.getHumanReadable()[0]);
}, [this](const ServerScanner::ServerInfo& info) {
//Server removed from list
if (info.address.getHumanReadable().empty()) return;
auto addr_str = info.address.getHumanReadable()[0];
server_list->removeEntry(server_list->indexByValue(addr_str));
server_list.erase(std::remove_if(server_list.begin(), server_list.end(), [&info](const ServerScanner::ServerInfo& entry){
return info.type == entry.type && info.address == entry.address && info.port == entry.port;
}), server_list.end());
});
server_list->setPosition(0, 50, sp::Alignment::TopCenter)->setSize(700, 600);
server_list_box->setPosition(0, 50, sp::Alignment::TopCenter)->setSize(700, 600);
updateServerList();
}

void ServerBrowserMenu::updateServerList()
{
server_list_box->setOptions({});
if (PreferencesManager::get("last_server", "") != "") {
server_list_box->addEntry(tr("Last Session ({last})").format({{"last", PreferencesManager::get("last_server", "")}}), "last_server");
}
std::stable_sort(server_list.begin(), server_list.end(), [](const auto& a, const auto& b) {
//Sort by type, then by server name, and finally by IP address (prefering short addresses first)
if (a.type == b.type && a.name == b.name) {
auto aa = a.address.getHumanReadable()[0];
auto ba = b.address.getHumanReadable()[0];
if (aa.size() == ba.size())
return aa < ba;
return aa.size() < ba.size();
}
if (a.type == b.type)
return a.name < b.name;
return a.type < b.type;
});
for(int idx = 0; idx < int(server_list.size()); idx++) {
const auto& entry = server_list[idx];
auto label = entry.name + " (" + entry.address.getHumanReadable()[0] + ")";
switch(entry.type) {
case ServerScanner::ServerType::Manual:
break;
case ServerScanner::ServerType::LAN:
label = "LAN: " + label;
break;
case ServerScanner::ServerType::MasterServer:
label = "Internet: " + label;
break;
case ServerScanner::ServerType::SteamFriend:
label = "Steam: " + entry.name;
break;
}
server_list_box->addEntry(label, string(idx));
}
}


ServerBrowserMenu::~ServerBrowserMenu()
{
scanner->destroy();
Expand All @@ -119,15 +157,16 @@ void ServerBrowserMenu::connect(string host)
port = host.substr(host.find(":") + 1).toInt64();
host = host.substr(0, host.find(":"));
}
#ifdef STEAMSDK
if (host.lower() == "steam")
{
new JoinServerScreen(lan_internet_selector->getSelectionIndex() == 0 ? Local : Internet, port);
} else {
#endif
new JoinServerScreen(lan_internet_selector->getSelectionIndex() == 0 ? Local : Internet, sp::io::network::Address(host), port);
#ifdef STEAMSDK
}
#endif
ServerScanner::ServerInfo info;
info.type = ServerScanner::ServerType::Manual;
info.name = host;
info.port = port;
info.address = sp::io::network::Address(host);
connect(info);
}

void ServerBrowserMenu::connect(const ServerScanner::ServerInfo& info)
{
new JoinServerScreen(info);
destroy();
}
19 changes: 9 additions & 10 deletions src/menus/serverBrowseMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "gui/gui2_canvas.h"
#include "multiplayer_client.h"
#include "multiplayer_server_scanner.h"
#include <optional>

class GuiTextEntry;
Expand All @@ -14,23 +15,21 @@ class ServerScanner;
class ServerBrowserMenu : public GuiCanvas
{
public:
enum SearchSource
{
Local,
Internet
};
ServerBrowserMenu(std::optional<GameClient::DisconnectReason> last_attempt = {});
virtual ~ServerBrowserMenu();

private:
GuiTextEntry* manual_ip;
GuiButton* connect_button;
GuiListbox* server_list;
GuiSelector* lan_internet_selector;
GuiListbox* server_list_box;

P<ServerScanner> scanner;
std::vector<ServerScanner::ServerInfo> server_list;
std::optional<ServerScanner::ServerInfo> selected_server;

void updateServerList();
void connect(string host);
public:
ServerBrowserMenu(SearchSource source, std::optional<GameClient::DisconnectReason> last_attempt = {});
virtual ~ServerBrowserMenu();
void connect(const ServerScanner::ServerInfo& info);
};

#endif//SERVER_BROWSE_MENU_H

0 comments on commit ebcb2c6

Please sign in to comment.