The Battle for Wesnoth  1.15.12+dev
player.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 
17 wesnothd::player::player(const std::string& n, simple_wml::node& cfg, int id,
18  bool registered, const std::string& version, const std::string& source, unsigned long long login_id, const std::size_t max_messages,
19  const std::size_t time_period,
20  const bool moderator)
21  : name_(n)
22  , version_(version)
23  , source_(source)
24  , cfg_(cfg)
25  , registered_(registered)
26  , flood_start_(0)
27  , messages_since_flood_start_(0)
28  , MaxMessages(max_messages)
29  , TimePeriod(time_period)
30  , status_(LOBBY)
31  , moderator_(moderator)
32  , login_id_(login_id)
33 {
34  cfg_.set_attr_dup("name", n.c_str());
35  cfg_.set_attr("registered", registered ? "yes" : "no");
36  cfg_.set_attr("moderator", moderator ? "yes" : "no");
37  cfg_.set_attr_int("forum_id", id);
39 }
40 
42 {
43  status_ = status;
44  switch (status)
45  {
47  cfg_.set_attr("status", "lobby");
48  break;
50  cfg_.set_attr("status", "playing");
51  break;
53  cfg_.set_attr("status", "observing");
54  break;
55  default:
56  cfg_.set_attr("status", "unknown");
57  }
58 }
59 
60 // keep 'available' and game name ('location') for backward compatibility
61 void wesnothd::player::mark_available(const int game_id,
62  const std::string& location)
63 {
64  if (game_id == 0) {
65  cfg_.set_attr("available", "yes");
67  } else {
68  cfg_.set_attr("available", "no");
69  }
70  cfg_.set_attr_dup("game_id", std::to_string(game_id).c_str());
71  cfg_.set_attr_dup("location", location.c_str());
72 }
73 
75 {
76  cfg_.set_attr("registered", registered ? "yes" : "no");
78 }
79 
81 {
82  const std::time_t now = std::time(nullptr);
83  if (flood_start_ == 0) {
84  flood_start_ = now;
85  return false;
86  }
87 
89 
90  if (now - flood_start_ > TimePeriod) {
92  flood_start_ = now;
94  return true;
95  }
96  return false;
97 }
node & set_attr_int(const char *key, int value)
Definition: simple_wml.cpp:439
node & set_attr(const char *key, const char *value)
Definition: simple_wml.cpp:411
const std::time_t TimePeriod
Definition: player.hpp:70
std::time_t flood_start_
Definition: player.hpp:67
void mark_available(const int game_id=0, const std::string &location="")
Definition: player.cpp:61
player(const std::string &n, simple_wml::node &cfg, int id, bool registered, const std::string &version, const std::string &source, unsigned long long login_id, const std::size_t max_messages=4, const std::size_t time_period=10, const bool moderator=false)
Definition: player.cpp:17
simple_wml::node & cfg_
Definition: player.hpp:63
bool registered_
Definition: player.hpp:65
bool registered() const
Definition: player.hpp:44
void mark_registered(bool registered=true)
Definition: player.cpp:74
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:427
STATUS status_
Definition: player.hpp:71
unsigned int messages_since_flood_start_
Definition: player.hpp:68
bool is_message_flooding()
Definition: player.cpp:80
void set_status(STATUS status)
Definition: player.cpp:41
static map_location::DIRECTION n
const std::size_t MaxMessages
Definition: player.hpp:69