nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
inbox.cc
Go to the documentation of this file.
1#include "client/inbox.h"
2
3#include <cstddef>
4#include <optional>
5#include <string_view>
6#include <utility>
7#include <vector>
8
9namespace nanommo {
10
11namespace {
12
13constexpr std::size_t kChatLines = 100;
14
15} // namespace
16
17absl::Status Inbox::Take(std::string_view message) {
18 protocol::World world;
19 if (!world.ParseFromArray(message.data(), static_cast<int>(message.size()))) {
20 return absl::DataLossError("couldn't read the server's message");
21 }
22 // Chat, moves, our bag, our quests, our guild, our trade, our duel and the
23 // events come on their own, with nothing of the world in them.
24 if (world.has_move()) {
25 move_ = world.move();
26 } else if (world.has_inventory()) {
27 inventory_ = world.inventory();
28 } else if (world.has_quests()) {
29 quests_ = world.quests();
30 } else if (world.has_guild()) {
31 guild_ = world.guild();
32 } else if (world.has_trade()) {
33 trade_ = world.trade();
34 } else if (world.has_duel()) {
35 duel_ = world.duel();
36 } else if (world.has_invite()) {
37 invite_ = world.invite();
38 } else if (!world.events().empty()) {
39 events_.insert(events_.end(), world.events().begin(), world.events().end());
40 } else if (world.chat().empty()) {
41 Add(world);
42 } else {
43 chat_.insert(chat_.end(), world.chat().begin(), world.chat().end());
44 while (chat_.size() > kChatLines) {
45 chat_.pop_front();
46 }
47 }
48 return absl::OkStatus();
49}
50
51void Inbox::Add(const protocol::World& part) {
52 parts_.Add(part);
53 world_ = parts_.world();
54}
55
56std::vector<protocol::Event> Inbox::TakeEvents() {
57 return std::exchange(events_, {});
58}
59
60std::optional<protocol::GuildInvite> Inbox::TakeInvite() {
61 return std::exchange(invite_, std::nullopt);
62}
63
64} // namespace nanommo
void Add(const protocol::World &part)
A part of the world that came on its own, as a datagram brings it.
Definition inbox.cc:51
const protocol::World & world() const
Definition inbox.h:29
std::vector< protocol::Event > TakeEvents()
The events since the last time, oldest first, handed over once.
Definition inbox.cc:56
std::optional< protocol::GuildInvite > TakeInvite()
The guild that asked us to join since the last time, once.
Definition inbox.cc:60
absl::Status Take(std::string_view message)
Files one of the server's messages, or returns why it isn't one.
Definition inbox.cc:17
Definition chat.cc:9