10#include "google/protobuf/util/message_differencer.h"
20bool SameItem(
const items::Stack* there,
const items::Stack& offered) {
21 if (there ==
nullptr) {
24 items::Stack same = *there;
25 same.set_count(offered.count());
26 return google::protobuf::util::MessageDifferencer::Equals(same, offered);
29absl::Status NotTrading() {
30 return Refusal(absl::StatusCode::kFailedPrecondition,
31 Msg(
"You're not trading with anyone."));
37 const auto asked = asked_.find(other);
38 if (asked == asked_.end() || asked->second !=
id) {
44 sides_[id] = {.with = other};
45 sides_[other] = {.with =
id};
46 news_.insert({id, other});
51 const auto side = sides_.find(
id);
52 return side != sides_.end() ? side->second.with : 0;
56 return sides_.at(
id).offer;
60 const auto side = sides_.find(
id);
61 return side != sides_.end() ? side->second.revision : 0;
65 const auto side = sides_.find(
id);
66 return side != sides_.end() && place.has_bag() &&
67 side->second.offer.bag().contains(place.bag());
72 const protocol::TradeOffer& offer) {
73 const auto side = sides_.find(
id);
74 if (side == sides_.end()) {
77 protocol::TradeSide offered;
78 for (
const uint32_t slot : offer.bag()) {
79 const items::Stack* stack = bag.
At(
InBag(slot));
80 if (stack ==
nullptr) {
81 return Refusal(absl::StatusCode::kNotFound,
82 Msg(
"There's nothing there."));
84 if (!items.
by_id.contains(stack->item())) {
85 return Refusal(absl::StatusCode::kFailedPrecondition,
86 Msg(
"You can't trade that."));
88 (*offered.mutable_bag())[slot] = *stack;
90 if (offer.gold() > bag.
gold()) {
91 return Refusal(absl::StatusCode::kFailedPrecondition,
92 Msg(
"You haven't that much gold."));
94 offered.set_gold(offer.gold());
95 side->second.offer = std::move(offered);
98 const uint32_t other = side->second.with;
99 sides_.at(other).offer.set_ready(
false);
100 sides_.at(other).revision = ++side->second.revision;
101 news_.insert({id, other});
102 return absl::OkStatus();
105absl::StatusOr<bool>
Trades::Ready(uint32_t
id,
bool ready, uint32_t revision) {
106 const auto side = sides_.find(
id);
107 if (side == sides_.end()) {
112 if (ready && revision != side->second.revision) {
113 return Refusal(absl::StatusCode::kFailedPrecondition,
114 Msg(
"The trade has changed, so look at it again."));
116 side->second.offer.set_ready(ready);
117 const uint32_t other = side->second.with;
118 news_.insert({id, other});
119 return ready &&
SideOf(other).ready();
124 const uint32_t other =
With(
id);
125 const std::array offers = {&
SideOf(
id), &
SideOf(other)};
127 std::array<Inventory, 2> after = {bag, other_bag};
128 const auto refuse = [&](
const absl::Status& why) {
129 sides_.at(
id).offer.set_ready(
false);
130 sides_.at(other).offer.set_ready(
false);
131 news_.insert({id, other});
138 for (std::size_t side = 0; side < 2; ++side) {
139 for (
const auto& [slot, stack] : offers[side]->bag()) {
140 if (!SameItem(after[side].At(
InBag(slot)), stack) ||
141 !after[side].Take(
InBag(slot), stack.count()).ok()) {
143 Refusal(absl::StatusCode::kFailedPrecondition,
144 Msg(
"The items offered aren't all there any more.")));
147 if (!after[side].TakeGold(offers[side]->gold()).ok()) {
148 return refuse(
Refusal(absl::StatusCode::kFailedPrecondition,
149 Msg(
"The gold offered isn't all there any more.")));
153 for (std::size_t side = 0; side < 2; ++side) {
155 for (
const auto& [slot, stack] : offers[side]->bag()) {
156 if (to.
Add(items, stack) > 0) {
158 Refusal(absl::StatusCode::kResourceExhausted,
159 Msg(
"There isn't room for the trade in one of the bags.")));
162 if (to.
AddGold(offers[side]->gold()) > 0) {
163 return refuse(
Refusal(absl::StatusCode::kResourceExhausted,
164 Msg(
"That's more gold than can be carried.")));
167 bag = std::move(after[0]);
168 other_bag = std::move(after[1]);
170 return absl::OkStatus();
175 std::erase_if(asked_, [
id](
const auto& asked) {
return asked.second == id; });
176 const auto side = sides_.find(
id);
177 if (side == sides_.end()) {
180 const uint32_t other = side->second.with;
183 news_.insert({id, other});
uint32_t AddGold(uint32_t amount)
Adds the gold, up to kMostGold, and returns how much didn't fit.
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.
uint32_t End(uint32_t id)
absl::Status Complete(uint32_t id, Inventory &bag, Inventory &other_bag, const ItemTable &items)
bool Ask(uint32_t id, uint32_t other)
uint32_t With(uint32_t id) const
Who the player's trading with, by their id, or 0 for nobody.
const protocol::TradeSide & SideOf(uint32_t id) const
What the player offers, and whether they're ready, while they trade.
bool Holds(uint32_t id, const protocol::Place &place) const
absl::Status Offer(uint32_t id, const Inventory &bag, const ItemTable &items, const protocol::TradeOffer &offer)
std::set< uint32_t > TakeNews()
uint32_t RevisionOf(uint32_t id) const
absl::StatusOr< bool > Ready(uint32_t id, bool ready, uint32_t revision)
absl::Status Refusal(absl::StatusCode code, const Sentence &why)
Sentence Msg(std::string_view text, Parts... parts)
protocol::Place InBag(uint32_t slot)
A place in the bag, by its slot from zero.
The items in the items file, by id.
std::map< uint32_t, items::Item > by_id