-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerShipRepository.cc
310 lines (268 loc) · 8.72 KB
/
PlayerShipRepository.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "PlayerShipRepository.hh"
namespace bsgo {
PlayerShipRepository::PlayerShipRepository(const DbConnectionShPtr &connection)
: AbstractRepository("player", connection)
{
addModule("ship");
}
namespace {
constexpr auto FIND_ALL_QUERY_NAME = "player_ship_find_all";
constexpr auto FIND_ALL_QUERY = "SELECT id FROM player_ship WHERE player = $1";
constexpr auto FIND_ONE_QUERY_NAME = "player_ship_find_one";
constexpr auto FIND_ONE_QUERY = R"(
SELECT
s.faction,
s.class,
s.id,
ps.name,
ps.player,
ps.active,
ps.hull_points,
s.max_hull_points,
s.hull_points_regen,
ps.power_points,
s.max_power_points,
s.power_points_regen,
s.max_acceleration,
s.max_speed,
s.radius,
ps.x_pos,
ps.y_pos,
ps.z_pos,
ss.system,
ss.docked,
sc.jump_time_ms,
sc.jump_time_threat_ms,
sj.system
FROM
player_ship AS ps
LEFT JOIN ship AS s ON ps.ship = s.id
LEFT JOIN ship_class AS sc ON s.class = sc.name
LEFT JOIN ship_system AS ss ON ps.id = ss.ship
LEFT JOIN ship_jump AS sj ON ps.id = sj.ship
WHERE
ps.id = $1;
)";
constexpr auto FIND_ONE_BY_PLAYER_AND_ACTIVE_QUERY_NAME = "player_ship_find_one_by_player_and_active";
constexpr auto FIND_ONE_BY_PLAYER_AND_ACTIVE_QUERY
= "SELECT id FROM player_ship WHERE player = $1 AND active = 'true'";
constexpr auto FIND_SLOTS_QUERY_NAME = "player_ship_find_slots";
constexpr auto FIND_SLOTS_QUERY = R"(
SELECT
ss.type,
COUNT(ss.id)
FROM
player_ship AS ps
LEFT JOIN ship AS s ON ps.ship = s.id
LEFT JOIN ship_slot AS ss ON s.id = ss.ship
WHERE
ps.id = $1
GROUP BY
ss.type
)";
constexpr auto FIND_EMPTY_WEAPON_SLOTS_QUERY_NAME = "player_ship_find_empty_slots";
constexpr auto FIND_EMPTY_WEAPON_SLOTS_QUERY = R"(
SELECT
ss.id
FROM
ship AS s
LEFT JOIN ship_slot AS ss ON s.id = ss.ship
LEFT JOIN player_ship AS ps ON ps.ship = s.id
LEFT JOIN ship_weapon AS sw ON sw.ship = ps.id AND sw.slot = ss.id
WHERE
ps.id = $1
AND ss.type = 'weapon'
AND sw.weapon IS NULL
)";
constexpr auto UPDATE_SHIP_QUERY_NAME = "player_ship_update";
constexpr auto UPDATE_SHIP_QUERY = R"(
INSERT INTO player_ship (ship, player, name, active, hull_points, power_points, x_pos, y_pos, z_pos)
VALUES ($1, $2, $3, $4, $5, $6, 0, 0, 0)
ON CONFLICT (ship, player) DO UPDATE
SET
name = excluded.name,
active = excluded.active,
hull_points = excluded.hull_points,
power_points = excluded.power_points,
x_pos = excluded.x_pos,
y_pos = excluded.y_pos,
z_pos = excluded.z_pos
WHERE
player_ship.ship = excluded.ship
AND player_ship.player = excluded.player
)";
constexpr auto UPDATE_SHIP_JUMP_QUERY_NAME = "player_ship_update_jump";
constexpr auto UPDATE_SHIP_JUMP_QUERY = R"(
INSERT INTO ship_jump (ship, system)
VALUES ($1, $2)
ON CONFLICT (ship, system) DO UPDATE
SET
system = excluded.system
WHERE
ship_jump.ship = excluded.ship
)";
constexpr auto CANCEL_SHIP_JUMP_QUERY_NAME = "player_ship_cancel_jump";
constexpr auto CANCEL_SHIP_JUMP_QUERY = "DELETE FROM ship_jump WHERE ship = $1";
} // namespace
void PlayerShipRepository::initialize()
{
m_connection->prepare(FIND_ALL_QUERY_NAME, FIND_ALL_QUERY);
m_connection->prepare(FIND_ONE_QUERY_NAME, FIND_ONE_QUERY);
m_connection->prepare(FIND_ONE_BY_PLAYER_AND_ACTIVE_QUERY_NAME,
FIND_ONE_BY_PLAYER_AND_ACTIVE_QUERY);
m_connection->prepare(FIND_SLOTS_QUERY_NAME, FIND_SLOTS_QUERY);
m_connection->prepare(FIND_EMPTY_WEAPON_SLOTS_QUERY_NAME, FIND_EMPTY_WEAPON_SLOTS_QUERY);
m_connection->prepare(UPDATE_SHIP_QUERY_NAME, UPDATE_SHIP_QUERY);
m_connection->prepare(UPDATE_SHIP_JUMP_QUERY_NAME, UPDATE_SHIP_JUMP_QUERY);
m_connection->prepare(CANCEL_SHIP_JUMP_QUERY_NAME, CANCEL_SHIP_JUMP_QUERY);
}
auto PlayerShipRepository::findOneById(const Uuid ship) const -> PlayerShip
{
auto out = fetchShipBase(ship);
fetchSlots(ship, out);
return out;
}
auto PlayerShipRepository::findOneByPlayerAndActive(const Uuid player) const -> PlayerShip
{
const auto query = [player](pqxx::nontransaction &work) {
return work.exec_prepared1(FIND_ONE_BY_PLAYER_AND_ACTIVE_QUERY_NAME, toDbId(player));
};
const auto record = m_connection->executeQueryReturningSingleRow(query);
const auto shipId = fromDbId(record[0].as<int>());
auto out = fetchShipBase(shipId);
fetchSlots(shipId, out);
return out;
}
auto PlayerShipRepository::findAllByPlayer(const Uuid player) const -> std::unordered_set<Uuid>
{
const auto query = [player](pqxx::nontransaction &work) {
return work.exec_prepared(FIND_ALL_QUERY_NAME, toDbId(player));
};
const auto rows = m_connection->executeQuery(query);
std::unordered_set<Uuid> out;
for (const auto record : rows)
{
out.emplace(fromDbId(record[0].as<int>()));
}
return out;
}
auto PlayerShipRepository::findAllAvailableWeaponSlotByShip(const Uuid ship) -> std::set<Uuid>
{
const auto query = [ship](pqxx::nontransaction &work) {
return work.exec_prepared(FIND_EMPTY_WEAPON_SLOTS_QUERY_NAME, toDbId(ship));
};
const auto rows = m_connection->executeQuery(query);
std::set<Uuid> out;
for (const auto record : rows)
{
out.emplace(fromDbId(record[0].as<int>()));
}
return out;
}
void PlayerShipRepository::save(const PlayerShip &ship)
{
auto query = [&ship](pqxx::work &transaction) {
return transaction.exec_prepared0(UPDATE_SHIP_QUERY_NAME,
toDbId(ship.ship),
toDbId(*ship.player),
ship.name,
ship.active,
ship.hullPoints,
ship.powerPoints);
};
auto res = m_connection->tryExecuteTransaction(query);
if (res.error)
{
error("Failed to save player ship: " + *res.error);
}
if (ship.jumpSystem)
{
registerShipJump(ship.id, *ship.jumpSystem);
}
else
{
cancelShipJump(ship.id);
}
}
auto PlayerShipRepository::fetchShipBase(const Uuid ship) const -> PlayerShip
{
const auto query = [ship](pqxx::nontransaction &work) {
return work.exec_prepared1(FIND_ONE_QUERY_NAME, toDbId(ship));
};
const auto record = m_connection->executeQueryReturningSingleRow(query);
PlayerShip out;
out.id = ship;
out.faction = fromDbFaction(record[0].as<std::string>());
out.shipClass = fromDbShipClass(record[1].as<std::string>());
out.ship = fromDbId(record[2].as<int>());
out.name = record[3].as<std::string>();
if (!record[4].is_null())
{
out.player = fromDbId(record[4].as<int>());
}
out.active = record[5].as<bool>();
out.hullPoints = record[6].as<float>();
out.maxHullPoints = record[7].as<float>();
out.hullPointsRegen = record[8].as<float>();
out.powerPoints = record[9].as<float>();
out.maxPowerPoints = record[10].as<float>();
out.powerRegen = record[11].as<float>();
out.acceleration = record[12].as<float>();
out.speed = record[13].as<float>();
out.radius = record[14].as<float>();
const auto x = record[15].as<float>();
const auto y = record[16].as<float>();
const auto z = record[17].as<float>();
out.position = Eigen::Vector3f(x, y, z);
if (!record[18].is_null())
{
out.system = fromDbId(record[18].as<int>());
}
if (!record[19].is_null())
{
out.docked = record[19].as<bool>();
}
out.jumpTime = utils::Milliseconds(record[20].as<int>());
out.jumpTimeInThreat = utils::Milliseconds(record[21].as<int>());
if (!record[22].is_null())
{
out.jumpSystem = fromDbId(record[22].as<int>());
}
return out;
}
void PlayerShipRepository::fetchSlots(const Uuid ship, PlayerShip &out) const
{
const auto query = [ship](pqxx::nontransaction &work) {
return work.exec_prepared(FIND_SLOTS_QUERY_NAME, toDbId(ship));
};
const auto rows = m_connection->executeQuery(query);
for (const auto record : rows)
{
const auto slot = fromDbSlot(record[0].as<std::string>());
const auto count = record[1].as<int>();
out.slots[slot] = count;
}
}
void PlayerShipRepository::registerShipJump(const Uuid ship, const Uuid system) const
{
const auto query = [&ship, &system](pqxx::work &transaction) {
return transaction.exec_prepared0(UPDATE_SHIP_JUMP_QUERY_NAME, toDbId(ship), toDbId(system));
};
const auto res = m_connection->tryExecuteTransaction(query);
if (res.error)
{
error("Failed to save player ship jump: " + *res.error);
}
}
void PlayerShipRepository::cancelShipJump(const Uuid ship) const
{
const auto query = [&ship](pqxx::work &transaction) {
return transaction.exec_prepared0(CANCEL_SHIP_JUMP_QUERY_NAME, toDbId(ship));
};
const auto res = m_connection->tryExecuteTransaction(query);
if (res.error)
{
error("Failed to cancel player ship jump: " + *res.error);
}
}
} // namespace bsgo