[Global Loot] Convert Global Loot to Repositories (#3962)

* [Global Loot] Convert Global Loot to Repositories

# Notes
- Convert `LoadGlobalLoot()` to repositories.

* Update repository-generator.pl

* Final push.

* Update loottables.cpp

---------

Co-authored-by: Akkadius <akkadius1@gmail.com>
This commit is contained in:
Alex King 2024-01-13 00:37:26 -05:00 committed by GitHub
parent b9f997015a
commit 238c9fe667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 108 deletions

View File

@ -16,6 +16,7 @@
#include "../../strings.h" #include "../../strings.h"
#include <ctime> #include <ctime>
class BaseGlobalLootRepository { class BaseGlobalLootRepository {
public: public:
struct GlobalLoot { struct GlobalLoot {
@ -179,21 +180,21 @@ public:
if (results.RowCount() == 1) { if (results.RowCount() == 1) {
GlobalLoot e{}; GlobalLoot e{};
e.id = static_cast<int32_t>(atoi(row[0])); e.id = row[0] ? static_cast<int32_t>(atoi(row[0])) : 0;
e.description = row[1] ? row[1] : ""; e.description = row[1] ? row[1] : "";
e.loottable_id = static_cast<int32_t>(atoi(row[2])); e.loottable_id = row[2] ? static_cast<int32_t>(atoi(row[2])) : 0;
e.enabled = static_cast<int8_t>(atoi(row[3])); e.enabled = row[3] ? static_cast<int8_t>(atoi(row[3])) : 1;
e.min_level = static_cast<int32_t>(atoi(row[4])); e.min_level = row[4] ? static_cast<int32_t>(atoi(row[4])) : 0;
e.max_level = static_cast<int32_t>(atoi(row[5])); e.max_level = row[5] ? static_cast<int32_t>(atoi(row[5])) : 0;
e.rare = static_cast<int8_t>(atoi(row[6])); e.rare = row[6] ? static_cast<int8_t>(atoi(row[6])) : 0;
e.raid = static_cast<int8_t>(atoi(row[7])); e.raid = row[7] ? static_cast<int8_t>(atoi(row[7])) : 0;
e.race = row[8] ? row[8] : ""; e.race = row[8] ? row[8] : "";
e.class_ = row[9] ? row[9] : ""; e.class_ = row[9] ? row[9] : "";
e.bodytype = row[10] ? row[10] : ""; e.bodytype = row[10] ? row[10] : "";
e.zone = row[11] ? row[11] : ""; e.zone = row[11] ? row[11] : "";
e.hot_zone = static_cast<int8_t>(atoi(row[12])); e.hot_zone = row[12] ? static_cast<int8_t>(atoi(row[12])) : 0;
e.min_expansion = static_cast<int8_t>(atoi(row[13])); e.min_expansion = row[13] ? static_cast<int8_t>(atoi(row[13])) : -1;
e.max_expansion = static_cast<int8_t>(atoi(row[14])); e.max_expansion = row[14] ? static_cast<int8_t>(atoi(row[14])) : -1;
e.content_flags = row[15] ? row[15] : ""; e.content_flags = row[15] ? row[15] : "";
e.content_flags_disabled = row[16] ? row[16] : ""; e.content_flags_disabled = row[16] ? row[16] : "";
@ -362,21 +363,21 @@ public:
for (auto row = results.begin(); row != results.end(); ++row) { for (auto row = results.begin(); row != results.end(); ++row) {
GlobalLoot e{}; GlobalLoot e{};
e.id = static_cast<int32_t>(atoi(row[0])); e.id = row[0] ? static_cast<int32_t>(atoi(row[0])) : 0;
e.description = row[1] ? row[1] : ""; e.description = row[1] ? row[1] : "";
e.loottable_id = static_cast<int32_t>(atoi(row[2])); e.loottable_id = row[2] ? static_cast<int32_t>(atoi(row[2])) : 0;
e.enabled = static_cast<int8_t>(atoi(row[3])); e.enabled = row[3] ? static_cast<int8_t>(atoi(row[3])) : 1;
e.min_level = static_cast<int32_t>(atoi(row[4])); e.min_level = row[4] ? static_cast<int32_t>(atoi(row[4])) : 0;
e.max_level = static_cast<int32_t>(atoi(row[5])); e.max_level = row[5] ? static_cast<int32_t>(atoi(row[5])) : 0;
e.rare = static_cast<int8_t>(atoi(row[6])); e.rare = row[6] ? static_cast<int8_t>(atoi(row[6])) : 0;
e.raid = static_cast<int8_t>(atoi(row[7])); e.raid = row[7] ? static_cast<int8_t>(atoi(row[7])) : 0;
e.race = row[8] ? row[8] : ""; e.race = row[8] ? row[8] : "";
e.class_ = row[9] ? row[9] : ""; e.class_ = row[9] ? row[9] : "";
e.bodytype = row[10] ? row[10] : ""; e.bodytype = row[10] ? row[10] : "";
e.zone = row[11] ? row[11] : ""; e.zone = row[11] ? row[11] : "";
e.hot_zone = static_cast<int8_t>(atoi(row[12])); e.hot_zone = row[12] ? static_cast<int8_t>(atoi(row[12])) : 0;
e.min_expansion = static_cast<int8_t>(atoi(row[13])); e.min_expansion = row[13] ? static_cast<int8_t>(atoi(row[13])) : -1;
e.max_expansion = static_cast<int8_t>(atoi(row[14])); e.max_expansion = row[14] ? static_cast<int8_t>(atoi(row[14])) : -1;
e.content_flags = row[15] ? row[15] : ""; e.content_flags = row[15] ? row[15] : "";
e.content_flags_disabled = row[16] ? row[16] : ""; e.content_flags_disabled = row[16] ? row[16] : "";
@ -403,21 +404,21 @@ public:
for (auto row = results.begin(); row != results.end(); ++row) { for (auto row = results.begin(); row != results.end(); ++row) {
GlobalLoot e{}; GlobalLoot e{};
e.id = static_cast<int32_t>(atoi(row[0])); e.id = row[0] ? static_cast<int32_t>(atoi(row[0])) : 0;
e.description = row[1] ? row[1] : ""; e.description = row[1] ? row[1] : "";
e.loottable_id = static_cast<int32_t>(atoi(row[2])); e.loottable_id = row[2] ? static_cast<int32_t>(atoi(row[2])) : 0;
e.enabled = static_cast<int8_t>(atoi(row[3])); e.enabled = row[3] ? static_cast<int8_t>(atoi(row[3])) : 1;
e.min_level = static_cast<int32_t>(atoi(row[4])); e.min_level = row[4] ? static_cast<int32_t>(atoi(row[4])) : 0;
e.max_level = static_cast<int32_t>(atoi(row[5])); e.max_level = row[5] ? static_cast<int32_t>(atoi(row[5])) : 0;
e.rare = static_cast<int8_t>(atoi(row[6])); e.rare = row[6] ? static_cast<int8_t>(atoi(row[6])) : 0;
e.raid = static_cast<int8_t>(atoi(row[7])); e.raid = row[7] ? static_cast<int8_t>(atoi(row[7])) : 0;
e.race = row[8] ? row[8] : ""; e.race = row[8] ? row[8] : "";
e.class_ = row[9] ? row[9] : ""; e.class_ = row[9] ? row[9] : "";
e.bodytype = row[10] ? row[10] : ""; e.bodytype = row[10] ? row[10] : "";
e.zone = row[11] ? row[11] : ""; e.zone = row[11] ? row[11] : "";
e.hot_zone = static_cast<int8_t>(atoi(row[12])); e.hot_zone = row[12] ? static_cast<int8_t>(atoi(row[12])) : 0;
e.min_expansion = static_cast<int8_t>(atoi(row[13])); e.min_expansion = row[13] ? static_cast<int8_t>(atoi(row[13])) : -1;
e.max_expansion = static_cast<int8_t>(atoi(row[14])); e.max_expansion = row[14] ? static_cast<int8_t>(atoi(row[14])) : -1;
e.content_flags = row[15] ? row[15] : ""; e.content_flags = row[15] ? row[15] : "";
e.content_flags_disabled = row[16] ? row[16] : ""; e.content_flags_disabled = row[16] ? row[16] : "";

View File

@ -68,7 +68,8 @@ my $database_schema = $json->decode($output);
# database # database
############################################# #############################################
my $content; my $content;
open(my $fh, '<', $config_path) or die "cannot open file $config_path"; { open(my $fh, '<', $config_path) or die "cannot open file $config_path";
{
local $/; local $/;
$content = <$fh>; $content = <$fh>;
} }
@ -336,8 +337,8 @@ foreach my $table_to_generate (@tables) {
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtoll(row[%s] ? row[%s] : \"-1\", nullptr, 10);\n", $column_name_formatted, $index, $index); $find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtoll(row[%s] ? row[%s] : \"-1\", nullptr, 10);\n", $column_name_formatted, $index, $index);
} }
elsif ($data_type =~ /int/) { elsif ($data_type =~ /int/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = static_cast<%s>(atoi(row[%s]));\n", $column_name_formatted, $struct_data_type, $index); $all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? static_cast<%s>(atoi(row[%s])) : %s;\n", $column_name_formatted, $index, $struct_data_type, $index, $default_value);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = static_cast<%s>(atoi(row[%s]));\n", $column_name_formatted, $struct_data_type, $index); $find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? static_cast<%s>(atoi(row[%s])) : %s;\n", $column_name_formatted, $index, $struct_data_type, $index, $default_value);
} }
elsif ($data_type =~ /float|decimal/) { elsif ($data_type =~ /float|decimal/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? strtof(row[%s], nullptr) : %s;\n", $column_name_formatted, $index, $index, $default_value); $all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? strtof(row[%s], nullptr) : %s;\n", $column_name_formatted, $index, $index, $default_value);
@ -348,8 +349,8 @@ foreach my $table_to_generate (@tables) {
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? strtod(row[%s], nullptr) : %s;\n", $column_name_formatted, $index, $index, $default_value); $find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? strtod(row[%s], nullptr) : %s;\n", $column_name_formatted, $index, $index, $default_value);
} }
else { else {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? row[%s] : \"\";\n", $column_name_formatted, $index, $index); $all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? row[%s] : %s;\n", $column_name_formatted, $index, $index, $default_value);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? row[%s] : \"\";\n", $column_name_formatted, $index, $index); $find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? row[%s] : %s;\n", $column_name_formatted, $index, $index, $default_value);
} }
# print $column_name . "\n"; # print $column_name . "\n";
@ -512,7 +513,7 @@ foreach my $table_to_generate (@tables) {
# write extended repository # write extended repository
############################################# #############################################
if ($repository_generation_option eq "all" || $repository_generation_option eq "extended") { if ($repository_generation_option eq "all" || $repository_generation_option eq "extended") {
my $generated_repository = './common/repositories/' . $table_to_generate . '_repository.h'; my $generated_repository = './common/repositories/' . $table_to_generate . '_repository.h';
# check if file exists firsts # check if file exists firsts
if (-e $generated_repository) { if (-e $generated_repository) {
@ -538,15 +539,17 @@ print $generated_base_repository_files . "\n";
print "\n#Extended Repositories\n"; print "\n#Extended Repositories\n";
print $generated_repository_files . "\n"; print $generated_repository_files . "\n";
sub trim { sub trim
{
my $string = $_[0]; my $string = $_[0];
$string =~ s/^\s+//; $string =~ s/^\s+//;
$string =~ s/\s+$//; $string =~ s/\s+$//;
return $string; return $string;
} }
sub translate_mysql_data_type_to_c { sub translate_mysql_data_type_to_c
my $mysql_data_type = $_[0]; {
my $mysql_data_type = $_[0];
my $mysql_column_type = $_[1]; my $mysql_column_type = $_[1];
my $struct_data_type = "std::string"; my $struct_data_type = "std::string";
@ -590,7 +593,8 @@ sub translate_mysql_data_type_to_c {
} }
# This is so we can change reserved words on the cpp side to something that will continue be functional in the compilers # This is so we can change reserved words on the cpp side to something that will continue be functional in the compilers
sub get_reserved_cpp_variable_names { sub get_reserved_cpp_variable_names
{
return ( return (
"class", "class",
"int", "int",
@ -599,7 +603,8 @@ sub get_reserved_cpp_variable_names {
); );
} }
sub format_column_name_for_cpp_var { sub format_column_name_for_cpp_var
{
my $column_name = $_[0]; my $column_name = $_[0];
for my $word (get_reserved_cpp_variable_names()) { for my $word (get_reserved_cpp_variable_names()) {
@ -611,7 +616,8 @@ sub format_column_name_for_cpp_var {
return $column_name; return $column_name;
} }
sub format_column_name_for_mysql { sub format_column_name_for_mysql
{
my $column_name = $_[0]; my $column_name = $_[0];
for my $word (get_reserved_cpp_variable_names()) { for my $word (get_reserved_cpp_variable_names()) {
if ($word eq $column_name) { if ($word eq $column_name) {

View File

@ -27,6 +27,7 @@
#include "zonedb.h" #include "zonedb.h"
#include "global_loot_manager.h" #include "global_loot_manager.h"
#include "../common/repositories/criteria/content_filter_criteria.h" #include "../common/repositories/criteria/content_filter_criteria.h"
#include "../common/repositories/global_loot_repository.h"
#include "quest_parser_collection.h" #include "quest_parser_collection.h"
#ifdef _WINDOWS #ifdef _WINDOWS
@ -624,107 +625,90 @@ void NPC::AddLootTable(uint32 loottable_id)
void NPC::CheckGlobalLootTables() void NPC::CheckGlobalLootTables()
{ {
auto tables = zone->GetGlobalLootTables(this); const auto& l = zone->GetGlobalLootTables(this);
for (auto &id : tables) for (const auto& e : l) {
database.AddLootTableToNPC(this, id, &itemlist, nullptr, nullptr, nullptr, nullptr); database.AddLootTableToNPC(this, e, &itemlist, nullptr, nullptr, nullptr, nullptr);
}
} }
void ZoneDatabase::LoadGlobalLoot() void ZoneDatabase::LoadGlobalLoot()
{ {
auto query = fmt::format( const auto& l = GlobalLootRepository::GetWhere(
SQL *this,
( fmt::format(
SELECT "`enabled` = 1 {}",
id, ContentFilterCriteria::apply()
loottable_id, )
description,
min_level,
max_level,
rare,
raid,
race,
class,
bodytype,
zone,
hot_zone
FROM
global_loot
WHERE
enabled = 1
{}
),
ContentFilterCriteria::apply()
); );
auto results = QueryDatabase(query); if (l.empty()) {
if (!results.Success() || results.RowCount() == 0) {
return; return;
} }
LogInfo("Loaded [{}] global loot entries", Strings::Commify(results.RowCount())); LogInfo(
"Loaded [{}] Global Loot Entr{}.",
Strings::Commify(l.size()),
l.size() != 1 ? "ies" : "y"
);
// we might need this, lets not keep doing it in a loop const std::string& zone_id = std::to_string(zone->GetZoneID());
auto zoneid = std::to_string(zone->GetZoneID());
for (auto row = results.begin(); row != results.end(); ++row) {
// checking zone limits
if (row[10]) {
auto zones = Strings::Split(row[10], '|');
auto it = std::find(zones.begin(), zones.end(), zoneid); for (const auto& e : l) {
if (it == zones.end()) { // not in here, skip if (!e.zone.empty()) {
const auto& zones = Strings::Split(e.zone, "|");
if (!Strings::Contains(zones, zone_id)) {
continue; continue;
} }
} }
GlobalLootEntry e(Strings::ToInt(row[0]), Strings::ToInt(row[1]), row[2] ? row[2] : ""); GlobalLootEntry gle(e.id, e.loottable_id, e.description);
auto min_level = Strings::ToInt(row[3]); if (e.min_level) {
if (min_level) { gle.AddRule(GlobalLoot::RuleTypes::LevelMin, e.min_level);
e.AddRule(GlobalLoot::RuleTypes::LevelMin, min_level);
} }
auto max_level = Strings::ToInt(row[4]); if (e.max_level) {
if (max_level) { gle.AddRule(GlobalLoot::RuleTypes::LevelMax, e.max_level);
e.AddRule(GlobalLoot::RuleTypes::LevelMax, max_level);
} }
// null is not used if (e.rare) {
if (row[5]) { gle.AddRule(GlobalLoot::RuleTypes::Rare, e.rare);
e.AddRule(GlobalLoot::RuleTypes::Rare, Strings::ToInt(row[5]));
} }
// null is not used if (e.raid) {
if (row[6]) { gle.AddRule(GlobalLoot::RuleTypes::Raid, e.raid);
e.AddRule(GlobalLoot::RuleTypes::Raid, Strings::ToInt(row[6]));
} }
if (row[7]) { if (!e.race.empty()) {
auto races = Strings::Split(row[7], '|'); const auto& races = Strings::Split(e.race, "|");
for (auto &r : races) for (const auto& r : races) {
e.AddRule(GlobalLoot::RuleTypes::Race, Strings::ToInt(r)); gle.AddRule(GlobalLoot::RuleTypes::Race, Strings::ToInt(r));
}
} }
if (row[8]) { if (!e.class_.empty()) {
auto classes = Strings::Split(row[8], '|'); const auto& classes = Strings::Split(e.class_, "|");
for (auto &c : classes) for (const auto& c : classes) {
e.AddRule(GlobalLoot::RuleTypes::Class, Strings::ToInt(c)); gle.AddRule(GlobalLoot::RuleTypes::Class, Strings::ToInt(c));
}
} }
if (row[9]) { if (!e.bodytype.empty()) {
auto bodytypes = Strings::Split(row[9], '|'); const auto& bodytypes = Strings::Split(e.bodytype, "|");
for (auto &b : bodytypes) for (const auto& b : bodytypes) {
e.AddRule(GlobalLoot::RuleTypes::BodyType, Strings::ToInt(b)); gle.AddRule(GlobalLoot::RuleTypes::BodyType, Strings::ToInt(b));
}
} }
// null is not used if (e.hot_zone) {
if (row[11]) { gle.AddRule(GlobalLoot::RuleTypes::HotZone, e.hot_zone);
e.AddRule(GlobalLoot::RuleTypes::HotZone, Strings::ToInt(row[11]));
} }
zone->AddGlobalLootEntry(e); zone->AddGlobalLootEntry(gle);
} }
} }