The Battle for Wesnoth  1.17.0-dev
lobby_info.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
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 #pragma once
17 
18 #include "config.hpp"
20 
21 #include <boost/dynamic_bitset.hpp>
22 
23 #include <memory>
24 
25 namespace mp
26 {
27 /**
28  * This class represents the collective information the client has
29  * about the players and games on the server
30  */
32 {
33 public:
34  lobby_info();
35 
36  typedef std::map<int, game_info> game_info_map;
37 
38  /** Process a full game list. Current info is discarded. */
39  void process_gamelist(const config& data);
40 
41  /**
42  * Process a gamelist diff.
43  *
44  * @param data Raw game list data, usually received from the MP server.
45  *
46  * @returns True on success, false on failure (e.g. when the diff did
47  * not apply correctly).
48  */
49  bool process_gamelist_diff(const config& data);
50 
51  bool process_gamelist_diff_impl(const config& data);
52 
54 
55  /**
56  * Generates a new vector of game pointers from the ID/game map.
57  * The games will be referenced in ascending order by ID.
58  */
59  void make_games_vector();
60 
61  /** Returns the raw game list config data. */
62  const config& gamelist() const
63  {
64  return gamelist_;
65  }
66 
67  using game_filter_func = std::function<bool(const game_info&)>;
68 
69  /** Adds a new filter function to be considered when @ref apply_game_filter is called. */
71  {
72  game_filters_.push_back(func);
73  }
74 
75  /** Clears all game filter functions. */
77  {
78  game_filters_.clear();
79  }
80 
81  /** Sets whether the result of each game filter should be inverted. */
82  void set_game_filter_invert(std::function<bool(bool)> value)
83  {
84  game_filter_invert_ = value;
85  }
86 
87  /** Returns whether the game would be visible after the game filters are applied */
88  bool is_game_visible(const game_info&);
89 
90  /** Generates a new list of games that match the current filter functions and inversion setting. */
91  void apply_game_filter();
92 
93  /** Returns info on a game with the given game ID. */
94  game_info* get_game_by_id(int id);
95 
96  /** Const overload of @ref get_game_by_id. */
97  const game_info* get_game_by_id(int id) const;
98 
99  /** Returns info on the user with the given name, or nullptr if they don't eixst. */
100  user_info* get_user(const std::string& name);
101 
102  void update_user_statuses(int game_id);
103 
104  const std::vector<game_info*>& games() const
105  {
106  return games_;
107  }
108 
109  const boost::dynamic_bitset<>& games_visibility() const
110  {
111  return games_visibility_;
112  }
113 
114  const std::vector<user_info>& users() const
115  {
116  return users_;
117  }
118 
119  std::vector<user_info>& users()
120  {
121  return users_;
122  }
123 
124  bool gamelist_initialized() const
125  {
126  return gamelist_initialized_;
127  }
128 
130 
131 private:
132  void process_userlist();
133 
134  std::vector<std::string> installed_addons_;
135 
137 
139 
140  game_info_map games_by_id_;
141 
142  std::vector<game_info*> games_;
143 
144  std::vector<user_info> users_;
145 
146  std::vector<game_filter_func> game_filters_;
147 
148  std::function<bool(bool)> game_filter_invert_;
149 
150  boost::dynamic_bitset<> games_visibility_;
151 };
152 
154 {
155 public:
156  /** Open a new chat room with the given name. */
157  void open_room(const std::string& name);
158 
159  /** Close the chat room with the given name. */
160  void close_room(const std::string& name);
161 
162  /** Returns whether a room with the given name has been opened. */
163  bool has_room(const std::string& name) const;
164 
165  /** Returns info on room with the given name, or nullptr if it doesn't exist. */
166  room_info* get_room(const std::string& name);
167 
168  /** Const overload of @ref get_room. */
169  const room_info* get_room(const std::string& name) const;
170 
171  chat_session& get_whisper_log(const std::string& name)
172  {
173  return whispers_[name];
174  }
175 
176  const std::map<std::string, room_info>& rooms() const
177  {
178  return rooms_;
179  }
180 
181 private:
182  std::map<std::string, room_info> rooms_;
183 
184  std::map<std::string, chat_session> whispers_;
185 };
186 
199 };
200 
201 void do_notify(notify_mode mode, const std::string& sender = "", const std::string& message = "");
202 }
const config & gamelist() const
Returns the raw game list config data.
Definition: lobby_info.hpp:62
user_info * get_user(const std::string &name)
Returns info on the user with the given name, or nullptr if they don&#39;t eixst.
Definition: lobby_info.cpp:317
bool is_game_visible(const game_info &)
Returns whether the game would be visible after the game filters are applied.
Definition: lobby_info.cpp:355
void clear_game_filters()
Clears all game filter functions.
Definition: lobby_info.hpp:76
This class represents the info a client has about a game on the server.
Definition: lobby_data.hpp:141
std::map< int, game_info > game_info_map
Definition: lobby_info.hpp:36
This class represents the collective information the client has about the players and games on the se...
Definition: lobby_info.hpp:31
void set_game_filter_invert(std::function< bool(bool)> value)
Sets whether the result of each game filter should be inverted.
Definition: lobby_info.hpp:82
void add_game_filter(game_filter_func func)
Adds a new filter function to be considered when apply_game_filter is called.
Definition: lobby_info.hpp:70
std::function< bool(bool)> game_filter_invert_
Definition: lobby_info.hpp:148
std::vector< game_filter_func > game_filters_
Definition: lobby_info.hpp:146
Definitions for the interface to Wesnoth Markup Language (WML).
this class memorizes a chat session.
Definition: lobby_data.hpp:43
void process_gamelist(const config &data)
Process a full game list.
Definition: lobby_info.cpp:120
void apply_game_filter()
Generates a new list of games that match the current filter functions and inversion setting...
Definition: lobby_info.cpp:366
std::vector< std::string > installed_addons_
Definition: lobby_info.hpp:134
game_info * get_game_by_id(int id)
Returns info on a game with the given game ID.
Definition: lobby_info.cpp:282
void make_games_vector()
Generates a new vector of game pointers from the ID/game map.
Definition: lobby_info.cpp:340
Main entry points of multiplayer mode.
Definition: lobby_data.cpp:52
void do_notify(notify_mode mode, const std::string &sender, const std::string &message)
Definition: lobby_info.cpp:62
void update_user_statuses(int game_id)
Definition: lobby_info.cpp:377
chat_session & get_whisper_log(const std::string &name)
Definition: lobby_info.hpp:171
boost::dynamic_bitset games_visibility_
Definition: lobby_info.hpp:150
void sync_games_display_status()
Definition: lobby_info.cpp:261
game_info_map games_by_id_
Definition: lobby_info.hpp:140
bool gamelist_initialized_
Definition: lobby_info.hpp:138
const boost::dynamic_bitset & games_visibility() const
Definition: lobby_info.hpp:109
std::vector< user_info > & users()
Definition: lobby_info.hpp:119
const std::vector< game_info * > & games() const
Definition: lobby_info.hpp:104
std::function< bool(const game_info &)> game_filter_func
Definition: lobby_info.hpp:67
bool gamelist_initialized() const
Definition: lobby_info.hpp:124
This class represents the information a client has about a room.
Definition: lobby_data.hpp:68
bool process_gamelist_diff(const config &data)
Process a gamelist diff.
Definition: lobby_info.cpp:140
void refresh_installed_addons_cache()
Definition: lobby_info.cpp:54
void process_userlist()
Definition: lobby_info.cpp:228
This class represents the information a client has about another player.
Definition: lobby_data.hpp:105
std::map< std::string, room_info > rooms_
Definition: lobby_info.hpp:182
notify_mode
Definition: lobby_info.hpp:187
const std::vector< user_info > & users() const
Definition: lobby_info.hpp:114
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::vector< user_info > users_
Definition: lobby_info.hpp:144
std::vector< game_info * > games_
Definition: lobby_info.hpp:142
bool process_gamelist_diff_impl(const config &data)
Definition: lobby_info.cpp:151
std::map< std::string, chat_session > whispers_
Definition: lobby_info.hpp:184
const std::map< std::string, room_info > & rooms() const
Definition: lobby_info.hpp:176