nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
guilds.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_GUILDS_H_
2#define NANOMMO_SERVER_GUILDS_H_
3
4#include <chrono>
5#include <cstdint>
6#include <string>
7#include <string_view>
8#include <vector>
9
10#include "absl/status/status.h"
11#include "absl/status/statusor.h"
12#include "protocol/nanommo.pb.h"
13#include "server/database.h"
14#include "server/redis.h"
15#include "server/wars.h"
16
17namespace nanommo {
18
19/// Whether a guild may be called that: 3 to 24 letters, with single spaces
20/// between words, or an InvalidArgument error that says what's wrong.
21absl::Status CheckGuildName(std::string_view name);
22
23/// The guild a character is in, and its rank there.
24struct Membership {
25 uint32_t guild = 0;
26 std::string name;
27 protocol::Rank rank = protocol::RANK_MEMBER;
28};
29
30/// What leaving a guild did.
31struct Left {
32 uint32_t guild = 0;
33 std::string name;
34 /// Who leads it now, when the one who left led it, and someone's still in
35 /// it, by their character's id and name.
36 uint32_t successor = 0;
37 std::string successor_name;
38 /// Whether they were the last one in it, so it's gone.
39 bool ended = false;
40};
41
42/// What a change to another member of a guild did, for the news.
43struct Changed {
44 uint32_t guild = 0;
45 std::string guild_name;
46 /// The member it changed, by their name.
47 std::string player_name;
48};
49
50/// The character leaves their guild. When they led it, the one who's been in
51/// it longest leads it now, an officer before a member, and the last one out
52/// ends it. A NotFound error says they're in none, and FailedPrecondition that
53/// it's at war, which nobody leaves until it's over. It runs in a transaction
54/// the caller has begun: Leave's own, or a character's deletion, which calls
55/// it too, so that a guild never goes without a leader, and only if the
56/// character goes.
57absl::StatusOr<Left> LeaveGuild(Database& database, uint32_t player);
58
59/// Guilds and who's in them, in MySQL. A guild is in one world, as its
60/// characters are, and a character is in one guild at most. Unlike a player,
61/// whose own world saves them, a guild changes for players other than the one
62/// who acts, so it isn't in Redis: each change is a transaction. Only the
63/// invitations and the asks to war, which run out, are in Redis.
64class Guilds {
65 public:
66 /// How many characters a guild can have.
67 static constexpr int kMostMembers = 100;
68 /// How long an invitation waits for an answer.
69 static constexpr std::chrono::seconds kInvitationLife{60};
70 /// How long a guild's ask to war waits for the other's yes.
71 static constexpr std::chrono::seconds kWarAskLife{60};
72 /// How long a war lasts, from the yes.
73 static constexpr std::chrono::minutes kWarLength{30};
74
75 Guilds(Database& database, Redis& redis)
76 : database_(database), redis_(redis) {}
77
78 /// The guild the character is in, or a NotFound error for none.
79 absl::StatusOr<Membership> Of(uint32_t player);
80
81 /// A new guild in the world, which the character founds and leads. An
82 /// InvalidArgument error says what's wrong with the name, and an
83 /// AlreadyExists error that the name is taken in the world, or that the
84 /// character is in a guild already.
85 absl::StatusOr<Membership> Create(const std::string& world, uint32_t player,
86 const std::string& name);
87
88 /// Asks the player to join the guild of the one who asks, which an officer
89 /// or the leader can do. A NotFound error says the one who asks is in no
90 /// guild, a PermissionDenied one that they're a member, and AlreadyExists
91 /// that a guild has asked the player, a minute ago at most, and they haven't
92 /// answered. Returns the guild asked to join.
93 absl::StatusOr<Membership> Invite(uint32_t from, uint32_t player);
94
95 /// The player joins the guild that asked them, as a member. A NotFound
96 /// error says nobody asked, or that it's run out, or the guild is gone,
97 /// AlreadyExists that they're in a guild, and ResourceExhausted that the
98 /// guild is full.
99 absl::StatusOr<Membership> Accept(uint32_t player);
100
101 /// The player says no to the guild that asked, so that another can ask.
102 absl::Status Decline(uint32_t player);
103
104 /// The player leaves their guild, as LeaveGuild says, in a transaction.
105 absl::StatusOr<Left> Leave(uint32_t player);
106
107 /// The leader makes a member an officer, or an officer a member again. A
108 /// PermissionDenied error says the one who asks doesn't lead, NotFound that
109 /// the player isn't in their guild, and InvalidArgument that the rank isn't
110 /// one they can give.
111 absl::StatusOr<Changed> SetRank(uint32_t from, uint32_t player,
112 protocol::Rank rank);
113
114 /// Takes a player out of the guild of the one who asks, whose rank has to
115 /// be above theirs: the leader removes officers and members, and officers
116 /// members. A PermissionDenied error says it isn't, NotFound that the
117 /// player isn't in their guild, and FailedPrecondition that it's at war.
118 absl::StatusOr<Changed> Kick(uint32_t from, uint32_t player);
119
120 /// The leader ends their guild, for everyone in it, and its name is free
121 /// again. A PermissionDenied error says the one who asks doesn't lead it,
122 /// and FailedPrecondition that it's at war.
123 /// Returns the guild that ended.
124 absl::StatusOr<Membership> Disband(uint32_t from);
125
126 /// Who's in the guild, the highest rank first, and by name, as MySQL has
127 /// them: their levels are a few seconds behind a world's, and none of them
128 /// online, which only a world knows.
129 absl::StatusOr<std::vector<protocol::GuildMember>> Roster(uint32_t guild);
130
131 /// The leader asks the guild of that name in the world, whatever its case,
132 /// to war, and it returns the two, with no id, as the war hasn't started. Or
133 /// when that guild asked theirs first, a minute ago at most, it's a yes, and
134 /// it returns the war, which starts now. A NotFound error says there's no
135 /// guild of that name, or the one who asks is in none, PermissionDenied
136 /// that they don't lead it, InvalidArgument that it's their own,
137 /// AlreadyExists that they've asked it already, and FailedPrecondition
138 /// that either guild is at war.
139 absl::StatusOr<War> AskToWar(const std::string& world, uint32_t from,
140 const std::string& name);
141
142 /// The world's wars that haven't ended, with each side's kills, as MySQL
143 /// has them.
144 absl::StatusOr<std::vector<War>> Wars(const std::string& world);
145
146 private:
147 /// The leader of the guild the war's challenged says yes to the
148 /// challenger's ask, and the war starts, in a transaction.
149 absl::StatusOr<War> StartWar(const std::string& world, uint32_t from,
150 War war);
151
152 Database& database_;
153 Redis& redis_;
154};
155
156} // namespace nanommo
157
158#endif // NANOMMO_SERVER_GUILDS_H_
absl::StatusOr< Membership > Of(uint32_t player)
The guild the character is in, or a NotFound error for none.
Definition guilds.cc:158
absl::StatusOr< Membership > Accept(uint32_t player)
Definition guilds.cc:243
static constexpr std::chrono::seconds kWarAskLife
How long a guild's ask to war waits for the other's yes.
Definition guilds.h:71
absl::StatusOr< Membership > Invite(uint32_t from, uint32_t player)
Definition guilds.cc:219
absl::StatusOr< War > AskToWar(const std::string &world, uint32_t from, const std::string &name)
Definition guilds.cc:457
absl::StatusOr< Left > Leave(uint32_t player)
The player leaves their guild, as LeaveGuild says, in a transaction.
Definition guilds.cc:622
static constexpr std::chrono::minutes kWarLength
How long a war lasts, from the yes.
Definition guilds.h:73
absl::StatusOr< Changed > Kick(uint32_t from, uint32_t player)
Definition guilds.cc:349
Guilds(Database &database, Redis &redis)
Definition guilds.h:75
absl::StatusOr< std::vector< protocol::GuildMember > > Roster(uint32_t guild)
Definition guilds.cc:425
absl::Status Decline(uint32_t player)
The player says no to the guild that asked, so that another can ask.
Definition guilds.cc:618
absl::StatusOr< Changed > SetRank(uint32_t from, uint32_t player, protocol::Rank rank)
Definition guilds.cc:308
absl::StatusOr< std::vector< War > > Wars(const std::string &world)
Definition guilds.cc:581
static constexpr int kMostMembers
How many characters a guild can have.
Definition guilds.h:67
absl::StatusOr< Membership > Create(const std::string &world, uint32_t player, const std::string &name)
Definition guilds.cc:182
absl::StatusOr< Membership > Disband(uint32_t from)
Definition guilds.cc:395
static constexpr std::chrono::seconds kInvitationLife
How long an invitation waits for an answer.
Definition guilds.h:69
Definition chat.cc:9
absl::Status CheckGuildName(std::string_view name)
Definition guilds.cc:138
absl::StatusOr< Left > LeaveGuild(Database &database, uint32_t player)
Definition guilds.cc:634
What a change to another member of a guild did, for the news.
Definition guilds.h:43
uint32_t guild
Definition guilds.h:44
std::string guild_name
Definition guilds.h:45
std::string player_name
The member it changed, by their name.
Definition guilds.h:47
What leaving a guild did.
Definition guilds.h:31
bool ended
Whether they were the last one in it, so it's gone.
Definition guilds.h:39
uint32_t successor
Definition guilds.h:36
std::string successor_name
Definition guilds.h:37
uint32_t guild
Definition guilds.h:32
std::string name
Definition guilds.h:33
The guild a character is in, and its rank there.
Definition guilds.h:24
std::string name
Definition guilds.h:26
protocol::Rank rank
Definition guilds.h:27
uint32_t guild
Definition guilds.h:25