eqemu-server/common/repositories/base/base_bot_data_repository.h
nytmyr f466964db8
[Bots] Bot Overhaul (#4580)
* Implement spell AI pulling, fix throw stone

* more pull tweaks

* holding check at start of ai process

* fully implement ^pull logic to always return, can still be overidden by ^attack

* Rewrite ^pull logic and handling. **MORE**

Add ^setassistee command to set who your bots will assist. Bots will always assist you first before anyone else.

If the rule Bots, AllowCrossGroupRaidAssist is enabled bots will assist the group or raid main assists.

Rewrites logic in handling of pull and returning to ensure bots make it back to their location.

* Move HateLine to a better ID

* cleanup ST_Self logic in CastChecks

* Removed unused BotSpellTypeRequiresLoS

* Move fizzle message to define

* add timer checks to Idle/Engaged/Pursue CastCheck to early terminate

* Add back !IsBotNonSpellFighter() check to the different CastCheck

* Correct IsValidSpellRange

* Implement AAs and harmtouch/layonhands to ^cast --- fix IsValidSpellRange

* Add PetDamageShields and PetResistBuffs to IsPetBotSpellType()

* Add priorities to HateLine inserts for db update

* Remove SpellTypeRequiresCastChecks

* Add bot check to DetermineSpellTargets for IsIllusionSpell

* merge with previous

* Correct bot checks for ST_GroupClientAndPet

* Remove misc target_type checks

* Add lull/aelull to ^cast

* Add more checks for CommandedSubTypes::AETarget

* remove unneeded checks on IsValidSpellTypeBySpellID

* add to aelull

* rewrite GetCorrectSpellType

* Add IsBlockedBuff to CastChecks

* Add spellid option to ^cast to allow casting of a specific spell by ID

* ^cast adjustments for spellid casts

* Add missing alert round for ranged attacks

* More castcheck improvements

* CanUseBotSpell for ^cast

* remove ht/loh from attack ai

* remove SetCombatRoundForAlerts that triggered every engagement

* Add RangedAttackImmunity checks before trying to ranged attack

* move bot backstab to mob

* fix MinStatusToBypassCreateLimit

* more backstab to mob cleanup

* add bot checks to tryheadshot / tryassassinate

* adjust version number for bots

* add back m_mob_check_moving_timer, necessary?

* add sanity checks for classattacks

* Get rid of Bots:BotGroupXP and change logic to support Bots:SameRaidGroupForXP

Bots won't do anything if not in the same group so this should more accurately control only when in the same raid group.

* add "confirm" check to ^delete

* Update bot.cpp

* Remove `id` from bot_settings, correct types

* Implement blocked_buffs and blocked_pet_buffs

* more blocked buff tweaks

* add beneficial check to ^blockedbuffs

* command grammar

* missing )

* Move getnames for categories and settings to mob, rename hptomed/manatomed

* add GetBotSpellCategoryIDByShortName and CopyBotBlockedPetBuffs, update ^defaultsettings command

* cls cleanup

* Allow bots to clear HasProjectIllusion flag

* Add PercentChanceToCastGroupCure

* Implmenet PetCures, add some missing types for defaults/chance to cast

* Change GetRaidByBotName to GetRaidByBot

* Typo on PetBuffs implement

* Change GetSpellListSpellType to GetParentSpellType

* missing from GetChanceToCastBySpellType

* Fix performance in IsValidSpellRange by flipping HasProjectIllusion

* merge with prev

* merge with cls cleanup

* Reorder IsTargetAlreadyReceivingSpell/CheckSpellLevelRestriction/IsBlockedBuff

* Combine GatherGroupSpellTargets and GatherSpellTargets

* Cleanup IsTargetAlreadyReceivingSpell

* Fix ^petsettype to account for usable levels of spells and remove hardcoded level limits.

* Remove Bot_AICheckCloseBeneficialSpells and use AttemptCloseBeneficialSpells for better performance

* remove default hold for resist buffa

* move IsValidSpellRange further down castchecks

* raid optimizations

* correct name checking to match players

* more name checks and add proper soft deletes to bots

* organize some checks in IsImmuneToBotSpell

* Fix GetRaidByBotName and GetRaidByBot checks to not loop unnecessarily

* Move GatherSpellTargets to mob

* Change GetPrioritizedBotSpellsBySpellType to vector

Some slipped through in "organize some checks in IsImmuneToBotSpell"

* Move GatherSpellTargets and Raid to stored variables.

Missing some in "organize some checks in IsImmuneToBotSpell"

* comment out precheck, delays, thresholds, etc logging

missed some in "organize some checks in IsImmuneToBotSpell"

* Missing IsInGroupOrRaid cleanup

* Implement AIBot_spells_by_type to reduce looping when searching for spells

* Add _tempSpellType as placeholder for any future passthru

* todo

* Move bot_list from std::list to std::unordered_map like other entities

* Fix missing raid assignment for GetStoredRaid in IsInGroupOrRaid

* TempPet owned by bots that get the kill will now give exp like a client would

