nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
checks.h
Go to the documentation of this file.
1#ifndef NANOMMO_CONTENT_CHECKS_H_
2#define NANOMMO_CONTENT_CHECKS_H_
3
4#include <cstdint>
5#include <map>
6
7#include "absl/status/status.h"
8#include "absl/status/statusor.h"
9#include "absl/strings/str_cat.h"
10#include "google/protobuf/repeated_ptr_field.h"
11#include "terrain/lands.h"
12
13namespace nanommo {
14
15/// Items, or kinds of monster or NPC, by id, or what's wrong with an id: an
16/// id of zero, which is what protobuf gives a field we leave out, or one that
17/// two of them have.
18template <typename Entry>
19absl::StatusOr<std::map<uint32_t, Entry>> ById(
20 const google::protobuf::RepeatedPtrField<Entry>& entries) {
21 std::map<uint32_t, Entry> by_id;
22 for (const Entry& entry : entries) {
23 if (entry.id() == 0) {
24 return absl::InvalidArgumentError(
25 absl::StrCat("the ", entry.name(), " has no id"));
26 }
27 if (!by_id.emplace(entry.id(), entry).second) {
28 return absl::InvalidArgumentError(
29 absl::StrCat("two of them have the id ", entry.id()));
30 }
31 }
32 return by_id;
33}
34
35/// Whether a spawn names a kind we have, in a land we have.
36template <typename Kind, typename Spawn>
37absl::Status CheckSpawn(const std::map<uint32_t, Kind>& kinds,
38 const Spawn& spawn) {
39 if (!kinds.contains(spawn.kind())) {
40 return absl::InvalidArgumentError(
41 absl::StrCat("there's no kind ", spawn.kind()));
42 }
43 if (!IsLand(spawn.land())) {
44 return absl::InvalidArgumentError(
45 absl::StrCat("there's no land called ", spawn.land()));
46 }
47 return absl::OkStatus();
48}
49
50} // namespace nanommo
51
52#endif // NANOMMO_CONTENT_CHECKS_H_
Definition chat.cc:9
absl::StatusOr< std::map< uint32_t, Entry > > ById(const google::protobuf::RepeatedPtrField< Entry > &entries)
Definition checks.h:19
absl::Status CheckSpawn(const std::map< uint32_t, Kind > &kinds, const Spawn &spawn)
Whether a spawn names a kind we have, in a land we have.
Definition checks.h:37
bool IsLand(std::string_view name)
Definition lands.cc:19