-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeaponRepository.cc
62 lines (47 loc) · 1.65 KB
/
WeaponRepository.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
#include "WeaponRepository.hh"
namespace bsgo {
WeaponRepository::WeaponRepository(const DbConnectionShPtr &connection)
: AbstractRepository("weapon", connection)
{}
namespace {
constexpr auto FIND_ALL_QUERY_NAME = "weapon_find_all";
constexpr auto FIND_ALL_QUERY = "SELECT id FROM weapon";
constexpr auto FIND_ONE_QUERY_NAME = "weapon_find_one";
constexpr auto FIND_ONE_QUERY
= "SELECT name, min_damage, max_damage, power_cost, range, reload_time_ms FROM weapon WHERE id = $1";
} // namespace
void WeaponRepository::initialize()
{
m_connection->prepare(FIND_ALL_QUERY_NAME, FIND_ALL_QUERY);
m_connection->prepare(FIND_ONE_QUERY_NAME, FIND_ONE_QUERY);
}
auto WeaponRepository::findAll() const -> std::unordered_set<Uuid>
{
const auto query = [](pqxx::nontransaction &work) {
return work.exec_prepared(FIND_ALL_QUERY_NAME);
};
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 WeaponRepository::findOneById(const Uuid weapon) const -> Weapon
{
const auto query = [weapon](pqxx::nontransaction &work) {
return work.exec_prepared1(FIND_ONE_QUERY_NAME, toDbId(weapon));
};
const auto record = m_connection->executeQueryReturningSingleRow(query);
Weapon out{};
out.id = weapon;
out.name = record[0].as<std::string>();
out.minDamage = record[1].as<float>();
out.maxDamage = record[2].as<float>();
out.powerCost = record[3].as<float>();
out.range = record[4].as<float>();
out.reloadTime = utils::Milliseconds(record[5].as<int>());
return out;
}
} // namespace bsgo