nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
messages.cc
Go to the documentation of this file.
1#include "protocol/messages.h"
2
3#include <cstddef>
4#include <cstdint>
5#include <optional>
6#include <span>
7#include <string>
8#include <string_view>
9#include <utility>
10#include <vector>
11
12#include "absl/status/status.h"
13#include "absl/strings/ascii.h"
14#include "absl/strings/cord.h"
15#include "absl/strings/str_cat.h"
16
17namespace nanommo {
18
19namespace {
20
21// Where a Refusal keeps its sentence in a status, by the message's type.
22constexpr std::string_view kPayload =
23 "type.googleapis.com/nanommo.protocol.Message";
24
25Part Made(protocol::Arg arg, std::string english) {
26 return {.arg = std::move(arg), .english = std::move(english)};
27}
28
29// The English of so many, from a plural's two forms.
30std::string Plural(const std::pair<std::string, std::string>& forms,
31 uint32_t count) {
32 const std::string counted = absl::StrCat(count);
33 return Fill(count == 1 ? forms.first : forms.second, {&counted, 1});
34}
35
36template <typename Table>
37const auto* Find(const Table& table, uint32_t id) {
38 const auto found = table.find(id);
39 return found != table.end() ? &found->second : nullptr;
40}
41
42// An arg's English, from the content's names by their ids, as it was made.
43std::string EnglishOf(const protocol::Arg& arg, const Content& content) {
44 // What the content hasn't got.
45 const std::string something = "something";
46 switch (arg.value_case()) {
47 case protocol::Arg::kNumber:
48 return absl::StrCat(arg.number());
49 case protocol::Arg::kName:
50 return arg.name();
51 case protocol::Arg::kPhrase:
52 return English(arg.phrase(), content);
53 case protocol::Arg::kItem: {
54 const auto* item = Find(content.items.by_id, arg.item());
55 return item != nullptr ? Of(*item, arg.count()).english : something;
56 }
57 case protocol::Arg::kMonster: {
58 const auto* kind = Find(content.monsters.kinds, arg.monster());
59 return kind != nullptr ? Of(*kind, arg.count()).english : something;
60 }
61 case protocol::Arg::kNpc: {
62 const auto* kind = Find(content.npcs.kinds, arg.npc());
63 return kind != nullptr ? Of(*kind).english : something;
64 }
65 case protocol::Arg::kQuest: {
66 const auto* quest = Find(content.quests, arg.quest());
67 return quest != nullptr ? Of(*quest).english : something;
68 }
69 case protocol::Arg::kSkill: {
70 const auto* skill = Find(content.skills.by_id, arg.skill());
71 return skill != nullptr ? Of(*skill).english : something;
72 }
73 case protocol::Arg::kSlot:
74 return Of(arg.slot()).english;
75 case protocol::Arg::kFaction: {
76 const auto* faction = Find(content.factions, arg.faction());
77 return faction != nullptr ? Of(*faction).english : something;
78 }
79 case protocol::Arg::VALUE_NOT_SET:
80 break;
81 }
82 return "";
83}
84
85} // namespace
86
87std::string Fill(std::string_view text, std::span<const std::string> args) {
88 std::string filled;
89 for (std::size_t i = 0; i < text.size(); ++i) {
90 const char next = i + 1 < text.size() ? text[i + 1] : '\0';
91 if (text[i] == '$' && next == '$') {
92 filled += '$';
93 ++i;
94 } else if (text[i] == '$' && absl::ascii_isdigit(next)) {
95 if (const auto at = static_cast<std::size_t>(next - '0');
96 at < args.size()) {
97 filled += args[at];
98 }
99 ++i;
100 } else {
101 filled += text[i];
102 }
103 }
104 return filled;
105}
106
107Part Number(uint64_t number) {
108 protocol::Arg arg;
109 arg.set_number(number);
110 return Made(std::move(arg), absl::StrCat(number));
111}
112
113Part Name(std::string_view name) {
114 protocol::Arg arg;
115 arg.set_name(name);
116 return Made(std::move(arg), std::string(name));
117}
118
119std::pair<std::string, std::string> Counted(const items::Item& item) {
120 const std::string_view name = item.name();
121 // A name that's plural already, such as trousers, shoes or boots, comes in
122 // pairs.
123 if (name.ends_with('s')) {
124 return {absl::StrCat("a pair of ", name),
125 absl::StrCat("$0 pairs of ", name)};
126 }
127 const bool vowel =
128 !name.empty() &&
129 std::string_view("AEIOU").find(name.front()) != std::string_view::npos;
130 return {absl::StrCat(vowel ? "an " : "a ", name),
131 absl::StrCat("$0 ", name, "s")};
132}
133
134std::pair<std::string, std::string> Counted(const monsters::Kind& kind) {
135 return {absl::StrCat("$0 ", kind.name()),
136 absl::StrCat("$0 ", kind.name(), "s")};
137}
138
139Part Of(const items::Item& item, uint32_t count) {
140 protocol::Arg arg;
141 arg.set_item(item.id());
142 arg.set_count(count);
143 return Made(std::move(arg),
144 count == 0 ? item.name() : Plural(Counted(item), count));
145}
146
147Part Of(const ItemTable& items, uint32_t item, uint32_t count) {
148 const items::Item* found = Find(items.by_id, item);
149 return found != nullptr ? Of(*found, count) : Of(Msg("something"));
150}
151
152Part Of(const monsters::Kind& kind, uint32_t count) {
153 protocol::Arg arg;
154 arg.set_monster(kind.id());
155 arg.set_count(count);
156 return Made(std::move(arg),
157 count == 0 ? kind.name() : Plural(Counted(kind), count));
158}
159
160Part Of(const npcs::Kind& kind) {
161 protocol::Arg arg;
162 arg.set_npc(kind.id());
163 return Made(std::move(arg), kind.name());
164}
165
166Part Of(const quests::Quest& quest) {
167 protocol::Arg arg;
168 arg.set_quest(quest.id());
169 return Made(std::move(arg), quest.name());
170}
171
172Part Of(const skills::Skill& skill) {
173 protocol::Arg arg;
174 arg.set_skill(skill.id());
175 return Made(std::move(arg), skill.name());
176}
177
178Part Of(const factions::Faction& faction) {
179 protocol::Arg arg;
180 arg.set_faction(faction.id());
181 return Made(std::move(arg), faction.name());
182}
183
184Part Of(items::Slot slot) {
185 protocol::Arg arg;
186 arg.set_slot(slot);
187 return Made(std::move(arg), std::string(SlotName(slot)));
188}
189
190Part Of(Sentence phrase) {
191 protocol::Arg arg;
192 *arg.mutable_phrase() = std::move(phrase.message);
193 return Made(std::move(arg), std::move(phrase.english));
194}
195
197
198Sentence Make(std::string_view one, std::string_view many, uint64_t count,
199 std::vector<Part> parts) {
200 Sentence made;
201 made.message.set_text(one);
202 if (!many.empty()) {
203 made.message.set_plural(many);
204 made.message.set_count(count);
205 }
206 std::vector<std::string> english;
207 english.reserve(parts.size());
208 for (Part& part : parts) {
209 *made.message.add_args() = std::move(part.arg);
210 english.push_back(std::move(part.english));
211 }
212 made.english = Fill(!many.empty() && count != 1 ? many : one, english);
213 return made;
214}
215
216} // namespace messages_internal
217
218absl::Status Refusal(absl::StatusCode code, const Sentence& why) {
219 absl::Status refused(code, why.english);
220 refused.SetPayload(kPayload, absl::Cord(why.message.SerializeAsString()));
221 return refused;
222}
223
224Sentence SentenceOf(const absl::Status& status) {
225 Sentence said;
226 said.english = std::string(status.message());
227 const std::optional<absl::Cord> payload = status.GetPayload(kPayload);
228 if (!payload || !said.message.ParseFromString(std::string(*payload))) {
229 said.message.Clear();
230 said.message.set_text(said.english);
231 }
232 return said;
233}
234
235std::string English(const protocol::Message& message, const Content& content) {
236 std::vector<std::string> args;
237 args.reserve(message.args_size());
238 for (const protocol::Arg& arg : message.args()) {
239 args.push_back(EnglishOf(arg, content));
240 }
241 const bool many = !message.plural().empty() && message.count() != 1;
242 return Fill(many ? message.plural() : message.text(), args);
243}
244
245} // namespace nanommo
Sentence Make(std::string_view one, std::string_view many, uint64_t count, std::vector< Part > parts)
Definition messages.cc:198
Definition chat.cc:9
Part Number(uint64_t number)
A number, as it is.
Definition messages.cc:107
std::pair< std::string, std::string > Counted(const items::Item &item)
Definition messages.cc:119
absl::Status Refusal(absl::StatusCode code, const Sentence &why)
Definition messages.cc:218
Sentence SentenceOf(const absl::Status &status)
Definition messages.cc:224
std::string Fill(std::string_view text, std::span< const std::string > args)
Definition messages.cc:87
std::string English(const protocol::Message &message, const Content &content)
Definition messages.cc:235
Sentence Msg(std::string_view text, Parts... parts)
Definition messages.h:92
Part Name(std::string_view name)
Definition messages.cc:113
Part Of(const items::Item &item, uint32_t count)
Definition messages.cc:139
std::string_view SlotName(items::Slot slot)
The slot's name, as a player would say it, such as "main hand".
Definition items.cc:106
The items in the items file, by id.
Definition items.h:18
std::map< uint32_t, items::Item > by_id
Definition items.h:19
std::string english
Definition messages.h:28
protocol::Message message
Definition messages.h:34
std::string english
Definition messages.h:35