-
-
Notifications
You must be signed in to change notification settings - Fork 993
/
lobby_info.cpp
410 lines (345 loc) · 9.48 KB
/
lobby_info.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
Copyright (C) 2009 - 2017 by Tomasz Sniatowski <kailoran@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "game_initialization/lobby_info.hpp"
#include "config.hpp"
#include "preferences/game.hpp"
#include "formula/string_utils.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "map/map.hpp"
#include "map/exception.hpp"
#include "wml_exception.hpp"
#include "wesnothd_connection.hpp"
#include "mp_ui_alerts.hpp"
#include <iterator>
static lg::log_domain log_engine("engine");
#define WRN_NG LOG_STREAM(warn, log_engine)
static lg::log_domain log_lobby("lobby");
#define DBG_LB LOG_STREAM(debug, log_lobby)
#define WRN_LB LOG_STREAM(warn, log_lobby)
#define ERR_LB LOG_STREAM(err, log_lobby)
#define SCOPE_LB log_scope2(log_lobby, __func__)
namespace mp {
lobby_info::lobby_info(const config& game_config, const std::vector<std::string>& installed_addons)
: game_config_(game_config)
, installed_addons_(installed_addons)
, gamelist_()
, gamelist_initialized_(false)
, rooms_()
, games_by_id_()
, games_()
, games_filtered_()
, users_()
, users_sorted_()
, whispers_()
, game_filters_()
, game_filter_invert_(false)
, games_visibility_()
{
}
void do_notify(notify_mode mode, const std::string & sender, const std::string & message)
{
switch (mode) {
case NOTIFY_WHISPER:
case NOTIFY_WHISPER_OTHER_WINDOW:
case NOTIFY_OWN_NICK:
mp_ui_alerts::private_message(true, sender, message);
break;
case NOTIFY_FRIEND_MESSAGE:
mp_ui_alerts::friend_message(true, sender, message);
break;
case NOTIFY_SERVER_MESSAGE:
mp_ui_alerts::server_message(true, sender, message);
break;
case NOTIFY_LOBBY_QUIT:
mp_ui_alerts::player_leaves(true);
break;
case NOTIFY_LOBBY_JOIN:
mp_ui_alerts::player_joins(true);
break;
case NOTIFY_MESSAGE:
mp_ui_alerts::public_message(true, sender, message);
break;
default:
break;
}
}
namespace
{
std::string dump_games_map(const lobby_info::game_info_map& games)
{
std::stringstream ss;
for(const auto & v : games) {
const game_info& game = *v.second;
ss << "G" << game.id << "(" << game.name << ") "
<< game.display_status_string() << " ";
}
ss << "\n";
return ss.str();
}
std::string dump_games_config(const config& gamelist)
{
std::stringstream ss;
for(const auto & c : gamelist.child_range("game")) {
ss << "g" << c["id"] << "(" << c["name"] << ") "
<< c[config::diff_track_attribute] << " ";
}
ss << "\n";
return ss.str();
}
} // end anonymous namespace
void lobby_info::process_gamelist(const config& data)
{
SCOPE_LB;
gamelist_ = data;
gamelist_initialized_ = true;
games_by_id_.clear();
for(const auto & c : gamelist_.child("gamelist").child_range("game")) {
std::unique_ptr<game_info> game(new game_info(c, game_config_, installed_addons_));
games_by_id_.emplace(game->id, std::move(game));
}
DBG_LB << dump_games_map(games_by_id_);
DBG_LB << dump_games_config(gamelist_.child("gamelist"));
process_userlist();
}
bool lobby_info::process_gamelist_diff(const config& data)
{
SCOPE_LB;
if(!gamelist_initialized_) {
return false;
}
DBG_LB << "prediff " << dump_games_config(gamelist_.child("gamelist"));
try {
gamelist_.apply_diff(data, true);
} catch(config::error& e) {
ERR_LB << "Error while applying the gamelist diff: '" << e.message
<< "' Getting a new gamelist.\n";
return false;
}
DBG_LB << "postdiff " << dump_games_config(gamelist_.child("gamelist"));
DBG_LB << dump_games_map(games_by_id_);
for(config& c : gamelist_.child("gamelist").child_range("game")) {
DBG_LB << "data process: " << c["id"] << " (" << c[config::diff_track_attribute] << ")\n";
const int game_id = c["id"];
if(game_id == 0) {
ERR_LB << "game with id 0 in gamelist config" << std::endl;
return false;
}
game_info_map::iterator current_i = games_by_id_.find(game_id);
const std::string& diff_result = c[config::diff_track_attribute];
if(diff_result == "new" || diff_result == "modified") {
if(current_i == games_by_id_.end()) {
games_by_id_.emplace(game_id, std::unique_ptr<game_info>(new game_info(c, game_config_, installed_addons_)));
continue;
}
// Had a game with that id, so update it and mark it as such
*(current_i->second) = game_info(c, game_config_, installed_addons_);
current_i->second->display_status = game_info::UPDATED;
} else if(diff_result == "deleted") {
if(current_i == games_by_id_.end()) {
WRN_LB << "Would have to delete a game that I don't have: " << game_id << "\n";
continue;
}
if(current_i->second->display_status == game_info::NEW) {
// This means the game never made it through to the user interface,
// so just deleting it is fine.
games_by_id_.erase(current_i);
} else {
current_i->second->display_status = game_info::DELETED;
}
}
}
DBG_LB << dump_games_map(games_by_id_);
try {
gamelist_.clear_diff_track(data);
} catch(config::error& e) {
ERR_LB << "Error while applying the gamelist diff (2): '" << e.message
<< "' Getting a new gamelist.\n";
return false;
}
DBG_LB << "postclean " << dump_games_config(gamelist_.child("gamelist"));
process_userlist();
return true;
}
void lobby_info::process_userlist()
{
SCOPE_LB;
users_.clear();
for(const auto & c : gamelist_.child_range("user")) {
users_.emplace_back(c);
}
for(auto & ui : users_) {
if(ui.game_id == 0) {
continue;
}
game_info* g = get_game_by_id(ui.game_id);
if(!g) {
WRN_NG << "User " << ui.name << " has unknown game_id: " << ui.game_id << "\n";
continue;
}
switch(ui.relation) {
case user_info::FRIEND:
g->has_friends = true;
break;
case user_info::IGNORED:
g->has_ignored = true;
break;
default:
break;
}
}
}
void lobby_info::sync_games_display_status()
{
DBG_LB << "lobby_info::sync_games_display_status";
DBG_LB << "games_by_id_ size: " << games_by_id_.size();
game_info_map::iterator i = games_by_id_.begin();
while(i != games_by_id_.end()) {
if(i->second->display_status == game_info::DELETED) {
i = games_by_id_.erase(i);
} else {
i->second->display_status = game_info::CLEAN;
++i;
}
}
DBG_LB << " -> " << games_by_id_.size() << "\n";
make_games_vector();
}
game_info* lobby_info::get_game_by_id(int id)
{
game_info_map::iterator i = games_by_id_.find(id);
return i == games_by_id_.end() ? nullptr : i->second.get();
}
const game_info* lobby_info::get_game_by_id(int id) const
{
game_info_map::const_iterator i = games_by_id_.find(id);
return i == games_by_id_.end() ? nullptr : i->second.get();
}
room_info* lobby_info::get_room(const std::string& name)
{
for(auto & r : rooms_) {
if(r.name() == name)
return &r;
}
return nullptr;
}
const room_info* lobby_info::get_room(const std::string& name) const
{
for(const auto & r : rooms_) {
if(r.name() == name) {
return &r;
}
}
return nullptr;
}
bool lobby_info::has_room(const std::string& name) const
{
return get_room(name) != nullptr;
}
user_info* lobby_info::get_user(const std::string& name)
{
for(auto& user : users_) {
if(user.name == name) {
return &user;
}
}
return nullptr;
}
chat_session& lobby_info::get_whisper_log(const std::string& name)
{
return whispers_[name];
}
void lobby_info::open_room(const std::string& name)
{
if(!has_room(name)) {
rooms_.emplace_back(name);
}
}
void lobby_info::close_room(const std::string& name)
{
DBG_LB << "lobby info: closing room " << name << "\n";
if(room_info* r = get_room(name)) {
rooms_.erase(rooms_.begin() + (r - &rooms_[0]));
}
}
const std::vector<game_info*>& lobby_info::games_filtered() const
{
return games_filtered_;
}
void lobby_info::add_game_filter(game_filter_func func)
{
game_filters_.push_back(func);
}
void lobby_info::clear_game_filter()
{
game_filters_.clear();
}
void lobby_info::set_game_filter_invert(bool value)
{
game_filter_invert_ = value;
}
void lobby_info::make_games_vector()
{
games_filtered_.clear();
games_visibility_.clear();
games_.clear();
for(const auto & v : games_by_id_) {
games_.push_back(v.second.get());
}
}
void lobby_info::apply_game_filter()
{
games_filtered_.clear();
games_visibility_.clear();
for(auto g : games_) {
game_info& gi = *g;
bool show = true;
for(const auto& filter_func : game_filters_) {
show = filter_func(gi);
if(!show) {
break;
}
}
if(game_filter_invert_) {
show = !show;
}
games_visibility_.push_back(show);
if(show) {
games_filtered_.push_back(&gi);
}
}
}
void lobby_info::update_user_statuses(int game_id, const room_info* room)
{
for(auto & user : users_) {
user.update_state(game_id, room);
}
}
void lobby_info::sort_users(bool by_name, bool by_relation)
{
users_sorted_.clear();
for(auto& u : users_) {
users_sorted_.push_back(&u);
}
std::sort(users_sorted_.begin(), users_sorted_.end(), [&](const user_info* u1, const user_info* u2) {
if(by_name && by_relation) {
return u1->relation < u2->relation ||
(u1->relation == u2->relation && translation::icompare(u1->name, u2->name) < 0);
}
return (by_name && translation::icompare(u1->name, u2->name) < 0) || (by_relation && u1->relation < u2->relation);
});
}
const std::vector<user_info*>& lobby_info::users_sorted() const
{
return users_sorted_;
}
}