mirror of
https://github.com/mod-playerbots/mod-playerbots.git
synced 2026-01-13 09:07:19 +00:00
Big update.
This commit is contained in:
88
src/PlayerbotDbStore.cpp
Normal file
88
src/PlayerbotDbStore.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
|
||||
*/
|
||||
|
||||
#include "PlayerbotDbStore.h"
|
||||
#include "Playerbots.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void PlayerbotDbStore::Load(PlayerbotAI* botAI)
|
||||
{
|
||||
ObjectGuid::LowType guid = botAI->GetBot()->GetGUID().GetCounter();
|
||||
|
||||
PlayerbotsDatabasePreparedStatement* stmt = PlayerbotsDatabase.GetPreparedStatement(PLAYERBOTS_SEL_DB_STORE);
|
||||
stmt->SetData(0, guid);
|
||||
if (PreparedQueryResult result = PlayerbotsDatabase.Query(stmt))
|
||||
{
|
||||
botAI->ClearStrategies(BOT_STATE_COMBAT);
|
||||
botAI->ClearStrategies(BOT_STATE_NON_COMBAT);
|
||||
botAI->ChangeStrategy("+chat", BOT_STATE_COMBAT);
|
||||
botAI->ChangeStrategy("+chat", BOT_STATE_NON_COMBAT);
|
||||
|
||||
std::vector<std::string> values;
|
||||
do
|
||||
{
|
||||
Field* fields = result->Fetch();
|
||||
std::string const key = fields[0].Get<std::string>();
|
||||
std::string const value = fields[1].Get<std::string>();
|
||||
|
||||
if (key == "value")
|
||||
values.push_back(value);
|
||||
else if (key == "co")
|
||||
botAI->ChangeStrategy(value, BOT_STATE_COMBAT);
|
||||
else if (key == "nc")
|
||||
botAI->ChangeStrategy(value, BOT_STATE_NON_COMBAT);
|
||||
else if (key == "dead")
|
||||
botAI->ChangeStrategy(value, BOT_STATE_DEAD);
|
||||
}
|
||||
while (result->NextRow());
|
||||
|
||||
botAI->GetAiObjectContext()->Load(values);
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerbotDbStore::Save(PlayerbotAI* botAI)
|
||||
{
|
||||
ObjectGuid::LowType guid = botAI->GetBot()->GetGUID().GetCounter();
|
||||
|
||||
Reset(botAI);
|
||||
|
||||
std::vector<std::string> data = botAI->GetAiObjectContext()->Save();
|
||||
for (std::vector<std::string>::iterator i = data.begin(); i != data.end(); ++i)
|
||||
{
|
||||
SaveValue(guid, "value", *i);
|
||||
}
|
||||
|
||||
SaveValue(guid, "co", FormatStrategies("co", botAI->GetStrategies(BOT_STATE_COMBAT)));
|
||||
SaveValue(guid, "nc", FormatStrategies("nc", botAI->GetStrategies(BOT_STATE_NON_COMBAT)));
|
||||
SaveValue(guid, "dead", FormatStrategies("dead", botAI->GetStrategies(BOT_STATE_DEAD)));
|
||||
}
|
||||
|
||||
std::string const PlayerbotDbStore::FormatStrategies(std::string const type, std::vector<std::string> strategies)
|
||||
{
|
||||
std::ostringstream out;
|
||||
for (std::vector<std::string>::iterator i = strategies.begin(); i != strategies.end(); ++i)
|
||||
out << "+" << (*i).c_str() << ",";
|
||||
|
||||
std::string const res = out.str();
|
||||
return res.substr(0, res.size() - 1);
|
||||
}
|
||||
|
||||
void PlayerbotDbStore::Reset(PlayerbotAI* botAI)
|
||||
{
|
||||
ObjectGuid::LowType guid = botAI->GetBot()->GetGUID().GetCounter();
|
||||
|
||||
PlayerbotsDatabasePreparedStatement* stmt = PlayerbotsDatabase.GetPreparedStatement(PLAYERBOTS_DEL_CUSTOM_STRATEGY);
|
||||
stmt->SetData(0, guid);
|
||||
PlayerbotsDatabase.Execute(stmt);
|
||||
}
|
||||
|
||||
void PlayerbotDbStore::SaveValue(uint32 guid, std::string const key, std::string const value)
|
||||
{
|
||||
PlayerbotsDatabasePreparedStatement* stmt = PlayerbotsDatabase.GetPreparedStatement(PLAYERBOTS_INS_DB_STORE);
|
||||
stmt->SetData(0, guid);
|
||||
stmt->SetData(1, key);
|
||||
stmt->SetData(2, value);
|
||||
PlayerbotsDatabase.Execute(stmt);
|
||||
}
|
||||
Reference in New Issue
Block a user