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

View File

@ -68,7 +68,8 @@ my $database_schema = $json->decode($output);
# database
#############################################
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 $/;
$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);
}
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);
$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);
$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 = 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/) {
$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);
}
else {
$all_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] : \"\";\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] : %s;\n", $column_name_formatted, $index, $index, $default_value);
}
# print $column_name . "\n";
@ -512,7 +513,7 @@ foreach my $table_to_generate (@tables) {
# write extended repository
#############################################
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
if (-e $generated_repository) {
@ -538,15 +539,17 @@ print $generated_base_repository_files . "\n";
print "\n#Extended Repositories\n";
print $generated_repository_files . "\n";
sub trim {
sub trim
{
my $string = $_[0];
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub translate_mysql_data_type_to_c {
my $mysql_data_type = $_[0];
sub translate_mysql_data_type_to_c
{
my $mysql_data_type = $_[0];
my $mysql_column_type = $_[1];
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
sub get_reserved_cpp_variable_names {
sub get_reserved_cpp_variable_names
{
return (
"class",
"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];
for my $word (get_reserved_cpp_variable_names()) {
@ -611,7 +616,8 @@ sub format_column_name_for_cpp_var {
return $column_name;
}
sub format_column_name_for_mysql {
sub format_column_name_for_mysql
{
my $column_name = $_[0];
for my $word (get_reserved_cpp_variable_names()) {
if ($word eq $column_name) {

View File

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