Skip to content

Commit

Permalink
Store percentages as floats
Browse files Browse the repository at this point in the history
  • Loading branch information
dedmen committed May 10, 2024
1 parent 739ae72 commit 7487f3f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
15 changes: 15 additions & 0 deletions data/sql/db-world/updates/01_FloatPercentages.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ALTER TABLE `mod_auctionhousebot`
MODIFY COLUMN `percentgreytradegoods` float(11) NULL DEFAULT 0 COMMENT 'Sets the percentage of the Grey Trade Goods auction items' AFTER `maxitems`,
MODIFY COLUMN `percentwhitetradegoods` float(11) NULL DEFAULT 27 COMMENT 'Sets the percentage of the White Trade Goods auction items' AFTER `percentgreytradegoods`,
MODIFY COLUMN `percentgreentradegoods` float(11) NULL DEFAULT 12 COMMENT 'Sets the percentage of the Green Trade Goods auction items' AFTER `percentwhitetradegoods`,
MODIFY COLUMN `percentbluetradegoods` float(11) NULL DEFAULT 10 COMMENT 'Sets the percentage of the Blue Trade Goods auction items' AFTER `percentgreentradegoods`,
MODIFY COLUMN `percentpurpletradegoods` float(11) NULL DEFAULT 1 COMMENT 'Sets the percentage of the Purple Trade Goods auction items' AFTER `percentbluetradegoods`,
MODIFY COLUMN `percentorangetradegoods` float(11) NULL DEFAULT 0 COMMENT 'Sets the percentage of the Orange Trade Goods auction items' AFTER `percentpurpletradegoods`,
MODIFY COLUMN `percentyellowtradegoods` float(11) NULL DEFAULT 0 COMMENT 'Sets the percentage of the Yellow Trade Goods auction items' AFTER `percentorangetradegoods`,
MODIFY COLUMN `percentgreyitems` float(11) NULL DEFAULT 0 COMMENT 'Sets the percentage of the non trade Grey auction items' AFTER `percentyellowtradegoods`,
MODIFY COLUMN `percentwhiteitems` float(11) NULL DEFAULT 10 COMMENT 'Sets the percentage of the non trade White auction items' AFTER `percentgreyitems`,
MODIFY COLUMN `percentgreenitems` float(11) NULL DEFAULT 30 COMMENT 'Sets the percentage of the non trade Green auction items' AFTER `percentwhiteitems`,
MODIFY COLUMN `percentblueitems` float(11) NULL DEFAULT 8 COMMENT 'Sets the percentage of the non trade Blue auction items' AFTER `percentgreenitems`,
MODIFY COLUMN `percentpurpleitems` float(11) NULL DEFAULT 2 COMMENT 'Sets the percentage of the non trade Purple auction items' AFTER `percentblueitems`,
MODIFY COLUMN `percentorangeitems` float(11) NULL DEFAULT 0 COMMENT 'Sets the percentage of the non trade Orange auction items' AFTER `percentpurpleitems`,
MODIFY COLUMN `percentyellowitems` float(11) NULL DEFAULT 0 COMMENT 'Sets the percentage of the non trade Yellow auction items' AFTER `percentorangeitems`;
40 changes: 20 additions & 20 deletions src/AuctionHouseBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,22 +712,22 @@ void AuctionHouseBot::Commands(uint32 command, uint32 ahMapID, uint32 col, char*
char * param12 = strtok(NULL, " ");
char * param13 = strtok(NULL, " ");
char * param14 = strtok(NULL, " ");
uint32 greytg = (uint32) strtoul(param1, NULL, 0);
uint32 whitetg = (uint32) strtoul(param2, NULL, 0);
uint32 greentg = (uint32) strtoul(param3, NULL, 0);
uint32 bluetg = (uint32) strtoul(param4, NULL, 0);
uint32 purpletg = (uint32) strtoul(param5, NULL, 0);
uint32 orangetg = (uint32) strtoul(param6, NULL, 0);
uint32 yellowtg = (uint32) strtoul(param7, NULL, 0);
uint32 greyi = (uint32) strtoul(param8, NULL, 0);
uint32 whitei = (uint32) strtoul(param9, NULL, 0);
uint32 greeni = (uint32) strtoul(param10, NULL, 0);
uint32 bluei = (uint32) strtoul(param11, NULL, 0);
uint32 purplei = (uint32) strtoul(param12, NULL, 0);
uint32 orangei = (uint32) strtoul(param13, NULL, 0);
uint32 yellowi = (uint32) strtoul(param14, NULL, 0);

std::array<uint32, AHB_MAX_QUALITY> percentages =
float greytg = strtof(param1, NULL);
float whitetg = strtof(param2, NULL);
float greentg = strtof(param3, NULL);
float bluetg = strtof(param4, NULL);
float purpletg = strtof(param5, NULL);
float orangetg = strtof(param6, NULL);
float yellowtg = strtof(param7, NULL);
float greyi = strtof(param8, NULL);
float whitei = strtof(param9, NULL);
float greeni = strtof(param10, NULL);
float bluei = strtof(param11, NULL);
float purplei = strtof(param12, NULL);
float orangei = strtof(param13, NULL);
float yellowi = strtof(param14, NULL);

std::array<float, AHB_MAX_QUALITY> percentages =
{
greytg, whitetg, greentg, bluetg, purpletg, orangetg, yellowtg, greyi, whitei, greeni, bluei, purplei, orangei, yellowi
};
Expand Down Expand Up @@ -853,8 +853,8 @@ void AuctionHouseBot::LoadValues(AHBConfig* config)
maxstackgrey, maxstackwhite, maxstackgreen, maxstackblue, maxstackpurple, maxstackorange, maxstackyellow,
auctionName]
= result->FetchTuple<uint32, uint32,
uint32, uint32, uint32, uint32, uint32, uint32, uint32,
uint32, uint32, uint32, uint32, uint32, uint32, uint32,
float, float, float, float, float, float, float,
float, float, float, float, float, float, float,
uint32, uint32, uint32, uint32, uint32, uint32, uint32,
uint32, uint32, uint32, uint32, uint32, uint32, uint32,
uint32, uint32, uint32, uint32, uint32, uint32, uint32,
Expand All @@ -866,10 +866,10 @@ void AuctionHouseBot::LoadValues(AHBConfig* config)
config->SetMinItems(minitems);
config->SetMaxItems(maxitems);

