cache the ignores and friends list

This commit is contained in:
Gunter Labes 2009-09-09 11:28:36 +00:00
parent e2b087cec7
commit 182da61ef2
5 changed files with 58 additions and 6 deletions

View file

@ -21,6 +21,7 @@
#include "game_preferences.hpp"
#include "gamestatus.hpp"
#include "gettext.hpp"
#include "generic_event.hpp"
#include "hotkeys.hpp"
#include "log.hpp"
#include "network.hpp" // ping_timeout
@ -61,6 +62,9 @@ void remove_relation(const std::string nick, const std::string relation) {
preferences::set(relation, utils::join(r));
}
events::generic_event friend_added_event_("friend_added");
events::generic_event ignore_added_event_("ignore_added");
} // anon namespace
namespace preferences {
@ -116,12 +120,14 @@ std::string get_ignores() {
bool add_friend(const std::string nick) {
if (!utils::isvalid_username(nick)) return false;
add_relation(nick, "friends");
friend_added_event_.notify_observers();
return true;
}
bool add_ignore(const std::string nick) {
if (!utils::isvalid_username(nick)) return false;
add_relation(nick, "ignores");
ignore_added_event_.notify_observers();
return true;
}
@ -732,4 +738,14 @@ void encounter_map_terrain(gamemap& map){
}
}
events::generic_event & friend_added_event()
{
return friend_added_event_;
}
events::generic_event & ignore_added_event()
{
return ignore_added_event_;
}
} // preferences namespace

View file

@ -27,6 +27,10 @@ class unit_map;
#include <utility>
#include <set>
namespace events {
class generic_event;
}
namespace preferences {
struct manager
@ -208,6 +212,9 @@ namespace preferences {
void encounter_recallable_units(game_state& gamestate);
// Add all terrains on the map as encountered terrains.
void encounter_map_terrain(gamemap& map);
events::generic_event & friend_added_event();
events::generic_event & ignore_added_event();
}
#endif

View file

@ -1915,7 +1915,7 @@ private:
const std::string& help_chat_help = _("Commands: msg/whisper <nick>"
" <message>, list <subcommand> [<argument>], me/emote <message>."
" Type /help [<command>] for detailed instructions.");
if ((cmd == "me" || cmd == "emote") && argc > 0) {
if ((cmd == "me" || cmd == "emote") && argc >= 0) {
//emote message
send_chat_message("/me" + message.substr(cmd.size() + 1), allies_only);
} else if (cmd == "query" || cmd == "ban" || cmd == "kick"

View file

@ -17,6 +17,7 @@
#include "config.hpp"
#include "construct_dialog.hpp"
#include "game_display.hpp"
#include "generic_event.hpp"
#include "font.hpp"
#include "marked-up_text.hpp"
#include "gettext.hpp"
@ -262,6 +263,27 @@ ui::ui(game_display& disp, const std::string& title, const config& cfg, chat& c,
const SDL_Rect area = { 0, 0, disp.video().getx(), disp.video().gety() };
users_menu_.set_numeric_keypress_selection(false);
set_location(area);
friends_ = utils::split(preferences::get("friends"));
ignores_ = utils::split(preferences::get("ignores"));
preferences::friend_added_event().attach_handler(this);
preferences::ignore_added_event().attach_handler(this);
}
ui::~ui() {
preferences::friend_added_event().detach_handler(this);
preferences::ignore_added_event().detach_handler(this);
}
void ui::handle_generic_event(const std::string& event_name)
{
if(event_name == "ignore_added") {
ignores_ = utils::split(preferences::get("ignores"));
}
else if(event_name == "ignore_added") {
friends_ = utils::split(preferences::get("friends"));
}
}
void ui::process_network()
@ -614,13 +636,12 @@ void ui::gamelist_updated(bool silent)
if(!(**user)["location"].empty()) {
u_elem.location = (**user)["location"];
}
std::vector<std::string> friends = utils::split(preferences::get("friends"));
std::vector<std::string> ignores = utils::split(preferences::get("ignores"));
if (u_elem.name == preferences::login()) {
u_elem.relation = ME;
} else if (std::find(ignores.begin(), ignores.end(), u_elem.name) != ignores.end()) {
} else if (std::find(ignores_.begin(), ignores_.end(), u_elem.name) != ignores_.end()) {
u_elem.relation = IGNORED;
} else if (std::find(friends.begin(), friends.end(), u_elem.name) != friends.end()) {
} else if (std::find(friends_.begin(), friends_.end(), u_elem.name) != friends_.end()) {
u_elem.relation = FRIEND;
} else {
u_elem.relation = NEUTRAL;

View file

@ -15,6 +15,7 @@
#define MULTIPLAYER_UI_HPP_INCLUDED
#include "hotkeys.hpp"
#include "generic_event.hpp"
#include "network.hpp"
#include "preferences_display.hpp"
#include "widgets/label.hpp"
@ -69,7 +70,7 @@ private:
//a base class for the different multiplayer base dialogs: game list, create
//game, wait game, game setup
class ui : public gui::widget, private events::chat_handler, private font::floating_label_context
class ui : public gui::widget, private events::chat_handler, private font::floating_label_context, public events::observer
{
public:
enum result { CONTINUE, JOIN, OBSERVE, CREATE, PREFERENCES, PLAY, QUIT };
@ -77,6 +78,10 @@ public:
ui(game_display& d, const std::string& title,
const config& cfg, chat& c, config& gamelist);
virtual ~ui();
virtual void handle_generic_event(const std::string& event_name);
// Asks the multiplayer_ui to pump some data from the network, and then
// to process it. The actual processing will be left to the child
// classes, through process_network_data and process_network_error
@ -100,6 +105,9 @@ protected:
SDL_Rect client_area() const;
std::vector<std::string> friends_;
std::vector<std::string> ignores_;
game_display& disp_;
game_display& disp() { return disp_; };