nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
events.cc
Go to the documentation of this file.
1#include "server/events.h"
2
3#include <optional>
4#include <string>
5#include <string_view>
6#include <vector>
7
8#include "nlohmann/json.hpp"
9#include "terrain/lands.h"
10
11namespace nanommo {
12
13namespace {
14
15std::string_view Name(Event::Kind kind) {
16 switch (kind) {
18 return "kill";
20 return "death";
22 return "loot";
24 return "prize";
26 return "level";
28 return "take";
30 return "turn_in";
32 return "use";
34 return "hit";
36 return "hurt";
38 return "heal";
40 return "craft";
41 }
42 return "";
43}
44
45// The name of what the event's about: the monster killed, or that killed the
46// player, the item, such as one made, or the quest.
47std::string What(const Event& event, const Content& content) {
48 switch (event.kind) {
52 case Event::Kind::kHurt: {
53 const auto kind = content.monsters.kinds.find(event.monster);
54 return kind != content.monsters.kinds.end() ? kind->second.name() : "";
55 }
60 const auto item = content.items.by_id.find(event.item);
61 return item != content.items.by_id.end() ? item->second.name() : "";
62 }
65 const auto quest = content.quests.find(event.quest);
66 return quest != content.quests.end() ? quest->second.name() : "";
67 }
70 return "";
71 }
72 return "";
73}
74
75} // namespace
76
77std::string Row(const Event& event, const Content& content,
78 std::string_view world, absl::Time time) {
79 const nlohmann::json row = {
80 // To the millisecond, in UTC, as ClickHouse reads a DateTime64(3).
81 {"time",
82 absl::FormatTime("%Y-%m-%d %H:%M:%E3S", time, absl::UTCTimeZone())},
83 {"world", world},
84 {"land", event.land >= 0 && event.land < static_cast<int>(kLands.size())
85 ? kLands[event.land].name
86 : ""},
87 {"kind", Name(event.kind)},
88 {"what", What(event, content)},
89 {"player", event.player},
90 {"x", event.x},
91 {"z", event.z},
92 {"monster", event.monster},
93 {"item", event.item},
94 {"count", event.count},
95 {"quest", event.quest},
96 {"level", event.level},
97 };
98 return row.dump();
99}
100
102 switch (kind) {
112 return true;
116 return false;
117 }
118 return false;
119}
120
121absl::Status Events::Add(const std::vector<std::string>& rows) {
122 std::vector<std::vector<std::string>> commands;
123 commands.reserve(rows.size());
124 for (const std::string& row : rows) {
125 // With ~, Redis trims the stream a block at a time, which costs less than
126 // trimming it to the entry.
127 commands.push_back({"XADD", kEventStream, "MAXLEN", "~",
128 std::to_string(kKeep), "*", "row", row});
129 }
130 return redis_.Pipeline(commands).status();
131}
132
133} // namespace nanommo
static constexpr int kKeep
Definition events.h:38
absl::Status Add(const std::vector< std::string > &rows)
Adds the rows to the stream, all of them in a single round trip.
Definition events.cc:121
Definition chat.cc:9
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
constexpr char kEventStream[]
Definition events.h:20
Part Name(std::string_view name)
Definition messages.cc:113
bool ForAnalytics(Event::Kind kind)
Definition events.cc:101
std::string Row(const Event &event, const Content &content, std::string_view world, absl::Time time)
Definition events.cc:77