std::array<uint32, AHB_MAX_QUALITY> percetages = { percentgreytradegoods, percentwhitetradegoods, percentgreentradegoods, percentbluetradegoods, percentpurpletradegoods, percentorangetradegoods, percentyellowtradegoods,
std::array<float, AHB_MAX_QUALITY> percentages = { percentgreytradegoods, percentwhitetradegoods, percentgreentradegoods, percentbluetradegoods, percentpurpletradegoods, percentorangetradegoods, percentyellowtradegoods,
percentgreyitems, percentwhiteitems, percentgreenitems, percentblueitems, percentpurpleitems, percentorangeitems, percentyellowitems };

config->SetPercentages(percetages);
config->SetPercentages(percentages);

// Load min and max prices
config->SetMinPrice(ITEM_QUALITY_POOR, minpricegrey);
Expand Down
13 changes: 7 additions & 6 deletions src/AuctionHouseBotConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ uint32 AHBConfig::GetMinItems()
return _minItems;
}

void AHBConfig::SetPercentages(std::array<uint32, AHB_MAX_QUALITY>& percentages)
void AHBConfig::SetPercentages(std::array<float, AHB_MAX_QUALITY>& percentages)
{
uint32 totalPercent = std::accumulate(percentages.begin(), percentages.end(), 0);
float totalPercent = std::accumulate(percentages.begin(), percentages.end(), 0.f);

if (totalPercent == 0)
{
_maxItems = 0;
}
else if (totalPercent != 100)
else if (std::fabs(totalPercent-100) > 0.1) // float approximate equal
{
// re-normalize all percentages
const float fixMultiplier = 100.f / static_cast<float>(totalPercent);
const float fixMultiplier = 100.f / totalPercent;

for (auto& it : percentages)
it *= fixMultiplier;
Expand Down Expand Up @@ -225,20 +225,21 @@ void AHBConfig::CalculatePercents()
for (size_t i = 0; i < AHB_MAX_QUALITY; i++)
{
double itemPercent = _itemsPercent[i];
_itemsPercentages[i] = uint32(itemPercent / 100 * _maxItems);
_itemsPercentages[i] = uint32(std::ceil(itemPercent / 100 * _maxItems));
}

uint32 totalPercent = std::accumulate(_itemsPercentages.begin(), _itemsPercentages.end(), 0);
int32 diff = (_maxItems - totalPercent);

// If we don't match the expected maxItems, fill it up using Normal/Uncommon items
if (diff < 0)
{
if (_itemsPercentages[AHB_ITEM_QUALITY_NORMAL] - diff > 0)
_itemsPercentages[AHB_ITEM_QUALITY_NORMAL] -= diff;
else if (_itemsPercentages[AHB_ITEM_QUALITY_UNCOMMON] - diff > 0)
_itemsPercentages[AHB_ITEM_QUALITY_UNCOMMON] -= diff;
}
else if (diff < 0)
else if (diff > 0)
_itemsPercentages[AHB_ITEM_QUALITY_NORMAL] += diff;
}

Expand Down
4 changes: 2 additions & 2 deletions src/AuctionHouseBotConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AHBConfig
return _maxItems;
}

void SetPercentages(std::array<uint32, AHB_MAX_QUALITY>& percentages);
void SetPercentages(std::array<float, AHB_MAX_QUALITY>& percentages);

uint32 GetPercentages(uint32 color);

Expand Down Expand Up @@ -161,7 +161,7 @@ class AHBConfig
Minutes _buyerBiddingInterval{ 0min };
uint32 _buyerBidsPerInterval{ 0 };

std::array<uint32, AHB_MAX_QUALITY> _itemsPercent{};
std::array<float, AHB_MAX_QUALITY> _itemsPercent{};
std::array<uint32, AHB_MAX_QUALITY> _itemsPercentages{};
std::array<uint32, AHB_DEFAULT_QUALITY_SIZE> _buyerPrice{};
std::array<uint32, AHB_DEFAULT_QUALITY_SIZE> _maxStack{};
Expand Down

0 comments on commit 7487f3f

Please sign in to comment.