Skip to content

Commit

Permalink
MP: prompt for server address before loading game config
Browse files Browse the repository at this point in the history
This allows players to cancel and return to titlescreen quicker if they so choose.
  • Loading branch information
Vultraz authored and GregoryLundberg committed Nov 30, 2017
1 parent 6f4b86c commit c4cbb31
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
29 changes: 7 additions & 22 deletions src/game_initialization/multiplayer.cpp
Expand Up @@ -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"
Expand All @@ -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();
}
wesnothd_connection_ptr sock;

h = preferences::network_host();
if(host.empty()) {
return sock;
}

wesnothd_connection_ptr sock;

const int colon_index = h.find_first_of(":");
std::string host;
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.
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions src/game_launcher.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/gui/dialogs/multiplayer/mp_connect.hpp
Expand Up @@ -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;
Expand Down

0 comments on commit c4cbb31

Please sign in to comment.