[Expeditions] Refactor expedition requests (#1301)

Move ExpeditionLockoutTimer to common

This simplifies expedition request conflict checks and uses repository
for the queries instead of processing the query result directly.
This commit is contained in:
hg
2021-03-19 00:42:41 -04:00
committed by GitHub
parent ee4af65268
commit 5b74f1e756
15 changed files with 207 additions and 173 deletions
@@ -22,8 +22,10 @@
#define EQEMU_CHARACTER_EXPEDITION_LOCKOUTS_REPOSITORY_H
#include "../database.h"
#include "../expedition_lockout_timer.h"
#include "../string_util.h"
#include "base/base_character_expedition_lockouts_repository.h"
#include <unordered_map>
class CharacterExpeditionLockoutsRepository: public BaseCharacterExpeditionLockoutsRepository {
public:
@@ -65,6 +67,78 @@ public:
// Custom extended repository methods here
struct CharacterExpeditionLockoutsTimeStamp {
int id;
int character_id;
std::string expedition_name;
std::string event_name;
time_t expire_time;
int duration;
std::string from_expedition_uuid;
};
static ExpeditionLockoutTimer GetExpeditionLockoutTimerFromEntry(
CharacterExpeditionLockoutsTimeStamp&& entry)
{
ExpeditionLockoutTimer lockout_timer{
std::move(entry.from_expedition_uuid),
std::move(entry.expedition_name),
std::move(entry.event_name),
static_cast<uint64_t>(entry.expire_time),
static_cast<uint32_t>(entry.duration)
};
return lockout_timer;
}
static std::unordered_map<uint32_t, std::vector<ExpeditionLockoutTimer>> GetManyCharacterLockoutTimers(
Database& db, const std::vector<uint32_t>& character_ids,
const std::string& expedition_name, const std::string& ordered_event_name)
{
auto joined_character_ids = fmt::join(character_ids, ",");
auto results = db.QueryDatabase(fmt::format(SQL(
SELECT
character_id,
UNIX_TIMESTAMP(expire_time),
duration,
event_name,
from_expedition_uuid
FROM character_expedition_lockouts
WHERE
character_id IN ({})
AND expire_time > NOW()
AND expedition_name = '{}'
ORDER BY
FIELD(character_id, {}),
FIELD(event_name, '{}') DESC
),
joined_character_ids,
EscapeString(expedition_name),
joined_character_ids,
EscapeString(ordered_event_name)
));
std::unordered_map<uint32_t, std::vector<ExpeditionLockoutTimer>> lockouts;
for (auto row = results.begin(); row != results.end(); ++row)
{
CharacterExpeditionLockoutsTimeStamp entry{};
int col = 0;
entry.character_id = std::strtoul(row[col++], nullptr, 10);
entry.expire_time = std::strtoull(row[col++], nullptr, 10);
entry.duration = std::strtoul(row[col++], nullptr, 10);
entry.event_name = row[col++];
entry.expedition_name = expedition_name;
entry.from_expedition_uuid = row[col++];
auto lockout = GetExpeditionLockoutTimerFromEntry(std::move(entry));
lockouts[entry.character_id].emplace_back(std::move(lockout));
}
return lockouts;
}
};
#endif //EQEMU_CHARACTER_EXPEDITION_LOCKOUTS_REPOSITORY_H