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) {
192 connection->wait_and_receive_data(data);
199 this->lobby_info.process_gamelist(data);
203 else if(
const auto gamelist_diff = data.
optional_child(
"gamelist_diff")) {
204 this->lobby_info.process_gamelist_diff(*gamelist_diff);
209 for(
const auto& handler : process_handlers) {
223 std::unique_ptr<wesnothd_connection> mp_manager::open_connection(std::string host)
225 DBG_MP <<
"opening connection";
232 std::set<std::pair<std::string, std::string>> shown_hosts;
233 auto addr = shown_hosts.end();
237 }
catch(
const std::runtime_error&) {
238 throw wesnothd_error(
_(
"Invalid address specified for multiplayer server"));
245 auto conn = std::make_unique<wesnothd_connection>(addr->first, addr->second);
248 conn->wait_for_handshake();
257 conn->wait_and_receive_data(data);
263 version = (*reject)[
"accepted_versions"].str();
266 version = data[
"version"].str();
270 i18n_symbols[
"required_version"] = version;
273 const std::string errorstring =
VGETTEXT(
"The server accepts versions '$required_version', but you are using version '$your_version'", i18n_symbols);
279 auto redirect_host = (*redirect)[
"host"].str();
280 auto redirect_port = (*redirect)[
"port"].str(
"15000");
283 std::tie(std::ignore, recorded_host) = shown_hosts.emplace(redirect_host, redirect_port);
293 conn = std::make_unique<wesnothd_connection>(redirect_host, redirect_port);
296 conn->wait_for_handshake();
307 conn->send_data(res);
325 sp[
"username"] =
login;
327 conn->send_data(response);
328 conn->wait_and_receive_data(data);
333 std::string warning_msg;
336 warning_msg =
VGETTEXT(
"The nickname ‘$nick’ is inactive. " 337 "You cannot claim ownership of this nickname until you " 338 "activate your account via email or ask an " 339 "administrator to do it for you.", {{
"nick", login}});
341 warning_msg = (*warning)[
"message"].str();
344 warning_msg +=
"\n\n";
345 warning_msg +=
_(
"Do you want to continue?");
362 const bool fall_through = (*error)[
"force_confirmation"].to_bool()
373 if(!(*error)[
"password_request"].empty() && !password.empty() && !fall_through && (conn->using_tls() ||
game_config::allow_insecure)) {
382 conn->send_data(response);
383 conn->wait_and_receive_data(data);
398 std::string error_message;
400 i18n_symbols[
"nick"] =
login;
402 const auto extra_data = error->optional_child(
"data");
407 const std::string ec = (*error)[
"error_code"];
410 error_message =
_(
"The remote server requested a password while using an insecure connection.");
412 error_message =
_(
"You must login first.");
414 error_message =
VGETTEXT(
"The nickname ‘$nick’ is already taken.", i18n_symbols);
416 error_message =
VGETTEXT(
"The nickname ‘$nick’ contains invalid " 417 "characters. Only alpha-numeric characters (one at minimum), underscores and " 418 "hyphens are allowed.", i18n_symbols);
420 error_message =
VGETTEXT(
"The nickname ‘$nick’ is too long. Nicks must be 20 characters or less.", i18n_symbols);
422 error_message =
VGETTEXT(
"The nickname ‘$nick’ is reserved and cannot be used by players.", i18n_symbols);
424 error_message =
VGETTEXT(
"The nickname ‘$nick’ is not registered on this server.", i18n_symbols)
425 +
_(
" This server disallows unregistered nicknames.");
428 error_message =
VGETTEXT(
"The nickname ‘$nick’ is banned on this server’s forums for $duration|.", i18n_symbols);
430 error_message =
VGETTEXT(
"The nickname ‘$nick’ is banned on this server’s forums.", i18n_symbols);
434 error_message =
VGETTEXT(
"Your IP address is banned on this server’s forums for $duration|.", i18n_symbols);
436 error_message =
_(
"Your IP address is banned on this server’s forums.");
440 error_message =
VGETTEXT(
"The email address for the nickname ‘$nick’ is banned on this server’s forums for $duration|.", i18n_symbols);
442 error_message =
VGETTEXT(
"The email address for the nickname ‘$nick’ is banned on this server’s forums.", i18n_symbols);
445 error_message =
VGETTEXT(
"The nickname ‘$nick’ is registered on this server.", i18n_symbols);
447 error_message =
VGETTEXT(
"The nickname ‘$nick’ is registered on this server.", i18n_symbols)
448 +
"\n\n" +
_(
"WARNING: There is already a client using this nickname, " 449 "logging in will cause that client to be kicked!");
451 error_message =
_(
"The password you provided was incorrect.");
453 error_message =
_(
"You have made too many login attempts.");
455 error_message =
_(
"Password hashing failed.");
457 error_message = (*error)[
"message"].str();
484 session_info = { join_lobby.value() };
494 void mp_manager::run_lobby_loop()
503 while(!enter_lobby_mode()) {
508 lobby_info.refresh_installed_addons_cache();
510 connection->send_data(
config(
"refresh_lobby"));
514 bool mp_manager::enter_lobby_mode()
516 DBG_MP <<
"entering lobby mode";
524 for(
const config&
i : cfg.child_range(
"music")) {
535 int dlg_joined_game_id = 0;
565 connection->send_data(
config(
"refresh_lobby"));
572 void mp_manager::enter_create_mode()
574 DBG_MP <<
"entering create mode";
576 if(gui2::dialogs::mp_create_game::execute(state, connection ==
nullptr)) {
577 enter_staging_mode();
578 }
else if(connection) {
579 connection->send_data(
config(
"refresh_lobby"));
583 void mp_manager::enter_staging_mode()
585 DBG_MP <<
"entering connect mode";
587 std::unique_ptr<mp_game_metadata> metadata;
591 metadata = std::make_unique<mp_game_metadata>(*connection);
599 dlg_ok = gui2::dialogs::mp_staging::execute(connect_engine, connection.get());
609 connection->send_data(
config(
"leave_game"));
613 void mp_manager::enter_wait_mode(
int game_id,
bool observe)
615 DBG_MP <<
"entering wait mode";
625 if(
const mp::game_info* gi = lobby_info.get_game_by_id(game_id)) {
639 connection->send_data(
config(
"leave_game"));
652 connection->send_data(
config(
"leave_game"));
657 return gui2::dialogs::mp_staging::execute(engine, connection.get());
660 bool mp_manager::post_scenario_wait(
bool observe)
665 connection->send_data(
config(
"leave_game"));
682 DBG_MP <<
"starting client";
683 mp_manager(host).run_lobby_loop();
688 DBG_MP <<
"starting local game";
692 mp_manager(std::nullopt).enter_create_mode();
697 DBG_MP <<
"starting local MP game from commandline";
706 DBG_MP <<
"entering create mode";
716 parameters.
name =
"multiplayer_The_Freelands";
726 DBG_MP <<
"ignoring map settings";
754 if(
const config& cfg_multiplayer = game_config.
find_child(
"multiplayer",
"id", parameters.
name)) {
757 PLAIN_LOG <<
"Could not find [multiplayer] '" << parameters.
name <<
"'";
777 DBG_MP <<
"entering connect mode";
794 for(
unsigned int i = 0;
i < repeat;
i++){
803 return manager && manager->post_scenario_staging(engine);
808 return manager && manager->post_scenario_wait(observe);
813 return manager && manager->get_session_info().is_moderator;
819 const std::string& prefix = manager->get_session_info().profile_url_prefix;
821 if(!prefix.empty()) {
822 return prefix + std::to_string(user_id);
831 if(manager && manager->connection) {
832 manager->connection->send_data(data);
839 remove_handler = manager->add_network_handler(func);
852 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)
Report what is being loaded to the loading screen.
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.