54 #define DBG_MP LOG_STREAM(debug, log_mp) 55 #define LOG_MP LOG_STREAM(info, log_mp) 56 #define WRN_MP LOG_STREAM(warn, log_mp) 57 #define ERR_MP LOG_STREAM(err, log_mp) 64 class mp_manager* manager =
nullptr;
75 mp_manager(
const std::optional<std::string> host);
82 if(network_worker.joinable()) {
84 network_worker.join();
94 void run_lobby_loop();
98 bool post_scenario_wait(
bool observe);
102 struct session_metadata
104 session_metadata() =
default;
106 session_metadata(
const config& cfg)
107 : is_moderator(cfg[
"is_moderator"].to_bool(
false))
108 , profile_url_prefix(cfg[
"profile_url_prefix"].str())
113 bool is_moderator =
false;
116 std::string profile_url_prefix;
120 std::unique_ptr<wesnothd_connection> open_connection(std::string host);
123 bool enter_lobby_mode();
126 void enter_create_mode();
129 void enter_staging_mode();
132 void enter_wait_mode(
int game_id,
bool observe);
135 std::thread network_worker;
138 std::atomic_bool stop;
141 std::unique_ptr<wesnothd_connection> connection;
144 session_metadata session_info;
151 std::list<mp::network_registrar::handler> process_handlers;
154 const session_metadata& get_session_info()
const 159 auto add_network_handler(decltype(process_handlers)::value_type func)
161 return [
this, iter = process_handlers.insert(process_handlers.end(), func)]() { process_handlers.erase(iter); };
165 mp_manager::mp_manager(
const std::optional<std::string> host)
168 , connection(
nullptr)
174 state.classification().type = campaign_type::type::multiplayer;
178 connection = open_connection(*host);
183 if(connection ==
nullptr) {
189 std::promise<void> received_initial_gamelist;
191 network_worker = std::thread([
this, &received_initial_gamelist]() {
195 connection->wait_and_receive_data(data);
202 this->lobby_info.process_gamelist(data);
205 received_initial_gamelist.set_value();
208 }
catch(
const std::future_error&
e) {
209 if(e.code() == std::future_errc::promise_already_satisfied) {
215 else if(
const auto gamelist_diff = data.
optional_child(
"gamelist_diff")) {
216 this->lobby_info.process_gamelist_diff(*gamelist_diff);
221 for(
const auto& handler : process_handlers) {
229 received_initial_gamelist.get_future().wait();
239 std::unique_ptr<wesnothd_connection> mp_manager::open_connection(std::string host)
241 DBG_MP <<
"opening connection" << std::endl;
248 std::set<std::pair<std::string, std::string>> shown_hosts;
249 auto addr = shown_hosts.end();
253 }
catch(
const std::runtime_error&) {
254 throw wesnothd_error(
_(
"Invalid address specified for multiplayer server"));
261 auto conn = std::make_unique<wesnothd_connection>(addr->first, addr->second);
264 conn->wait_for_handshake();
273 conn->wait_and_receive_data(data);
279 version = (*reject)[
"accepted_versions"].str();
282 version = data[
"version"].str();
286 i18n_symbols[
"required_version"] = version;
289 const std::string errorstring =
VGETTEXT(
"The server accepts versions '$required_version', but you are using version '$your_version'", i18n_symbols);
295 auto redirect_host = (*redirect)[
"host"].str();
296 auto redirect_port = (*redirect)[
"port"].str(
"15000");
299 std::tie(std::ignore, recorded_host) = shown_hosts.emplace(redirect_host, redirect_port);
309 conn = std::make_unique<wesnothd_connection>(redirect_host, redirect_port);
312 conn->wait_for_handshake();
323 conn->send_data(res);
341 sp[
"username"] =
login;
343 conn->send_data(response);
344 conn->wait_and_receive_data(data);
349 std::string warning_msg;
352 warning_msg =
VGETTEXT(
"The nickname ‘$nick’ is inactive. " 353 "You cannot claim ownership of this nickname until you " 354 "activate your account via email or ask an " 355 "administrator to do it for you.", {{
"nick", login}});
357 warning_msg = (*warning)[
"message"].str();
360 warning_msg +=
"\n\n";
361 warning_msg +=
_(
"Do you want to continue?");
378 const bool fall_through = (*error)[
"force_confirmation"].to_bool()
389 if(!(*error)[
"password_request"].empty() && !password.empty() && !fall_through && (conn->using_tls() ||
game_config::allow_insecure)) {
398 conn->send_data(response);
399 conn->wait_and_receive_data(data);
414 std::string error_message;
416 i18n_symbols[
"nick"] =
login;
418 const auto extra_data = error->optional_child(
"data");
423 const std::string ec = (*error)[
"error_code"];
426 error_message =
_(
"The remote server requested a password while using an insecure connection.");
428 error_message =
_(
"You must login first.");
430 error_message =
VGETTEXT(
"The nickname ‘$nick’ is already taken.", i18n_symbols);
432 error_message =
VGETTEXT(
"The nickname ‘$nick’ contains invalid " 433 "characters. Only alpha-numeric characters (one at minimum), underscores and " 434 "hyphens are allowed.", i18n_symbols);
436 error_message =
VGETTEXT(
"The nickname ‘$nick’ is too long. Nicks must be 20 characters or less.", i18n_symbols);
438 error_message =
VGETTEXT(
"The nickname ‘$nick’ is reserved and cannot be used by players.", i18n_symbols);
440 error_message =
VGETTEXT(
"The nickname ‘$nick’ is not registered on this server.", i18n_symbols)
441 +
_(
" This server disallows unregistered nicknames.");
444 error_message =
VGETTEXT(
"The nickname ‘$nick’ is banned on this server’s forums for $duration|.", i18n_symbols);
446 error_message =
VGETTEXT(
"The nickname ‘$nick’ is banned on this server’s forums.", i18n_symbols);
450 error_message =
VGETTEXT(
"Your IP address is banned on this server’s forums for $duration|.", i18n_symbols);
452 error_message =
_(
"Your IP address is banned on this server’s forums.");
456 error_message =
VGETTEXT(
"The email address for the nickname ‘$nick’ is banned on this server’s forums for $duration|.", i18n_symbols);
458 error_message =
VGETTEXT(
"The email address for the nickname ‘$nick’ is banned on this server’s forums.", i18n_symbols);
461 error_message =
VGETTEXT(
"The nickname ‘$nick’ is registered on this server.", i18n_symbols);
463 error_message =
VGETTEXT(
"The nickname ‘$nick’ is registered on this server.", i18n_symbols)
464 +
"\n\n" +
_(
"WARNING: There is already a client using this nickname, " 465 "logging in will cause that client to be kicked!");
467 error_message =
_(
"The password you provided was incorrect.");
469 error_message =
_(
"You have made too many login attempts.");
471 error_message =
_(
"Password hashing failed.");
473 error_message = (*error)[
"message"].str();
500 session_info = { join_lobby.value() };
510 void mp_manager::run_lobby_loop()
519 while(!enter_lobby_mode()) {
524 lobby_info.refresh_installed_addons_cache();
526 connection->send_data(
config(
"refresh_lobby"));
530 bool mp_manager::enter_lobby_mode()
532 DBG_MP <<
"entering lobby mode" << std::endl;
540 for(
const config&
i : cfg.child_range(
"music")) {
551 int dlg_joined_game_id = 0;
581 connection->send_data(
config(
"refresh_lobby"));
588 void mp_manager::enter_create_mode()
590 DBG_MP <<
"entering create mode" << std::endl;
592 if(gui2::dialogs::mp_create_game::execute(state, connection ==
nullptr)) {
593 enter_staging_mode();
594 }
else if(connection) {
595 connection->send_data(
config(
"refresh_lobby"));
599 void mp_manager::enter_staging_mode()
601 DBG_MP <<
"entering connect mode" << std::endl;
603 std::unique_ptr<mp_game_metadata> metadata;
607 metadata = std::make_unique<mp_game_metadata>(*connection);
615 dlg_ok = gui2::dialogs::mp_staging::execute(connect_engine, connection.get());
625 connection->send_data(
config(
"leave_game"));
629 void mp_manager::enter_wait_mode(
int game_id,
bool observe)
631 DBG_MP <<
"entering wait mode" << std::endl;
641 if(
const mp::game_info* gi = lobby_info.get_game_by_id(game_id)) {
655 connection->send_data(
config(
"leave_game"));
668 connection->send_data(
config(
"leave_game"));
673 return gui2::dialogs::mp_staging::execute(engine, connection.get());
676 bool mp_manager::post_scenario_wait(
bool observe)
681 connection->send_data(
config(
"leave_game"));
698 DBG_MP <<
"starting client" << std::endl;
699 mp_manager(host).run_lobby_loop();
704 DBG_MP <<
"starting local game" << std::endl;
708 mp_manager(std::nullopt).enter_create_mode();
713 DBG_MP <<
"starting local MP game from commandline" << std::endl;
722 DBG_MP <<
"entering create mode" << std::endl;
732 parameters.
name =
"multiplayer_The_Freelands";
742 DBG_MP <<
"ignoring map settings" << std::endl;
770 if(
const config& cfg_multiplayer = game_config.
find_child(
"multiplayer",
"id", parameters.
name)) {
773 std::cerr <<
"Could not find [multiplayer] '" << parameters.
name <<
"'\n";
793 DBG_MP <<
"entering connect mode" << std::endl;
810 for(
unsigned int i = 0;
i < repeat;
i++){
819 return manager && manager->post_scenario_staging(engine);
824 return manager && manager->post_scenario_wait(observe);
829 return manager && manager->get_session_info().is_moderator;
835 const std::string& prefix = manager->get_session_info().profile_url_prefix;
837 if(!prefix.empty()) {
838 return prefix + std::to_string(user_id);
847 if(manager && manager->connection) {
848 manager->connection->send_data(data);
855 remove_handler = manager->add_network_handler(func);
868 return manager ? &manager->
lobby_info :
nullptr;
#define MP_HASHING_PASSWORD_FAILED
An error occurred during when trying to communicate with the wesnothd server.
Dialog was closed with the CANCEL button.
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Error used when the client is rejected by the MP server.
std::map< std::string, t_string > string_map
#define MP_NAME_AUTH_BAN_USER_ERROR
void add_log_data(const std::string &key, const std::string &var)
#define MP_TOO_MANY_ATTEMPTS_ERROR
level_result::type play_game()
bool has_attribute(config_key_type key) const
This class represents the info a client has about a game on the server.
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Shows an ok and cancel button.
static void progress(loading_stage stage=loading_stage::none)
void load_game_config_for_game(const game_classification &classification, const std::string &scenario_id)
void expand_random_scenario()
takes care of generate_map=, generate_scenario=, map= attributes This should be called before expandi...
This class represents the collective information the client has about the players and games on the se...
bool multiplayer_ignore_map_settings
True if –ignore-map-settings was given at the command line.
int get_village_gold(const std::string &value, const game_classification *classification)
Gets the village gold.
std::optional< std::string > multiplayer_label
Non-empty if –label was given on the command line.
std::pair< std::string, std::string > parse_network_address(const std::string &address, const std::string &default_port)
Parse a host:port style network address, supporting [] notation for ipv6 addresses.
bool goto_mp_wait(bool observe)
Opens the MP Join Game screen and sets the game state according to the changes made.
#define MP_NAME_TOO_LONG_ERROR
Define the errors the server may send during the login procedure.
static std::string _(const char *str)
bool show(const unsigned auto_close_time=0)
Shows the window.
void call_in_main_thread(const std::function< void(void)> &f)
std::string get_scenario_id() const
#define MP_INCORRECT_PASSWORD_ERROR
int get_village_support(const std::string &value)
Gets the village unit level support.
bool logged_in_as_moderator()
Gets whether the currently logged-in user is a moderator.
Main entry points of multiplayer mode.
std::string get_profile_link(int user_id)
Gets the forum profile link for the given user.
static game_config_manager * get()
#define MP_NAME_UNREGISTERED_ERROR
std::function< void(const config &)> handler
std::string label
What to show in the filter's drop-down list.
void start_game_commandline(const commandline_options &cmdline_opts, const game_config_view &game_config)
void expand_mp_options()
adds values of [option]s into [carryover_sides_start][variables] so that they are applied in the next...
void start_local_game_commandline(const commandline_options &cmdline_opts)
Starts a multiplayer game in single-user mode using command line settings.
std::string dist_channel_id()
Return the distribution channel identifier, or "Default" if missing.
const game_config_view & game_config() const
config & get_starting_point()
Shows a yes and no button.
utils::optional_reference< config > optional_child(config_key_type key, int n=0)
Euivalent to child, but returns an empty optional if the nth child was not found. ...
void set_message_private(bool value)
General settings and defaults for scenarios.
std::string format_timespan(std::time_t time, bool detailed)
Formats a timespan into human-readable text for player authentication functions.
std::string era_define
If there is a define the era uses to customize data.
network_registrar(handler func)
std::optional< std::string > multiplayer_era
Non-empty if –era was given on the command line.
void load_game_config_for_create(bool is_mp, bool is_test=false)
Game configuration data as global variables.
std::string scenario_define
If there is a define the scenario uses to customize data.
std::string password(const std::string &server, const std::string &login)
This shows the dialog to log in to the MP server.
int get_xp_modifier(const std::string &value)
Gets the xp modifier.
void expand_mp_events()
adds [event]s from [era] and [modification] into this scenario does NOT expand [option]s because vari...
#define MP_PASSWORD_REQUEST
static lg::log_domain log_mp("mp/main")
#define MP_NAME_INACTIVE_WARNING
const version_info wesnoth_version(VERSION)
config & add_child(config_key_type key)
std::optional< std::string > multiplayer_scenario
Non-empty if –scenario was given on the command line.
const config & find_child(config_key_type key, const std::string &name, const std::string &value) const
#define MP_PASSWORD_REQUEST_FOR_LOGGED_IN_NAME
#define MP_NAME_TAKEN_ERROR
int get_retval() const
Returns the cached window exit code.
static void display(std::function< void()> f)
void play_music_config(const config &music_node, bool allow_interrupt_current_track, int i)
game_classification & classification()
void set_carryover_sides_start(config carryover_sides_start)
void set_mp_info(mp_game_metadata *mp_info)
Standard logging facilities (interface).
std::string str() const
Serializes the version number into string form.
lobby_info * get_lobby_info()
Returns the lobby_info object for the given session.
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
bool goto_mp_staging(ng::connect_engine &engine)
Opens the MP Staging screen and sets the game state according to the changes made.
void send_to_server(const config &data)
Attempts to send given data to server if a connection is open.
void reload_changed_game_config()
std::optional< unsigned int > multiplayer_repeat
Repeats specified by –multiplayer-repeat option.
#define MP_NAME_RESERVED_ERROR
void commit_music_changes()
Dialog was closed with the OK button.
A config object defines a single node in a WML file, with access to child nodes.
#define MP_NAME_AUTH_BAN_EMAIL_ERROR
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
void start_client(const std::string &host)
Pubic entry points for the MP workflow.
int get_turns(const std::string &value)
Gets the number of turns.
#define MP_NAME_AUTH_BAN_IP_ERROR
#define MP_INVALID_CHARS_IN_NAME_ERROR
void start_local_game()
Starts a multiplayer game in single-user mode.