The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
player.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
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
18wesnothd::player::player(const std::string& n,
20 long id,
21 bool registered,
22 const std::string& version,
23 const std::string& source,
24 unsigned long long login_id,
25 const std::size_t max_messages,
26 const std::chrono::seconds& time_period,
27 const bool moderator)
28 : name_(n)
29 , version_(version)
30 , source_(source)
31 , cfg_(cfg)
32 , registered_(registered)
33 , flood_start_()
34 , messages_since_flood_start_(0)
35 , MaxMessages(max_messages)
36 , TimePeriod(time_period)
37 , status_(LOBBY)
38 , moderator_(moderator)
39 , login_id_(login_id)
40 , in_queues_()
41{
42 cfg_.set_attr_dup("name", n.c_str());
43 cfg_.set_attr("registered", registered ? "yes" : "no");
44 cfg_.set_attr("moderator", moderator ? "yes" : "no");
45 cfg_.set_attr_int("forum_id", id);
47}
48
50{
51 status_ = status;
52 switch (status)
53 {
55 cfg_.set_attr("status", "lobby");
56 break;
58 cfg_.set_attr("status", "playing");
59 break;
61 cfg_.set_attr("status", "observing");
62 break;
63 default:
64 cfg_.set_attr("status", "unknown");
65 }
66}
67
68// keep 'available' and game name ('location') for backward compatibility
69void wesnothd::player::mark_available(const int game_id,
70 const std::string& location)
71{
72 if (game_id == 0) {
73 cfg_.set_attr("available", "yes");
74 set_status(LOBBY);
75 } else {
76 cfg_.set_attr("available", "no");
77 }
78 cfg_.set_attr_dup("game_id", std::to_string(game_id).c_str());
79 cfg_.set_attr_dup("location", location.c_str());
80}
81
83{
84 cfg_.set_attr("registered", registered ? "yes" : "no");
85 registered_ = registered;
86}
87
89{
90 const auto now = std::chrono::steady_clock::now();
91 if (flood_start_ == std::chrono::steady_clock::time_point{}) {
92 flood_start_ = now;
93 return false;
94 }
95
96 ++messages_since_flood_start_;
97
98 if (now - flood_start_ > TimePeriod) {
99 messages_since_flood_start_ = 0;
100 flood_start_ = now;
101 } else if (messages_since_flood_start_ >= MaxMessages) {
102 return true;
103 }
104 return false;
105}
node & set_attr_int(const char *key, int value)
node & set_attr(const char *key, const char *value)
node & set_attr_dup(const char *key, const char *value)
void mark_available(const int game_id=0, const std::string &location="")
Definition player.cpp:69
void set_status(STATUS status)
Definition player.cpp:49
simple_wml::node & cfg_
Definition player.hpp:76
void mark_registered(bool registered=true)
Definition player.cpp:82
bool registered() const
Definition player.hpp:52
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::chrono::seconds &time_period=std::chrono::seconds{10}, const bool moderator=false)
Definition player.cpp:18
bool is_message_flooding()
Definition player.cpp:88
const config * cfg
static map_location::direction n