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