[Bots] Convert Load, Save, SaveNew, and Delete to Repositories. (#2614)

* [Bots] Convert Load, Save, SaveNew, and Delete to Repositories.

# Notes
- General code cleanup, as manually adding to these queries doesn't scale very well.

* FindOne.

* Update base_bot_data_repository.h

* Update template.
This commit is contained in:
Alex King 2022-12-04 18:22:35 -05:00 committed by GitHub
parent 9a35cacf27
commit 423e6ae751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 168 additions and 338 deletions

View File

@ -300,7 +300,8 @@ public:
{ {
auto results = db.QueryDatabase( auto results = db.QueryDatabase(
fmt::format( fmt::format(
"{} WHERE id = {} LIMIT 1", "{} WHERE {} = {} LIMIT 1",
PrimaryKey(),
BaseSelect(), BaseSelect(),
bot_data_id bot_data_id
) )

View File

@ -104,7 +104,8 @@ public:
{ {
auto results = db.QueryDatabase( auto results = db.QueryDatabase(
fmt::format( fmt::format(
"{} WHERE id = {} LIMIT 1", "{} WHERE {} = {} LIMIT 1",
PrimaryKey(),
BaseSelect(), BaseSelect(),
{{TABLE_NAME_VAR}}_id {{TABLE_NAME_VAR}}_id
) )

View File

@ -38,9 +38,9 @@
#include <sstream> #include <sstream>
constexpr float BOT_FOLLOW_DISTANCE_DEFAULT = 184.0f; // as DSq value (~13.565 units) constexpr uint32 BOT_FOLLOW_DISTANCE_DEFAULT = 184; // as DSq value (~13.565 units)
constexpr float BOT_FOLLOW_DISTANCE_DEFAULT_MAX = 2500.0f; // as DSq value (50 units) constexpr uint32 BOT_FOLLOW_DISTANCE_DEFAULT_MAX = 2500; // as DSq value (50 units)
constexpr float BOT_FOLLOW_DISTANCE_WALK = 1000.0f; // as DSq value (~31.623 units) constexpr uint32 BOT_FOLLOW_DISTANCE_WALK = 1000; // as DSq value (~31.623 units)
constexpr uint32 BOT_KEEP_ALIVE_INTERVAL = 5000; // 5 seconds constexpr uint32 BOT_KEEP_ALIVE_INTERVAL = 5000; // 5 seconds

View File

@ -24,6 +24,7 @@
#include "../common/strings.h" #include "../common/strings.h"
#include "../common/eqemu_logsys.h" #include "../common/eqemu_logsys.h"
#include "../common/repositories/bot_data_repository.h"
#include "../common/repositories/bot_inventories_repository.h" #include "../common/repositories/bot_inventories_repository.h"
#include "zonedb.h" #include "zonedb.h"
@ -379,86 +380,41 @@ bool BotDatabase::LoadBot(const uint32 bot_id, Bot*& loaded_bot)
return false; return false;
} }
query = fmt::format( const auto& l = BotDataRepository::FindOne(database, bot_id);
"SELECT" if (!l.bot_id) {
" `owner_id`," // 0
" `spells_id`," // 1
" `name`," // 2
" `last_name`," // 3
" `title`," // 4
" `suffix`," // 5
" `level`," // 6
" `race`," // 7
" `class`," // 8
" `gender`," // 9
" `size`," // 10
" `face`," // 11
" `hair_style`," // 12
" `hair_color`," // 13
" `eye_color_1`," // 14
" `eye_color_2`," // 15
" `beard`," // 16
" `beard_color`," // 17
" `drakkin_heritage`," // 18
" `drakkin_tattoo`," // 19
" `drakkin_details`," // 20
" `hp`," // 21
" `mana`," // 22
" `time_spawned`," // 23
" `zone_id`," // 24
" `show_helm`," // 25
" `follow_distance`," // 26
" `stop_melee_level`," // 27
" `expansion_bitmask`," // 28
" `enforce_spell_settings`," // 29
" `archery_setting`" // 30
" FROM `bot_data`"
" WHERE `bot_id` = {}"
" LIMIT 1",
bot_id
);
auto results = database.QueryDatabase(query);
if (!results.Success()) {
return false; return false;
} }
if (!results.RowCount()) {
return true;
}
auto row = results.begin();
auto d = Bot::CreateDefaultNPCTypeStructForBot( auto d = Bot::CreateDefaultNPCTypeStructForBot(
std::string(row[2]), // Name l.name,
std::string(row[3]), // Last Name l.last_name,
static_cast<uint8>(EQ::Clamp(std::stoi(row[6]), 1, 255)), // Level l.level,
static_cast<uint16>(std::stoul(row[8])), // Race l.race,
static_cast<uint8>(EQ::Clamp(std::stoi(row[9]), WARRIOR, BERSERKER)), // Class l.class_,
static_cast<uint8>(EQ::Clamp(std::stoi(row[7]), MALE, FEMALE)) // Gender l.gender
); );
auto t = Bot::FillNPCTypeStruct( auto t = Bot::FillNPCTypeStruct(
std::stoul(row[1]), // Spells ID l.spells_id,
std::string(row[2]), // Name l.name,
std::string(row[3]), // Last Name l.last_name,
static_cast<uint8>(EQ::Clamp(std::stoi(row[6]), 1, 255)), // Level l.level,
static_cast<uint16>(std::stoul(row[7])), // Race l.race,
static_cast<uint8>(EQ::Clamp(std::stoi(row[8]), WARRIOR, BERSERKER)), // Class l.class_,
static_cast<uint8>(EQ::Clamp(std::stoi(row[9]), MALE, FEMALE)), // Gender l.gender,
std::stof(row[10]), // Size l.size,
std::stoul(row[11]), // Face l.face,
std::stoul(row[12]), // Hair Style l.hair_style,
std::stoul(row[13]), // Hair Color l.hair_color,
std::stoul(row[14]), // Eye Color 1 l.eye_color_1,
std::stoul(row[15]), // Eye Color 2 l.eye_color_2,
std::stoul(row[16]), // Beard l.beard,
std::stoul(row[17]), // Beard Color l.beard_color,
std::stoul(row[18]), // Drakkin Heritage l.drakkin_heritage,
std::stoul(row[19]), // Drakkin Tattoo l.drakkin_tattoo,
std::stoul(row[20]), // Drakkin Details l.drakkin_details,
std::stoi(row[21]), // Health l.hp,
std::stoi(row[22]), // Mana l.mana,
d->MR, d->MR,
d->CR, d->CR,
d->DR, d->DR,
@ -480,40 +436,30 @@ bool BotDatabase::LoadBot(const uint32 bot_id, Bot*& loaded_bot)
loaded_bot = new Bot( loaded_bot = new Bot(
bot_id, bot_id,
std::stoul(row[0]), // Owner ID l.owner_id,
std::stoul(row[1]), // Spells ID l.spells_id,
std::stof(row[23]), // Total Play Time l.time_spawned,
std::stoul(row[24]), // Last Zone ID l.zone_id,
t t
); );
if (loaded_bot) { if (loaded_bot) {
loaded_bot->SetSurname(row[3]); loaded_bot->SetSurname(l.last_name);
loaded_bot->SetTitle(row[4]); loaded_bot->SetTitle(l.title);
loaded_bot->SetSuffix(row[5]); loaded_bot->SetSuffix(l.suffix);
loaded_bot->SetShowHelm((std::stoi(row[25]) > 0 ? true : false)); loaded_bot->SetShowHelm((l.show_helm ? true : false));
auto bfd = std::stoul(row[26]);
if (bfd < 1) {
bfd = 1;
}
if (bfd > BOT_FOLLOW_DISTANCE_DEFAULT_MAX) {
bfd = BOT_FOLLOW_DISTANCE_DEFAULT_MAX;
}
auto bfd = EQ::Clamp(l.follow_distance, static_cast<uint32>(1), BOT_FOLLOW_DISTANCE_DEFAULT_MAX);
loaded_bot->SetFollowDistance(bfd); loaded_bot->SetFollowDistance(bfd);
auto sml = static_cast<uint8>(EQ::Clamp(std::stoi(row[27]), 1, 255)); loaded_bot->SetStopMeleeLevel(l.stop_melee_level);
loaded_bot->SetStopMeleeLevel(sml);
auto eb = std::stoi(row[28]); loaded_bot->SetExpansionBitmask(l.expansion_bitmask, false);
loaded_bot->SetExpansionBitmask(eb, false);
loaded_bot->SetBotEnforceSpellSetting((std::stoi(row[29]) > 0 ? true : false)); loaded_bot->SetBotEnforceSpellSetting((l.enforce_spell_settings ? true : false));
loaded_bot->SetBotArcherySetting((std::stoi(row[30]) > 0 ? true : false)); loaded_bot->SetBotArcherySetting((l.archery_setting ? true : false));
} }
return true; return true;
@ -521,263 +467,147 @@ bool BotDatabase::LoadBot(const uint32 bot_id, Bot*& loaded_bot)
bool BotDatabase::SaveNewBot(Bot* bot_inst, uint32& bot_id) bool BotDatabase::SaveNewBot(Bot* bot_inst, uint32& bot_id)
{ {
if (!bot_inst) if (!bot_inst) {
return false; return false;
}
query = StringFormat( auto e = BotDataRepository::NewEntity();
"INSERT INTO `bot_data` ("
" `owner_id`," e.owner_id = bot_inst->GetBotOwnerCharacterID();
" `spells_id`," e.spells_id = bot_inst->GetBotSpellID();
" `name`," e.name = bot_inst->GetCleanName();
" `last_name`," e.last_name = bot_inst->GetLastName();
" `zone_id`," e.title = bot_inst->GetTitle();
" `gender`," e.suffix = bot_inst->GetSuffix();
" `race`," e.zone_id = bot_inst->GetLastZoneID();
" `class`," e.gender = bot_inst->GetGender();
" `level`," e.race = bot_inst->GetBaseRace();
" `creation_day`," e.class_ = bot_inst->GetClass();
" `last_spawn`," e.level = bot_inst->GetLevel();
" `time_spawned`," e.creation_day = std::time(nullptr);
" `size`," e.last_spawn = std::time(nullptr);
" `face`," e.size = bot_inst->GetSize();
" `hair_color`," e.face = bot_inst->GetLuclinFace();
" `hair_style`," e.hair_color = bot_inst->GetHairColor();
" `beard`," e.hair_style = bot_inst->GetHairStyle();
" `beard_color`," e.beard = bot_inst->GetBeard();
" `eye_color_1`," e.beard_color = bot_inst->GetBeardColor();
" `eye_color_2`," e.eye_color_1 = bot_inst->GetEyeColor1();
" `drakkin_heritage`," e.eye_color_2 = bot_inst->GetEyeColor2();
" `drakkin_tattoo`," e.drakkin_heritage = bot_inst->GetDrakkinHeritage();
" `drakkin_details`," e.drakkin_tattoo = bot_inst->GetDrakkinTattoo();
" `ac`," e.drakkin_details = bot_inst->GetDrakkinDetails();
" `atk`," e.ac = bot_inst->GetBaseAC();
" `hp`," e.atk = bot_inst->GetBaseATK();
" `mana`," e.hp = bot_inst->GetHP();
" `str`," e.mana = bot_inst->GetMana();
" `sta`," e.str = bot_inst->GetBaseSTR();
" `cha`," e.sta = bot_inst->GetBaseSTA();
" `dex`," e.cha = bot_inst->GetBaseCHA();
" `int`," e.dex = bot_inst->GetBaseDEX();
" `agi`," e.int_ = bot_inst->GetBaseINT();
" `wis`," e.agi = bot_inst->GetBaseAGI();
" `fire`," e.wis = bot_inst->GetBaseWIS();
" `cold`," e.fire = bot_inst->GetBaseFR();
" `magic`," e.cold = bot_inst->GetBaseCR();
" `poison`," e.magic = bot_inst->GetBaseMR();
" `disease`," e.poison = bot_inst->GetBasePR();
" `corruption`," e.disease = bot_inst->GetBaseDR();
" `show_helm`," e.corruption = bot_inst->GetBaseCorrup();
" `follow_distance`," e.show_helm = bot_inst->GetShowHelm() ? 1 : 0;
" `stop_melee_level`" e.follow_distance = bot_inst->GetFollowDistance();
")" e.stop_melee_level = bot_inst->GetStopMeleeLevel();
" VALUES (" e.expansion_bitmask = bot_inst->GetExpansionBitmask();
"'%u'," /* owner_id */ e.enforce_spell_settings = bot_inst->GetBotEnforceSpellSetting();
" '%u'," /* spells_id */ e.archery_setting = bot_inst->IsBotArcher() ? 1 : 0;
" '%s'," /* name */
" '%s'," /* last_name */ auto b = BotDataRepository::InsertOne(database, e);
" '%i'," /* zone_id */ if (!b.bot_id) {
" '%i'," /* gender */
" '%i'," /* race */
" '%i'," /* class */
" '%u'," /* level */
" UNIX_TIMESTAMP()," /* creation_day */
" UNIX_TIMESTAMP()," /* last_spawn */
" 0," /* time_spawned */
" '%f'," /* size */
" '%i'," /* face */
" '%i'," /* hair_color */
" '%i'," /* hair_style */
" '%i'," /* beard */
" '%i'," /* beard_color */
" '%i'," /* eye_color_1 */
" '%i'," /* eye_color_2 */
" '%i'," /* drakkin_heritage */
" '%i'," /* drakkin_tattoo */
" '%i'," /* drakkin_details */
" '%i'," /* ac */
" '%i'," /* atk */
" '%i'," /* hp */
" '%i'," /* mana */
" '%i'," /* str */
" '%i'," /* sta */
" '%i'," /* cha */
" '%i'," /* dex */
" '%i'," /* int */
" '%i'," /* agi */
" '%i'," /* wis */
" '%i'," /* fire */
" '%i'," /* cold */
" '%i'," /* magic */
" '%i'," /* poison */
" '%i'," /* disease */
" '%i'," /* corruption */
" '1'," /* show_helm */
" '%i'," /* follow_distance */
" '%u'" /* stop_melee_level */
")",
bot_inst->GetBotOwnerCharacterID(),
bot_inst->GetBotSpellID(),
bot_inst->GetCleanName(),
bot_inst->GetLastName(),
bot_inst->GetLastZoneID(),
bot_inst->GetGender(),
bot_inst->GetRace(),
bot_inst->GetClass(),
bot_inst->GetLevel(),
bot_inst->GetSize(),
bot_inst->GetLuclinFace(),
bot_inst->GetHairColor(),
bot_inst->GetHairStyle(),
bot_inst->GetBeard(),
bot_inst->GetBeardColor(),
bot_inst->GetEyeColor1(),
bot_inst->GetEyeColor2(),
bot_inst->GetDrakkinHeritage(),
bot_inst->GetDrakkinTattoo(),
bot_inst->GetDrakkinDetails(),
bot_inst->GetAC(),
bot_inst->GetATK(),
bot_inst->GetHP(),
bot_inst->GetMana(),
bot_inst->GetSTR(),
bot_inst->GetSTA(),
bot_inst->GetCHA(),
bot_inst->GetDEX(),
bot_inst->GetINT(),
bot_inst->GetAGI(),
bot_inst->GetWIS(),
bot_inst->GetFR(),
bot_inst->GetCR(),
bot_inst->GetMR(),
bot_inst->GetPR(),
bot_inst->GetDR(),
bot_inst->GetCorrup(),
(uint32)BOT_FOLLOW_DISTANCE_DEFAULT,
(IsCasterClass(bot_inst->GetClass()) ? (uint8)RuleI(Bots, CasterStopMeleeLevel) : 255)
);
auto results = database.QueryDatabase(query);
if (!results.Success())
return false; return false;
}
bot_id = results.LastInsertedID(); bot_id = b.bot_id;
return true; return true;
} }
bool BotDatabase::SaveBot(Bot* bot_inst) bool BotDatabase::SaveBot(Bot* bot_inst)
{ {
if (!bot_inst) if (!bot_inst) {
return false; return false;
}
query = StringFormat( auto l = BotDataRepository::FindOne(database, bot_inst->GetBotID());
"UPDATE `bot_data`" if (!l.bot_id) {
" SET"
" `owner_id` = '%u',"
" `spells_id` = '%u',"
" `name` = '%s',"
" `last_name` = '%s',"
" `zone_id` = '%i',"
" `gender` = '%i',"
" `race` = '%i',"
" `class` = '%i',"
" `level` = '%u',"
" `last_spawn` = UNIX_TIMESTAMP(),"
" `time_spawned` = '%u',"
" `size` = '%f',"
" `face` = '%i',"
" `hair_color` = '%i',"
" `hair_style` = '%i',"
" `beard` = '%i',"
" `beard_color` = '%i',"
" `eye_color_1` = '%i',"
" `eye_color_2` = '%i',"
" `drakkin_heritage` = '%i',"
" `drakkin_tattoo` = '%i',"
" `drakkin_details` = '%i',"
" `ac` = '%i',"
" `atk` = '%i',"
" `hp` = '%i',"
" `mana` = '%i',"
" `str` = '%i',"
" `sta` = '%i',"
" `cha` = '%i',"
" `dex` = '%i',"
" `int` = '%i',"
" `agi` = '%i',"
" `wis` = '%i',"
" `fire` = '%i',"
" `cold` = '%i',"
" `magic` = '%i',"
" `poison` = '%i',"
" `disease` = '%i',"
" `corruption` = '%i',"
" `show_helm` = '%i',"
" `follow_distance` = '%i',"
" `stop_melee_level` = '%u',"
" `title` = '%s',"
" `suffix` = '%s'"
" WHERE `bot_id` = '%u'",
bot_inst->GetBotOwnerCharacterID(),
bot_inst->GetBotSpellID(),
bot_inst->GetCleanName(),
bot_inst->GetSurname().c_str(),
bot_inst->GetLastZoneID(),
bot_inst->GetBaseGender(),
bot_inst->GetBaseRace(),
bot_inst->GetClass(),
bot_inst->GetLevel(),
bot_inst->GetTotalPlayTime(),
bot_inst->GetBaseSize(),
bot_inst->GetLuclinFace(),
bot_inst->GetHairColor(),
bot_inst->GetHairStyle(),
bot_inst->GetBeard(),
bot_inst->GetBeardColor(),
bot_inst->GetEyeColor1(),
bot_inst->GetEyeColor2(),
bot_inst->GetDrakkinHeritage(),
bot_inst->GetDrakkinTattoo(),
bot_inst->GetDrakkinDetails(),
bot_inst->GetBaseAC(),
bot_inst->GetBaseATK(),
bot_inst->GetHP(),
bot_inst->GetMana(),
bot_inst->GetBaseSTR(),
bot_inst->GetBaseSTA(),
bot_inst->GetBaseCHA(),
bot_inst->GetBaseDEX(),
bot_inst->GetBaseINT(),
bot_inst->GetBaseAGI(),
bot_inst->GetBaseWIS(),
bot_inst->GetBaseFR(),
bot_inst->GetBaseCR(),
bot_inst->GetBaseMR(),
bot_inst->GetBasePR(),
bot_inst->GetBaseDR(),
bot_inst->GetBaseCorrup(),
((bot_inst->GetShowHelm()) ? (1) : (0)),
bot_inst->GetFollowDistance(),
bot_inst->GetStopMeleeLevel(),
bot_inst->GetTitle().c_str(),
bot_inst->GetSuffix().c_str(),
bot_inst->GetBotID()
);
auto results = database.QueryDatabase(query);
if (!results.Success())
return false; return false;
}
l.owner_id = bot_inst->GetBotOwnerCharacterID();
l.spells_id = bot_inst->GetBotSpellID();
l.name = bot_inst->GetCleanName();
l.last_name = bot_inst->GetLastName();
l.title = bot_inst->GetTitle();
l.suffix = bot_inst->GetSuffix();
l.zone_id = bot_inst->GetLastZoneID();
l.gender = bot_inst->GetGender();
l.race = bot_inst->GetBaseRace();
l.class_ = bot_inst->GetClass();
l.level = bot_inst->GetLevel();
l.last_spawn = std::time(nullptr);
l.time_spawned = bot_inst->GetTotalPlayTime();
l.size = bot_inst->GetSize();
l.face = bot_inst->GetLuclinFace();
l.hair_color = bot_inst->GetHairColor();
l.hair_style = bot_inst->GetHairStyle();
l.beard = bot_inst->GetBeard();
l.beard_color = bot_inst->GetBeardColor();
l.eye_color_1 = bot_inst->GetEyeColor1();
l.eye_color_2 = bot_inst->GetEyeColor2();
l.drakkin_heritage = bot_inst->GetDrakkinHeritage();
l.drakkin_tattoo = bot_inst->GetDrakkinTattoo();
l.drakkin_details = bot_inst->GetDrakkinDetails();
l.ac = bot_inst->GetBaseAC();
l.atk = bot_inst->GetBaseATK();
l.hp = bot_inst->GetHP();
l.mana = bot_inst->GetMana();
l.str = bot_inst->GetBaseSTR();
l.sta = bot_inst->GetBaseSTA();
l.cha = bot_inst->GetBaseCHA();
l.dex = bot_inst->GetBaseDEX();
l.int_ = bot_inst->GetBaseINT();
l.agi = bot_inst->GetBaseAGI();
l.wis = bot_inst->GetBaseWIS();
l.fire = bot_inst->GetBaseFR();
l.cold = bot_inst->GetBaseCR();
l.magic = bot_inst->GetBaseMR();
l.poison = bot_inst->GetBasePR();
l.disease = bot_inst->GetBaseDR();
l.corruption = bot_inst->GetBaseCorrup();
l.show_helm = bot_inst->GetShowHelm() ? 1 : 0;
l.follow_distance = bot_inst->GetFollowDistance();
l.stop_melee_level = bot_inst->GetStopMeleeLevel();
l.expansion_bitmask = bot_inst->GetExpansionBitmask();
l.enforce_spell_settings = bot_inst->GetBotEnforceSpellSetting();
l.archery_setting = bot_inst->IsBotArcher() ? 1 : 0;
const auto updated = BotDataRepository::UpdateOne(database, l);
if (!updated) {
return false;
}
return true; return true;
} }
bool BotDatabase::DeleteBot(const uint32 bot_id) bool BotDatabase::DeleteBot(const uint32 bot_id)
{ {
if (!bot_id) if (!bot_id) {
return false; return false;
}
query = StringFormat("DELETE FROM `bot_data` WHERE `bot_id` = '%u'", bot_id); const auto deleted = BotDataRepository::DeleteOne(database, bot_id);
auto results = database.QueryDatabase(query); if (!deleted) {
if (!results.Success())
return false; return false;
}
return true; return true;
} }
@ -3310,7 +3140,6 @@ bool BotDatabase::SaveBotArcherSetting(const uint32 bot_id, const bool bot_arche
if (!bot_id) { if (!bot_id) {
return false; return false;
} }
query = fmt::format( query = fmt::format(
"UPDATE `bot_data`" "UPDATE `bot_data`"
"SET `archery_setting` = {} " "SET `archery_setting` = {} "
@ -3322,7 +3151,6 @@ bool BotDatabase::SaveBotArcherSetting(const uint32 bot_id, const bool bot_arche
if (!results.Success()) { if (!results.Success()) {
return false; return false;
} }
return true; return true;
} }