nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
trades.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_TRADES_H_
2#define NANOMMO_SERVER_TRADES_H_
3
4#include <cstdint>
5#include <map>
6#include <set>
7
8#include "absl/status/status.h"
9#include "absl/status/statusor.h"
10#include "content/items.h"
11#include "protocol/nanommo.pb.h"
12#include "server/inventory.h"
13
14namespace nanommo {
15
16/// The trades between the players in one copy of a land, which its World
17/// keeps, so that both players are always in it. A player asks another to
18/// trade, and the trade opens once the other asks them back. Each side offers
19/// stacks from their bag, by their places, and gold, and once both sides are
20/// ready, each gets what the other offered.
21///
22/// Nothing a client says is trusted. What's offered is held where it is until
23/// the trade's over, each change to an offer takes the trade to its next
24/// revision, which a ready has to name, and the trade gives nothing unless
25/// each place still has what was offered.
26class Trades {
27 public:
28 /// The player asks the other to trade, in place of anyone they asked before.
29 /// If the other asked them first, the trade between them opens, and it
30 /// returns true. Neither may be trading already.
31 bool Ask(uint32_t id, uint32_t other);
32
33 /// Who the player's trading with, by their id, or 0 for nobody.
34 uint32_t With(uint32_t id) const;
35
36 /// What the player offers, and whether they're ready, while they trade.
37 const protocol::TradeSide& SideOf(uint32_t id) const;
38
39 /// The revision of the player's trade, from 0 as it opens, which each change
40 /// to either offer takes to the next.
41 uint32_t RevisionOf(uint32_t id) const;
42
43 /// Whether the place is one the player offers from, which stays as it is
44 /// until the trade's over, as the trade takes what was offered from there.
45 bool Holds(uint32_t id, const protocol::Place& place) const;
46
47 /// What the player offers from their bag, in place of what they offered
48 /// before: copies of the stacks at the offer's places, whole, and gold they
49 /// have. Neither side is ready any more, as there's something new to look
50 /// at, and the trade's at its next revision. Or the error says why not, in
51 /// words for the player.
52 absl::Status Offer(uint32_t id, const Inventory& bag, const ItemTable& items,
53 const protocol::TradeOffer& offer);
54
55 /// Whether the player's ready to trade what's offered, both ways, at the
56 /// revision they saw. Returns whether both sides are now, or the error if
57 /// they aren't trading, or are ready for offers that have changed since.
58 absl::StatusOr<bool> Ready(uint32_t id, bool ready, uint32_t revision);
59
60 /// Once both sides are ready: each bag gives up what it offered and gets
61 /// what the other offered, all of it or none, and the trade's over. Each
62 /// place offered has to have at least as many as it did, of the same item
63 /// with the same rolls. If one hasn't, a bag hasn't room, or a side hasn't
64 /// the gold it offered, neither side is ready any more, and the error says
65 /// why, in words for both players.
66 absl::Status Complete(uint32_t id, Inventory& bag, Inventory& other_bag,
67 const ItemTable& items);
68
69 /// Ends the player's trade, if they're in one, their asking, and anyone's
70 /// asking them. Returns who they were trading with, or 0.
71 uint32_t End(uint32_t id);
72
73 /// The players whose trade has opened, changed or ended since the last
74 /// time, which it hands over.
75 std::set<uint32_t> TakeNews();
76
77 private:
78 /// One player's side of a trade: who with, by their id, their offer, and
79 /// the trade's revision, the same on both sides.
80 struct Side {
81 uint32_t with = 0;
82 protocol::TradeSide offer;
83 uint32_t revision = 0;
84 };
85
86 /// Who each player has asked to trade, by their ids.
87 std::map<uint32_t, uint32_t> asked_;
88 /// Both sides of each trade, by each player's id.
89 std::map<uint32_t, Side> sides_;
90 std::set<uint32_t> news_;
91};
92
93} // namespace nanommo
94
95#endif // NANOMMO_SERVER_TRADES_H_
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
The items in the items file, by id.
Definition items.h:18