10#include "absl/strings/str_cat.h"
20absl::Status CheckParts(
const items::Item& item) {
21 const std::string& name = item.name();
22 if (item.has_weapon() && item.equip().slot() != items::SLOT_MAIN_HAND) {
23 return absl::InvalidArgumentError(absl::StrCat(
24 "the ", name,
" is a weapon, so it's held in the main hand"));
26 if (item.has_equip() && item.equip().slot() == items::SLOT_NONE) {
27 return absl::InvalidArgumentError(
28 absl::StrCat(
"the ", name,
" is worn, but has no slot to be worn in"));
32 if (
StackSize(item) > 1 && (item.has_equip() || !item.rolls().empty())) {
33 return absl::InvalidArgumentError(absl::StrCat(
34 "the ", name,
" stacks, so it can't be worn, or roll its own"));
36 for (
const items::Modifier& modifier : item.equip().modifiers()) {
37 if (modifier.stat() == items::STAT_NONE) {
38 return absl::InvalidArgumentError(
39 absl::StrCat(
"the ", name,
" adds to no stat"));
42 for (
const figures::Part& part : item.look().parts()) {
43 const absl::Status checked =
CheckPart(part);
45 return absl::InvalidArgumentError(
46 absl::StrCat(
"the ", name,
"'s look: ", checked.message()));
49 for (
const items::Roll& roll : item.rolls()) {
50 if (roll.stat() == items::STAT_NONE || roll.min() > roll.max()) {
51 return absl::InvalidArgumentError(
52 absl::StrCat(
"the ", name,
" rolls from ", roll.min(),
" to ",
53 roll.max(),
" on ", items::Stat_Name(roll.stat())));
56 return absl::OkStatus();
61absl::StatusOr<ItemTable>
ParseItems(std::string_view text) {
63 const absl::Status parsed =
ParseText(text, file);
67 absl::StatusOr<std::map<uint32_t, items::Item>> by_id =
ById(file.items());
69 return by_id.status();
71 for (
const auto& [
id, item] : *by_id) {
72 const absl::Status checked = CheckParts(item);
76 if (std::ranges::find(file.retired(),
id) != file.retired().end()) {
77 return absl::InvalidArgumentError(absl::StrCat(
78 "the ", item.name(),
" has the id ",
id,
", which is retired"));
81 ItemTable table{.by_id = *std::move(by_id),
82 .starting = {file.starting().begin(), file.starting().end()}};
83 for (
const items::Stack& stack : table.
starting) {
84 const auto item = table.
by_id.find(stack.item());
85 if (item == table.
by_id.end()) {
86 return absl::InvalidArgumentError(
87 absl::StrCat(
"there's no item ", stack.item(),
" to start with"));
90 return absl::InvalidArgumentError(
91 absl::StrCat(
"a new player starts with ",
CountOf(stack),
" of the ",
92 item->second.name(),
", which don't fit in a slot"));
99 return std::max(item.stack(), 1u);
103 return std::max(stack.count(), 1u);
108 case items::SLOT_MAIN_HAND:
110 case items::SLOT_OFF_HAND:
112 case items::SLOT_HEAD:
114 case items::SLOT_BODY:
116 case items::SLOT_LEGS:
118 case items::SLOT_FEET:
uint32_t CountOf(const items::Stack &stack)
How many are in the stack, which is one when it doesn't say.
absl::StatusOr< std::map< uint32_t, Entry > > ById(const google::protobuf::RepeatedPtrField< Entry > &entries)
absl::StatusOr< ItemTable > ParseItems(std::string_view text)
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
absl::Status CheckPart(const figures::Part &part)
uint32_t StackSize(const items::Item &item)
How many of the item go in one slot of a bag.
std::string_view SlotName(items::Slot slot)
The slot's name, as a player would say it, such as "main hand".
The items in the items file, by id.
std::vector< items::Stack > starting
What each new player starts with.
std::map< uint32_t, items::Item > by_id