nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
trades.cc
Go to the documentation of this file.
1#include "server/trades.h"
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <map>
7#include <set>
8#include <utility>
9
10#include "google/protobuf/util/message_differencer.h"
11#include "protocol/messages.h"
12#include "protocol/places.h"
13
14namespace nanommo {
15
16namespace {
17
18// Whether what's there is the item that was offered, with the same rolls,
19// however many of it there are.
20bool SameItem(const items::Stack* there, const items::Stack& offered) {
21 if (there == nullptr) {
22 return false;
23 }
24 items::Stack same = *there;
25 same.set_count(offered.count());
26 return google::protobuf::util::MessageDifferencer::Equals(same, offered);
27}
28
29absl::Status NotTrading() {
30 return Refusal(absl::StatusCode::kFailedPrecondition,
31 Msg("You're not trading with anyone."));
32}
33
34} // namespace
35
36bool Trades::Ask(uint32_t id, uint32_t other) {
37 const auto asked = asked_.find(other);
38 if (asked == asked_.end() || asked->second != id) {
39 asked_[id] = other;
40 return false;
41 }
42 asked_.erase(id);
43 asked_.erase(other);
44 sides_[id] = {.with = other};
45 sides_[other] = {.with = id};
46 news_.insert({id, other});
47 return true;
48}
49
50uint32_t Trades::With(uint32_t id) const {
51 const auto side = sides_.find(id);
52 return side != sides_.end() ? side->second.with : 0;
53}
54
55const protocol::TradeSide& Trades::SideOf(uint32_t id) const {
56 return sides_.at(id).offer;
57}
58
59uint32_t Trades::RevisionOf(uint32_t id) const {
60 const auto side = sides_.find(id);
61 return side != sides_.end() ? side->second.revision : 0;
62}
63
64bool Trades::Holds(uint32_t id, const protocol::Place& place) const {
65 const auto side = sides_.find(id);
66 return side != sides_.end() && place.has_bag() &&
67 side->second.offer.bag().contains(place.bag());
68}
69
70absl::Status Trades::Offer(uint32_t id, const Inventory& bag,
71 const ItemTable& items,
72 const protocol::TradeOffer& offer) {
73 const auto side = sides_.find(id);
74 if (side == sides_.end()) {
75 return NotTrading();
76 }
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."));
83 }
84 if (!items.by_id.contains(stack->item())) {
85 return Refusal(absl::StatusCode::kFailedPrecondition,
86 Msg("You can't trade that."));
87 }
88 (*offered.mutable_bag())[slot] = *stack;
89 }
90 if (offer.gold() > bag.gold()) {
91 return Refusal(absl::StatusCode::kFailedPrecondition,
92 Msg("You haven't that much gold."));
93 }
94 offered.set_gold(offer.gold());
95 side->second.offer = std::move(offered);
96 // There's something new to look at, so neither side is ready, and a ready
97 // sent for what was there before is for the revision before.
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();
103}
104
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()) {
108 return NotTrading();
109 }
110 // An offer can change while a ready's on its way, and the ready is for the
111 // offers the player saw.
112 if (ready && revision != side->second.revision) {
113 return Refusal(absl::StatusCode::kFailedPrecondition,
114 Msg("The trade has changed, so look at it again."));
115 }
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();
120}
121
122absl::Status Trades::Complete(uint32_t id, Inventory& bag, Inventory& other_bag,
123 const ItemTable& items) {
124 const uint32_t other = With(id);
125 const std::array offers = {&SideOf(id), &SideOf(other)};
126 // Into copies, so that it's all of it or none.
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});
132 return why;
133 };
134 // First each side gives up what it offered, so that its bag has that room
135 // for what it gets. Each place has to have the item still, and at least as
136 // many as were offered, or Take fails: a kill can add to a stack that's
137 // offered, and what it adds stays the player's.
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()) {
142 return refuse(
143 Refusal(absl::StatusCode::kFailedPrecondition,
144 Msg("The items offered aren't all there any more.")));
145 }
146 }
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.")));
150 }
151 }
152 // Then each gets the copies of what the other offered.
153 for (std::size_t side = 0; side < 2; ++side) {
154 Inventory& to = after[1 - side];
155 for (const auto& [slot, stack] : offers[side]->bag()) {
156 if (to.Add(items, stack) > 0) {
157 return refuse(
158 Refusal(absl::StatusCode::kResourceExhausted,
159 Msg("There isn't room for the trade in one of the bags.")));
160 }
161 }
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.")));
165 }
166 }
167 bag = std::move(after[0]);
168 other_bag = std::move(after[1]);
169 End(id);
170 return absl::OkStatus();
171}
172
173uint32_t Trades::End(uint32_t id) {
174 asked_.erase(id);
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()) {
178 return 0;
179 }
180 const uint32_t other = side->second.with;
181 sides_.erase(side);
182 sides_.erase(other);
183 news_.insert({id, other});
184 return other;
185}
186
187std::set<uint32_t> Trades::TakeNews() { return std::exchange(news_, {}); }
188
189} // namespace nanommo
uint32_t AddGold(uint32_t amount)
Adds the gold, up to kMostGold, and returns how much didn't fit.
Definition inventory.cc:170
uint32_t Add(const ItemTable &table, const items::Stack &stack)
Definition inventory.cc:121
uint32_t gold() const
Definition inventory.h:37
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.
Definition inventory.cc:90
uint32_t End(uint32_t id)
Definition trades.cc:173
absl::Status Complete(uint32_t id, Inventory &bag, Inventory &other_bag, const ItemTable &items)
Definition trades.cc:122
bool Ask(uint32_t id, uint32_t other)
Definition trades.cc:36
uint32_t With(uint32_t id) const
Who the player's trading with, by their id, or 0 for nobody.
Definition trades.cc:50
const protocol::TradeSide & SideOf(uint32_t id) const
What the player offers, and whether they're ready, while they trade.
Definition trades.cc:55
bool Holds(uint32_t id, const protocol::Place &place) const
Definition trades.cc:64
absl::Status Offer(uint32_t id, const Inventory &bag, const ItemTable &items, const protocol::TradeOffer &offer)
Definition trades.cc:70
std::set< uint32_t > TakeNews()
Definition trades.cc:187
uint32_t RevisionOf(uint32_t id) const
Definition trades.cc:59
absl::StatusOr< bool > Ready(uint32_t id, bool ready, uint32_t revision)
Definition trades.cc:105
Definition chat.cc:9
absl::Status Refusal(absl::StatusCode code, const Sentence &why)
Definition messages.cc:218
Sentence Msg(std::string_view text, Parts... parts)
Definition messages.h:92
protocol::Place InBag(uint32_t slot)
A place in the bag, by its slot from zero.
Definition places.cc:7
The items in the items file, by id.
Definition items.h:18
std::map< uint32_t, items::Item > by_id
Definition items.h:19