nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
wars.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_WARS_H_
2#define NANOMMO_SERVER_WARS_H_
3
4#include <chrono>
5#include <cstdint>
6#include <map>
7#include <string>
8#include <utility>
9
10namespace nanommo {
11
12/// A war between two guilds of a world, as MySQL has it: the guild that
13/// asked, the one that said yes, by their ids and names, how many of the
14/// other side's players each has killed, and how long it has left. A guild is
15/// in one war at a time.
16struct War {
17 uint32_t id = 0;
18 uint32_t challenger = 0;
19 std::string challenger_name;
20 uint32_t challenged = 0;
21 std::string challenged_name;
22 uint32_t challenger_kills = 0;
23 uint32_t challenged_kills = 0;
24 std::chrono::seconds left{0};
25
26 /// The other side from the guild's, by its id.
27 uint32_t Against(uint32_t guild) const {
28 return guild == challenger ? challenged : challenger;
29 }
30};
31
32/// A player killed by an enemy at war, for the war's score: the war, the
33/// killer and the one they killed, by their ids, and the killer's guild, whose
34/// side it counts for. The server gives it an id of its own, so that the saver
35/// records it once, however many times it's handed it.
36struct WarKill {
37 std::string id;
38 uint32_t war = 0;
39 uint32_t killer = 0;
40 uint32_t victim = 0;
41 uint32_t guild = 0;
42};
43
44/// Which war kills score: a victim scores for a war once every five minutes
45/// at most, so that a player can't run up their guild's score on a friend in
46/// the other guild who keeps coming back to die. The kill happens either way.
47/// It's the server's to remember, so an instance starts with none.
48class Scoring {
49 public:
50 static constexpr std::chrono::minutes kOnceEvery{5};
51
52 /// Whether the kill, now, scores. One that does is remembered for
53 /// kOnceEvery.
54 bool Scores(const WarKill& kill, std::chrono::steady_clock::time_point now) {
55 // Those who scored long enough ago can score again.
56 std::erase_if(scored_, [now](const auto& victim) {
57 return now - victim.second >= kOnceEvery;
58 });
59 return scored_.try_emplace({kill.war, kill.victim}, now).second;
60 }
61
62 private:
63 /// When each victim last scored, by the war and their id.
64 std::map<std::pair<uint32_t, uint32_t>, std::chrono::steady_clock::time_point>
65 scored_;
66};
67
68} // namespace nanommo
69
70#endif // NANOMMO_SERVER_WARS_H_
static constexpr std::chrono::minutes kOnceEvery
Definition wars.h:50
bool Scores(const WarKill &kill, std::chrono::steady_clock::time_point now)
Definition wars.h:54
Definition chat.cc:9
uint32_t killer
Definition wars.h:39
uint32_t victim
Definition wars.h:40
std::string id
Definition wars.h:37
uint32_t war
Definition wars.h:38
uint32_t guild
Definition wars.h:41
uint32_t challenged_kills
Definition wars.h:23
std::string challenged_name
Definition wars.h:21
uint32_t challenger_kills
Definition wars.h:22
std::chrono::seconds left
Definition wars.h:24
std::string challenger_name
Definition wars.h:19
uint32_t challenger
Definition wars.h:18
uint32_t Against(uint32_t guild) const
The other side from the guild's, by its id.
Definition wars.h:27
uint32_t challenged
Definition wars.h:20