Separate tradeskill queries to not be cross-boundary, clean up logic

This commit is contained in:
Akkadius
2020-03-30 04:29:52 -05:00
parent e9b361445f
commit 128cc458fd
7 changed files with 837 additions and 411 deletions
@@ -0,0 +1,117 @@
/**
* EQEmulator: Everquest Server Emulator
* Copyright (C) 2001-2020 EQEmulator Development Team (https://github.com/EQEmu/Server)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY except by those people which sell it, which
* are required to give you total support for your newly bought product;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef EQEMU_CHARACTER_RECIPE_LIST_REPOSITORY_H
#define EQEMU_CHARACTER_RECIPE_LIST_REPOSITORY_H
#include "../database.h"
#include "../string_util.h"
class CharacterRecipeListRepository {
public:
struct CharacterRecipeList {
int character_id;
int recipe_id;
int made_count;
};
static std::vector<std::string> Columns()
{
return {
"char_id",
"recipe_id",
"madecount",
};
}
static std::string ColumnsRaw()
{
return std::string(implode(", ", Columns()));
}
static std::string TableName()
{
return std::string("char_recipe_list");
}
static std::string BaseSelect()
{
return std::string(
fmt::format(
"SELECT {} FROM {}",
ColumnsRaw(),
TableName()
)
);
}
static CharacterRecipeList GetDefaults()
{
CharacterRecipeList character_recipe_list_entry;
character_recipe_list_entry.character_id = 0;
character_recipe_list_entry.made_count = 0;
character_recipe_list_entry.recipe_id = 0;
return character_recipe_list_entry;
}
static std::vector<CharacterRecipeList> GetLearnedRecipeList(int character_id)
{
std::vector<CharacterRecipeList> character_recipe_list;
auto results = database.QueryDatabase(
fmt::format(
"{} WHERE char_id = {}",
BaseSelect(),
character_id
)
);
for (auto row = results.begin(); row != results.end(); ++row) {
CharacterRecipeList character_recipe_list_entry;
character_recipe_list_entry.character_id = character_id;
character_recipe_list_entry.recipe_id = atoi(row[1]);
character_recipe_list_entry.made_count = atoi(row[2]);
character_recipe_list.push_back(character_recipe_list_entry);
}
return character_recipe_list;
}
static CharacterRecipeList GetRecipe(
std::vector<CharacterRecipeList> character_recipe_list,
int recipe_id
)
{
for (auto &row : character_recipe_list) {
if (row.recipe_id == recipe_id) {
return row;
}
}
return GetDefaults();
}
};
#endif //EQEMU_CHARACTER_RECIPE_LIST_REPOSITORY_H
@@ -0,0 +1,134 @@
/**
* EQEmulator: Everquest Server Emulator
* Copyright (C) 2001-2020 EQEmulator Development Team (https://github.com/EQEmu/Server)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY except by those people which sell it, which
* are required to give you total support for your newly bought product;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef EQEMU_TRADESKILL_RECIPE_REPOSITORY_H
#define EQEMU_TRADESKILL_RECIPE_REPOSITORY_H
#include "../database.h"
#include "../string_util.h"
class TradeskillRecipeRepository {
public:
struct TradeskillRecipe {
int id;
std::string name;
int tradeskill;
int skillneeded;
int trivial;
uint8 nofail;
int replace_container;
std::string notes;
uint8 must_learn;
uint8 quest;
uint8 enabled;
};
static std::vector<std::string> Columns()
{
return {
"id",
"name",
"tradeskill",
"skillneeded",
"trivial",
"nofail",
"replace_container",
"notes",
"must_learn",
"quest",
"enabled",
};
}
static std::string ColumnsRaw()
{
return std::string(implode(", ", Columns()));
}
static std::string TableName()
{
return std::string("tradeskill_recipe");
}
static std::string BaseSelect()
{
return std::string(
fmt::format(
"SELECT {} FROM {}",
ColumnsRaw(),
TableName()
)
);
}
static TradeskillRecipe New()
{
TradeskillRecipe entry;
entry.id = 0;
entry.name = "";
entry.tradeskill = 0;
entry.skillneeded = 0;
entry.trivial = 0;
entry.nofail = 0;
entry.replace_container = 0;
entry.notes = "";
entry.must_learn = 0;
entry.quest = 0;
entry.enabled = 0;
return entry;
}
static TradeskillRecipe GetRecipe(int recipe_id)
{
auto results = content_db.QueryDatabase(
fmt::format(
"{} WHERE id = {}",
BaseSelect(),
recipe_id
)
);
TradeskillRecipe tradeskill_recipe = New();
auto row = results.begin();
if (results.RowCount() == 0) {
return tradeskill_recipe;
}
tradeskill_recipe.id = atoi(row[0]);
tradeskill_recipe.name = (row[1] ? row[1] : "");
tradeskill_recipe.tradeskill = atoi(row[2]);
tradeskill_recipe.skillneeded = atoi(row[3]);
tradeskill_recipe.trivial = atoi(row[4]);
tradeskill_recipe.nofail = atoi(row[5]);
tradeskill_recipe.replace_container = atoi(row[6]);
tradeskill_recipe.notes = (row[7] ? row[7] : "");
tradeskill_recipe.must_learn = atoi(row[8]);
tradeskill_recipe.quest = atoi(row[9]);
tradeskill_recipe.enabled = atoi(row[10]);
return tradeskill_recipe;
}
};
#endif