MP: prompt for server address before loading game config

This allows players to cancel and return to titlescreen quicker if they so choose.
This commit is contained in:
Charles Dang 2017-11-06 18:28:29 +11:00
parent e226ef324a
commit 9019074182
3 changed files with 30 additions and 23 deletions

View file

@ -24,7 +24,6 @@
#include "gettext.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/multiplayer/lobby.hpp"
#include "gui/dialogs/multiplayer/mp_connect.hpp"
#include "gui/dialogs/multiplayer/mp_create_game.hpp"
#include "gui/dialogs/multiplayer/mp_join_game.hpp"
#include "gui/dialogs/multiplayer/mp_login.hpp"
@ -48,34 +47,24 @@ static lg::log_domain log_mp("mp/main");
#define DBG_MP LOG_STREAM(debug, log_mp)
/** Opens a new server connection and prompts the client for login credentials, if necessary. */
static wesnothd_connection_ptr open_connection(CVideo& video, const std::string& original_host)
static wesnothd_connection_ptr open_connection(CVideo& video, std::string host)
{
DBG_MP << "opening connection" << std::endl;
std::string h = original_host;
if(h.empty()) {
gui2::dialogs::mp_connect dlg;
dlg.show(video);
if(dlg.get_retval() != gui2::window::OK) {
return wesnothd_connection_ptr();
}
h = preferences::network_host();
}
wesnothd_connection_ptr sock;
const int colon_index = h.find_first_of(":");
std::string host;
if(host.empty()) {
return sock;
}
const int colon_index = host.find_first_of(":");
unsigned int port;
if(colon_index == -1) {
host = h;
port = 15000;
} else {
host = h.substr(0, colon_index);
port = lexical_cast_default<unsigned int>(h.substr(colon_index + 1), 15000);
port = lexical_cast_default<unsigned int>(host.substr(colon_index + 1), 15000);
host = host.substr(0, colon_index);
}
// shown_hosts is used to prevent the client being locked in a redirect loop.
@ -315,10 +304,6 @@ static wesnothd_connection_ptr open_connection(CVideo& video, const std::string&
} // end login loop
} while(!(data.child("join_lobby") || data.child("join_game")));
if(h != preferences::server_list().front().address) {
preferences::set_network_host(h);
}
if(data.child("join_lobby")) {
return sock;
}

View file

@ -31,6 +31,7 @@
#include "gui/dialogs/language_selection.hpp" // for language_selection
#include "gui/dialogs/loading_screen.hpp"
#include "gui/dialogs/message.hpp" //for show error message
#include "gui/dialogs/multiplayer/mp_connect.hpp"
#include "gui/dialogs/multiplayer/mp_host_game_prompt.hpp" //for host game prompt
#include "gui/dialogs/multiplayer/mp_method_selection.hpp"
#include "gui/dialogs/outro.hpp"
@ -826,6 +827,21 @@ bool game_launcher::play_multiplayer(mp_selection res)
}
// If a server address wasn't specified, prompt for it now.
if(multiplayer_server_.empty()) {
if(!gui2::dialogs::mp_connect::execute(CVideo::get_singleton())) {
return false;
}
// The prompt saves its input to preferences.
multiplayer_server_ = preferences::network_host();
if(multiplayer_server_ != preferences::server_list().front().address) {
preferences::set_network_host(multiplayer_server_);
}
}
//create_engine already calls game_config_manager::get()->load_config but maybe its better to have MULTIPLAYER defined while we are in the lobby.
game_config_manager::get()->load_game_config_for_create(true);

View file

@ -29,6 +29,12 @@ class mp_connect : public modal_dialog
public:
mp_connect();
/** The execute function. See @ref modal_dialog for more information. */
static bool execute(CVideo& video)
{
return mp_connect().show(video);
}
private:
/** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const override;