Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "../global.hpp"
00017
00018 #include "player.hpp"
00019 #include "serialization/string_utils.hpp"
00020 #include "util.hpp"
00021
00022 wesnothd::player::player(const std::string& n, simple_wml::node& cfg,
00023 bool registered, const size_t max_messages,
00024 const size_t time_period, const bool sp,
00025 const bool moderator)
00026 : name_(n)
00027 , cfg_(cfg)
00028 , selective_ping_(sp)
00029 , registered_(registered)
00030 , flood_start_(0)
00031 , messages_since_flood_start_(0)
00032 , MaxMessages(max_messages)
00033 , TimePeriod(time_period)
00034 , status_(LOBBY)
00035 , moderator_(moderator)
00036 {
00037 cfg_.set_attr_dup("name", n.c_str());
00038 cfg_.set_attr("registered", registered ? "yes" : "no");
00039 mark_available();
00040 }
00041
00042
00043 void wesnothd::player::set_status(wesnothd::player::STATUS status)
00044 {
00045 status_ = status;
00046 switch (status)
00047 {
00048 case wesnothd::player::LOBBY:
00049 cfg_.set_attr("status", "lobby");
00050 break;
00051 case wesnothd::player::PLAYING:
00052 cfg_.set_attr("status", "playing");
00053 break;
00054 case wesnothd::player::OBSERVING:
00055 cfg_.set_attr("status", "observing");
00056 break;
00057 default:
00058 cfg_.set_attr("status", "unknown");
00059 }
00060 }
00061
00062
00063 void wesnothd::player::mark_available(const int game_id,
00064 const std::string& location)
00065 {
00066 if (game_id == 0) {
00067 cfg_.set_attr("available", "yes");
00068 set_status(LOBBY);
00069 } else {
00070 cfg_.set_attr("available", "no");
00071 }
00072 cfg_.set_attr_dup("game_id", lexical_cast<std::string>(game_id).c_str());
00073 cfg_.set_attr_dup("location", location.c_str());
00074 }
00075
00076 void wesnothd::player::mark_registered(bool registered)
00077 {
00078 cfg_.set_attr("registered", registered ? "yes" : "no");
00079 registered_ = registered;
00080 }
00081
00082 bool wesnothd::player::is_message_flooding()
00083 {
00084 const time_t now = time(NULL);
00085 if (flood_start_ == 0) {
00086 flood_start_ = now;
00087 return false;
00088 }
00089
00090 ++messages_since_flood_start_;
00091
00092 if (now - flood_start_ > TimePeriod) {
00093 messages_since_flood_start_ = 0;
00094 flood_start_ = now;
00095 } else if (messages_since_flood_start_ >= MaxMessages) {
00096 return true;
00097 }
00098 return false;
00099 }