nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
characters.cc
Go to the documentation of this file.
1#include "server/characters.h"
2
3#include <map>
4#include <optional>
5#include <string>
6#include <utility>
7#include <vector>
8
9#include "absl/strings/numbers.h"
10#include "absl/strings/str_cat.h"
11#include "protocol/limits.h"
12#include "protocol/messages.h"
13#include "server/accounts.h"
14#include "server/guilds.h"
15#include "server/levels.h"
16
17namespace nanommo {
18
19absl::StatusOr<std::vector<protocol::CharacterListing>> Characters::List(
20 uint32_t account) {
21 const absl::StatusOr<Result> rows = database_.Run(
22 "SELECT id, name, world, experience, land, COALESCE(weapon, 0), "
23 "items_revision IS NULL, build, skin, hair_color, hair, shirt "
24 "FROM players WHERE account_id = ? ORDER BY id",
25 {absl::StrCat(account)});
26 if (!rows.ok()) {
27 return rows.status();
28 }
29 std::vector<protocol::CharacterListing> characters;
30 std::map<uint32_t, protocol::CharacterListing*> by_id;
31 characters.reserve(rows->rows.size());
32 for (const std::vector<std::string>& row : rows->rows) {
33 protocol::CharacterListing& character = characters.emplace_back();
34 uint32_t id = 0;
35 uint32_t experience = 0;
36 uint32_t weapon = 0;
37 if (!absl::SimpleAtoi(row[0], &id) ||
38 !absl::SimpleAtoi(row[3], &experience) ||
39 !absl::SimpleAtoi(row[5], &weapon)) {
40 return absl::InternalError("a character that isn't numbers");
41 }
42 character.set_id(id);
43 character.set_name(row[1]);
44 character.set_world(row[2]);
45 character.set_level(static_cast<uint32_t>(LevelFor(experience).number));
46 character.set_land(row[4]);
47 character.set_weapon(weapon);
48 character.set_fresh(row[6] == "1");
49 // Its looks, unless it's from before there were looks to choose.
50 if (!row[7].empty()) {
51 uint32_t build = 0;
52 uint32_t skin = 0;
53 uint32_t hair_color = 0;
54 uint32_t hair = 0;
55 if (!absl::SimpleAtoi(row[7], &build) ||
56 !absl::SimpleAtoi(row[8], &skin) ||
57 !absl::SimpleAtoi(row[9], &hair_color) ||
58 !absl::SimpleAtoi(row[10], &hair)) {
59 return absl::InternalError("looks that aren't numbers");
60 }
61 protocol::Looks& looks = *character.mutable_looks();
62 looks.set_build(build);
63 looks.set_skin(skin);
64 looks.set_hair_color(hair_color);
65 looks.set_hair(hair);
66 // And the colour of their clothes, unless they're from before there was
67 // one to choose.
68 uint32_t shirt = 0;
69 if (!row[11].empty()) {
70 if (!absl::SimpleAtoi(row[11], &shirt)) {
71 return absl::InternalError("looks that aren't numbers");
72 }
73 looks.set_shirt(shirt);
74 }
75 }
76 }
77 for (protocol::CharacterListing& character : characters) {
78 by_id[character.id()] = &character;
79 }
80 if (characters.empty()) {
81 return characters;
82 }
83 // What they wear: whatever's in a slot, not in the bag. The main hand's is
84 // the weapon, which the column above had for a character from before bags.
85 const absl::StatusOr<Result> worn = database_.Run(
86 "SELECT player_items.player, player_items.place, player_items.item "
87 "FROM player_items JOIN players ON players.id = player_items.player "
88 "WHERE players.account_id = ? AND player_items.place NOT LIKE 'bag %' "
89 "ORDER BY player_items.player, player_items.place",
90 {absl::StrCat(account)});
91 if (!worn.ok()) {
92 return worn.status();
93 }
94 for (const std::vector<std::string>& row : worn->rows) {
95 uint32_t id = 0;
96 uint32_t item = 0;
97 if (!absl::SimpleAtoi(row[0], &id) || !absl::SimpleAtoi(row[2], &item)) {
98 return absl::InternalError("a worn item that isn't numbers");
99 }
100 const auto found = by_id.find(id);
101 if (found == by_id.end()) {
102 continue;
103 }
104 if (row[1] == "main hand") {
105 found->second->set_weapon(item);
106 } else {
107 found->second->add_wearing(item);
108 }
109 }
110 return characters;
111}
112
113absl::StatusOr<uint32_t> Characters::Create(
114 uint32_t account, const std::string& world, const std::string& name,
115 const std::optional<protocol::Looks>& looks) {
116 if (const absl::Status checked = CheckUsername(name); !checked.ok()) {
117 return checked;
118 }
119 // The login server has no content to tell how many of each look there are,
120 // so it keeps any that fit their columns, and every client wraps each
121 // round to what there is: a look that's out of range draws as one there
122 // is, the same for everyone, so there's nothing in it to cheat with.
123 std::vector<std::string> values = {absl::StrCat(account), world, name};
124 const char* chosen = "NULL, NULL, NULL, NULL, NULL";
125 if (looks) {
126 for (const uint32_t look :
127 {looks->build(), looks->skin(), looks->hair_color(), looks->hair()}) {
128 if (look > kMostLook) {
129 return Refusal(absl::StatusCode::kInvalidArgument,
130 Msg("That isn't a look there is."));
131 }
132 values.push_back(absl::StrCat(look));
133 }
134 chosen = "?, ?, ?, ?, NULL";
135 // A 0.94.0 client sends no colour for their clothes, and their id picks
136 // it, as it did then.
137 if (looks->has_shirt()) {
138 if (looks->shirt() > kMostLook) {
139 return Refusal(absl::StatusCode::kInvalidArgument,
140 Msg("That isn't a look there is."));
141 }
142 values.push_back(absl::StrCat(looks->shirt()));
143 chosen = "?, ?, ?, ?, ?";
144 }
145 }
146 values.insert(values.end(), {absl::StrCat(account), world,
147 absl::StrCat(kCharactersPerWorld)});
148 // Counting and inserting in one statement, so that two at once can't both
149 // fit in the last place. The unique key on the world and the name says
150 // whether it's taken.
151 const absl::StatusOr<Result> inserted = database_.Run(
152 absl::StrCat("INSERT INTO players (account_id, world, name, x, z, build, "
153 "skin, hair_color, hair, shirt) SELECT ?, ?, ?, 0, 0, ",
154 chosen,
155 " FROM DUAL WHERE (SELECT COUNT(*) FROM players WHERE "
156 "account_id = ? AND world = ?) < ?"),
157 values);
158 if (absl::IsAlreadyExists(inserted.status())) {
159 return Refusal(absl::StatusCode::kAlreadyExists,
160 Msg("Someone in $0 is called that already.", Name(world)));
161 }
162 if (!inserted.ok()) {
163 return inserted.status();
164 }
165 if (inserted->insert_id == 0) {
166 return Refusal(absl::StatusCode::kResourceExhausted,
167 MsgN("You have $0 character in $1 already.",
168 "You have $0 characters in $1 already.",
170 }
171 return static_cast<uint32_t>(inserted->insert_id);
172}
173
174absl::Status Characters::Delete(uint32_t account, uint32_t id) {
175 // Only the account's own, which hands on a guild it leads as it goes, in
176 // one transaction, so that the guild changes only if the character goes.
177 return database_.Transaction([&]() -> absl::Status {
178 if (const absl::StatusOr<std::string> world = WorldOf(account, id);
179 !world.ok()) {
180 return world.status();
181 }
182 if (const absl::StatusOr<Left> left = LeaveGuild(database_, id);
183 !left.ok() && !absl::IsNotFound(left.status())) {
184 return left.status();
185 }
186 const absl::StatusOr<Result> deleted =
187 database_.Run("DELETE FROM players WHERE id = ? AND account_id = ?",
188 {absl::StrCat(id), absl::StrCat(account)});
189 if (!deleted.ok()) {
190 return deleted.status();
191 }
192 if (deleted->affected_rows == 0) {
193 return Refusal(absl::StatusCode::kNotFound,
194 Msg("That character isn't yours."));
195 }
196 return absl::OkStatus();
197 });
198}
199
200absl::StatusOr<std::string> Characters::WorldOf(uint32_t account, uint32_t id) {
201 const absl::StatusOr<Result> rows =
202 database_.Run("SELECT world FROM players WHERE id = ? AND account_id = ?",
203 {absl::StrCat(id), absl::StrCat(account)});
204 if (!rows.ok()) {
205 return rows.status();
206 }
207 if (rows->rows.empty()) {
208 return Refusal(absl::StatusCode::kNotFound,
209 Msg("That character isn't yours."));
210 }
211 return rows->rows.front()[0];
212}
213
214} // namespace nanommo
absl::StatusOr< uint32_t > Create(uint32_t account, const std::string &world, const std::string &name, const std::optional< protocol::Looks > &looks=std::nullopt)
absl::Status Delete(uint32_t account, uint32_t id)
absl::StatusOr< std::vector< protocol::CharacterListing > > List(uint32_t account)
Definition characters.cc:19
static constexpr uint32_t kMostLook
The most each of a character's looks can be, as its column keeps it.
Definition characters.h:22
absl::StatusOr< std::string > WorldOf(uint32_t account, uint32_t id)
Definition chat.cc:9
absl::Status CheckUsername(std::string_view username)
An InvalidArgument error says what's wrong, in words for the player.
Definition accounts.cc:13
Sentence MsgN(std::string_view one, std::string_view many, uint64_t count, Parts... parts)
Definition messages.h:100
absl::Status Refusal(absl::StatusCode code, const Sentence &why)
Definition messages.cc:218
constexpr int kCharactersPerWorld
Definition limits.h:8
Level LevelFor(uint32_t experience)
Definition levels.cc:5
Sentence Msg(std::string_view text, Parts... parts)
Definition messages.h:92
Part Name(std::string_view name)
Definition messages.cc:113
absl::StatusOr< Left > LeaveGuild(Database &database, uint32_t player)
Definition guilds.cc:634