The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
lobby.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2009 - 2025
3 by Tomasz Sniatowski <kailoran@gmail.com>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#define GETTEXT_DOMAIN "wesnoth-lib"
17
19
25
26#include "gui/core/timer.hpp"
28#include "gui/widgets/image.hpp"
29#include "gui/widgets/label.hpp"
38
39#include "addon/client.hpp"
40#include "addon/manager.hpp"
41#include "addon/manager_ui.hpp"
42#include "chat_log.hpp"
43#include "desktop/open.hpp"
45#include "font/pango/escape.hpp"
46#include "formatter.hpp"
49#include "gettext.hpp"
50#include "help/help.hpp"
52
53#include <functional>
54
55static lg::log_domain log_lobby("lobby");
56#define DBG_LB LOG_STREAM(debug, log_lobby)
57#define LOG_LB LOG_STREAM(info, log_lobby)
58#define ERR_LB LOG_STREAM(err, log_lobby)
59#define SCOPE_LB log_scope2(log_lobby, __func__)
60
61namespace gui2::dialogs
62{
63REGISTER_DIALOG(mp_lobby)
64
65bool mp_lobby::logout_prompt()
66{
67 return show_prompt(_("Do you really want to log out?"));
68}
69
70mp_lobby::mp_lobby(mp::lobby_info& info, wesnothd_connection& connection, int& joined_game)
71 : modal_dialog(window_id())
72 , quit_confirmation(&mp_lobby::logout_prompt)
73 , gamelistbox_(nullptr)
74 , lobby_info_(info)
75 , chatbox_(nullptr)
76 , filter_friends_(register_bool("filter_with_friends",
77 true,
78 []() {return prefs::get().fi_friends_in_game();},
79 [](bool v) {prefs::get().set_fi_friends_in_game(v);},
80 std::bind(&mp_lobby::update_gamelist_filter, this)))
81 , filter_ignored_(register_bool("filter_with_ignored",
82 true,
83 []() {return prefs::get().fi_blocked_in_game();},
84 [](bool v) {prefs::get().set_fi_blocked_in_game(v);},
85 std::bind(&mp_lobby::update_gamelist_filter, this)))
86 , filter_slots_(register_bool("filter_vacant_slots",
87 true,
88 []() {return prefs::get().fi_vacant_slots();},
89 [](bool v) {prefs::get().set_fi_vacant_slots(v);},
90 std::bind(&mp_lobby::update_gamelist_filter, this)))
91 , filter_invert_(register_bool("filter_invert",
92 true,
93 []() {return prefs::get().fi_invert();},
94 [](bool v) {prefs::get().set_fi_invert(v);},
95 std::bind(&mp_lobby::update_gamelist_filter, this)))
96 , filter_auto_hosted_(false)
97 , filter_game_presets_(false)
98 , filter_text_(nullptr)
99 , selected_game_id_()
100 , player_list_(std::bind(&mp_lobby::user_dialog_callback, this, std::placeholders::_1),
101 std::bind(&mp_lobby::select_game_by_player, this, std::placeholders::_1))
102 , player_list_dirty_(true)
103 , gamelist_dirty_(true)
104 , last_lobby_update_()
105 , gamelist_diff_update_(true)
106 , network_connection_(connection)
107 , lobby_update_timer_(0)
108 , gamelist_id_at_row_()
109 , delay_playerlist_update_(false)
110 , delay_gamelist_update_(false)
111 , joined_game_id_(joined_game)
112 , queue_game_server_preset_()
113{
114 set_show_even_without_video(true);
115 set_allow_plugin_skip(false);
116 set_always_save_fields(true);
117
118 /*** Local hotkeys. ***/
120 [](auto&&...) { help::show_help(); return true; });
121
123 [this](auto&&...) { show_preferences_button_callback(); return true; });
124}
125
138
145
146namespace
147{
148void modify_grid_with_data(grid* grid, const widget_data& map)
149{
150 for(const auto& v : map) {
151 const std::string& key = v.first;
152 const widget_item& strmap = v.second;
153
154 widget* w = grid->find(key, false);
155 if(!w) {
156 continue;
157 }
158
159 styled_widget* c = dynamic_cast<styled_widget*>(w);
160 if(!c) {
161 continue;
162 }
163
164 for(const auto & vv : strmap) {
165 if(vv.first == "label") {
166 c->set_label(vv.second);
167 } else if(vv.first == "tooltip") {
168 c->set_tooltip(vv.second);
169 }
170 }
171 }
172}
173
174bool handle_addon_requirements_gui(const std::vector<mp::game_info::required_addon>& reqs, mp::game_info::addon_req addon_outcome)
175{
176 if(addon_outcome == mp::game_info::addon_req::CANNOT_SATISFY) {
177 std::string e_title = _("Incompatible User-made Content");
178 std::string err_msg = _("This game cannot be joined because the host has out-of-date add-ons that are incompatible with your version. You might wish to suggest that the host’s add-ons be updated.");
179
180 err_msg +="\n\n";
181 err_msg += _("Details:");
182 err_msg += "\n";
183
184 for(const mp::game_info::required_addon & a : reqs) {
186 err_msg += font::unicode_bullet + " " + a.message + "\n";
187 }
188 }
189 gui2::show_message(e_title, err_msg, message::auto_close, true);
190
191 return false;
192 } else if(addon_outcome == mp::game_info::addon_req::NEED_DOWNLOAD) {
193 std::string e_title = _("Missing User-made Content");
194 std::string err_msg = _("This game requires one or more user-made addons to be installed or updated in order to join.\nDo you want to try to install them?");
195
196 err_msg +="\n\n";
197 err_msg += _("Details:");
198 err_msg += "\n";
199
200 std::vector<std::string> needs_download;
201 for(const mp::game_info::required_addon & a : reqs) {
203 err_msg += font::unicode_bullet + " " + a.message + "\n";
204
205 needs_download.push_back(a.addon_id);
206 }
207 }
208
209 assert(needs_download.size() > 0);
210
211 if(gui2::show_message(e_title, err_msg, message::yes_no_buttons, true) == gui2::retval::OK) {
212 // Begin download session
213 try {
214 return ad_hoc_addon_fetch_session(needs_download);
215 } catch (const addons_client::user_exit&) {
216 } catch (const addons_client::user_disconnect&) {
217 }
218 }
219 }
220
221 return false;
222}
223
224} // end anonymous namespace
225
227{
228 if(delay_gamelist_update_) return;
229
230 SCOPE_LB;
232 gamelist_id_at_row_.clear();
233
234 const auto finish_state_sync = lobby_info_.begin_state_sync();
235
236 int select_row = -1;
237 for(unsigned i = 0; i < lobby_info_.games().size(); ++i) {
238 const mp::game_info& game = *lobby_info_.games()[i];
239
240 if(game.id == selected_game_id_) {
241 select_row = i;
242 }
243
244 gamelist_id_at_row_.push_back(game.id);
245 LOG_LB << "Adding game to listbox (1)" << game.id;
247
249 }
250
251 if(select_row >= 0 && select_row != gamelistbox_->get_selected_row()) {
252 gamelistbox_->select_row(select_row);
253 }
254
256 gamelist_dirty_ = false;
257 last_lobby_update_ = std::chrono::steady_clock::now();
258 finish_state_sync();
260}
261
263{
264 if(delay_gamelist_update_) return;
265
266 SCOPE_LB;
267 int select_row = -1;
268 unsigned list_i = 0;
269 int list_rows_deleted = 0;
270
271 const auto finish_state_sync = lobby_info_.begin_state_sync();
272
273 std::vector<int> next_gamelist_id_at_row;
274 for(unsigned i = 0; i < lobby_info_.games().size(); ++i) {
275 const mp::game_info& game = *lobby_info_.games()[i];
276
277 if(game.display_status == mp::game_info::disp_status::NEW) {
278 // call void do_notify(notify_mode mode, const std::string& sender, const std::string& message)
279 // sender will be the game_info.scenario (std::string) and message will be game_info.name (std::string)
281 do_notify(mp::notify_mode::game_created, game.scenario, game.name);
282 }
283
284 LOG_LB << "Adding game to listbox " << game.id;
285
286 if(list_i != gamelistbox_->get_item_count()) {
288 DBG_LB << "Added a game listbox row not at the end" << list_i
289 << " " << gamelistbox_->get_item_count();
290 list_rows_deleted--;
291 } else {
293 }
294
297
298 list_i++;
299 next_gamelist_id_at_row.push_back(game.id);
300 } else {
301 if(list_i >= gamelistbox_->get_item_count()) {
302 ERR_LB << "Ran out of listbox items -- triggering a full refresh";
304 return;
305 }
306
307 if(list_i + list_rows_deleted >= gamelist_id_at_row_.size()) {
308 ERR_LB << "gamelist_id_at_row_ overflow! " << list_i << " + "
309 << list_rows_deleted
310 << " >= " << gamelist_id_at_row_.size()
311 << " -- triggering a full refresh";
313 return;
314 }
315
316 int listbox_game_id = gamelist_id_at_row_[list_i + list_rows_deleted];
317 if(game.id != listbox_game_id) {
318 ERR_LB << "Listbox game id does not match expected id "
319 << listbox_game_id << " " << game.id << " (row " << list_i << ")";
321 return;
322 }
323
324 if(game.display_status == mp::game_info::disp_status::UPDATED) {
325 LOG_LB << "Modifying game in listbox " << game.id << " (row " << list_i << ")";
327 modify_grid_with_data(grid, make_game_row_data(game));
329 ++list_i;
330 next_gamelist_id_at_row.push_back(game.id);
331 } else if(game.display_status == mp::game_info::disp_status::DELETED) {
332 LOG_LB << "Deleting game from listbox " << game.id << " (row "
333 << list_i << ")";
334 gamelistbox_->remove_row(list_i);
335 ++list_rows_deleted;
336 } else {
337 // clean
338 LOG_LB << "Clean game in listbox " << game.id << " (row " << list_i << ")";
339 next_gamelist_id_at_row.push_back(game.id);
340 ++list_i;
341 }
342 }
343 }
344
345 for(unsigned i = 0; i < next_gamelist_id_at_row.size(); ++i) {
346 if(next_gamelist_id_at_row[i] == selected_game_id_) {
347 select_row = i;
348 }
349 }
350
351 next_gamelist_id_at_row.swap(gamelist_id_at_row_);
352 if(select_row >= static_cast<int>(gamelistbox_->get_item_count())) {
353 ERR_LB << "Would select a row beyond the listbox" << select_row << " "
355 select_row = gamelistbox_->get_item_count() - 1;
356 }
357
358 if(select_row >= 0 && select_row != gamelistbox_->get_selected_row()) {
359 gamelistbox_->select_row(select_row);
360 }
361
363 gamelist_dirty_ = false;
364 last_lobby_update_ = std::chrono::steady_clock::now();
365 finish_state_sync();
367}
368
370{
371 const std::string games_string = VGETTEXT("Games: showing $num_shown out of $num_total", {
372 {"num_shown", std::to_string(lobby_info_.games_visibility().count())},
373 {"num_total", std::to_string(lobby_info_.games().size())}
374 });
375
376 gamelistbox_->find_widget<label>("map").set_label(games_string);
377
379}
380
382{
384 widget_item item;
385
386 item["use_markup"] = "true";
387
388 color_t color_string;
389 if(game.vacant_slots > 0) {
390 color_string = (game.reloaded || game.started) ? font::YELLOW_COLOR : font::GOOD_COLOR;
391 }
392
393 const std::string scenario_text = VGETTEXT("$game_name (Era: $era_name)", {
394 {"game_name", game.scenario},
395 {"era_name", game.era}
396 });
397
398 item["label"] = game.vacant_slots > 0 ? markup::span_color(color_string, game.name) : game.name;
399 data.emplace("name", item);
400
401 item["label"] = markup::span_color(font::GRAY_COLOR, game.type_marker, markup::italic(font::escape_text(scenario_text)));
402 data.emplace("scenario", item);
403
404 item["label"] = markup::span_color(color_string, game.status);
405 data.emplace("status", item);
406
407 return data;
408}
409
411{
414
415 toggle_panel& row_panel = grid->find_widget<toggle_panel>("panel");
416
417 //
418 // Game info
419 //
420 std::ostringstream ss;
421
422 const auto mark_missing = [&ss]() {
423 ss << ' ' << markup::span_color(font::BAD_COLOR, "(", _("era_or_mod^not installed"), ")");
424 };
425
426 ss << markup::tag("big", markup::span_color(font::TITLE_COLOR, _("Era"))) << "\n" << game.era;
427
428 if(!game.have_era) {
429 // NOTE: not using colorize() here deliberately to avoid awkward string concatenation.
430 mark_missing();
431 }
432
433 ss << "\n\n" << markup::tag("big", markup::span_color(font::TITLE_COLOR, _("Modifications"))) << "\n";
434
435 auto mods = game.mod_info;
436
437 if(mods.empty()) {
438 ss << _("active_modifications^None") << "\n";
439 } else {
440 for(const auto& mod : mods) {
441 ss << mod.first;
442
443 if(!mod.second) {
444 mark_missing();
445 }
446
447 ss << '\n';
448 }
449 }
450
451 // TODO: move to some general area of the code.
452 const auto yes_or_no = [](bool val) { return val ? _("yes") : _("no"); };
453
454 ss << "\n" << markup::tag("big", markup::span_color(font::TITLE_COLOR, _("Settings"))) << "\n";
455 ss << _("Experience modifier:") << " " << game.xp << "\n";
456 ss << _("Gold per village:") << " " << game.gold << "\n";
457 ss << _("Map size:") << " " << game.map_size_info << "\n";
458 ss << _("Reloaded:") << " " << yes_or_no(game.reloaded) << "\n";
459 ss << _("Shared vision:") << " " << game.vision << "\n";
460 ss << _("Shuffle sides:") << " " << yes_or_no(game.shuffle_sides) << "\n";
461 ss << _("Time limit:") << " " << game.time_limit << "\n";
462 ss << _("Use map settings:") << " " << yes_or_no(game.use_map_settings);
463
464 image& info_icon = grid->find_widget<image>("game_info");
465
466 if(!game.have_era || !game.have_all_mods || !game.required_addons.empty()) {
467 info_icon.set_label("icons/icon-info-error.png");
468
469 ss << "\n\n" << markup::span_color("#f00", markup::span_size("x-large", "! "));
470 ss << _("One or more add-ons need to be installed\nin order to join this game.");
471 } else {
472 info_icon.set_label("icons/icon-info.png");
473 }
474
475 info_icon.set_tooltip(ss.str());
476
477 //
478 // Password icon
479 //
480 image& password_icon = grid->find_widget<image>("needs_password");
481
482 if(game.password_required) {
484 } else {
486 }
487
488 //
489 // Observer icon
490 //
491 image& observer_icon = grid->find_widget<image>("observer_icon");
492
493 if(game.observers) {
494 observer_icon.set_label("misc/eye.png");
495 observer_icon.set_tooltip( _("Observers allowed"));
496 } else {
497 observer_icon.set_label("misc/no_observer.png");
498 observer_icon.set_tooltip( _("Observers not allowed"));
499 }
500
501 //
502 // Minimap
503 //
504 minimap& map = grid->find_widget<minimap>("minimap");
505
506 map.set_map_data(game.map_data);
507
508 if(!add_callbacks) {
509 return;
510 }
511
512 //
513 // Presets buttons
514 //
515 if(game.game_preset) {
516 button& delete_preset = grid->find_widget<button>("delete_preset");
517
518 connect_signal_mouse_left_click(delete_preset, std::bind(&mp_lobby::delete_preset, this, game.id));
519 } else {
521 }
522
523 //
524 // Enter game by double clicking row
525 //
526 connect_signal_mouse_left_double_click(row_panel,
527 std::bind(&mp_lobby::enter_game_by_id, this, game.id, DO_EITHER));
528}
529
530void mp_lobby::delete_preset(const int game_id)
531{
534}
535
537{
538 DBG_LB << "mp_lobby::update_gamelist_filter";
540 DBG_LB << "Games in lobby_info: " << lobby_info_.games().size()
541 << ", games in listbox: " << gamelistbox_->get_item_count();
542 assert(lobby_info_.games().size() == gamelistbox_->get_item_count());
543
545}
546
548{
549 if(delay_playerlist_update_) return;
550
551 SCOPE_LB;
552 DBG_LB << "Playerlist update: " << lobby_info_.users().size();
553
555
556 player_list_dirty_ = false;
557 last_lobby_update_ = std::chrono::steady_clock::now();
558}
559
561{
562 const int idx = gamelistbox_->get_selected_row();
563 bool can_join = false, can_observe = false;
564
565 if(idx >= 0) {
566 const mp::game_info& game = *lobby_info_.games()[idx];
567 can_observe = game.can_observe();
568 can_join = game.can_join();
570 } else {
572 }
573
574 find_widget<button>("observe_global").set_active(can_observe);
575 find_widget<button>("join_global").set_active(can_join);
576
577 player_list_dirty_ = true;
578}
579
581{
582 SCOPE_LB;
583
584 gamelistbox_ = find_widget<listbox>("game_list", false, true);
585
586 connect_signal_notify_modified(*gamelistbox_,
587 std::bind(&mp_lobby::update_selected_game, this));
588
589 player_list_.init(*this);
590
591 set_enter_disabled(true);
592
593 // Exit hook to add a confirmation when quitting the Lobby.
594 set_exit_hook(window::exit_hook::always, [this] { return get_retval() == retval::CANCEL ? quit() : true; });
595
596 chatbox_ = find_widget<chatbox>("chat", false, true);
597
599
602
603 find_widget<button>("create").set_retval(CREATE);
604
605 connect_signal_mouse_left_click(
606 find_widget<button>("show_preferences"),
608
609 connect_signal_mouse_left_click(
610 find_widget<button>("join_global"),
611 std::bind(&mp_lobby::enter_selected_game, this, DO_JOIN));
612
613 find_widget<button>("join_global").set_active(false);
614
615 connect_signal_mouse_left_click(
616 find_widget<button>("observe_global"),
617 std::bind(&mp_lobby::enter_selected_game, this, DO_OBSERVE));
618
619 connect_signal_mouse_left_click(
620 find_widget<button>("server_info"),
621 std::bind(&mp_lobby::show_server_info, this));
622
623 find_widget<button>("observe_global").set_active(false);
624
625 menu_button& replay_options = find_widget<menu_button>("replay_options");
626
627 if(prefs::get().skip_mp_replay()) {
628 replay_options.set_selected(1);
629 }
630
631 if(prefs::get().blindfold_replay()) {
632 replay_options.set_selected(2);
633 }
634
635 connect_signal_notify_modified(replay_options,
637
638 filter_text_ = find_widget<text_box>("filter_text", false, true);
639
640 connect_signal_pre_key_press(
642 std::bind(&mp_lobby::game_filter_keypress_callback, this, std::placeholders::_5));
643
644 chatbox_->room_window_open(N_("lobby"), true, false);
646
648
649 // Force first update to be directly.
652
653 // TODO: currently getting a crash in the chatbox if we use
654 // -- vultraz, 2017-11-10
655 //mp_lobby::network_handler();
656
659
660 //
661 // Profile box
662 //
663 if(panel* profile_panel = find_widget<panel>("profile", false, false)) {
664 auto your_info = std::find_if(lobby_info_.users().begin(), lobby_info_.users().end(),
665 [](const auto& u) { return u.get_relation() == mp::user_info::user_relation::ME; });
666
667 if(your_info != lobby_info_.users().end()) {
668 profile_panel->find_widget<label>("username").set_label(your_info->name);
669
670 auto& profile_button = profile_panel->find_widget<button>("view_profile");
671 connect_signal_mouse_left_click(profile_button, std::bind(&mp_lobby::open_profile_url, this));
672
673 auto& history_button = profile_panel->find_widget<button>("view_match_history");
674 connect_signal_mouse_left_click(history_button, std::bind(&mp_lobby::open_match_history, this));
675 }
676 }
677
678 listbox& tab_bar = find_widget<listbox>("games_list_tab_bar");
679 connect_signal_notify_modified(tab_bar, std::bind(&mp_lobby::tab_switch_callback, this));
680
681 // server-side queue join button
682 button* queue_join_button = find_widget<button>("join_queue", false, true);
683 connect_signal_mouse_left_click(*queue_join_button, std::bind(&mp_lobby::join_queue, this));
684
685 // server-side queue leave button
686 button* queue_leave_button = find_widget<button>("leave_queue", false, true);
687 connect_signal_mouse_left_click(*queue_leave_button, std::bind(&mp_lobby::leave_queue, this));
688
689 // server-side queues list
691
692 // Set up Lua plugin context
693 plugins_context_.reset(new plugins_context("Multiplayer Lobby"));
694
695 plugins_context_->set_callback("join", [&, this](const config&) {
697 }, true);
698
699 plugins_context_->set_callback("observe", [&, this](const config&) {
701 }, true);
702
703 plugins_context_->set_callback("create", [this](const config&) { set_retval(CREATE); }, true);
704 plugins_context_->set_callback("quit", [this](const config&) { set_retval(retval::CANCEL); }, false);
705
706 plugins_context_->set_callback("chat", [this](const config& cfg) { chatbox_->send_chat_message(cfg["message"], false); }, true);
707 plugins_context_->set_callback("select_game", [this](const config& cfg) {
708 selected_game_id_ = cfg.has_attribute("id") ? cfg["id"].to_int() : lobby_info_.games()[cfg["index"].to_int()]->id;
709 }, true);
710
711 plugins_context_->set_accessor("game_list", [this](const config&) { return lobby_info_.gamelist(); });
712}
713
715{
716 filter_auto_hosted_ = find_widget<listbox>("games_list_tab_bar").get_selected_row() == 1;
717 filter_game_presets_ = find_widget<listbox>("games_list_tab_bar").get_selected_row() == 2;
719}
720
722{
724 if(info && info->forum_id != 0) {
726 }
727}
728
730{
731 listbox* queues_listbox = find_widget<listbox>("queue_list", false, true);
732 const std::vector<mp::queue_info>& queues = mp::get_server_queues();
733 if(queues.size() > static_cast<std::size_t>(queues_listbox->get_selected_row())) {
734 const mp::queue_info& queue = queues[queues_listbox->get_selected_row()];
735
736 std::vector<std::string> installed = installed_addons();
737 std::vector<mp::game_info::required_addon> missing;
738 for(const std::string& required : queue.required_addons) {
739 if(!utils::contains(installed, required)) {
741 req.addon_id = required;
742 req.message = required;
744 missing.emplace_back(req);
745 }
746 }
747
748 if(!missing.empty()) {
749 if(handle_addon_requirements_gui(missing, mp::game_info::addon_req::NEED_DOWNLOAD)) {
751 } else {
752 return;
753 }
754 }
755
756 mp::send_to_server(config{"join_server_queue", config{"queue_id", queue.id}});
757 } else {
758 ERR_LB << "Attempted to join queue but couldn't find queue info";
759 }
760}
761
763{
764 listbox* queues_listbox = find_widget<listbox>("queue_list", false, true);
765 const std::vector<mp::queue_info>& queues = mp::get_server_queues();
766 if(queues.size() > static_cast<std::size_t>(queues_listbox->get_selected_row())) {
767 const mp::queue_info& queue = queues[queues_listbox->get_selected_row()];
768 mp::send_to_server(config{"leave_server_queue", config{"queue_id", queue.id}});
769 } else {
770 ERR_LB << "Attempted to join queue but couldn't find queue info";
771 }
772}
773
780
782{
784 if(info) {
786 }
787}
788
790{
791 try {
792 config data;
795 }
796 } catch (const wesnothd_error& e) {
797 LOG_LB << "caught wesnothd_error in network_handler: " << e.message;
798 throw;
799 }
800
801 if(std::chrono::steady_clock::now() - last_lobby_update_ < game_config::lobby_refresh) {
802 return;
803 }
804
806 //don't process a corrupted gamelist further to prevent crashes later.
807 return;
808 }
809
813 } else {
816 }
817 }
818
822 }
823}
824
826{
827 listbox* queues_listbox = find_widget<listbox>("queue_list", false, true);
828 queues_listbox->clear();
829
830 std::vector<mp::queue_info>& queues = mp::get_server_queues();
831 std::sort(queues.begin(), queues.end(), [](const mp::queue_info& q1, const mp::queue_info& q2) { return q1.display_name < q2.display_name; });
832
833 for(const mp::queue_info& info : queues) {
835 widget_item item;
836
837 item["label"] = info.current_players.count(prefs::get().login()) > 0 ? "x" : "o";
838 data.emplace("is_in_queue", item);
839
840 item["label"] = info.display_name;
841 data.emplace("queue_name", item);
842
843 item["label"] = std::to_string(info.current_players.size())+"/"+std::to_string(info.players_required);
844 data.emplace("queue_player_count", item);
845
846 queues_listbox->add_row(data);
847 }
848}
849
851{
852 if(auto error = data.optional_child("error")) {
853 throw wesnothd_error(error["message"]);
854 } else if(data.has_child("gamelist")) {
856 } else if(auto gamelist_diff = data.optional_child("gamelist_diff")) {
857 process_gamelist_diff(*gamelist_diff);
858 } else if(auto info = data.optional_child("message")) {
859 if(info["type"] == "server_info") {
860 server_information_ = info["message"].str();
861 return;
862 } else if(info["type"] == "announcements") {
863 announcements_ = info["message"].str();
864 return;
865 }
866 } else if(auto create = data.optional_child("create_game")) {
867 queue_game_server_preset_ = create.value().mandatory_child("game");
868 queue_id_ = create["queue_id"].to_int();
870 return;
871 } else if(auto join_game = data.optional_child("join_game")) {
872 enter_game_by_id(join_game["id"].to_int(), JOIN_MODE::DO_JOIN);
873 return;
874 } else if(auto queue_update = data.optional_child("queue_update")) {
875 std::vector<mp::queue_info>& queues = mp::get_server_queues();
876 if(queue_update["action"].str() == "add") {
877 mp::queue_info& new_info = queues.emplace_back();
878 new_info.id = queue_update["queue_id"].to_int();
879 new_info.players_required = queue_update["players_required"].to_int();
880 new_info.display_name = queue_update["display_name"].str();
881 } else {
882 for(mp::queue_info& info : queues) {
883 if(info.id == queue_update["queue_id"].to_int()) {
884 if(queue_update["action"].str() == "remove") {
885 utils::erase_if(queues, [&](const mp::queue_info& i) { return i.id == queue_update["queue_id"].to_int(); });
886 } else if(queue_update["action"].str() == "update") {
887 if(queue_update->has_attribute("display_name")) {
888 info.display_name = queue_update["display_name"].str();
889 }
890 if(queue_update->has_attribute("players_required")) {
891 info.players_required = queue_update["players_required"].to_int();
892 }
893 if(queue_update->has_attribute("current_players")){
894 info.current_players = utils::split_set(queue_update["current_players"].str());
895 }
896 if(queue_update->has_attribute("addons")){
897 info.required_addons = utils::split_set(queue_update["addons"].str());
898 }
899 } else {
900 continue;
901 }
902 }
903 }
904 }
905
907
908 return;
909 }
910
912}
913
915{
917
919 DBG_LB << "Received gamelist";
920 gamelist_dirty_ = true;
921 gamelist_diff_update_ = false;
922}
923
925{
927
929 DBG_LB << "Received gamelist diff";
930 gamelist_dirty_ = true;
931 } else {
932 ERR_LB << "process_gamelist_diff failed!";
934 }
935 const int joined = data.child_count("insert_child");
936 const int left = data.child_count("remove_child");
937 if(joined > 0 || left > 0) {
938 if(left > joined) {
940 } else {
942 }
943 }
944}
945
947{
948 if(game.id < 0) {
950
951 if(preset) {
953 queue_id_ = preset["id"].to_int();
955 return;
956 }
957 }
958
959 switch(mode) {
960 case DO_JOIN:
961 if(!game.can_join()) {
962 ERR_LB << "Attempted to join a game with no vacant slots";
963 return;
964 }
965
966 break;
967 case DO_OBSERVE:
968 if(!game.can_observe()) {
969 ERR_LB << "Attempted to observe a game with observers disabled";
970 return;
971 }
972
973 break;
974 case DO_EITHER:
975 if(game.can_join()) {
976 mode = DO_JOIN;
977 } else if(game.can_observe()) {
979 } else {
980 DBG_LB << "Cannot join or observe a game.";
981 return;
982 }
983
984 break;
985 }
986
987 // prompt moderators for whether they want to join a game with observers disabled
988 if(!game.observers && mp::logged_in_as_moderator()) {
989 if(gui2::show_message(_("Observe"), _("This game doesn’t allow observers. Observe using moderator rights anyway?"), gui2::dialogs::message::yes_no_buttons) != gui2::retval::OK) {
990 return;
991 }
992 }
993
994 const bool try_join = mode == DO_JOIN;
995 const bool try_obsv = mode == DO_OBSERVE;
996
997 // Prompt user to download this game's required addons if its requirements have not been met
998 if(game.addons_outcome != mp::game_info::addon_req::SATISFIED) {
999 if(game.required_addons.empty()) {
1000 gui2::show_error_message(_("Something is wrong with the addon version check database supporting the multiplayer lobby. Please report this at https://bugs.wesnoth.org."));
1001 return;
1002 }
1003
1004 if(!handle_addon_requirements_gui(game.required_addons, game.addons_outcome)) {
1005 return;
1006 }
1007
1008 // Addons have been downloaded, so the game_config and installed addons list need to be reloaded.
1009 // The lobby is closed and reopened.
1011 return;
1012 }
1013
1014 config join_data;
1015 join_data["id"] = std::to_string(game.id);
1016 join_data["observe"] = try_obsv;
1017
1018 if(mp::logged_in_as_moderator() && game.password_required) {
1019 if(gui2::show_message(_("Join"), _("This game is password protected. Join using moderator rights anyway?"), gui2::dialogs::message::yes_no_buttons) != gui2::retval::OK) {
1020 return;
1021 }
1022 } else if(!join_data.empty() && game.password_required) {
1023 std::string password;
1024
1025 if(!gui2::dialogs::mp_join_game_password_prompt::execute(password)) {
1026 return;
1027 }
1028
1029 join_data["password"] = password;
1030 }
1031
1032 join_data["mp_scenario"] = game.scenario_id;
1033 mp::send_to_server(config{"join", std::move(join_data)});
1034
1035 joined_game_id_ = game.id;
1036
1037 // We're all good. Close lobby and proceed to game!
1038 set_retval(try_join ? JOIN : OBSERVE);
1039}
1040
1041void mp_lobby::enter_game_by_id(const int game_id, JOIN_MODE mode)
1042{
1043 // 0 or positive is a normal game
1044 // negative is a preset
1045 mp::game_info* game_ptr = game_id >= 0
1046 ? lobby_info_.get_game_by_id(game_id)
1048
1049 if(!game_ptr) {
1050 ERR_LB << "Attempted to join/observe a game with an invalid id: " << game_id;
1051 return;
1052 }
1053
1054 enter_game(*game_ptr, mode);
1055}
1056
1061
1063{
1064 mp::send_to_server(config("refresh_lobby"));
1065}
1066
1068{
1069 if(info->game_id == 0 || info->game_id == selected_game_id_) {
1070 return;
1071 }
1072
1073 for(unsigned i = 0; i < lobby_info_.games().size(); ++i) {
1074 if(lobby_info_.games()[i]->id == info->game_id) {
1077 break;
1078 }
1079 }
1080}
1081
1083{
1084 gui2::dialogs::preferences_dialog::display();
1085
1086 refresh_lobby();
1087}
1088
1093
1095{
1097
1098 lobby_info_.add_game_filter([this](const mp::game_info& info) {
1099 for(const auto& s : utils::split(filter_text_->get_value(), ' ')) {
1100 if(!info.match_string_filter(s)) {
1101 return false;
1102 }
1103 }
1104
1105 return true;
1106 });
1107
1108 lobby_info_.add_game_filter([this](const mp::game_info& info) {
1109 return filter_friends_->get_widget_value() ? info.has_friends == true : true;
1110 });
1111
1112 // Unlike the friends filter, this is an inclusion filter (do we want to also show
1113 // games with blocked players) rather than an exclusion filter (do we want to show
1114 // only games with friends).
1115 lobby_info_.add_game_filter([this](const mp::game_info& info) {
1116 return filter_ignored_->get_widget_value() == false ? info.has_ignored == false : true;
1117 });
1118
1119 lobby_info_.add_game_filter([this](const mp::game_info& info) {
1120 return filter_slots_->get_widget_value() ? info.vacant_slots > 0 : true;
1121 });
1122
1123 lobby_info_.add_game_filter([this](const mp::game_info& info) {
1124 return info.auto_hosted == filter_auto_hosted_;
1125 });
1126
1127 lobby_info_.add_game_filter([this](const mp::game_info& info) {
1128 return info.game_preset == filter_game_presets_;
1129 });
1130
1132 [this](bool val) { return filter_invert_->get_widget_value() ? !val : val; });
1133}
1134
1136{
1137 if(key == SDLK_RETURN || key == SDLK_KP_ENTER) {
1139 }
1140}
1141
1143{
1146
1148 dlg.show();
1149
1150 if(dlg.result_open_whisper()) {
1151 lobby_chat_window* t = chatbox_->whisper_window_open(info->name, true);
1153 }
1154
1155 // Only follow a player to their game if they are actually in one.
1156 // Otherwise leave the current selection alone instead of losing it on refresh.
1157 // GitHub #1197.
1158 if(info->game_id != 0) {
1159 selected_game_id_ = info->game_id;
1160 }
1161
1162 // do not update here as it can cause issues with removing the widget
1163 // from within it's event handler. Should get updated as soon as possible
1164 // update_gamelist();
1166 player_list_dirty_ = true;
1167 refresh_lobby();
1168}
1169
1171{
1172 // TODO: this prefence should probably be controlled with an enum
1173 const int value = find_widget<menu_button>("replay_options").get_value();
1174 prefs::get().set_skip_mp_replay(value == 1);
1175 prefs::get().set_blindfold_replay(value == 2);
1176}
1177
1178} // namespace dialogs
std::vector< std::string > installed_addons()
Retrieves the names of all installed add-ons.
Definition manager.cpp:186
double t
std::map< std::string, chatroom_log > default_chat_log
Definition chat_log.cpp:18
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
bool has_attribute(std::string_view key) const
Definition config.cpp:157
bool empty() const
Definition config.cpp:823
config & mandatory_child(std::string_view key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition config.cpp:362
Simple push button.
Definition button.hpp:36
lobby_chat_window * room_window_open(const std::string &room, const bool open_new, const bool allow_close=true)
Check if a room window for "room" is open, if open_new is true then it will be created if not found.
Definition chatbox.cpp:379
virtual void send_chat_message(const std::string &message, bool allies_only) override
Inherited form chat_handler.
Definition chatbox.cpp:253
lobby_chat_window * whisper_window_open(const std::string &name, bool open_new)
Check if a whisper window for user "name" is open, if open_new is true then it will be created if not...
Definition chatbox.cpp:385
void switch_to_window(lobby_chat_window *t)
Switch to the window given by a valid pointer (e.g.
Definition chatbox.cpp:130
void process_network_data(const ::config &data)
Definition chatbox.cpp:625
void set_active_window_changed_callback(const std::function< void(void)> &f)
Definition chatbox.hpp:82
void load_log(std::map< std::string, chatroom_log > &log, bool show_lobby)
Definition chatbox.cpp:90
void active_window_changed()
Definition chatbox.cpp:115
@ yes_no_buttons
Shows a yes and no button.
Definition message.hpp:81
@ auto_close
Enables auto close.
Definition message.hpp:71
Abstract base class for all modal dialogs.
bool show(const unsigned auto_close_time=0)
Shows the window.
void enter_selected_game(JOIN_MODE mode)
Enter game by index, where index is the selected game listbox row.
Definition lobby.cpp:1057
void process_gamelist(const config &data)
Definition lobby.cpp:914
field_bool * filter_slots_
Definition lobby.hpp:155
virtual void post_show() override
Actions to be taken after the window has been shown.
Definition lobby.cpp:774
mp::lobby_info & lobby_info_
Definition lobby.hpp:149
text_box * filter_text_
Definition lobby.hpp:160
lobby_player_list_helper player_list_
Definition lobby.hpp:164
void skip_replay_changed_callback()
Definition lobby.cpp:1170
static std::string announcements_
Definition lobby.hpp:193
wesnothd_connection & network_connection_
Definition lobby.hpp:174
void update_gamelist_filter()
Definition lobby.cpp:536
field_bool * filter_friends_
Definition lobby.hpp:153
void process_network_data(const config &data)
Definition lobby.cpp:850
widget_data make_game_row_data(const mp::game_info &game)
Definition lobby.cpp:381
void select_game_by_player(const mp::user_info *info)
Definition lobby.cpp:1067
field_bool * filter_ignored_
Definition lobby.hpp:154
void game_filter_keypress_callback(const SDL_Keycode key)
Definition lobby.cpp:1135
field_bool * filter_invert_
Definition lobby.hpp:156
void enter_game_by_id(const int game_id, JOIN_MODE mode)
Entry wrapper for enter_game, where game is located by game id.
Definition lobby.cpp:1041
void show_preferences_button_callback()
Definition lobby.cpp:1082
static std::string server_information_
Definition lobby.hpp:192
void enter_game(const mp::game_info &game, JOIN_MODE mode)
Exits the lobby and enters the given game.
Definition lobby.cpp:946
void user_dialog_callback(const mp::user_info *info)
Definition lobby.cpp:1142
mp_lobby(mp::lobby_info &info, wesnothd_connection &connection, int &joined_game)
Definition lobby.cpp:70
void process_gamelist_diff(const config &data)
Definition lobby.cpp:924
std::size_t lobby_update_timer_
Timer for updating the lobby.
Definition lobby.hpp:177
config queue_game_server_preset_
Definition lobby.hpp:187
virtual void pre_show() override
Actions to be taken before showing the window.
Definition lobby.cpp:580
void adjust_game_row_contents(const mp::game_info &game, grid *grid, bool add_callbacks=true)
Definition lobby.cpp:410
void delete_preset(const int game_id)
Definition lobby.cpp:530
std::vector< int > gamelist_id_at_row_
Definition lobby.hpp:179
std::chrono::steady_clock::time_point last_lobby_update_
Definition lobby.hpp:170
void network_handler()
Network polling callback.
Definition lobby.cpp:789
@ RELOAD_CONFIG
player clicked the Create button
Definition lobby.hpp:65
static void display(const std::string &player_name, wesnothd_connection &connection, bool wait_for_response=true)
The display function.
std::unique_ptr< plugins_context > plugins_context_
static void display(const std::string &info, const std::string &announcements)
The display function.
void register_hotkey(const hotkey::HOTKEY_COMMAND id, Func &&function)
Registers a hotkey.
T get_widget_value()
Gets the value of the field.
Definition field.hpp:378
Base container class.
Definition grid.hpp:32
widget * find(const std::string_view id, const bool must_be_active) override
See widget::find.
Definition grid.cpp:645
The listbox class.
Definition listbox.hpp:41
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition listbox.cpp:163
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition listbox.cpp:92
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition listbox.cpp:256
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition listbox.cpp:267
void remove_row(const unsigned row, unsigned count=1)
Removes a row in the listbox.
Definition listbox.cpp:108
void clear()
Removes all the rows in the listbox, clearing it.
Definition listbox.cpp:147
int get_selected_row() const
Returns the first selected row.
Definition listbox.cpp:290
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition listbox.cpp:153
const mp::user_info * get_selected_info() const
void update(const std::vector< mp::user_info > &user_info, int focused_game)
Updates the tree contents based on the given user data.
void set_selected(unsigned selected, bool fire_event=true)
void set_map_data(const std::string &map_data)
Definition minimap.cpp:74
void set_tooltip(const t_string &tooltip)
virtual void set_label(const t_string &text)
virtual void set_use_markup(bool use_markup)
std::string get_value() const
Base class for all widgets.
Definition widget.hpp:55
T * find_widget(const std::string_view id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition widget.hpp:747
void set_visible(const visibility visible)
Definition widget.cpp:479
@ visible
The user sets the widget visible, that means:
@ invisible
The user set the widget invisible, that means:
@ hidden
The user sets the widget hidden, that means:
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition window.hpp:313
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition window.hpp:387
show_mode mode() const
Returns the dialog mode for this window.
Definition window.hpp:669
void keyboard_capture(widget *widget)
Definition window.cpp:1197
void set_exit_hook(exit_hook mode, const Func &hook)
Sets the window's exit hook.
Definition window.hpp:435
int get_retval()
Definition window.hpp:394
This class represents the collective information the client has about the players and games on the se...
void add_game_filter(game_filter_func func)
Adds a new filter function to be considered when apply_game_filter is called.
void set_game_filter_invert(std::function< bool(bool)> value)
Sets whether the result of each game filter should be inverted.
const std::vector< user_info > & users() const
bool is_game_visible(const game_info &)
Returns whether the game would be visible after the game filters are applied.
void clear_game_filters()
Clears all game filter functions.
const std::vector< game_info * > & games() const
void process_gamelist(const config &data)
Process a full game list.
bool process_gamelist_diff(const config &data)
Process a gamelist diff.
std::function< void()> begin_state_sync()
Updates the game pointer list and returns a second stage cleanup function to be called after any acti...
const boost::dynamic_bitset & games_visibility() const
game_info * get_game_by_id(int id)
Returns info on a game with the given game ID.
const config & gamelist() const
Returns the raw game list config data.
bool gamelist_initialized() const
void apply_game_filter()
Generates a new list of games that match the current filter functions and inversion setting.
static prefs & get()
void remove_game_preset(int id)
optional_const_config get_game_preset(int id)
std::string login()
Implements a quit confirmation dialog.
static bool quit()
Shows the quit confirmation if needed.
A class that represents a TCP/IP connection to the wesnothd server.
bool receive_data(config &result)
Receives the next pending data pack from the server, if available.
Networked add-ons (campaignd) client interface.
const config * cfg
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
#define N_(String)
Definition gettext.hpp:108
static std::string _(const char *str)
Definition gettext.hpp:100
static lg::log_domain log_lobby("lobby")
#define LOG_LB
#define ERR_LB
#define DBG_LB
#define SCOPE_LB
bool ad_hoc_addon_fetch_session(const std::vector< std::string > &addon_ids)
Conducts an ad-hoc add-ons server connection to download an add-on with a particular id and all it's ...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
bool open_object(const std::string &path_or_url)
Opens the specified object with the default application configured for its type.
Definition open.cpp:50
const color_t YELLOW_COLOR
const color_t GOOD_COLOR
const color_t BAD_COLOR
const color_t TITLE_COLOR
const color_t GRAY_COLOR
std::string escape_text(std::string_view text)
Escapes the pango markup characters in a text.
Definition escape.hpp:33
const std::string unicode_bullet
Definition constants.cpp:47
std::chrono::milliseconds lobby_refresh
std::chrono::milliseconds lobby_network_timer
std::size_t add_timer(const std::chrono::milliseconds &interval, const std::function< void(std::size_t id)> &callback, const bool repeat)
Adds a new timer.
Definition timer.cpp:123
std::map< std::string, widget_item > widget_data
Definition widget.hpp:36
std::map< std::string, t_string > widget_item
Definition widget.hpp:33
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition message.cpp:201
bool remove_timer(const std::size_t id)
Removes a timer.
Definition timer.cpp:164
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.
Definition message.cpp:148
@ OK
Dialog was closed with the OK button.
Definition retval.hpp:35
@ CANCEL
Dialog was closed with the CANCEL button.
Definition retval.hpp:38
void show_help(const std::string &show_topic)
Open the help browser.
Definition help.cpp:84
Functions to load and save images from/to disk.
std::string italic(Args &&... data)
Applies italic Pango markup to the input.
Definition markup.hpp:176
std::string span_color(const color_t &color, Args &&... data)
Applies Pango markup to the input specifying its display color.
Definition markup.hpp:110
std::string tag(std::string_view tag, Args &&... data)
Wraps the given data in the specified tag.
Definition markup.hpp:45
std::string span_size(std::string_view size, Args &&... data)
Applies Pango markup to the input specifying its display size.
Definition markup.hpp:146
void send_to_server(const config &data)
Attempts to send given data to server if a connection is open.
std::vector< queue_info > & get_server_queues()
Gets the list of server-side queues received on login.
std::string get_profile_link(int user_id)
Gets the forum profile link for the given user.
bool logged_in_as_moderator()
Gets whether the currently logged-in user is a moderator.
std::set< std::string > split_set(std::string_view s, char sep, const int flags)
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition general.hpp:88
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
Definition general.hpp:108
std::vector< std::string > split(const config_attribute_value &val)
Desktop environment interaction functions.
int w
Definition pathfind.cpp:188
std::string_view data
Definition picture.cpp:188
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
The basic class for representing 8-bit RGB or RGBA colour values.
Definition color.hpp:51
This class represents the info a client has about a game on the server.
std::string display_name
std::set< std::string > required_addons
This class represents the information a client has about another player.
An error occurred during when trying to communicate with the wesnothd server.
static map_location::direction s
Contains the gui2 timer routines.
#define i
#define g
#define e
#define c