/src/server/characters.cc
Line | Count | Source |
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 | | |
17 | | namespace nanommo { |
18 | | |
19 | | absl::StatusOr<std::vector<protocol::CharacterListing>> Characters::List( |
20 | 23 | uint32_t account) { |
21 | 23 | const absl::StatusOr<Result> rows = database_.Run( |
22 | 23 | "SELECT id, name, world, experience, land, COALESCE(weapon, 0), " |
23 | 23 | "items_revision IS NULL, build, skin, hair_color, hair, shirt " |
24 | 23 | "FROM players WHERE account_id = ? ORDER BY id", |
25 | 23 | {absl::StrCat(account)}); |
26 | 23 | if (!rows.ok()) { |
27 | 0 | return rows.status(); |
28 | 0 | } |
29 | 23 | std::vector<protocol::CharacterListing> characters; |
30 | 23 | std::map<uint32_t, protocol::CharacterListing*> by_id; |
31 | 23 | characters.reserve(rows->rows.size()); |
32 | 33 | for (const std::vector<std::string>& row : rows->rows) { |
33 | 33 | protocol::CharacterListing& character = characters.emplace_back(); |
34 | 33 | uint32_t id = 0; |
35 | 33 | uint32_t experience = 0; |
36 | 33 | uint32_t weapon = 0; |
37 | 33 | if (!absl::SimpleAtoi(row[0], &id) || |
38 | 33 | !absl::SimpleAtoi(row[3], &experience) || |
39 | 33 | !absl::SimpleAtoi(row[5], &weapon)) { |
40 | 0 | return absl::InternalError("a character that isn't numbers"); |
41 | 0 | } |
42 | 33 | character.set_id(id); |
43 | 33 | character.set_name(row[1]); |
44 | 33 | character.set_world(row[2]); |
45 | 33 | character.set_level(static_cast<uint32_t>(LevelFor(experience).number)); |
46 | 33 | character.set_land(row[4]); |
47 | 33 | character.set_weapon(weapon); |
48 | 33 | character.set_fresh(row[6] == "1"); |
49 | | // Its looks, unless it's from before there were looks to choose. |
50 | 33 | if (!row[7].empty()) { |
51 | 0 | uint32_t build = 0; |
52 | 0 | uint32_t skin = 0; |
53 | 0 | uint32_t hair_color = 0; |
54 | 0 | uint32_t hair = 0; |
55 | 0 | if (!absl::SimpleAtoi(row[7], &build) || |
56 | 0 | !absl::SimpleAtoi(row[8], &skin) || |
57 | 0 | !absl::SimpleAtoi(row[9], &hair_color) || |
58 | 0 | !absl::SimpleAtoi(row[10], &hair)) { |
59 | 0 | return absl::InternalError("looks that aren't numbers"); |
60 | 0 | } |
61 | 0 | protocol::Looks& looks = *character.mutable_looks(); |
62 | 0 | looks.set_build(build); |
63 | 0 | looks.set_skin(skin); |
64 | 0 | looks.set_hair_color(hair_color); |
65 | 0 | looks.set_hair(hair); |
66 | | // And the colour of their clothes, unless they're from before there was |
67 | | // one to choose. |
68 | 0 | uint32_t shirt = 0; |
69 | 0 | if (!row[11].empty()) { |
70 | 0 | if (!absl::SimpleAtoi(row[11], &shirt)) { |
71 | 0 | return absl::InternalError("looks that aren't numbers"); |
72 | 0 | } |
73 | 0 | looks.set_shirt(shirt); |
74 | 0 | } |
75 | 0 | } |
76 | 33 | } |
77 | 33 | for (protocol::CharacterListing& character : characters)23 { |
78 | 33 | by_id[character.id()] = &character; |
79 | 33 | } |
80 | 23 | if (characters.empty()) { |
81 | 10 | return characters; |
82 | 10 | } |
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 | 13 | const absl::StatusOr<Result> worn = database_.Run( |
86 | 13 | "SELECT player_items.player, player_items.place, player_items.item " |
87 | 13 | "FROM player_items JOIN players ON players.id = player_items.player " |
88 | 13 | "WHERE players.account_id = ? AND player_items.place NOT LIKE 'bag %' " |
89 | 13 | "ORDER BY player_items.player, player_items.place", |
90 | 13 | {absl::StrCat(account)}); |
91 | 13 | if (!worn.ok()) { |
92 | 0 | return worn.status(); |
93 | 0 | } |
94 | 13 | for (const std::vector<std::string>& row : worn->rows) { |
95 | 0 | uint32_t id = 0; |
96 | 0 | uint32_t item = 0; |
97 | 0 | if (!absl::SimpleAtoi(row[0], &id) || !absl::SimpleAtoi(row[2], &item)) { |
98 | 0 | return absl::InternalError("a worn item that isn't numbers"); |
99 | 0 | } |
100 | 0 | const auto found = by_id.find(id); |
101 | 0 | if (found == by_id.end()) { |
102 | 0 | continue; |
103 | 0 | } |
104 | 0 | if (row[1] == "main hand") { |
105 | 0 | found->second->set_weapon(item); |
106 | 0 | } else { |
107 | 0 | found->second->add_wearing(item); |
108 | 0 | } |
109 | 0 | } |
110 | 13 | return characters; |
111 | 13 | } |
112 | | |
113 | | absl::StatusOr<uint32_t> Characters::Create( |
114 | | uint32_t account, const std::string& world, const std::string& name, |
115 | 249 | const std::optional<protocol::Looks>& looks) { |
116 | 249 | if (const absl::Status checked = CheckUsername(name); !checked.ok()) { |
117 | 1 | return checked; |
118 | 1 | } |
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 | 248 | std::vector<std::string> values = {absl::StrCat(account), world, name}; |
124 | 248 | const char* chosen = "NULL, NULL, NULL, NULL, NULL"; |
125 | 248 | if (looks) { |
126 | 0 | for (const uint32_t look : |
127 | 0 | {looks->build(), looks->skin(), looks->hair_color(), looks->hair()}) { |
128 | 0 | if (look > kMostLook) { |
129 | 0 | return Refusal(absl::StatusCode::kInvalidArgument, |
130 | 0 | Msg("That isn't a look there is.")); |
131 | 0 | } |
132 | 0 | values.push_back(absl::StrCat(look)); |
133 | 0 | } |
134 | 0 | 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 | 0 | if (looks->has_shirt()) { |
138 | 0 | if (looks->shirt() > kMostLook) { |
139 | 0 | return Refusal(absl::StatusCode::kInvalidArgument, |
140 | 0 | Msg("That isn't a look there is.")); |
141 | 0 | } |
142 | 0 | values.push_back(absl::StrCat(looks->shirt())); |
143 | 0 | chosen = "?, ?, ?, ?, ?"; |
144 | 0 | } |
145 | 0 | } |
146 | 248 | values.insert(values.end(), {absl::StrCat(account), world, |
147 | 248 | 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 | 248 | const absl::StatusOr<Result> inserted = database_.Run( |
152 | 248 | absl::StrCat("INSERT INTO players (account_id, world, name, x, z, build, " |
153 | 248 | "skin, hair_color, hair, shirt) SELECT ?, ?, ?, 0, 0, ", |
154 | 248 | chosen, |
155 | 248 | " FROM DUAL WHERE (SELECT COUNT(*) FROM players WHERE " |
156 | 248 | "account_id = ? AND world = ?) < ?"), |
157 | 248 | values); |
158 | 248 | if (absl::IsAlreadyExists(inserted.status())) { |
159 | 1 | return Refusal(absl::StatusCode::kAlreadyExists, |
160 | 1 | Msg("Someone in $0 is called that already.", Name(world))); |
161 | 1 | } |
162 | 247 | if (!inserted.ok()) { |
163 | 0 | return inserted.status(); |
164 | 0 | } |
165 | 247 | if (inserted->insert_id == 0) { |
166 | 2 | return Refusal(absl::StatusCode::kResourceExhausted, |
167 | 2 | MsgN("You have $0 character in $1 already.", |
168 | 2 | "You have $0 characters in $1 already.", |
169 | 2 | kCharactersPerWorld, kCharactersPerWorld, Name(world))); |
170 | 2 | } |
171 | 245 | return static_cast<uint32_t>(inserted->insert_id); |
172 | 247 | } |
173 | | |
174 | 4 | absl::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 | 4 | return database_.Transaction([&]() -> absl::Status { |
178 | 4 | if (const absl::StatusOr<std::string> world = WorldOf(account, id); |
179 | 4 | !world.ok()) { |
180 | 0 | return world.status(); |
181 | 0 | } |
182 | 4 | if (const absl::StatusOr<Left> left = LeaveGuild(database_, id); |
183 | 4 | !left.ok() && !absl::IsNotFound(left.status())2 ) { |
184 | 1 | return left.status(); |
185 | 1 | } |
186 | 3 | const absl::StatusOr<Result> deleted = |
187 | 3 | database_.Run("DELETE FROM players WHERE id = ? AND account_id = ?", |
188 | 3 | {absl::StrCat(id), absl::StrCat(account)}); |
189 | 3 | if (!deleted.ok()) { |
190 | 0 | return deleted.status(); |
191 | 0 | } |
192 | 3 | if (deleted->affected_rows == 0) { |
193 | 0 | return Refusal(absl::StatusCode::kNotFound, |
194 | 0 | Msg("That character isn't yours.")); |
195 | 0 | } |
196 | 3 | return absl::OkStatus(); |
197 | 3 | }); |
198 | 4 | } |
199 | | |
200 | 7 | absl::StatusOr<std::string> Characters::WorldOf(uint32_t account, uint32_t id) { |
201 | 7 | const absl::StatusOr<Result> rows = |
202 | 7 | database_.Run("SELECT world FROM players WHERE id = ? AND account_id = ?", |
203 | 7 | {absl::StrCat(id), absl::StrCat(account)}); |
204 | 7 | if (!rows.ok()) { |
205 | 0 | return rows.status(); |
206 | 0 | } |
207 | 7 | if (rows->rows.empty()) { |
208 | 2 | return Refusal(absl::StatusCode::kNotFound, |
209 | 2 | Msg("That character isn't yours.")); |
210 | 2 | } |
211 | 5 | return rows->rows.front()[0]; |
212 | 7 | } |
213 | | |
214 | | } // namespace nanommo |