Lobby Info: formatting and doc update

Also deployed some std::endl in place of "\n".
This commit is contained in:
Charles Dang 2017-10-31 12:24:54 +11:00
parent cadbe4a2a3
commit 0efd41ade1
2 changed files with 139 additions and 92 deletions

View file

@ -14,16 +14,16 @@
#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 "map/map.hpp"
#include "mp_ui_alerts.hpp"
#include "preferences/game.hpp"
#include "wesnothd_connection.hpp"
#include "wml_exception.hpp"
#include <iterator>
static lg::log_domain log_engine("engine");
@ -35,8 +35,8 @@ static lg::log_domain log_lobby("lobby");
#define ERR_LB LOG_STREAM(err, log_lobby)
#define SCOPE_LB log_scope2(log_lobby, __func__)
namespace mp {
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)
@ -55,9 +55,9 @@ lobby_info::lobby_info(const config& game_config, const std::vector<std::string>
{
}
void do_notify(notify_mode mode, const std::string & sender, const std::string & message)
void do_notify(notify_mode mode, const std::string& sender, const std::string& message)
{
switch (mode) {
switch(mode) {
case NOTIFY_WHISPER:
case NOTIFY_WHISPER_OTHER_WINDOW:
case NOTIFY_OWN_NICK:
@ -85,15 +85,14 @@ void do_notify(notify_mode mode, const std::string & sender, const std::string &
namespace
{
std::string dump_games_map(const lobby_info::game_info_map& games)
{
std::stringstream ss;
for(const auto & v : games) {
for(const auto& v : games) {
const game_info& game = v.second;
ss << "G" << game.id << "(" << game.name << ") "
<< game.display_status_string() << " ";
ss << "G" << game.id << "(" << game.name << ") " << game.display_status_string() << " ";
}
ss << "\n";
return ss.str();
}
@ -101,10 +100,10 @@ std::string dump_games_map(const lobby_info::game_info_map& games)
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] << " ";
for(const auto& c : gamelist.child_range("game")) {
ss << "g" << c["id"] << "(" << c["name"] << ") " << c[config::diff_track_attribute] << " ";
}
ss << "\n";
return ss.str();
}
@ -114,12 +113,13 @@ std::string dump_games_config(const config& gamelist)
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")) {
for(const auto& c : gamelist_.child("gamelist").child_range("game")) {
game_info game(c, game_config_, installed_addons_);
games_by_id_.emplace(game.id, std::move(game));
}
@ -138,11 +138,11 @@ bool lobby_info::process_gamelist_diff(const config& data)
}
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";
ERR_LB << "Error while applying the gamelist diff: '" << e.message << "' Getting a new gamelist.\n";
return false;
}
@ -151,6 +151,7 @@ bool lobby_info::process_gamelist_diff(const config& data)
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;
@ -160,6 +161,7 @@ bool lobby_info::process_gamelist_diff(const config& data)
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, game_info(c, game_config_, installed_addons_));
@ -171,7 +173,7 @@ bool lobby_info::process_gamelist_diff(const config& data)
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";
WRN_LB << "Would have to delete a game that I don't have: " << game_id << std::endl;
continue;
}
@ -186,48 +188,49 @@ bool lobby_info::process_gamelist_diff(const config& data)
}
DBG_LB << dump_games_map(games_by_id_);
try {
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";
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();
process_userlist();
return true;
}
void lobby_info::process_userlist()
{
SCOPE_LB;
users_.clear();
for(const auto & c : gamelist_.child_range("user")) {
for(const auto& c : gamelist_.child_range("user")) {
users_.emplace_back(c);
}
for(auto & ui : users_) {
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";
WRN_NG << "User " << ui.name << " has unknown game_id: " << ui.game_id << std::endl;
continue;
}
switch(ui.relation) {
case user_info::FRIEND:
g->has_friends = true;
break;
case user_info::IGNORED:
g->has_ignored = true;
break;
default:
break;
case user_info::FRIEND:
g->has_friends = true;
break;
case user_info::IGNORED:
g->has_ignored = true;
break;
default:
break;
}
}
}
@ -238,9 +241,9 @@ void 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;
@ -248,7 +251,8 @@ void lobby_info::sync_games_display_status()
}
}
DBG_LB << " -> " << games_by_id_.size() << "\n";
DBG_LB << " -> " << games_by_id_.size() << std::endl;
make_games_vector();
}
@ -266,9 +270,10 @@ const game_info* lobby_info::get_game_by_id(int id) const
room_info* lobby_info::get_room(const std::string& name)
{
for(auto & r : rooms_) {
if(r.name() == name)
for(auto& r : rooms_) {
if(r.name() == name) {
return &r;
}
}
return nullptr;
@ -276,7 +281,7 @@ room_info* lobby_info::get_room(const std::string& name)
const room_info* lobby_info::get_room(const std::string& name) const
{
for(const auto & r : rooms_) {
for(const auto& r : rooms_) {
if(r.name() == name) {
return &r;
}
@ -301,11 +306,6 @@ user_info* lobby_info::get_user(const std::string& name)
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)) {
@ -315,39 +315,20 @@ void lobby_info::open_room(const std::string& name)
void lobby_info::close_room(const std::string& name)
{
DBG_LB << "lobby info: closing room " << name << "\n";
DBG_LB << "lobby info: closing room " << name << std::endl;
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(auto & v : games_by_id_) {
for(auto& v : games_by_id_) {
games_.push_back(&v.second);
}
}
@ -356,12 +337,14 @@ 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;
}
@ -380,7 +363,7 @@ void lobby_info::apply_game_filter()
void lobby_info::update_user_statuses(int game_id, const room_info* room)
{
for(auto & user : users_) {
for(auto& user : users_) {
user.update_state(game_id, room);
}
}
@ -402,9 +385,4 @@ void lobby_info::sort_users(bool by_name, bool by_relation)
});
}
const std::vector<user_info*>& lobby_info::users_sorted() const
{
return users_sorted_;
}
}
} // end namespace mp

