The Battle for Wesnoth  1.15.1+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 
15 #include "server/player.hpp"
16 #include "lexical_cast.hpp"
17 
18 wesnothd::player::player(const std::string& n, simple_wml::node& cfg,
19  bool registered, const std::string& version, const std::size_t max_messages,
20  const std::size_t time_period,
21  const bool moderator)
22  : name_(n)
23  , version_(version)
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 {
33  cfg_.set_attr_dup("name", n.c_str());
34  cfg_.set_attr("registered", registered ? "yes" : "no");
36 }
37 
38 
40 {
41  status_ = status;
42  switch (status)
43  {
45  cfg_.set_attr("status", "lobby");
46  break;
48  cfg_.set_attr("status", "playing");
49  break;
51  cfg_.set_attr("status", "observing");
52  break;
53  default:
54  cfg_.set_attr("status", "unknown");
55  }
56 }
57 
58 // keep 'available' and game name ('location') for backward compatibility
59 void wesnothd::player::mark_available(const int game_id,
60  const std::string& location)
61 {
62  if (game_id == 0) {
63  cfg_.set_attr("available", "yes");
65  } else {
66  cfg_.set_attr("available", "no");
67  }
68  cfg_.set_attr_dup("game_id", lexical_cast<std::string>(game_id).c_str());
69  cfg_.set_attr_dup("location", location.c_str());
70 }
71 
73 {
74  cfg_.set_attr("registered", registered ? "yes" : "no");
76 }
77 
79 {
80  const std::time_t now = std::time(nullptr);
81  if (flood_start_ == 0) {
82  flood_start_ = now;
83  return false;
84  }
85 
87 
88  if (now - flood_start_ > TimePeriod) {
90  flood_start_ = now;
92  return true;
93  }
94  return false;
95 }
New lexcical_cast header.
player(const std::string &n, simple_wml::node &cfg, bool registered, const std::string &version, const std::size_t max_messages=4, const std::size_t time_period=10, const bool moderator=false)
Definition: player.cpp:18
node & set_attr(const char *key, const char *value)
Definition: simple_wml.cpp:411
const std::time_t TimePeriod
Definition: player.hpp:81
std::time_t flood_start_
Definition: player.hpp:78
void mark_available(const int game_id=0, const std::string &location="")
Definition: player.cpp:59
simple_wml::node & cfg_
Definition: player.hpp:74
bool registered_
Definition: player.hpp:76
bool registered() const
Definition: player.hpp:46
void mark_registered(bool registered=true)
Definition: player.cpp:72
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:427
STATUS status_
Definition: player.hpp:82
unsigned int messages_since_flood_start_
Definition: player.hpp:79
bool is_message_flooding()
Definition: player.cpp:78
void set_status(STATUS status)
Definition: player.cpp:39
static map_location::DIRECTION n
const std::size_t MaxMessages
Definition: player.hpp:80