From 602377f62bbadd123295286dafbd44ed7ebb2b3a Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 5 Nov 2024 23:23:43 -0300 Subject: [PATCH] improve: optimize experience gain and loss calculations (#2905) Optimized the experience gain and loss calculations in the `onGainExperience` and `onLoseExperience` functions, adjusting the application of bonuses like stamina, VIP, and boosted creatures. This ensures more efficient performance and more accurate handling of player experience management. --- data/events/scripts/player.lua | 97 ++++++++++++---------------------- data/libs/functions/player.lua | 22 ++++++++ 2 files changed, 56 insertions(+), 63 deletions(-) diff --git a/data/events/scripts/player.lua b/data/events/scripts/player.lua index 2efc7d92f75..78653733b58 100644 --- a/data/events/scripts/player.lua +++ b/data/events/scripts/player.lua @@ -535,20 +535,17 @@ function Player:onGainExperience(target, exp, rawExp) self:addCondition(soulCondition) end - -- XP Boost Bonus - useStaminaXpBoost(self) -- Use stamina XP boost (store or daily reward) + -- Apply XP Boost (Store or Daily Reward) + useStaminaXpBoost(self) local xpBoostTimeLeft = self:getXpBoostTime() - local stillHasXpBoost = xpBoostTimeLeft > 0 - local xpBoostPercent = stillHasXpBoost and self:getXpBoostPercent() or 0 - - self:setXpBoostPercent(xpBoostPercent) + local hasXpBoost = xpBoostTimeLeft > 0 + local xpBoostPercent = hasXpBoost and self:getXpBoostPercent() or 0 -- Stamina Bonus local staminaBonusXp = 1 - local isStaminaEnabled = configManager.getBoolean(configKeys.STAMINA_SYSTEM) - useStamina(self, isStaminaEnabled) - if isStaminaEnabled then + if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then + useStamina(self, true) staminaBonusXp = self:getFinalBonusStamina() self:setStaminaXpBoost(staminaBonusXp * 100) end @@ -556,12 +553,12 @@ function Player:onGainExperience(target, exp, rawExp) -- Concoction System useConcoctionTime(self) - -- Boosted creature - if target:getName():lower() == (Game.getBoostedCreature()):lower() then + -- Apply Boosted Creature Bonus + if target:getName():lower() == Game.getBoostedCreature():lower() then exp = exp * 2 end - -- Prey system + -- Prey System if configManager.getBoolean(configKeys.PREY_ENABLED) then local monsterType = target:getType() if monsterType and monsterType:raceId() > 0 then @@ -569,18 +566,20 @@ function Player:onGainExperience(target, exp, rawExp) end end + -- VIP Bonus Experience if configManager.getBoolean(configKeys.VIP_SYSTEM_ENABLED) then local vipBonusExp = configManager.getNumber(configKeys.VIP_BONUS_EXP) if vipBonusExp > 0 and self:isVip() then - vipBonusExp = (vipBonusExp > 100 and 100) or vipBonusExp - exp = exp * (1 + (vipBonusExp / 100)) + exp = exp * (1 + math.min(vipBonusExp, 100) / 100) end end - local lowLevelBonuxExp = self:getFinalLowLevelBonus() - local baseRate = self:getFinalBaseRateExperience() + -- Final Adjustments: Low Level Bonus and Base Rate + local lowLevelBonusExp = self:getFinalLowLevelBonus() + local baseRateExp = self:getFinalBaseRateExperience() - return (exp + (exp * (xpBoostPercent / 100) + (exp * (lowLevelBonuxExp / 100)))) * staminaBonusXp * baseRate + -- Return final experience value + return (exp * (1 + xpBoostPercent / 100 + lowLevelBonusExp / 100)) * staminaBonusXp * baseRateExp end function Player:onLoseExperience(exp) @@ -592,42 +591,35 @@ function Player:onGainSkillTries(skill, tries) if IsRunningGlobalDatapack() and isSkillGrowthLimited(self, skill) then return 0 end + if not APPLY_SKILL_MULTIPLIER then return tries end - -- Event scheduler skill rate - local STAGES_DEFAULT = nil - if configManager.getBoolean(configKeys.RATE_USE_STAGES) then - STAGES_DEFAULT = skillsStages - end - local SKILL_DEFAULT = self:getSkillLevel(skill) - local RATE_DEFAULT = configManager.getNumber(configKeys.RATE_SKILL) + -- Default skill rate settings + local rateSkillStages = configManager.getBoolean(configKeys.RATE_USE_STAGES) and skillsStages or nil + local currentSkillLevel = self:getSkillLevel(skill) + local baseRate = configManager.getNumber(configKeys.RATE_SKILL) + -- Special case for magic level if skill == SKILL_MAGLEVEL then - -- Magic Level - if configManager.getBoolean(configKeys.RATE_USE_STAGES) then - STAGES_DEFAULT = magicLevelStages - end - SKILL_DEFAULT = self:getBaseMagicLevel() - RATE_DEFAULT = configManager.getNumber(configKeys.RATE_MAGIC) + rateSkillStages = configManager.getBoolean(configKeys.RATE_USE_STAGES) and magicLevelStages or nil + currentSkillLevel = self:getBaseMagicLevel() + baseRate = configManager.getNumber(configKeys.RATE_MAGIC) end - local skillOrMagicRate = getRateFromTable(STAGES_DEFAULT, SKILL_DEFAULT, RATE_DEFAULT) + -- Calculate skill rate from stages and schedule + local skillRate = getRateFromTable(rateSkillStages, currentSkillLevel, baseRate) + skillRate = (SCHEDULE_SKILL_RATE ~= 100) and (skillRate * SCHEDULE_SKILL_RATE / 100) or skillRate - if SCHEDULE_SKILL_RATE ~= 100 then - skillOrMagicRate = math.max(0, (skillOrMagicRate * SCHEDULE_SKILL_RATE) / 100) + -- Apply VIP boost if applicable + if configManager.getBoolean(configKeys.VIP_SYSTEM_ENABLED) and self:isVip() then + local vipBonusSkill = math.min(configManager.getNumber(configKeys.VIP_BONUS_SKILL), 100) + skillRate = skillRate + (skillRate * (vipBonusSkill / 100)) end - if configManager.getBoolean(configKeys.VIP_SYSTEM_ENABLED) then - local vipBoost = configManager.getNumber(configKeys.VIP_BONUS_SKILL) - if vipBoost > 0 and self:isVip() then - vipBoost = (vipBoost > 100 and 100) or vipBoost - skillOrMagicRate = skillOrMagicRate + (skillOrMagicRate * (vipBoost / 100)) - end - end - - return tries / 100 * (skillOrMagicRate * 100) + -- Calculate and return the final experience gain + return tries * skillRate end function Player:onCombat(target, item, primaryDamage, primaryType, secondaryDamage, secondaryType) @@ -678,24 +670,3 @@ function Player:onChangeZone(zone) end function Player:onInventoryUpdate(item, slot, equip) end - -function Player:getURL() - local playerLink = string.gsub(self:getName(), "%s+", "+") - local serverURL = configManager.getString(configKeys.URL) - return serverURL .. "/characters/" .. playerLink -end - -function Player:getMarkdownLink() - local vocation = self:vocationAbbrev() - local emoji = ":school_satchel:" - if self:isKnight() then - emoji = ":crossed_swords:" - elseif self:isPaladin() then - emoji = ":bow_and_arrow:" - elseif self:isDruid() then - emoji = ":herb:" - elseif self:isSorcerer() then - emoji = ":crystal_ball:" - end - return "**[" .. self:getName() .. "](" .. self:getURL() .. ")** " .. emoji .. " [_" .. vocation .. "_]" -end diff --git a/data/libs/functions/player.lua b/data/libs/functions/player.lua index a8497b9bef1..3f7c8e17b75 100644 --- a/data/libs/functions/player.lua +++ b/data/libs/functions/player.lua @@ -987,3 +987,25 @@ function Player:canGetReward(rewardId, questName) return true end + +function Player.getURL(self) + local playerName = self:getName():gsub("%s+", "+") + local serverURL = configManager.getString(configKeys.URL) + + return serverURL .. "/characters/" .. playerName +end + +local emojiMap = { + ["knight"] = ":crossed_swords:", + ["paladin"] = ":bow_and_arrow:", + ["druid"] = ":herb:", + ["sorcerer"] = ":crystal_ball:", +} + +function Player.getMarkdownLink(self) + local vocation = self:vocationAbbrev() + local emoji = emojiMap[self:getVocation():getName():lower()] or ":school_satchel:" + local playerURL = self:getURL() + + return string.format("**[%s](%s)** %s [_%s_]", self:getName(), playerURL, emoji, vocation) +end