nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
messages.h
Go to the documentation of this file.
1#ifndef NANOMMO_PROTOCOL_MESSAGES_H_
2#define NANOMMO_PROTOCOL_MESSAGES_H_
3
4#include <concepts>
5#include <cstdint>
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 "content/content.h"
14#include "protocol/nanommo.pb.h"
15
16namespace nanommo {
17
18/// The text with each placeholder, $0 to $9, filled in with its arg, as
19/// absl::Substitute does, and $$ as a dollar. A placeholder without an arg
20/// comes out as nothing, as a sentence from a server can have any number.
21std::string Fill(std::string_view text, std::span<const std::string> args);
22
23/// What goes into one of a server's sentences: the arg, which the client
24/// shows in its player's language, and in English, for the servers' logs and
25/// tests.
26struct Part {
27 protocol::Arg arg;
28 std::string english;
29};
30
31/// A server's sentence: the message the client says in its player's
32/// language, and in English.
33struct Sentence {
34 protocol::Message message;
35 std::string english;
36};
37
38/// A number, as it is.
39Part Number(uint64_t number);
40
41/// A name a player gave, a character's, a guild's or a world's, or a
42/// version, which stays as it is in every language.
43Part Name(std::string_view name);
44
45/// An item, by its id: its name alone, "Bone", or, with a count, so many of
46/// it, "a Bone", "an Ash Blade" or "3 Bones".
47Part Of(const items::Item& item, uint32_t count = 0);
48
49/// So many of the item by its id, or "something" for one the table hasn't
50/// got.
51Part Of(const ItemTable& items, uint32_t item, uint32_t count);
52
53/// A kind of monster: its name alone, "Skeleton", or so many of it, "3
54/// Skeletons".
55Part Of(const monsters::Kind& kind, uint32_t count = 0);
56
57/// The rest of the content, by its id, and a slot of the bag, which have
58/// their names alone.
59Part Of(const npcs::Kind& kind);
60Part Of(const quests::Quest& quest);
61Part Of(const skills::Skill& skill);
62Part Of(const factions::Faction& faction);
63Part Of(items::Slot slot);
64
65/// A sentence inside another, which is translated on its own.
66Part Of(Sentence phrase);
67
68/// The two forms in English of so many of an item or of a kind of monster, a
69/// plural's, for one and for more, which the client's template has too: "a
70/// Bone" and "$0 Bones", and "$0 Skeleton" and "$0 Skeletons".
71std::pair<std::string, std::string> Counted(const items::Item& item);
72std::pair<std::string, std::string> Counted(const monsters::Kind& kind);
73
74namespace messages_internal {
75
76inline Part ToPart(Part part) { return part; }
77inline Part ToPart(Sentence phrase) { return Of(std::move(phrase)); }
78inline Part ToPart(std::integral auto number) {
79 return Number(static_cast<uint64_t>(number));
80}
81
82Sentence Make(std::string_view one, std::string_view many, uint64_t count,
83 std::vector<Part> parts);
84
85} // namespace messages_internal
86
87/// A server's sentence, by its English, with a part for each of its
88/// placeholders, in order: a number, a Name, the content's Of, or a sentence
89/// of its own. It marks the English for the template too, as N_ marks the
90/// client's.
91template <typename... Parts>
92Sentence Msg(std::string_view text, Parts... parts) {
94 text, "", 0, {messages_internal::ToPart(std::move(parts))...});
95}
96
97/// A plural: its English for one, and for any other count, which picks the
98/// form, in the client's language as TrN does.
99template <typename... Parts>
100Sentence MsgN(std::string_view one, std::string_view many, uint64_t count,
101 Parts... parts) {
103 one, many, count, {messages_internal::ToPart(std::move(parts))...});
104}
105
106/// A status that says why something was refused: in English as its message,
107/// and as a sentence, its payload, for the client to say in its player's
108/// language.
109absl::Status Refusal(absl::StatusCode code, const Sentence& why);
110
111/// What a status says: a Refusal's sentence, or, from a status that isn't
112/// one, its message as it is.
113Sentence SentenceOf(const absl::Status& status);
114
115/// A message as a client shows it in English, with the content's names from
116/// the content: what the servers' tests read of what a player hears.
117std::string English(const protocol::Message& message, const Content& content);
118
119} // namespace nanommo
120
121#endif // NANOMMO_PROTOCOL_MESSAGES_H_
Sentence Make(std::string_view one, std::string_view many, uint64_t count, std::vector< Part > parts)
Definition messages.cc:198
Part ToPart(Part part)
Definition messages.h:76
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
Sentence MsgN(std::string_view one, std::string_view many, uint64_t count, Parts... parts)
Definition messages.h:100
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
Mesh Parts(const Model &model, const std::vector< figures::Part > &parts, float metre)
Definition figures.cc:268
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
The items in the items file, by id.
Definition items.h:18
protocol::Arg arg
Definition messages.h:27
std::string english
Definition messages.h:28
protocol::Message message
Definition messages.h:34
std::string english
Definition messages.h:35