nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
players.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_PLAYERS_H_
2#define NANOMMO_SERVER_PLAYERS_H_
3
4#include <chrono>
5#include <cstddef>
6#include <cstdint>
7#include <map>
8#include <optional>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
15#include "protocol/nanommo.pb.h"
16#include "server/database.h"
17#include "server/redis.h"
18#include "server/world.h"
19
20namespace nanommo {
21
22/// What we keep of a player from one session to the next.
23struct Progress {
24 float x = 0.0f;
25 float z = 0.0f;
26 uint32_t experience = 0;
27 int health = 0;
28 int land = 0;
29 /// What's in their main hand, which servers from before bags read, and the
30 /// website might.
31 uint32_t weapon = 0;
32 std::set<uint32_t> skills;
33 /// Their bag's revision, which goes up with each change to it, so that
34 /// telling whether it needs saving takes a number, not the bag.
35 uint32_t items_revision = 0;
36 /// The quests they're on, with how many they've killed for each, and the
37 /// ones they've done, by their ids.
38 std::map<uint32_t, uint32_t> quests;
39 std::set<uint32_t> quests_done;
40 /// Their quests' revision, which goes up with each change to them. Saved
41 /// with them, it tells a player who's given up all they were on from a hash
42 /// from before there were quests, which leaves MySQL's.
43 uint32_t quests_revision = 0;
44 /// The faction they've sworn to, by its id, or 0 for none. They swear to it
45 /// in the world, which saves it with the rest of them, as it does what they
46 /// learn, through Redis.
47 uint32_t faction = 0;
48
49 bool operator==(const Progress&) const = default;
50};
51
52/// A trade that's done, for the record: an id of its own, and each side, with
53/// what they gave, serials and all.
54struct Traded {
55 std::string id;
56 uint32_t player = 0;
57 protocol::TradeSide player_gave;
58 uint32_t partner = 0;
59 protocol::TradeSide partner_gave;
60};
61
62class Players {
63 public:
64 static constexpr int kBatch = 500;
65 /// The most rows of the bags that go in one statement.
66 static constexpr std::size_t kItemRows = 1000;
67 /// How long a lease on a player lasts, unless its instance renews it.
68 static constexpr std::chrono::milliseconds kLease{10000};
69
70 Players(Database& database, Redis& redis)
71 : database_(database), redis_(redis) {}
72
73 /// The character, by its id, in the world, from MySQL, with what Redis has
74 /// on top, as that's newer. A NotFound error says there's no such character
75 /// in this world.
76 absl::StatusOr<Player> Load(const std::string& world, uint32_t id);
77
78 /// Leases the character in the world to the instance, so that no other
79 /// instance plays them until it's released or runs out, and returns the
80 /// lease as Redis has it: the instance's id, and a fencing number, higher
81 /// than any lease's before it. A lease handed to the instance in a handover
82 /// is its already. FailedPrecondition, naming the instance, when another
83 /// instance has it.
84 absl::StatusOr<std::string> Lease(const std::string& world, uint32_t id,
85 const std::string& instance);
86
87 /// Keeps the leases, by player, for another kLease, all in one round trip:
88 /// those that are still as given. Returns the players whose leases aren't.
89 absl::StatusOr<std::set<uint32_t>> Renew(
90 const std::string& world, const std::map<uint32_t, std::string>& leases);
91
92 /// Ends the player's lease, if it's still as given, so that they can play
93 /// on another instance at once. One that another instance has since stays.
94 absl::Status Release(const std::string& world, uint32_t id,
95 const std::string& lease);
96
97 /// Hands each of the leases, by player, that's still as given, to the next
98 /// instance, with a new fencing number, all in one round trip. Returns the
99 /// players whose leases it handed over.
100 absl::StatusOr<std::set<uint32_t>> Pass(
101 const std::string& world, const std::map<uint32_t, std::string>& leases,
102 const std::string& next);
103
104 /// Into Redis, all of them in a single round trip, and marked unsaved, with
105 /// the bags of those whose bags changed, which are marked unsaved apart. It's
106 /// one transaction, so Redis keeps all of it or none: the two bags a trade
107 /// changed are never saved one without the other. And only while each of the
108 /// leases, by player, is as given, so that an instance that's lost a player
109 /// to another can't write over what that one saves. Returns the players
110 /// whose leases aren't, when it saved none of it. A trade's record goes into
111 /// the trades stream with its two bags, for the saver to take to MySQL with
112 /// them, and each war kill into the war_kills stream, for its war's score.
113 absl::StatusOr<std::set<uint32_t>> Save(
114 const std::string& world, const std::map<uint32_t, Progress>& changed,
115 const std::map<uint32_t, protocol::Inventory>& bags = {},
116 const std::map<uint32_t, std::string>& leases = {},
117 const std::optional<Traded>& traded = std::nullopt,
118 const std::vector<WarKill>& kills = {});
119
120 /// Takes the unsaved players of all the worlds from Redis to MySQL, a batch
121 /// at a time, each in a transaction with the bags that changed, and returns
122 /// how many it saved. The trades that are waiting go first, each in a batch
123 /// with both its players, into the trades table, and the war kills, a row
124 /// each, into the war_kills table. A batch stays in Redis, in a set
125 /// of its own, until MySQL has it, so that one a flush took and didn't save,
126 /// as its saver died, goes back with the unsaved players as the next flush
127 /// starts. There's one saver at a time.
128 absl::StatusOr<int> Flush();
129
130 /// How many players are marked unsaved in Redis, waiting for a flush, or in
131 /// a batch that isn't in MySQL yet, or both, each once.
132 absl::StatusOr<int> Unsaved();
133
134 /// The serials that more than one item in MySQL has, as only a copy of an
135 /// item can have another's.
136 absl::StatusOr<int> Duplicates();
137
138 private:
139 /// Puts a batch that wasn't saved back into the sets of those unsaved.
140 absl::Status PutBack();
141
142 Database& database_;
143 Redis& redis_;
144};
145
146} // namespace nanommo
147
148#endif // NANOMMO_SERVER_PLAYERS_H_
static constexpr int kBatch
Definition players.h:64
absl::Status Release(const std::string &world, uint32_t id, const std::string &lease)
Definition players.cc:456
absl::StatusOr< std::set< uint32_t > > Renew(const std::string &world, const std::map< uint32_t, std::string > &leases)
Definition players.cc:435
absl::StatusOr< std::set< uint32_t > > Pass(const std::string &world, const std::map< uint32_t, std::string > &leases, const std::string &next)
Definition players.cc:462
absl::StatusOr< int > Duplicates()
Definition players.cc:913
absl::StatusOr< int > Flush()
Definition players.cc:578
absl::StatusOr< int > Unsaved()
Definition players.cc:898
static constexpr std::chrono::milliseconds kLease
How long a lease on a player lasts, unless its instance renews it.
Definition players.h:68
static constexpr std::size_t kItemRows
The most rows of the bags that go in one statement.
Definition players.h:66
absl::StatusOr< std::set< uint32_t > > Save(const std::string &world, const std::map< uint32_t, Progress > &changed, const std::map< uint32_t, protocol::Inventory > &bags={}, const std::map< uint32_t, std::string > &leases={}, const std::optional< Traded > &traded=std::nullopt, const std::vector< WarKill > &kills={})
Definition players.cc:483
Players(Database &database, Redis &redis)
Definition players.h:70
absl::StatusOr< Player > Load(const std::string &world, uint32_t id)
Definition players.cc:284
absl::StatusOr< std::string > Lease(const std::string &world, uint32_t id, const std::string &instance)
Definition players.cc:415
Definition chat.cc:9
What we keep of a player from one session to the next.
Definition players.h:23
bool operator==(const Progress &) const =default
std::set< uint32_t > quests_done
Definition players.h:39
uint32_t items_revision
Definition players.h:35
std::set< uint32_t > skills
Definition players.h:32
uint32_t weapon
Definition players.h:31
uint32_t experience
Definition players.h:26
uint32_t faction
Definition players.h:47
uint32_t quests_revision
Definition players.h:43
std::map< uint32_t, uint32_t > quests
Definition players.h:38
uint32_t partner
Definition players.h:58
std::string id
Definition players.h:55
uint32_t player
Definition players.h:56
protocol::TradeSide player_gave
Definition players.h:57
protocol::TradeSide partner_gave
Definition players.h:59