[Expeditions] Refactor expedition caching (#1315)

Add common expedition base class

Use repository for zone and world expedition caching

World now stores members and leader as Member objects instead of ids

This improves readability of the caching methods and lets world cache
expedition dzs and members like zone. World also now caches expeditions
as unique_ptr which will be necessary for future dz callback lambdas
that capture 'this' so addresses don't change on cache vector resizes.
This commit is contained in:
hg
2021-04-07 02:20:35 -04:00
committed by GitHub
parent 0534a2c6be
commit dadc1b2843
27 changed files with 580 additions and 559 deletions
@@ -78,7 +78,7 @@ public:
entry.id = 0;
entry.expedition_id = 0;
entry.event_name = "";
entry.expire_time = current_timestamp();
entry.expire_time = "";
entry.duration = 0;
entry.from_expedition_uuid = "";
@@ -65,6 +65,58 @@ public:
// Custom extended repository methods here
struct ExpeditionLockoutsWithTimestamp {
uint32_t id;
uint32_t expedition_id;
std::string event_name;
time_t expire_time;
int duration;
std::string from_expedition_uuid;
};
static std::vector<ExpeditionLockoutsWithTimestamp> GetWithTimestamp(
Database& db, const std::vector<uint32_t>& expedition_ids)
{
if (expedition_ids.empty())
{
return {};
}
std::vector<ExpeditionLockoutsWithTimestamp> all_entries;
auto results = db.QueryDatabase(fmt::format(SQL(
SELECT
id,
expedition_id,
event_name,
UNIX_TIMESTAMP(expire_time),
duration,
from_expedition_uuid
FROM expedition_lockouts
WHERE expedition_id IN ({})
),
fmt::join(expedition_ids, ",")
));
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row)
{
ExpeditionLockoutsWithTimestamp entry{};
int col = 0;
entry.id = strtoul(row[col++], nullptr, 10);
entry.expedition_id = strtoul(row[col++], nullptr, 10);
entry.event_name = row[col++];
entry.expire_time = strtoull(row[col++], nullptr, 10);
entry.duration = strtol(row[col++], nullptr, 10);
entry.from_expedition_uuid = row[col++];
all_entries.emplace_back(std::move(entry));
}
return all_entries;
}
};
#endif //EQEMU_EXPEDITION_LOCKOUTS_REPOSITORY_H
@@ -65,6 +65,68 @@ public:
// Custom extended repository methods here
struct MemberWithName {
uint32_t id;
uint32_t expedition_id;
uint32_t character_id;
int is_current_member;
std::string character_name;
};
static std::string SelectMembersWithNames()
{
return std::string(SQL(
SELECT
expedition_members.id,
expedition_members.expedition_id,
expedition_members.character_id,
expedition_members.is_current_member,
character_data.name
FROM expedition_members
INNER JOIN character_data ON expedition_members.character_id = character_data.id
));
}
static std::vector<MemberWithName> GetWithNames(Database& db,
const std::vector<uint32_t>& expedition_ids)
{
if (expedition_ids.empty())
{
return {};
}
std::vector<MemberWithName> all_entries;
auto results = db.QueryDatabase(fmt::format(SQL(
{}
WHERE expedition_members.expedition_id IN ({})
AND expedition_members.is_current_member = TRUE;
),
SelectMembersWithNames(),
fmt::join(expedition_ids, ",")
));
if (results.Success())
{
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row)
{
MemberWithName entry{};
int col = 0;
entry.id = strtoul(row[col++], nullptr, 10);
entry.expedition_id = strtoul(row[col++], nullptr, 10);
entry.character_id = strtoul(row[col++], nullptr, 10);
entry.is_current_member = strtoul(row[col++], nullptr, 10);
entry.character_name = row[col++];
all_entries.emplace_back(std::move(entry));
}
}
return all_entries;
}
};
#endif //EQEMU_EXPEDITION_MEMBERS_REPOSITORY_H
@@ -65,6 +65,100 @@ public:
// Custom extended repository methods here
struct ExpeditionWithLeader
{
uint32_t id;
std::string uuid;
uint32_t dynamic_zone_id;
std::string expedition_name;
uint32_t min_players;
uint32_t max_players;
int add_replay_on_join;
int is_locked;
uint32_t leader_id;
std::string leader_name;
};
static std::string SelectExpeditionsJoinLeader()
{
return std::string(SQL(
SELECT
expeditions.id,
expeditions.uuid,
expeditions.dynamic_zone_id,
expeditions.expedition_name,
expeditions.min_players,
expeditions.max_players,
expeditions.add_replay_on_join,
expeditions.is_locked,
expeditions.leader_id,
character_data.name leader_name
FROM expeditions
INNER JOIN character_data ON expeditions.leader_id = character_data.id
));
}
static ExpeditionWithLeader FillExpeditionWithLeaderFromRow(MySQLRequestRow& row)
{
ExpeditionWithLeader entry{};
int col = 0;
entry.id = strtoul(row[col++], nullptr, 10);
entry.uuid = row[col++];
entry.dynamic_zone_id = strtoul(row[col++], nullptr, 10);
entry.expedition_name = row[col++];
entry.min_players = strtoul(row[col++], nullptr, 10);
entry.max_players = strtoul(row[col++], nullptr, 10);
entry.add_replay_on_join = strtoul(row[col++], nullptr, 10);
entry.is_locked = strtoul(row[col++], nullptr, 10);
entry.leader_id = strtoul(row[col++], nullptr, 10);
entry.leader_name = row[col++];
return entry;
}
static std::vector<ExpeditionWithLeader> GetAllWithLeaderName(Database& db)
{
std::vector<ExpeditionWithLeader> all_entries;
auto results = db.QueryDatabase(fmt::format(
"{} ORDER BY expeditions.id;",
SelectExpeditionsJoinLeader()
));
if (results.Success())
{
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row)
{
ExpeditionWithLeader entry = FillExpeditionWithLeaderFromRow(row);
all_entries.emplace_back(std::move(entry));
}
}
return all_entries;
}
static ExpeditionWithLeader GetWithLeaderName(Database& db, uint32_t expedition_id)
{
ExpeditionWithLeader entry{};
auto results = db.QueryDatabase(fmt::format(
"{} WHERE expeditions.id = {};",
SelectExpeditionsJoinLeader(),
expedition_id
));
if (results.Success() && results.RowCount() > 0)
{
auto row = results.begin();
entry = FillExpeditionWithLeaderFromRow(row);
}
return entry;
}
struct CharacterExpedition
{
uint32_t id;