The Battle for Wesnoth  1.19.0-dev
player.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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");
67  set_status(LOBBY);
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 
75 void wesnothd::player::mark_registered(bool registered)
76 {
77  cfg_.set_attr("registered", registered ? "yes" : "no");
78  registered_ = registered;
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 
89  ++messages_since_flood_start_;
90 
91  if (now - flood_start_ > TimePeriod) {
92  messages_since_flood_start_ = 0;
93  flood_start_ = now;
94  } else if (messages_since_flood_start_ >= MaxMessages) {
95  return true;
96  }
97  return false;
98 }
node & set_attr_int(const char *key, int value)
Definition: simple_wml.cpp:441
node & set_attr(const char *key, const char *value)
Definition: simple_wml.cpp:413
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:429
void mark_available(const int game_id=0, const std::string &location="")
Definition: player.cpp:62
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
void set_status(STATUS status)
Definition: player.cpp:42
simple_wml::node & cfg_
Definition: player.hpp:64
void mark_registered(bool registered=true)
Definition: player.cpp:75
bool registered() const
Definition: player.hpp:45
bool is_message_flooding()
Definition: player.cpp:81
static map_location::DIRECTION n