* Remove unnecessary checks in bot process (closescanmoving timer, verify raid, send hp/mana/end packet

* Fix client spell commands from saving the wrong setting

* Cleanup ^copysettings command and add new commands

* Add pet option to ^taunt

No longer has toggle, required on/off option and an optional "pet" option to control pets' taunting state

* Allow pet types to ^cast, prevent failure spam, add cure check

* more raid optimizations, should be final.

10 clients, 710 bots, 10 raids, ~250 pets sits around 3.5% CPU idle

* Move spell range check to proper location

* Implement ^discipline

* remove ^aggressive/^defensive

* remove this for a separate PR

* cleanup

* Add BotGroupSay method

* todo list

* Add missing bot_blocked_buffs to schema

* Remove plural on ^spelltypeidsand ^spelltypenames

* Move spelltype names, spell subtypes, category names and setting names to maps.

* move los checks to mob.cpp

* Bot CampAll fix

* Bots special_attacks.cpp fix

* Add zero check for bot spawn limits

If the spawn limit rule is set to 0 and spawn limit is set by bucket, if no class buckets are set, it defaults to the rule of 0 and renders the player unable to spawn bots.

This adds a check where if the rule and class bucket are 0, it will check for the spawn limit bucket

* Add HasSkill checks to bot special abilities (kick/bash/etc)

* code cleanup 1

* code cleanup 2

* code cleanup 3

* code cleanup 4

* fix ^cast wirh commanded types

* Remove bcspells, fix helper_send_usage_required_bots

* linux build fix

* remove completed todo

* Allow inventory give to specific ID slots

* Update TODO

* Correct slot ranges for inventorygive

* Add zone specific spawn limits and zone specific forced spawn limits

* remove bd. from update queries where it doesn't exist

* Rename _spellSettings to m_bot_spell_settings

* Add IsPetOwnerOfClientBot(), add Lua and Perl methods

* Make botOwnerCharacterID snakecase

* Throw bot_camp_timer behind Bots:Enabled rule

* Move various Bot<>Checks logging to BotSpellChecks

* Remove from LogCategoryName

* Consolidate IsInGroupOrRaid

* Consolidate GatherSpellTargets

* Add missing Bot Spell Type Checks to log

* Add GetParentSpellType when checking spelltypes for idle, engaged, pursue CastChecks.

* Consolidate AttemptForcedCastSpell

* Consolidate SetBotBlockedBuff/SetBotBlockedPetBuff

* Add list option to ^spellpriority commands.

* Move client functions to client_bot

* Move mob functions to mob_bot

* Move bot spdat functions to spdat_bot

* Move SendCommandHelpWindow to SendBotCommandHelpWindow and simplify

* Change char_id to character_id for bot_settings

* update todo

* Fix typo on merge conflict

* Cleanup command format changes, remove hardcoded class IDs in examples.

* Set #illusionblock for players to guide access

* Move client commands for bot spells from gm commands to existing bot commands

* Fix alignment issues

* More alignment fixes

* More cleanup 1

* More cleanup 2

* Fix BotMeditate to med at proper percentages

* Correct GetStopMeleeLevel checks for some buff checks

* Add back hpmanaend update to bot raid, force timer update to prevent spamming

* Remove log

* Cleanup ranged and ammo calculations - Adds throwing check for match

* Add check in distance calculations to stay at range if set even if no ammo or ranged

* Move melee distance calculations to better function

* Add GetBuffTargets helper

* Missing p_item, s_item in CombatRangeInput

* Linux test?

* Reduce GetCorrectBotSpellType branching slightly

This is still an ugly ass function but my brain is melted

* Line fixes

* Make bot pets only do half damage in pvp

* Add bot pet pvp damage to tune

* Add bot pet check for AIYellForHelp

* Add bots to UseSpellImpliedTargeting

* Move toggleranged, togglehelm and illusionblock to new help window. Add actionable support

* Add bot and bot pet checks to various spells, auras and targeting checks that were missing.

* update todo

* New lines

* Correct DoLosChecks

* Remove Log TestDebug

* Remove _Struct from struct declarations

* Add bot check to IsAttackAllowed for GetUltimateOwner to skip entity list where possible

* Wrap SaveBotSettings in Bots Enabled check

* Remove comment

* Wrap bot setting loading for clients in bots enabled rule

* Cleanup BlockedBuffs logic in SpellOnTarget

* Rename BotSpells_Struct/BotSpells_Struct_wIndex

* Rename spawn/create status bypass rules, fix return for spawn limit

* Remove unnecessary return in CanBuffStack, cleanup

* Enable recastdelay support for clients

* Remove unused variables

* Rename _assistee to bot_assistee

* hardcode BotCommandHelpWindow colors

* todo

* Fix ^cast summoncorpse

* todo

* Reimplement secondary colors to BotSendCommandHelpWindow

* Give ^copysettings/^defaultsettings more options, cleanup.

* Cleanup some commands

* Add comment to CheckLosCheat/CheckLosCheatExempt

* Make struct BotSpellSettings snake case

* Allow duplicate casts of same spell on target for heals and cures

* Add default delay to cures

* Remove unused methods

* Implement missing ^spellresistlimits/^resistlimits command

* Move functions out of mob.h and cleanup

* Return for GetRawBotList

This checks offline bots too

* Rename BotGroupSay to RaidGroupSay

* Prevent bots from forming their own group if a bot that is a group leader is removed from the raid

* Linux fix?

* IsPetOwner fixes

* Add remove option to list for ^blockedbuffs / ^blockedpetbuffs

* Implement ^spellannouncecasts to toggle announcing casts of spell types

* Remove rule Bots:BardsAnnounceCasts

* Update bot.h

* Remove unused no_pets option from GatherSpellTargets

* Move ^attack response back to normal chat window (other)

* Set lower limit of spell delays to 100 rather than 1

* Correct pet checks on GetUltimateSpell functions

* Add rules (Bots, AICastSpellTypeDelay, Bots, AICastSpellTypeHeldDelay) to prevent spamming of failed spell type AI casts

* Correct pet buff type logic to catch DS/Resists with other spell effects in them

* Fix defaults for clients

* Add more logic for necros/shaman for default heal thresholds due to lich and canni

* Rename SpellHold, SpellDelay, SpellMinThreshold, SpellMaxThreshold, SpellRecastDelay to fit SpellType style naming

* Use GetTempSpellType() for announce check in RaidGroupSay

* Make all spell shortnames plural where applicable

* Update bot.cpp

* Bots:BotsUseLiveBlockedMessage filter to spell failure

* Move GetSpellTargetList to only get called when necessary to reduce overhead

* formatting

* Formatting

* Simplify case SE_Illusion and SE_IllusionCopy for GetIllusionBlock

* Clean up InterruptSpell

* Cleanup IsBot() checks for DetermineSpellTargets->ST_GroupClientAndPet

* Cleanup range/aoe_range check in SpellFinished

* Cleanup DetermineSpellTargets->ST_GroupNoPets

* Cleanup DetermineSpellTargets->ST_Self for bot summon corpse

* Cleanup DetermineSpellTargets->ST_Pet

* Cleanup bot logic in TryBackstab

* Cleanup IsAttackAllowed checks for bots and their pets

* Cleanup StopMoving for bots

* Cleanup CanThisClassTripleAttack

* Fix casting for GetIllusionBlock checks

* Formatting

* Fix DetermineSpellTargets for group spells (this also wasn't properly checking the rule Character:EnableTGB in master)

* Cleanup spelltarget grabbing logic, consolidate group heals in to GetNumberNeedingHealedInGroup

* Throw added client los pet checks behind LoS cheat rule for bots

* CLeanup give_exp on npc death logic and ensure client pets always pass.

* Undo unintended rename from previous refactor

* Remove pointless Bots, SameRaidGroupForXP rule

* Revision to 0690783a9d1e99005d6bee0824597ea920e26df9

---------

Co-authored-by: Akkadius <akkadius1@gmail.com>
2025-02-03 04:02:42 -06:00

920 lines
31 KiB
C++

/**
* DO NOT MODIFY THIS FILE
*
* This repository was automatically generated and is NOT to be modified directly.
* Any repository modifications are meant to be made to the repository extending the base.
* Any modifications to base repositories are to be made by the generator only
*
* @generator ./utils/scripts/generators/repository-generator.pl
* @docs https://docs.eqemu.io/developer/repositories
*/
#ifndef EQEMU_BASE_BOT_DATA_REPOSITORY_H
#define EQEMU_BASE_BOT_DATA_REPOSITORY_H
#include "../../database.h"
#include "../../strings.h"
#include <ctime>
class BaseBotDataRepository {
public:
struct BotData {
uint32_t bot_id;
uint32_t owner_id;
uint32_t spells_id;
std::string name;
std::string last_name;
std::string title;
std::string suffix;
int16_t zone_id;
int8_t gender;
int16_t race;
int8_t class_;
uint8_t level;
uint32_t deity;
uint32_t creation_day;
uint32_t last_spawn;
uint32_t time_spawned;
float size;
int32_t face;
int32_t hair_color;
int32_t hair_style;
int32_t beard;
int32_t beard_color;
int32_t eye_color_1;
int32_t eye_color_2;
int32_t drakkin_heritage;
int32_t drakkin_tattoo;
int32_t drakkin_details;
int16_t ac;
int32_t atk;
int32_t hp;
int32_t mana;
int32_t str;
int32_t sta;
int32_t cha;
int32_t dex;
int32_t int_;
int32_t agi;
int32_t wis;
int32_t extra_haste;
int16_t fire;
int16_t cold;
int16_t magic;
int16_t poison;
int16_t disease;
int16_t corruption;
};
static std::string PrimaryKey()
{
return std::string("bot_id");
}
static std::vector<std::string> Columns()
{
return {
"bot_id",
"owner_id",
"spells_id",
"name",
"last_name",
"title",
"suffix",
"zone_id",
"gender",
"race",
"`class`",
"level",
"deity",
"creation_day",
"last_spawn",
"time_spawned",
"size",
"face",
"hair_color",
"hair_style",
"beard",
"beard_color",
"eye_color_1",
"eye_color_2",
"drakkin_heritage",
"drakkin_tattoo",
"drakkin_details",
"ac",
"atk",
"hp",
"mana",
"str",
"sta",
"cha",
"dex",
"`int`",
"agi",
"wis",
"extra_haste",
"fire",
"cold",
"magic",
"poison",
"disease",
"corruption",
};
}
static std::vector<std::string> SelectColumns()
{
return {
"bot_id",
"owner_id",
"spells_id",
"name",
"last_name",
"title",
"suffix",
"zone_id",
"gender",
"race",
"`class`",
"level",
"deity",
"creation_day",
"last_spawn",
"time_spawned",
"size",
"face",
"hair_color",
"hair_style",
"beard",
"beard_color",
"eye_color_1",
"eye_color_2",
"drakkin_heritage",
"drakkin_tattoo",
"drakkin_details",
"ac",
"atk",
"hp",
"mana",
"str",
"sta",
"cha",
"dex",
"`int`",
"agi",
"wis",
"extra_haste",
"fire",
"cold",
"magic",
"poison",
"disease",
"corruption",
};
}
static std::string ColumnsRaw()
{
return std::string(Strings::Implode(", ", Columns()));
}
static std::string SelectColumnsRaw()
{
return std::string(Strings::Implode(", ", SelectColumns()));
}
static std::string TableName()
{
return std::string("bot_data");
}
static std::string BaseSelect()
{
return fmt::format(
"SELECT {} FROM {}",
SelectColumnsRaw(),
TableName()
);
}
static std::string BaseInsert()
{
return fmt::format(
"INSERT INTO {} ({}) ",
TableName(),
ColumnsRaw()
);
}
static BotData NewEntity()
{
BotData e{};
e.bot_id = 0;
e.owner_id = 0;
e.spells_id = 0;
e.name = "";
e.last_name = "";
e.title = "";
e.suffix = "";
e.zone_id = 0;
e.gender = 0;
e.race = 0;
e.class_ = 0;
e.level = 0;
e.deity = 0;
e.creation_day = 0;
e.last_spawn = 0;
e.time_spawned = 0;
e.size = 0;
e.face = 1;
e.hair_color = 1;
e.hair_style = 1;
e.beard = 0;
e.beard_color = 1;
e.eye_color_1 = 1;
e.eye_color_2 = 1;
e.drakkin_heritage = 0;
e.drakkin_tattoo = 0;
e.drakkin_details = 0;
e.ac = 0;
e.atk = 0;
e.hp = 0;
e.mana = 0;
e.str = 75;
e.sta = 75;
e.cha = 75;
e.dex = 75;
e.int_ = 75;
e.agi = 75;
e.wis = 75;
e.extra_haste = 0;
e.fire = 0;
e.cold = 0;
e.magic = 0;
e.poison = 0;
e.disease = 0;
e.corruption = 0;
return e;
}
static BotData GetBotData(
const std::vector<BotData> &bot_datas,
int bot_data_id
)
{
for (auto &bot_data : bot_datas) {
if (bot_data.bot_id == bot_data_id) {
return bot_data;
}
}
return NewEntity();
}
static BotData FindOne(
Database& db,
int bot_data_id
)
{
auto results = db.QueryDatabase(
fmt::format(
"{} WHERE {} = {} LIMIT 1",
BaseSelect(),
PrimaryKey(),
bot_data_id
)
);
auto row = results.begin();
if (results.RowCount() == 1) {
BotData e{};
e.bot_id = row[0] ? static_cast<uint32_t>(strtoul(row[0], nullptr, 10)) : 0;
e.owner_id = row[1] ? static_cast<uint32_t>(strtoul(row[1], nullptr, 10)) : 0;
e.spells_id = row[2] ? static_cast<uint32_t>(strtoul(row[2], nullptr, 10)) : 0;
e.name = row[3] ? row[3] : "";
e.last_name = row[4] ? row[4] : "";
e.title = row[5] ? row[5] : "";
e.suffix = row[6] ? row[6] : "";
e.zone_id = row[7] ? static_cast<int16_t>(atoi(row[7])) : 0;
e.gender = row[8] ? static_cast<int8_t>(atoi(row[8])) : 0;
e.race = row[9] ? static_cast<int16_t>(atoi(row[9])) : 0;
e.class_ = row[10] ? static_cast<int8_t>(atoi(row[10])) : 0;
e.level = row[11] ? static_cast<uint8_t>(strtoul(row[11], nullptr, 10)) : 0;
e.deity = row[12] ? static_cast<uint32_t>(strtoul(row[12], nullptr, 10)) : 0;
e.creation_day = row[13] ? static_cast<uint32_t>(strtoul(row[13], nullptr, 10)) : 0;
e.last_spawn = row[14] ? static_cast<uint32_t>(strtoul(row[14], nullptr, 10)) : 0;
e.time_spawned = row[15] ? static_cast<uint32_t>(strtoul(row[15], nullptr, 10)) : 0;
e.size = row[16] ? strtof(row[16], nullptr) : 0;
e.face = row[17] ? static_cast<int32_t>(atoi(row[17])) : 1;
e.hair_color = row[18] ? static_cast<int32_t>(atoi(row[18])) : 1;
e.hair_style = row[19] ? static_cast<int32_t>(atoi(row[19])) : 1;
e.beard = row[20] ? static_cast<int32_t>(atoi(row[20])) : 0;
e.beard_color = row[21] ? static_cast<int32_t>(atoi(row[21])) : 1;
e.eye_color_1 = row[22] ? static_cast<int32_t>(atoi(row[22])) : 1;
e.eye_color_2 = row[23] ? static_cast<int32_t>(atoi(row[23])) : 1;
e.drakkin_heritage = row[24] ? static_cast<int32_t>(atoi(row[24])) : 0;
e.drakkin_tattoo = row[25] ? static_cast<int32_t>(atoi(row[25])) : 0;
e.drakkin_details = row[26] ? static_cast<int32_t>(atoi(row[26])) : 0;
e.ac = row[27] ? static_cast<int16_t>(atoi(row[27])) : 0;
e.atk = row[28] ? static_cast<int32_t>(atoi(row[28])) : 0;
e.hp = row[29] ? static_cast<int32_t>(atoi(row[29])) : 0;
e.mana = row[30] ? static_cast<int32_t>(atoi(row[30])) : 0;
e.str = row[31] ? static_cast<int32_t>(atoi(row[31])) : 75;
e.sta = row[32] ? static_cast<int32_t>(atoi(row[32])) : 75;
e.cha = row[33] ? static_cast<int32_t>(atoi(row[33])) : 75;
e.dex = row[34] ? static_cast<int32_t>(atoi(row[34])) : 75;
e.int_ = row[35] ? static_cast<int32_t>(atoi(row[35])) : 75;
e.agi = row[36] ? static_cast<int32_t>(atoi(row[36])) : 75;
e.wis = row[37] ? static_cast<int32_t>(atoi(row[37])) : 75;
e.extra_haste = row[38] ? static_cast<int32_t>(atoi(row[38])) : 0;
e.fire = row[39] ? static_cast<int16_t>(atoi(row[39])) : 0;
e.cold = row[40] ? static_cast<int16_t>(atoi(row[40])) : 0;
e.magic = row[41] ? static_cast<int16_t>(atoi(row[41])) : 0;
e.poison = row[42] ? static_cast<int16_t>(atoi(row[42])) : 0;
e.disease = row[43] ? static_cast<int16_t>(atoi(row[43])) : 0;
e.corruption = row[44] ? static_cast<int16_t>(atoi(row[44])) : 0;
return e;
}
return NewEntity();
}
static int DeleteOne(
Database& db,
int bot_data_id
)
{
std::string query;
if (RuleB(Bots, BotSoftDeletes)) {
query = fmt::format(
"UPDATE {} SET name = SUBSTRING(CONCAT(name, '-deleted-', UNIX_TIMESTAMP()), 1, 64) WHERE {} = {}",
TableName(),
PrimaryKey(),
bot_data_id
);
}
else {
query = fmt::format(
"DELETE FROM {} WHERE {} = {}",
TableName(),
PrimaryKey(),
bot_data_id
);
}
auto results = db.QueryDatabase(query);
return (results.Success() ? results.RowsAffected() : 0);
}
static int UpdateOne(
Database& db,
const BotData &e
)
{
std::vector<std::string> v;
auto columns = Columns();
v.push_back(columns[1] + " = " + std::to_string(e.owner_id));
v.push_back(columns[2] + " = " + std::to_string(e.spells_id));
v.push_back(columns[3] + " = '" + Strings::Escape(e.name) + "'");
v.push_back(columns[4] + " = '" + Strings::Escape(e.last_name) + "'");
v.push_back(columns[5] + " = '" + Strings::Escape(e.title) + "'");
v.push_back(columns[6] + " = '" + Strings::Escape(e.suffix) + "'");
v.push_back(columns[7] + " = " + std::to_string(e.zone_id));
v.push_back(columns[8] + " = " + std::to_string(e.gender));
v.push_back(columns[9] + " = " + std::to_string(e.race));
v.push_back(columns[10] + " = " + std::to_string(e.class_));
v.push_back(columns[11] + " = " + std::to_string(e.level));
v.push_back(columns[12] + " = " + std::to_string(e.deity));
v.push_back(columns[13] + " = " + std::to_string(e.creation_day));
v.push_back(columns[14] + " = " + std::to_string(e.last_spawn));
v.push_back(columns[15] + " = " + std::to_string(e.time_spawned));
v.push_back(columns[16] + " = " + std::to_string(e.size));
v.push_back(columns[17] + " = " + std::to_string(e.face));
v.push_back(columns[18] + " = " + std::to_string(e.hair_color));
v.push_back(columns[19] + " = " + std::to_string(e.hair_style));
v.push_back(columns[20] + " = " + std::to_string(e.beard));
v.push_back(columns[21] + " = " + std::to_string(e.beard_color));
v.push_back(columns[22] + " = " + std::to_string(e.eye_color_1));
v.push_back(columns[23] + " = " + std::to_string(e.eye_color_2));
v.push_back(columns[24] + " = " + std::to_string(e.drakkin_heritage));
v.push_back(columns[25] + " = " + std::to_string(e.drakkin_tattoo));
v.push_back(columns[26] + " = " + std::to_string(e.drakkin_details));
v.push_back(columns[27] + " = " + std::to_string(e.ac));
v.push_back(columns[28] + " = " + std::to_string(e.atk));
v.push_back(columns[29] + " = " + std::to_string(e.hp));
v.push_back(columns[30] + " = " + std::to_string(e.mana));
v.push_back(columns[31] + " = " + std::to_string(e.str));
v.push_back(columns[32] + " = " + std::to_string(e.sta));
v.push_back(columns[33] + " = " + std::to_string(e.cha));
v.push_back(columns[34] + " = " + std::to_string(e.dex));
v.push_back(columns[35] + " = " + std::to_string(e.int_));
v.push_back(columns[36] + " = " + std::to_string(e.agi));
v.push_back(columns[37] + " = " + std::to_string(e.wis));
v.push_back(columns[38] + " = " + std::to_string(e.extra_haste));
v.push_back(columns[39] + " = " + std::to_string(e.fire));
v.push_back(columns[40] + " = " + std::to_string(e.cold));
v.push_back(columns[41] + " = " + std::to_string(e.magic));
v.push_back(columns[42] + " = " + std::to_string(e.poison));
v.push_back(columns[43] + " = " + std::to_string(e.disease));
v.push_back(columns[44] + " = " + std::to_string(e.corruption));
auto results = db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
Strings::Implode(", ", v),
PrimaryKey(),
e.bot_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static BotData InsertOne(
Database& db,
BotData e
)
{
std::vector<std::string> v;
v.push_back(std::to_string(e.bot_id));
v.push_back(std::to_string(e.owner_id));
v.push_back(std::to_string(e.spells_id));
v.push_back("'" + Strings::Escape(e.name) + "'");
v.push_back("'" + Strings::Escape(e.last_name) + "'");
v.push_back("'" + Strings::Escape(e.title) + "'");
v.push_back("'" + Strings::Escape(e.suffix) + "'");
v.push_back(std::to_string(e.zone_id));
v.push_back(std::to_string(e.gender));
v.push_back(std::to_string(e.race));
v.push_back(std::to_string(e.class_));
v.push_back(std::to_string(e.level));
v.push_back(std::to_string(e.deity));
v.push_back(std::to_string(e.creation_day));
v.push_back(std::to_string(e.last_spawn));
v.push_back(std::to_string(e.time_spawned));
v.push_back(std::to_string(e.size));
v.push_back(std::to_string(e.face));
v.push_back(std::to_string(e.hair_color));
v.push_back(std::to_string(e.hair_style));
v.push_back(std::to_string(e.beard));
v.push_back(std::to_string(e.beard_color));
v.push_back(std::to_string(e.eye_color_1));
v.push_back(std::to_string(e.eye_color_2));
v.push_back(std::to_string(e.drakkin_heritage));
v.push_back(std::to_string(e.drakkin_tattoo));
v.push_back(std::to_string(e.drakkin_details));
v.push_back(std::to_string(e.ac));
v.push_back(std::to_string(e.atk));
v.push_back(std::to_string(e.hp));
v.push_back(std::to_string(e.mana));
v.push_back(std::to_string(e.str));
v.push_back(std::to_string(e.sta));
v.push_back(std::to_string(e.cha));
v.push_back(std::to_string(e.dex));
v.push_back(std::to_string(e.int_));
v.push_back(std::to_string(e.agi));
v.push_back(std::to_string(e.wis));
v.push_back(std::to_string(e.extra_haste));
v.push_back(std::to_string(e.fire));
v.push_back(std::to_string(e.cold));
v.push_back(std::to_string(e.magic));
v.push_back(std::to_string(e.poison));
v.push_back(std::to_string(e.disease));
v.push_back(std::to_string(e.corruption));
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
Strings::Implode(",", v)
)
);
if (results.Success()) {
e.bot_id = results.LastInsertedID();
return e;
}
e = NewEntity();
return e;
}
static int InsertMany(
Database& db,
const std::vector<BotData> &entries
)
{
std::vector<std::string> insert_chunks;
for (auto &e: entries) {
std::vector<std::string> v;
v.push_back(std::to_string(e.bot_id));
v.push_back(std::to_string(e.owner_id));
v.push_back(std::to_string(e.spells_id));
v.push_back("'" + Strings::Escape(e.name) + "'");
v.push_back("'" + Strings::Escape(e.last_name) + "'");
v.push_back("'" + Strings::Escape(e.title) + "'");
v.push_back("'" + Strings::Escape(e.suffix) + "'");
v.push_back(std::to_string(e.zone_id));
v.push_back(std::to_string(e.gender));
v.push_back(std::to_string(e.race));
v.push_back(std::to_string(e.class_));
v.push_back(std::to_string(e.level));
v.push_back(std::to_string(e.deity));
v.push_back(std::to_string(e.creation_day));
v.push_back(std::to_string(e.last_spawn));
v.push_back(std::to_string(e.time_spawned));
v.push_back(std::to_string(e.size));
v.push_back(std::to_string(e.face));
v.push_back(std::to_string(e.hair_color));
v.push_back(std::to_string(e.hair_style));
v.push_back(std::to_string(e.beard));
v.push_back(std::to_string(e.beard_color));
v.push_back(std::to_string(e.eye_color_1));
v.push_back(std::to_string(e.eye_color_2));
v.push_back(std::to_string(e.drakkin_heritage));
v.push_back(std::to_string(e.drakkin_tattoo));
v.push_back(std::to_string(e.drakkin_details));
v.push_back(std::to_string(e.ac));
v.push_back(std::to_string(e.atk));
v.push_back(std::to_string(e.hp));
v.push_back(std::to_string(e.mana));
v.push_back(std::to_string(e.str));
v.push_back(std::to_string(e.sta));
v.push_back(std::to_string(e.cha));
v.push_back(std::to_string(e.dex));
v.push_back(std::to_string(e.int_));
v.push_back(std::to_string(e.agi));
v.push_back(std::to_string(e.wis));
v.push_back(std::to_string(e.extra_haste));
v.push_back(std::to_string(e.fire));
v.push_back(std::to_string(e.cold));
v.push_back(std::to_string(e.magic));
v.push_back(std::to_string(e.poison));
v.push_back(std::to_string(e.disease));
v.push_back(std::to_string(e.corruption));
insert_chunks.push_back("(" + Strings::Implode(",", v) + ")");
}
std::vector<std::string> v;
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES {}",
BaseInsert(),
Strings::Implode(",", insert_chunks)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static std::vector<BotData> All(Database& db)
{
std::vector<BotData> all_entries;
auto results = db.QueryDatabase(
fmt::format(
"{}",
BaseSelect()
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
BotData e{};
e.bot_id = row[0] ? static_cast<uint32_t>(strtoul(row[0], nullptr, 10)) : 0;
e.owner_id = row[1] ? static_cast<uint32_t>(strtoul(row[1], nullptr, 10)) : 0;
e.spells_id = row[2] ? static_cast<uint32_t>(strtoul(row[2], nullptr, 10)) : 0;
e.name = row[3] ? row[3] : "";
e.last_name = row[4] ? row[4] : "";
e.title = row[5] ? row[5] : "";
e.suffix = row[6] ? row[6] : "";
e.zone_id = row[7] ? static_cast<int16_t>(atoi(row[7])) : 0;
e.gender = row[8] ? static_cast<int8_t>(atoi(row[8])) : 0;
e.race = row[9] ? static_cast<int16_t>(atoi(row[9])) : 0;
e.class_ = row[10] ? static_cast<int8_t>(atoi(row[10])) : 0;
e.level = row[11] ? static_cast<uint8_t>(strtoul(row[11], nullptr, 10)) : 0;
e.deity = row[12] ? static_cast<uint32_t>(strtoul(row[12], nullptr, 10)) : 0;
e.creation_day = row[13] ? static_cast<uint32_t>(strtoul(row[13], nullptr, 10)) : 0;
e.last_spawn = row[14] ? static_cast<uint32_t>(strtoul(row[14], nullptr, 10)) : 0;
e.time_spawned = row[15] ? static_cast<uint32_t>(strtoul(row[15], nullptr, 10)) : 0;
e.size = row[16] ? strtof(row[16], nullptr) : 0;
e.face = row[17] ? static_cast<int32_t>(atoi(row[17])) : 1;
e.hair_color = row[18] ? static_cast<int32_t>(atoi(row[18])) : 1;
e.hair_style = row[19] ? static_cast<int32_t>(atoi(row[19])) : 1;
e.beard = row[20] ? static_cast<int32_t>(atoi(row[20])) : 0;
e.beard_color = row[21] ? static_cast<int32_t>(atoi(row[21])) : 1;
e.eye_color_1 = row[22] ? static_cast<int32_t>(atoi(row[22])) : 1;
e.eye_color_2 = row[23] ? static_cast<int32_t>(atoi(row[23])) : 1;
e.drakkin_heritage = row[24] ? static_cast<int32_t>(atoi(row[24])) : 0;
e.drakkin_tattoo = row[25] ? static_cast<int32_t>(atoi(row[25])) : 0;
e.drakkin_details = row[26] ? static_cast<int32_t>(atoi(row[26])) : 0;
e.ac = row[27] ? static_cast<int16_t>(atoi(row[27])) : 0;
e.atk = row[28] ? static_cast<int32_t>(atoi(row[28])) : 0;
e.hp = row[29] ? static_cast<int32_t>(atoi(row[29])) : 0;
e.mana = row[30] ? static_cast<int32_t>(atoi(row[30])) : 0;
e.str = row[31] ? static_cast<int32_t>(atoi(row[31])) : 75;
e.sta = row[32] ? static_cast<int32_t>(atoi(row[32])) : 75;
e.cha = row[33] ? static_cast<int32_t>(atoi(row[33])) : 75;
e.dex = row[34] ? static_cast<int32_t>(atoi(row[34])) : 75;
e.int_ = row[35] ? static_cast<int32_t>(atoi(row[35])) : 75;
e.agi = row[36] ? static_cast<int32_t>(atoi(row[36])) : 75;
e.wis = row[37] ? static_cast<int32_t>(atoi(row[37])) : 75;
e.extra_haste = row[38] ? static_cast<int32_t>(atoi(row[38])) : 0;
e.fire = row[39] ? static_cast<int16_t>(atoi(row[39])) : 0;
e.cold = row[40] ? static_cast<int16_t>(atoi(row[40])) : 0;
e.magic = row[41] ? static_cast<int16_t>(atoi(row[41])) : 0;
e.poison = row[42] ? static_cast<int16_t>(atoi(row[42])) : 0;
e.disease = row[43] ? static_cast<int16_t>(atoi(row[43])) : 0;
e.corruption = row[44] ? static_cast<int16_t>(atoi(row[44])) : 0;
all_entries.push_back(e);
}
return all_entries;
}
static std::vector<BotData> GetWhere(Database& db, const std::string &where_filter)
{
std::vector<BotData> all_entries;
auto results = db.QueryDatabase(
fmt::format(
"{} WHERE {}",
BaseSelect(),
where_filter
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
BotData e{};
e.bot_id = row[0] ? static_cast<uint32_t>(strtoul(row[0], nullptr, 10)) : 0;
e.owner_id = row[1] ? static_cast<uint32_t>(strtoul(row[1], nullptr, 10)) : 0;
e.spells_id = row[2] ? static_cast<uint32_t>(strtoul(row[2], nullptr, 10)) : 0;
e.name = row[3] ? row[3] : "";
e.last_name = row[4] ? row[4] : "";
e.title = row[5] ? row[5] : "";
e.suffix = row[6] ? row[6] : "";
e.zone_id = row[7] ? static_cast<int16_t>(atoi(row[7])) : 0;
e.gender = row[8] ? static_cast<int8_t>(atoi(row[8])) : 0;
e.race = row[9] ? static_cast<int16_t>(atoi(row[9])) : 0;
e.class_ = row[10] ? static_cast<int8_t>(atoi(row[10])) : 0;
e.level = row[11] ? static_cast<uint8_t>(strtoul(row[11], nullptr, 10)) : 0;
e.deity = row[12] ? static_cast<uint32_t>(strtoul(row[12], nullptr, 10)) : 0;
e.creation_day = row[13] ? static_cast<uint32_t>(strtoul(row[13], nullptr, 10)) : 0;
e.last_spawn = row[14] ? static_cast<uint32_t>(strtoul(row[14], nullptr, 10)) : 0;
e.time_spawned = row[15] ? static_cast<uint32_t>(strtoul(row[15], nullptr, 10)) : 0;
e.size = row[16] ? strtof(row[16], nullptr) : 0;
e.face = row[17] ? static_cast<int32_t>(atoi(row[17])) : 1;
e.hair_color = row[18] ? static_cast<int32_t>(atoi(row[18])) : 1;
e.hair_style = row[19] ? static_cast<int32_t>(atoi(row[19])) : 1;
e.beard = row[20] ? static_cast<int32_t>(atoi(row[20])) : 0;
e.beard_color = row[21] ? static_cast<int32_t>(atoi(row[21])) : 1;
e.eye_color_1 = row[22] ? static_cast<int32_t>(atoi(row[22])) : 1;
e.eye_color_2 = row[23] ? static_cast<int32_t>(atoi(row[23])) : 1;
e.drakkin_heritage = row[24] ? static_cast<int32_t>(atoi(row[24])) : 0;
e.drakkin_tattoo = row[25] ? static_cast<int32_t>(atoi(row[25])) : 0;
e.drakkin_details = row[26] ? static_cast<int32_t>(atoi(row[26])) : 0;
e.ac = row[27] ? static_cast<int16_t>(atoi(row[27])) : 0;
e.atk = row[28] ? static_cast<int32_t>(atoi(row[28])) : 0;
e.hp = row[29] ? static_cast<int32_t>(atoi(row[29])) : 0;
e.mana = row[30] ? static_cast<int32_t>(atoi(row[30])) : 0;
e.str = row[31] ? static_cast<int32_t>(atoi(row[31])) : 75;
e.sta = row[32] ? static_cast<int32_t>(atoi(row[32])) : 75;
e.cha = row[33] ? static_cast<int32_t>(atoi(row[33])) : 75;
e.dex = row[34] ? static_cast<int32_t>(atoi(row[34])) : 75;
e.int_ = row[35] ? static_cast<int32_t>(atoi(row[35])) : 75;
e.agi = row[36] ? static_cast<int32_t>(atoi(row[36])) : 75;
e.wis = row[37] ? static_cast<int32_t>(atoi(row[37])) : 75;
e.extra_haste = row[38] ? static_cast<int32_t>(atoi(row[38])) : 0;
e.fire = row[39] ? static_cast<int16_t>(atoi(row[39])) : 0;
e.cold = row[40] ? static_cast<int16_t>(atoi(row[40])) : 0;
e.magic = row[41] ? static_cast<int16_t>(atoi(row[41])) : 0;
e.poison = row[42] ? static_cast<int16_t>(atoi(row[42])) : 0;
e.disease = row[43] ? static_cast<int16_t>(atoi(row[43])) : 0;
e.corruption = row[44] ? static_cast<int16_t>(atoi(row[44])) : 0;
all_entries.push_back(e);
}
return all_entries;
}
static int DeleteWhere(Database& db, const std::string &where_filter)
{
auto results = db.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {}",
TableName(),
where_filter
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int Truncate(Database& db)
{
auto results = db.QueryDatabase(
fmt::format(
"TRUNCATE TABLE {}",
TableName()
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int64 GetMaxId(Database& db)
{
auto results = db.QueryDatabase(
fmt::format(
"SELECT COALESCE(MAX({}), 0) FROM {}",
PrimaryKey(),
TableName()
)
);
return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0);
}
static int64 Count(Database& db, const std::string &where_filter = "")
{
auto results = db.QueryDatabase(
fmt::format(
"SELECT COUNT(*) FROM {} {}",
TableName(),
(where_filter.empty() ? "" : "WHERE " + where_filter)
)
);
return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0);
}
static std::string BaseReplace()
{
return fmt::format(
"REPLACE INTO {} ({}) ",
TableName(),
ColumnsRaw()
);
}
static int ReplaceOne(
Database& db,
const BotData &e
)
{
std::vector<std::string> v;
v.push_back(std::to_string(e.bot_id));
v.push_back(std::to_string(e.owner_id));
v.push_back(std::to_string(e.spells_id));
v.push_back("'" + Strings::Escape(e.name) + "'");
v.push_back("'" + Strings::Escape(e.last_name) + "'");
v.push_back("'" + Strings::Escape(e.title) + "'");
v.push_back("'" + Strings::Escape(e.suffix) + "'");
v.push_back(std::to_string(e.zone_id));
v.push_back(std::to_string(e.gender));
v.push_back(std::to_string(e.race));
v.push_back(std::to_string(e.class_));
v.push_back(std::to_string(e.level));
v.push_back(std::to_string(e.deity));
v.push_back(std::to_string(e.creation_day));
v.push_back(std::to_string(e.last_spawn));
v.push_back(std::to_string(e.time_spawned));
v.push_back(std::to_string(e.size));
v.push_back(std::to_string(e.face));
v.push_back(std::to_string(e.hair_color));
v.push_back(std::to_string(e.hair_style));
v.push_back(std::to_string(e.beard));
v.push_back(std::to_string(e.beard_color));
v.push_back(std::to_string(e.eye_color_1));
v.push_back(std::to_string(e.eye_color_2));
v.push_back(std::to_string(e.drakkin_heritage));
v.push_back(std::to_string(e.drakkin_tattoo));
v.push_back(std::to_string(e.drakkin_details));
v.push_back(std::to_string(e.ac));
v.push_back(std::to_string(e.atk));
v.push_back(std::to_string(e.hp));
v.push_back(std::to_string(e.mana));
v.push_back(std::to_string(e.str));
v.push_back(std::to_string(e.sta));
v.push_back(std::to_string(e.cha));
v.push_back(std::to_string(e.dex));
v.push_back(std::to_string(e.int_));
v.push_back(std::to_string(e.agi));
v.push_back(std::to_string(e.wis));
v.push_back(std::to_string(e.extra_haste));
v.push_back(std::to_string(e.fire));
v.push_back(std::to_string(e.cold));
v.push_back(std::to_string(e.magic));
v.push_back(std::to_string(e.poison));
v.push_back(std::to_string(e.disease));
v.push_back(std::to_string(e.corruption));
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseReplace(),
Strings::Implode(",", v)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int ReplaceMany(
Database& db,
const std::vector<BotData> &entries
)
{
std::vector<std::string> insert_chunks;
for (auto &e: entries) {
std::vector<std::string> v;
v.push_back(std::to_string(e.bot_id));
v.push_back(std::to_string(e.owner_id));
v.push_back(std::to_string(e.spells_id));
v.push_back("'" + Strings::Escape(e.name) + "'");
v.push_back("'" + Strings::Escape(e.last_name) + "'");
v.push_back("'" + Strings::Escape(e.title) + "'");
v.push_back("'" + Strings::Escape(e.suffix) + "'");
v.push_back(std::to_string(e.zone_id));
v.push_back(std::to_string(e.gender));
v.push_back(std::to_string(e.race));
v.push_back(std::to_string(e.class_));
v.push_back(std::to_string(e.level));
v.push_back(std::to_string(e.deity));
v.push_back(std::to_string(e.creation_day));
v.push_back(std::to_string(e.last_spawn));
v.push_back(std::to_string(e.time_spawned));
v.push_back(std::to_string(e.size));
v.push_back(std::to_string(e.face));
v.push_back(std::to_string(e.hair_color));
v.push_back(std::to_string(e.hair_style));
v.push_back(std::to_string(e.beard));
v.push_back(std::to_string(e.beard_color));
v.push_back(std::to_string(e.eye_color_1));
v.push_back(std::to_string(e.eye_color_2));
v.push_back(std::to_string(e.drakkin_heritage));
v.push_back(std::to_string(e.drakkin_tattoo));
v.push_back(std::to_string(e.drakkin_details));
v.push_back(std::to_string(e.ac));
v.push_back(std::to_string(e.atk));
v.push_back(std::to_string(e.hp));
v.push_back(std::to_string(e.mana));
v.push_back(std::to_string(e.str));
v.push_back(std::to_string(e.sta));
v.push_back(std::to_string(e.cha));
v.push_back(std::to_string(e.dex));
v.push_back(std::to_string(e.int_));
v.push_back(std::to_string(e.agi));
v.push_back(std::to_string(e.wis));
v.push_back(std::to_string(e.extra_haste));
v.push_back(std::to_string(e.fire));
v.push_back(std::to_string(e.cold));
v.push_back(std::to_string(e.magic));
v.push_back(std::to_string(e.poison));
v.push_back(std::to_string(e.disease));
v.push_back(std::to_string(e.corruption));
insert_chunks.push_back("(" + Strings::Implode(",", v) + ")");
}
std::vector<std::string> v;
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES {}",
BaseReplace(),
Strings::Implode(",", insert_chunks)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
};
#endif //EQEMU_BASE_BOT_DATA_REPOSITORY_H