nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
recipes.cc
Go to the documentation of this file.
1#include "content/recipes.h"
2
3#include <cstdint>
4#include <map>
5#include <string>
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, recipes::Recipe>> ParseRecipes(
15 std::string_view text) {
16 recipes::Recipes file;
17 const absl::Status parsed = ParseText(text, file);
18 if (!parsed.ok()) {
19 return parsed;
20 }
21 absl::StatusOr<std::map<uint32_t, recipes::Recipe>> by_id =
22 ById(file.recipes());
23 if (!by_id.ok()) {
24 return by_id.status();
25 }
26 // Zero is what protobuf gives a field we leave out. A recipe whose level we
27 // forgot would be everyone's, and one that takes nothing makes something
28 // from nothing.
29 for (const auto& [id, recipe] : *by_id) {
30 const std::string& name = recipe.name();
31 if (recipe.level() == 0) {
32 return absl::InvalidArgumentError(absl::StrCat(name, " has no level"));
33 }
34 if (recipe.seconds() == 0) {
35 return absl::InvalidArgumentError(absl::StrCat(name, " takes no time"));
36 }
37 if (recipe.inputs().empty()) {
38 return absl::InvalidArgumentError(absl::StrCat(name, " takes nothing"));
39 }
40 for (const recipes::ItemCount& input : recipe.inputs()) {
41 if (input.count() == 0) {
42 return absl::InvalidArgumentError(
43 absl::StrCat(name, " takes none of item ", input.item()));
44 }
45 }
46 if (recipe.output().count() == 0) {
47 return absl::InvalidArgumentError(
48 absl::StrCat(name, " makes none of item ", recipe.output().item()));
49 }
50 }
51 return by_id;
52}
53
54} // namespace nanommo
Definition chat.cc:9
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, recipes::Recipe > > ParseRecipes(std::string_view text)
Definition recipes.cc:14