The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
player.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 "global.hpp"
16 
17 #include "player.hpp"
19 #include "util.hpp"
20 
22  bool registered, const size_t max_messages,
23  const size_t time_period, const bool sp,
24  const bool moderator)
25  : name_(n)
26  , cfg_(cfg)
27  , selective_ping_(sp)
28  , registered_(registered)
29  , flood_start_(0)
30  , messages_since_flood_start_(0)
31  , MaxMessages(max_messages)
32  , TimePeriod(time_period)
33  , status_(LOBBY)
34  , moderator_(moderator)
35 {
36  cfg_.set_attr_dup("name", n.c_str());
37  cfg_.set_attr("registered", registered ? "yes" : "no");
39 }
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", lexical_cast_default<std::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 time_t now = 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(const char *key, const char *value)
Definition: simple_wml.cpp:411
void mark_available(const int game_id=0, const std::string &location="")
Definition: player.cpp:62
simple_wml::node & cfg_
Definition: player.hpp:76
Templates and utility-routines for strings and numbers.
player(const std::string &n, simple_wml::node &cfg, bool registered, const size_t max_messages=4, const size_t time_period=10, const bool sp=false, const bool moderator=false)
Definition: player.cpp:21
Encapsulates the map of the game.
Definition: location.hpp:38
void mark_registered(bool registered=true)
Definition: player.cpp:75
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:427
bool is_message_flooding()
Definition: player.cpp:81
void set_status(STATUS status)
Definition: player.cpp:42
GLclampd n
Definition: glew.h:5903
GLsizei const GLcharARB ** string
Definition: glew.h:4503