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 "client/messages.h"
2
3#include <cstdint>
4#include <optional>
5#include <string>
6#include <utility>
7#include <vector>
8
9#include "absl/strings/str_cat.h"
10#include "client/names.h"
11#include "client/translations.h"
12#include "protocol/messages.h"
13
14namespace nanommo {
15
16namespace {
17
18template <typename Table>
19const auto* Find(const Table& table, uint32_t id) {
20 const auto found = table.find(id);
21 return found != table.end() ? &found->second : nullptr;
22}
23
24std::string Translated(const protocol::Message& message,
25 const Content* content);
26
27// The content's name, by the arg's id, in the language in use, or nothing
28// for what this client's content hasn't got, which ClientHash keeps from
29// happening.
30std::optional<std::string> NameIn(const protocol::Arg& arg,
31 const Content& content) {
32 switch (arg.value_case()) {
33 case protocol::Arg::kItem:
34 if (const auto* item = Find(content.items.by_id, arg.item())) {
35 return arg.count() == 0 ? NameOf(*item) : NameOf(*item, arg.count());
36 }
37 break;
38 case protocol::Arg::kMonster:
39 if (const auto* kind = Find(content.monsters.kinds, arg.monster())) {
40 return arg.count() == 0 ? NameOf(*kind) : NameOf(*kind, arg.count());
41 }
42 break;
43 case protocol::Arg::kNpc:
44 if (const auto* kind = Find(content.npcs.kinds, arg.npc())) {
45 return NameOf(*kind);
46 }
47 break;
48 case protocol::Arg::kQuest:
49 if (const auto* quest = Find(content.quests, arg.quest())) {
50 return NameOf(*quest);
51 }
52 break;
53 case protocol::Arg::kSkill:
54 if (const auto* skill = Find(content.skills.by_id, arg.skill())) {
55 return NameOf(*skill);
56 }
57 break;
58 case protocol::Arg::kFaction:
59 if (const auto* faction = Find(content.factions, arg.faction())) {
60 return NameOf(*faction);
61 }
62 break;
63 default:
64 break;
65 }
66 return std::nullopt;
67}
68
69// An arg as the language in use says it.
70std::string Shown(const protocol::Arg& arg, const Content* content) {
71 switch (arg.value_case()) {
72 case protocol::Arg::kNumber:
73 return absl::StrCat(arg.number());
74 case protocol::Arg::kName:
75 return arg.name();
76 case protocol::Arg::kPhrase:
77 return Translated(arg.phrase(), content);
78 case protocol::Arg::kSlot:
79 return NameOf(arg.slot());
80 case protocol::Arg::VALUE_NOT_SET:
81 return "";
82 default:
83 break;
84 }
85 std::optional<std::string> name;
86 if (content != nullptr) {
87 name = NameIn(arg, *content);
88 }
89 return name ? *std::move(name) : Tr("something");
90}
91
92std::string Translated(const protocol::Message& message,
93 const Content* content) {
94 std::vector<std::string> args;
95 args.reserve(message.args_size());
96 for (const protocol::Arg& arg : message.args()) {
97 args.push_back(Shown(arg, content));
98 }
99 const char* text = message.plural().empty()
100 ? Tr(message.text().c_str())
101 : TrN(message.text().c_str(), message.plural().c_str(),
102 message.count());
103 return Fill(text, args);
104}
105
106} // namespace
107
108std::string Translated(const protocol::Message& message,
109 const Content& content) {
110 return Translated(message, &content);
111}
112
113std::string Translated(const protocol::Message& message) {
114 return Translated(message, nullptr);
115}
116
117} // namespace nanommo
Definition chat.cc:9
std::string Translated(const protocol::Message &message, const Content &content)
Definition messages.cc:108
const char * Tr(const char *text)
std::string Fill(std::string_view text, std::span< const std::string > args)
Definition messages.cc:87
const char * TrN(const char *one, const char *many, uint64_t n)
The form for the count: in English, one for 1 and many for the rest.
const char * NameOf(const items::Item &item)
Definition names.cc:14
std::string Shown(const char *label)
The part of an ImGui label that's shown: what's before any "##".
Definition theme.cc:308