View file

@ -16,11 +16,13 @@
#include "config.hpp"
#include "game_initialization/lobby_data.hpp"
#include <boost/dynamic_bitset.hpp>
#include <memory>
namespace mp {
namespace mp
{
/**
* This class represents the collective information the client has
* about the players and games on the server
@ -32,48 +34,93 @@ public:
typedef std::map<int, game_info> game_info_map;
using game_filter_func = std::function<bool(const game_info&)>;
/**
* Process a full gamelist. Current info is discarded.
*/
/** Process a full game list. Current info is discarded. */
void process_gamelist(const config& data);
/**
* Process a gamelist diff.
* @return true on success, false on failure (e.g. when the
* diff did not apply correctly)
*
* @param data Raw game list data, usually received from the MP server.
*
* @returns True on success, false on failure (e.g. when the diff did
* not apply correctly).
*/
bool process_gamelist_diff(const config& data);
void sync_games_display_status();
/**
* Generates a new vector of game pointers from the ID/game map.
* The games will be referenced in ascending order by ID.
*/
void make_games_vector();
/** Returns the raw game list config data. */
const config& gamelist() const
{
return gamelist_;
}
void clear_game_filter();
void add_game_filter(game_filter_func func);
void set_game_filter_invert(bool value);
using game_filter_func = std::function<bool(const game_info&)>;
/** Adds a new filter function to be considered when @ref apply_game_filter is called. */
void add_game_filter(game_filter_func func)
{
game_filters_.push_back(func);
}
/** Clears all game filter functions. */
void clear_game_filter()
{
game_filters_.clear();
}
/** Sets whether the result of each game filter should be inverted. */
void set_game_filter_invert(bool value)
{
game_filter_invert_ = value;
}
/** Generates a new list of games that match the current filter functions and inversion setting. */
void apply_game_filter();
/** Returns info on a game with the given game ID. */
game_info* get_game_by_id(int id);
/** Const overload of @ref get_game_by_id. */
const game_info* get_game_by_id(int id) const;
/**
* Sorts the user list by the given parameters.
*
* @param by_name Whether to sort users alphabetically by name.
* @param by_relation Whether to sort users by their relation to each other (ie,
* display friends before blocked users).
*/
void sort_users(bool by_name, bool by_relation);
/** Open a new chat room with the given name. */
void open_room(const std::string& name);
/** Close the chat room with the given name. */
void close_room(const std::string& name);
/** Returns whether a room with the given name has been opened. */
bool has_room(const std::string& name) const;
/** Returns info on room with the given name, or nullptr if it doesn't exist. */
room_info* get_room(const std::string& name);
/** Const overload of @ref get_room. */
const room_info* get_room(const std::string& name) const;
/** Returns info on the user with the given name, or nullptr if they don't eixst. */
user_info* get_user(const std::string& name);
chat_session& get_whisper_log(const std::string& name);
chat_session& get_whisper_log(const std::string& name)
{
return whispers_[name];
}
void update_user_statuses(int game_id, const room_info* room);
@ -81,39 +128,59 @@ public:
{
return rooms_;
}
const std::vector<game_info*>& games() const
{
return games_;
}
const boost::dynamic_bitset<>& games_visibility() const
{
return games_visibility_;
}
const std::vector<game_info*>& games_filtered() const;
const std::vector<game_info*>& games_filtered() const
{
return games_filtered_;
}
const std::vector<user_info>& users() const
{
return users_;
}
const std::vector<user_info*>& users_sorted() const;
const std::vector<user_info*>& users_sorted() const
{
return users_sorted_;
}
private:
void process_userlist();
const config& game_config_;
const std::vector<std::string>& installed_addons_;
config gamelist_;
bool gamelist_initialized_;
std::vector<room_info> rooms_;
game_info_map games_by_id_;
std::vector<game_info*> games_;
std::vector<game_info*> games_filtered_;
std::vector<user_info> users_;
std::vector<user_info*> users_sorted_;
std::map<std::string, chat_session> whispers_;
std::vector<game_filter_func> game_filters_;
bool game_filter_invert_;
boost::dynamic_bitset<> games_visibility_;
};
@ -131,7 +198,9 @@ enum notify_mode {
NOTIFY_COUNT
};
void do_notify(notify_mode mode, const std::string & sender, const std::string & message);
inline void do_notify(notify_mode mode) { do_notify(mode, "", ""); }
void do_notify(notify_mode mode, const std::string& sender, const std::string& message);
inline void do_notify(notify_mode mode)
{
do_notify(mode, "", "");
}
}