nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
factions.cc
Go to the documentation of this file.
1#include "content/factions.h"
2
3#include <cstdint>
4#include <map>
5#include <set>
6#include <string_view>
7
8#include "absl/strings/str_cat.h"
9#include "content/checks.h"
10#include "content/text.h"
11
12namespace nanommo {
13
14absl::StatusOr<std::map<uint32_t, factions::Faction>> ParseFactions(
15 std::string_view text) {
16 factions::Factions file;
17 const absl::Status parsed = ParseText(text, file);
18 if (!parsed.ok()) {
19 return parsed;
20 }
21 absl::StatusOr<std::map<uint32_t, factions::Faction>> by_id =
22 ById(file.factions());
23 if (!by_id.ok()) {
24 return by_id.status();
25 }
26 // Zero is what protobuf gives a field we leave out: a faction nobody could
27 // see by a name, or swear to. A herald swears players to one faction.
28 std::set<uint32_t> heralds;
29 for (const auto& [id, faction] : *by_id) {
30 if (faction.color() == 0) {
31 return absl::InvalidArgumentError(
32 absl::StrCat(faction.name(), " has no colour"));
33 }
34 if (faction.herald() == 0) {
35 return absl::InvalidArgumentError(
36 absl::StrCat(faction.name(), " has no herald"));
37 }
38 if (!heralds.insert(faction.herald()).second) {
39 return absl::InvalidArgumentError(
40 absl::StrCat("two factions have the herald ", faction.herald()));
41 }
42 }
43 return by_id;
44}
45
46const factions::Faction* HeraldOf(
47 const std::map<uint32_t, factions::Faction>& factions, uint32_t kind) {
48 for (const auto& [id, faction] : factions) {
49 if (faction.herald() == kind) {
50 return &faction;
51 }
52 }
53 return nullptr;
54}
55
56} // namespace nanommo
Definition chat.cc:9
const factions::Faction * HeraldOf(const std::map< uint32_t, factions::Faction > &factions, uint32_t kind)
Definition factions.cc:46
absl::StatusOr< std::map< uint32_t, Entry > > ById(const google::protobuf::RepeatedPtrField< Entry > &entries)
Definition checks.h:19
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
Definition text.cc:32
absl::StatusOr< std::map< uint32_t, factions::Faction > > ParseFactions(std::string_view text)
Definition factions.cc:14