9#include "absl/status/status.h"
16bool IsPlace(
const protocol::Place& place) {
17 switch (place.where_case()) {
18 case protocol::Place::kBag:
20 case protocol::Place::kWorn:
21 return place.worn() != items::SLOT_NONE &&
22 items::Slot_IsValid(place.worn());
23 case protocol::Place::WHERE_NOT_SET:
30uint32_t Key(
const protocol::Place& place) {
31 return place.has_bag() ? place.bag() :
static_cast<uint32_t
>(place.worn());
34bool SamePlace(
const protocol::Place& a,
const protocol::Place& b) {
35 return a.where_case() == b.where_case() && Key(a) == Key(b);
38const items::Item* Find(
const ItemTable& table, uint32_t
id) {
39 const auto found = table.by_id.find(
id);
40 return found != table.by_id.end() ? &found->second :
nullptr;
43absl::Status Nothing() {
44 return Refusal(absl::StatusCode::kNotFound,
Msg(
"There's nothing there."));
47absl::Status NotThatMuchGold() {
48 return Refusal(absl::StatusCode::kFailedPrecondition,
49 Msg(
"You haven't that much gold."));
52uint32_t StackSizeOf(
const ItemTable& table, uint32_t
id) {
53 const items::Item* item = Find(table,
id);
54 return item !=
nullptr ?
StackSize(*item) : 1;
59absl::Status CheckFits(
const ItemTable& table,
const items::Stack& stack,
60 const protocol::Place& place) {
61 if (!place.has_worn()) {
62 return absl::OkStatus();
64 const items::Item* item = Find(table, stack.item());
65 if (item ==
nullptr) {
66 return Refusal(absl::StatusCode::kFailedPrecondition,
67 Msg(
"You can't wear that."));
69 if (!item->has_equip()) {
70 return Refusal(absl::StatusCode::kFailedPrecondition,
71 Msg(
"You can't wear the $0.",
Of(*item)));
73 if (item->equip().slot() != place.worn()) {
75 return Refusal(absl::StatusCode::kFailedPrecondition,
76 Msg(
"The $0 goes in the $1 slot.",
Of(*item),
77 Of(item->equip().slot())));
79 return absl::OkStatus();
86 std::random_device device;
87 return uint64_t{device()} << 32 | device();
91 if (!IsPlace(place)) {
94 const auto& slots = place.has_bag() ? items_.bag() : items_.worn();
95 const auto found = slots.find(Key(place));
96 return found != slots.end() ? &found->second :
nullptr;
99items::Stack* Inventory::MutableAt(
const protocol::Place& place) {
100 if (!IsPlace(place)) {
104 place.has_bag() ? *items_.mutable_bag() : *items_.mutable_worn();
105 const auto found = slots.find(Key(place));
106 return found != slots.end() ? &found->second :
nullptr;
109void Inventory::Put(
const protocol::Place& place, items::Stack stack) {
111 place.has_bag() ? *items_.mutable_bag() : *items_.mutable_worn();
112 slots[Key(place)] = std::move(stack);
115void Inventory::Clear(
const protocol::Place& place) {
117 place.has_bag() ? *items_.mutable_bag() : *items_.mutable_worn();
118 slots.erase(Key(place));
122 uint32_t left =
CountOf(stack);
123 const items::Item* item = Find(table, stack.item());
124 if (item ==
nullptr) {
127 const uint32_t before = left;
129 auto& bag = *items_.mutable_bag();
131 for (uint32_t slot = 0; slot < kBagSlots && left > 0; ++slot) {
132 const auto found = bag.find(slot);
133 if (found != bag.end() && found->second.item() == stack.item() &&
134 found->second.count() < size) {
135 const uint32_t onto = std::min(left, size - found->second.count());
136 found->second.set_count(found->second.count() + onto);
141 for (uint32_t slot = 0; slot < kBagSlots && left > 0; ++slot) {
142 if (!bag.contains(slot)) {
143 items::Stack put = stack;
144 put.set_count(std::min(left, size));
146 bag[slot] = std::move(put);
156 bool numbered =
false;
157 for (
auto* slots : {items_.mutable_bag(), items_.mutable_worn()}) {
158 for (
auto& [slot, stack] : *slots) {
159 if (stack.serial() == 0 && StackSizeOf(table, stack.item()) == 1) {
172 const uint32_t added = std::min(amount, room);
174 items_.set_gold(
gold() + added);
177 return amount - added;
181 const items::Item* item = Find(table, stack.item());
182 if (item ==
nullptr || !item->has_equip() ||
186 items::Stack worn = stack;
194 const protocol::Place& from,
195 const protocol::Place& to, uint32_t count) {
196 if (!IsPlace(from) || !IsPlace(to)) {
197 return Refusal(absl::StatusCode::kInvalidArgument,
198 Msg(
"There's no such place."));
200 items::Stack* moving = MutableAt(from);
201 if (moving ==
nullptr) {
204 if (SamePlace(from, to)) {
205 return absl::OkStatus();
207 if (
const absl::Status fits = CheckFits(table, *moving, to); !fits.ok()) {
210 const uint32_t all = moving->count();
211 const uint32_t moved = count == 0 ? all : std::min(count, all);
212 items::Stack* there = MutableAt(to);
213 if (there ==
nullptr) {
214 items::Stack part = *moving;
215 part.set_count(moved);
216 moving->set_count(all - moved);
217 if (moving->count() == 0) {
220 Put(to, std::move(part));
221 }
else if (there->item() == moving->item() &&
222 StackSizeOf(table, there->item()) > 1) {
223 const uint32_t size = StackSizeOf(table, there->item());
224 const uint32_t room = size > there->count() ? size - there->count() : 0;
226 return Refusal(absl::StatusCode::kFailedPrecondition,
227 Msg(
"That stack is full."));
229 const uint32_t onto = std::min(moved, room);
230 there->set_count(there->count() + onto);
231 moving->set_count(all - onto);
232 if (moving->count() == 0) {
237 return Refusal(absl::StatusCode::kFailedPrecondition,
238 Msg(
"Only a whole stack can swap places."));
240 if (
const absl::Status fits = CheckFits(table, *there, from); !fits.ok()) {
243 std::swap(*moving, *there);
246 return absl::OkStatus();
250 items::Stack* stack = MutableAt(place);
251 if (stack ==
nullptr) {
254 if (stack->count() > 1) {
255 stack->set_count(stack->count() - 1);
260 return absl::OkStatus();
264 items::Stack* stack = MutableAt(place);
265 if (stack ==
nullptr || stack->count() < count) {
266 return Refusal(absl::StatusCode::kNotFound,
267 Msg(
"There aren't that many there."));
269 if (stack->count() > count) {
270 stack->set_count(stack->count() - count);
275 return absl::OkStatus();
279 if (amount >
gold()) {
280 return NotThatMuchGold();
283 items_.set_gold(
gold() - amount);
286 return absl::OkStatus();
290 if (
At(place) ==
nullptr) {
295 return absl::OkStatus();
bool Wear(const ItemTable &table, const items::Stack &stack)
uint32_t AddGold(uint32_t amount)
Adds the gold, up to kMostGold, and returns how much didn't fit.
static constexpr uint32_t kBagSlots
absl::Status Destroy(const protocol::Place &place)
Throws away all of what's at the place.
absl::Status Move(const ItemTable &table, const protocol::Place &from, const protocol::Place &to, uint32_t count=0)
absl::Status TakeOne(const protocol::Place &place)
Takes one off the stack at the place, as using one does.
uint32_t Add(const ItemTable &table, const items::Stack &stack)
const items::Stack * At(const protocol::Place &place) const
What's at the place, or nullptr when it's empty, or isn't a place.
void Number(const ItemTable &table)
static constexpr uint32_t kMostGold
absl::Status Take(const protocol::Place &place, uint32_t count)
absl::Status TakeGold(uint32_t amount)
Takes the gold away, or the error says there isn't that much.
uint32_t CountOf(const items::Stack &stack)
How many are in the stack, which is one when it doesn't say.
absl::Status Refusal(absl::StatusCode code, const Sentence &why)
uint32_t StackSize(const items::Item &item)
How many of the item go in one slot of a bag.
protocol::Place Worn(items::Slot slot)
A slot that's worn.
Sentence Msg(std::string_view text, Parts... parts)
Part Of(const items::Item &item, uint32_t count)
The items in the items file, by id.