nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_SERVER_H_
2#define NANOMMO_SERVER_SERVER_H_
3
4#include <array>
5#include <chrono>
6#include <cstddef>
7#include <cstdint>
8#include <deque>
9#include <limits>
10#include <map>
11#include <memory>
12#include <optional>
13#include <set>
14#include <string>
15#include <string_view>
16#include <vector>
17
18#include "absl/status/status.h"
19#include "absl/status/statusor.h"
20#include "asio/io_context.hpp"
21#include "asio/ip/tcp.hpp"
22#include "asio/ip/udp.hpp"
23#include "asio/ssl/context.hpp"
24#include "asio/steady_timer.hpp"
25#include "content/content.h"
26#include "protocol/messages.h"
27#include "protocol/nanommo.pb.h"
28#include "server/copies.h"
29#include "server/events.h"
30#include "server/guilds.h"
31#include "server/inputs.h"
32#include "server/interest.h"
33#include "server/metrics.h"
34#include "server/players.h"
35#include "server/session.h"
36#include "server/storyteller.h"
37#include "server/token_bucket.h"
38#include "server/tokens.h"
39#include "server/wars.h"
40#include "server/world.h"
41
42namespace nanommo {
43
44class Server {
45 public:
46 /// Starts the server, or returns why it couldn't, such as the port
47 /// being taken. Clients connect over TLS, with the context. The name keeps
48 /// the world's players apart from other worlds', and the seed makes the
49 /// terrain.
50 static absl::StatusOr<std::unique_ptr<Server>> Create(
51 asio::io_context& io, uint16_t port, asio::ssl::context& tls,
52 std::chrono::nanoseconds interval, std::string name, uint32_t seed,
53 const Content& content, Metrics& metrics, Tokens& tokens,
54 Players& players, Guilds& guilds, Events& events,
55 Storyteller& storyteller);
56
57 /// In all its lands.
58 int players() const;
59
60 /// Hands the players over to the world's next instance, at the host and port
61 /// players reach it at: saves them, stops ticking and taking players, hands
62 /// their leases to it, and tells each client where to go. Returns how many
63 /// it told.
64 int HandOver(const std::string& host, uint16_t port);
65
66 /// The port it listens on, over TCP and UDP. Given port zero, Create has the
67 /// system pick a free one.
68 uint16_t port() const;
69
70 private:
71 Server(asio::io_context& io, asio::ssl::context& tls,
72 std::chrono::nanoseconds interval, std::string name, uint32_t seed,
73 const Content& content, Metrics& metrics, Tokens& tokens,
74 Players& players, Guilds& guilds, Events& events,
75 Storyteller& storyteller);
76
77 absl::Status Start(uint16_t port);
78 /// The World of the copy the player's in, or nullptr.
79 World* WorldOf(uint32_t id);
80 void Accept();
81 uint32_t HandleHello(Session& session, const std::string& bytes);
82 void Leave(uint32_t id);
83 void Say(uint32_t id, const std::string& text);
84 void Talk(uint32_t id, const protocol::Talk& talk);
85 void Learn(uint32_t id, const protocol::Learn& learn);
86 void TakeQuest(uint32_t id, const protocol::Quest& quest);
87 void TurnIn(uint32_t id, const protocol::Quest& quest);
88 void GiveUp(uint32_t id, uint32_t quest);
89 void Enlist(uint32_t id, uint32_t npc);
90 void Item(uint32_t id, const protocol::ItemAction& action);
91 void Trade(uint32_t id, const protocol::TradeAction& action);
92 void Duel(uint32_t id, const protocol::DuelAction& action);
93 void Craft(uint32_t id, const protocol::CraftAction& action);
94 void Guild(uint32_t id, const protocol::GuildAction& action);
95 void Found(uint32_t id, const std::string& name);
96 /// Asks the player of that name in, if they're playing in this world.
97 void Ask(uint32_t id, const std::string& name);
98 void Join(uint32_t id);
99 void QuitGuild(uint32_t id);
100 void SetRank(uint32_t id, uint32_t player, protocol::Rank rank);
101 void Kick(uint32_t id, uint32_t player);
102 void Disband(uint32_t id);
103 /// The leader asks the guild of that name to war, or says yes to it.
104 void AskToWar(uint32_t id, const std::string& name);
105 /// Reads the world's wars from MySQL, every few seconds, and tells the
106 /// guilds whose war has started, scored or ended.
107 void ReadWars();
108 /// Both guilds hear of each kill in their war.
109 void TellKills(const std::vector<WarKill>& kills);
110 /// The war the guild's in, or nullptr.
111 const War* WarOf(uint32_t guild) const;
112 /// To the guild's players in this world, at once, over TCP.
113 void GuildSay(uint32_t id, const std::string& text);
114 /// Sends each of the guild's players here a line from the server.
115 void TellGuild(uint32_t guild, const Sentence& news,
116 protocol::Channel channel = protocol::CHANNEL_WORLD);
117 /// Sends each of the guild's players here its roster with the next news.
118 void RefreshGuild(uint32_t guild);
119 /// Tells the player why their guild action failed: in the refusal's
120 /// sentence when it's about what they asked, and with "try again" when it's
121 /// MySQL.
122 void Refuse(uint32_t id, const absl::Status& why);
123 /// Sends the player a line from the server, which has no name: a sentence,
124 /// which their client says in its player's language.
125 void Notify(uint32_t id, const Sentence& said,
126 protocol::Channel channel = protocol::CHANNEL_WORLD);
127 void Notify(uint32_t id, const protocol::Message& message,
128 protocol::Channel channel = protocol::CHANNEL_WORLD);
129 void Receive();
130 /// Each player's next numbered input, one a tick, before the world steps.
131 void ApplyInputs();
132 void Tick();
133 /// Everyone who's changed since they were last saved, once a tick, with
134 /// the tick's war kills.
135 void SaveChanged(const std::vector<WarKill>& kills = {});
136 /// Keeps this instance's leases on its players, once a second.
137 void RenewLeases();
138 /// Those of the players who've changed since they were last saved, into
139 /// Redis, as one: everyone once a tick, the two sides of a trade as it's
140 /// done, and a player as they leave. Only while this instance has their
141 /// leases: it returns the players whose leases it's lost, when it saved
142 /// none of them, or an error when Redis couldn't take it, which it's said.
143 /// A trade's record goes in with them, and so do war kills.
144 absl::StatusOr<std::set<uint32_t>> Save(
145 const std::vector<uint32_t>& ids,
146 const std::optional<Traded>& traded = std::nullopt,
147 const std::vector<WarKill>& kills = {});
148 /// Closes the player's session without saving them, as another instance
149 /// has them, or what they have here can't be saved, and says why. They're
150 /// counted by the kind of why: "lease" or "trade".
151 void Drop(uint32_t id, const char* kind, std::string_view why);
152 /// What happened in the worlds this tick, into the events stream.
153 void RecordEvents(const std::vector<Event>& happened);
154 /// To each player near what happened, in their copy, after the world, so
155 /// that a client getting the world over TCP has the units it's about first.
156 void ShowEvents(const std::vector<Event>& happened);
157 void Broadcast();
158 void BroadcastCopy(const Copy& copy, bool full);
159 /// What goes to the player's client in this world: the pieces of the
160 /// players and monsters in view that have changed since the world it has
161 /// whole, and the rest by their ids, in still, which is one more piece. It
162 /// remembers what it sent, for the next.
163 std::vector<std::string_view> Delta(
164 uint32_t id, const Copy& copy, const View& view,
165 const std::map<uint32_t, std::string>& players,
166 const std::map<uint32_t, std::string>& monsters, int64_t sent,
167 std::string& still);
168 void SendChat();
169 /// To each player whose bag has changed since we last sent it.
170 void SendBags();
171 /// To each player as they join, and as their quests change.
172 void SendQuests();
173 /// To each player as they join, and as their guild changes.
174 void SendGuilds();
175 /// To each player whose trade opened, changed or ended this tick.
176 void SendTrades();
177 /// To each player whose duel was asked for, counted down, started or ended.
178 void SendDuels();
179 /// The guild's roster from MySQL, with who's playing here, and their level
180 /// as it is now.
181 std::vector<protocol::GuildMember> Roster(uint32_t guild);
182 void SendTo(const asio::ip::udp::endpoint& client,
183 std::shared_ptr<const std::string> part, std::string own);
184
185 asio::ip::tcp::acceptor acceptor_;
186 asio::ssl::context& tls_;
187 asio::ip::udp::socket udp_;
188 std::array<char, 1024> datagram_;
189 asio::ip::udp::endpoint sender_;
190 asio::steady_timer timer_;
191 std::chrono::nanoseconds interval_;
192 std::string name_;
193 /// The instance's id, its world's and its port's, as Consul has it, which
194 /// its leases on its players name.
195 std::string instance_;
196 uint32_t seed_;
197 Copies copies_;
198 Metrics& metrics_;
199 Tokens& tokens_;
200 Players& players_;
201 Guilds& guilds_;
202 Events& events_;
203 Storyteller& storyteller_;
204 std::map<uint32_t, std::shared_ptr<Session>> sessions_;
205 std::map<std::string, uint32_t> udp_tokens_;
206 std::map<uint32_t, asio::ip::udp::endpoint> udp_clients_;
207 std::map<uint32_t, Progress> saved_;
208 /// The lease this instance has on each of its players, as Redis has it.
209 std::map<uint32_t, std::string> leases_;
210 std::chrono::steady_clock::time_point renewed_;
211 int broadcasts_ = 0;
212 bool handing_over_ = false;
213 /// What the players said since the last tick.
214 protocol::World chat_;
215 std::map<uint32_t, TokenBucket> chat_limits_;
216 std::map<uint32_t, TokenBucket> story_limits_;
217 std::map<uint32_t, TokenBucket> item_limits_;
218 std::map<uint32_t, TokenBucket> guild_limits_;
219 std::map<uint32_t, TokenBucket> duel_limits_;
220 std::map<uint32_t, TokenBucket> craft_limits_;
221 /// The guild of each player here who's in one.
222 std::map<uint32_t, Membership> members_;
223 /// The players whose guild we've to send them.
224 std::set<uint32_t> guild_news_;
225 /// The world's wars, as MySQL had them when we last read them, and when
226 /// that was.
227 std::vector<War> wars_;
228 std::chrono::steady_clock::time_point wars_read_;
229 /// Which of the war kills here score.
230 Scoring scoring_;
231 /// Each player's numbered inputs that haven't been applied yet, and the
232 /// last one that has.
233 Inputs inputs_;
234 /// What each client was sent in its last worlds, by when each was sent:
235 /// the copy of the land it was in, as ids are a land's own, and each
236 /// player's and monster's piece, by its hash.
237 struct Sent {
238 const Copy* copy = nullptr;
239 std::map<uint64_t, std::size_t> pieces;
240 };
241 std::map<uint32_t, std::map<int64_t, Sent>> history_;
242 /// The newest world each client has whole, by when it was sent.
243 std::map<uint32_t, int64_t> seen_;
244 /// The revision of each player's bag we sent them last.
245 std::map<uint32_t, uint32_t> sent_bags_;
246 /// And of their quests.
247 std::map<uint32_t, uint32_t> sent_quests_;
248};
249
250} // namespace nanommo
251
252#endif // NANOMMO_SERVER_SERVER_H_
static absl::StatusOr< std::unique_ptr< Server > > Create(asio::io_context &io, uint16_t port, asio::ssl::context &tls, std::chrono::nanoseconds interval, std::string name, uint32_t seed, const Content &content, Metrics &metrics, Tokens &tokens, Players &players, Guilds &guilds, Events &events, Storyteller &storyteller)
Definition server.cc:124
int HandOver(const std::string &host, uint16_t port)
Definition server.cc:1067
int players() const
In all its lands.
Definition server.cc:159
uint16_t port() const
Definition server.cc:161
Definition chat.cc:9
A land, running: a World of it, with its NPCs in their cells.
Definition copies.h:20
What the players in a cell get: the players, monsters and NPCs near it.
Definition interest.h:45