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