From 2c6883842e5d0bfb7b6fce68bc0568525f42d607 Mon Sep 17 00:00:00 2001 From: Benau Date: Sun, 15 Apr 2018 14:03:26 +0800 Subject: [PATCH] Add connect-now equivalent to online screen Specify port to allow to connect to one of multiple servers in same machine --- data/gui/online/online.stkgui | 7 ++- src/states_screens/online_screen.cpp | 64 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/data/gui/online/online.stkgui b/data/gui/online/online.stkgui index dc7302a50..d10cce826 100644 --- a/data/gui/online/online.stkgui +++ b/data/gui/online/online.stkgui @@ -23,10 +23,13 @@ + I18N="Networking menu button" text="Local networking"/> + I18N="Networking menu button" text="Global networking"/> + diff --git a/src/states_screens/online_screen.cpp b/src/states_screens/online_screen.cpp index 54bfd8760..ecc75aef4 100644 --- a/src/states_screens/online_screen.cpp +++ b/src/states_screens/online_screen.cpp @@ -20,19 +20,26 @@ #include "config/player_manager.hpp" #include "config/user_config.hpp" +#include "guiengine/message_queue.hpp" #include "guiengine/widgets/check_box_widget.hpp" #include "guiengine/widgets/label_widget.hpp" #include "guiengine/widgets/list_widget.hpp" #include "guiengine/widgets/ribbon_widget.hpp" #include "input/device_manager.hpp" +#include "network/protocols/connect_to_server.hpp" +#include "network/protocols/client_lobby.hpp" #include "network/network_config.hpp" +#include "network/server.hpp" +#include "network/stk_host.hpp" #include "online/request_manager.hpp" +#include "states_screens/networking_lobby.hpp" #include "states_screens/online_lan.hpp" #include "states_screens/online_profile_achievements.hpp" #include "states_screens/online_profile_servers.hpp" #include "states_screens/online_screen.hpp" #include "states_screens/state_manager.hpp" #include "states_screens/user_screen.hpp" +#include "states_screens/dialogs/general_text_field_dialog.hpp" #include "states_screens/dialogs/message_dialog.hpp" #include "utils/string_utils.hpp" @@ -134,6 +141,13 @@ void OnlineScreen::onUpdate(float delta) m_online->setLabel(PlayerManager::getCurrentOnlineId() ? m_online_string : m_login_string); + // In case for entering server address finished + if (auto lb = LobbyProtocol::get()) + { + NetworkingLobby::getInstance()->setJoinedServer(nullptr); + StateManager::get()->resetAndSetStack( + NetworkConfig::get()->getResetScreens(true/*lobby*/).data()); + } } // onUpdate // ---------------------------------------------------------------------------- @@ -209,6 +223,56 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name, UserScreen::getInstance()->push(); } } + else if (selection == "enter-address") + { + if (NetworkConfig::get()->isAddingNetworkPlayers()) + { + core::stringw msg = + _("No player available for connecting to server."); + MessageQueue::add(MessageQueue::MT_ERROR, msg); + return; + } + core::stringw instruction = + _("Enter the server address with IP followed by : and then port."); + new GeneralTextFieldDialog(instruction.c_str(), + [](const irr::core::stringw& text) {}, + [](GUIEngine::LabelWidget* lw, GUIEngine::TextBoxWidget* tb)->bool + { + TransportAddress server_addr( + StringUtils::wideToUtf8(tb->getText())); + if (server_addr.isUnset()) + { + core::stringw err = _("Invalid server address: %s.", + tb->getText()); + lw->setText(err, true); + return false; + } + NetworkConfig::get()->setIsWAN(); + NetworkConfig::get()->setIsServer(false); + NetworkConfig::get()->setPassword(""); + auto server = std::make_shared(0, L"", 0, 0, 0, 0, + server_addr, false); + STKHost::create(); + auto cts = std::make_shared(server); + cts->setup(); + Log::info("OnlineScreen", "Trying to connect to server '%s'.", + server_addr.toString().c_str()); + if (!cts->handleDirectConnect(10000)) + { + core::stringw err = _("Cannot connect to server %s.", + server_addr.toString().c_str()); + STKHost::get()->shutdown(); + NetworkConfig::get()->unsetNetworking(); + lw->setText(err, true); + return false; + } + + auto cl = LobbyProtocol::create(); + cl->setAddress(server_addr); + cl->requestStart(); + return true; + }); + } } // eventCallback // ----------------------------------------------------------------------------