Skip to content

Commit

Permalink
fix: initialize totalCost correctly and refactor blessing purchase lo…
Browse files Browse the repository at this point in the history
…gic (#3142)

This fixes the initialization of the `totalCost` variable and
refactors the blessing purchase logic. Improvements include replacing
string concatenation with `string.format` for more efficient formatting,
as well as adjustments for better code clarity and readability. The code
is now more organized, and the calculation error has been fixed.
  • Loading branch information
omarcopires authored Nov 21, 2024
1 parent efbac6c commit ed8abb6
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions data/libs/systems/blessing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,9 @@ Blessings.BuyAllBlesses = function(player)
local hasToF = Blessings.Config.HasToF and player:hasBlessing(1) or true
local missingBless = player:getBlessings(nil, donthavefilter)
local missingBlessAmt = #missingBless + (hasToF and 0 or 1)
local totalCost
for i, bless in ipairs(missingBless) do
local totalCost = 0

for _, bless in ipairs(missingBless) do
totalCost = totalCost + Blessings.getBlessingCost(player:getLevel(), true, bless.id >= 7)
end

Expand All @@ -274,19 +275,19 @@ Blessings.BuyAllBlesses = function(player)
end

if player:removeMoneyBank(totalCost) then
metrics.addCounter("balance_decrease", remainsPrice, {
metrics.addCounter("balance_decrease", totalCost, {
player = player:getName(),
context = "blessings",
})

for i, v in ipairs(missingBless) do
player:addBlessing(v.id, 1)
for _, bless in ipairs(missingBless) do
player:addBlessing(bless.id, 1)
end

player:sendCancelMessage("You received the remaining " .. missingBlessAmt .. " blesses for a total of " .. totalCost .. " gold.")
player:sendCancelMessage(string.format("You received the remaining %d blesses for a total of %d gold.", missingBlessAmt, totalCost))
player:getPosition():sendMagicEffect(CONST_ME_HOLYAREA)
else
player:sendCancelMessage("You don't have enough money. You need " .. totalCost .. " to buy all blesses.", cid)
player:sendCancelMessage(string.format("You don't have enough money. You need %d to buy all blesses.", totalCost))
player:getPosition():sendMagicEffect(CONST_ME_POFF)
end

Expand Down

0 comments on commit ed8abb6

Please sign in to comment.