Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate onGainExperience logic into its own file #4505

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions data/events/scripts/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,86 +218,7 @@ function Player:onPodiumEdit(item, outfit, direction, isVisible)
item:setOutfit(podiumOutfit)
end

local soulCondition = Condition(CONDITION_SOUL, CONDITIONID_DEFAULT)
soulCondition:setTicks(4 * 60 * 1000)
soulCondition:setParameter(CONDITION_PARAM_SOULGAIN, 1)

local function useStamina(player)
local staminaMinutes = player:getStamina()
if staminaMinutes == 0 then
return
end

local playerId = player:getId()
if not nextUseStaminaTime[playerId] then
nextUseStaminaTime[playerId] = 0
end

local currentTime = os.time()
local timePassed = currentTime - nextUseStaminaTime[playerId]
if timePassed <= 0 then
return
end

if timePassed > 60 then
if staminaMinutes > 2 then
staminaMinutes = staminaMinutes - 2
else
staminaMinutes = 0
end
nextUseStaminaTime[playerId] = currentTime + 120
else
staminaMinutes = staminaMinutes - 1
nextUseStaminaTime[playerId] = currentTime + 60
end
player:setStamina(staminaMinutes)
end

function Player:onGainExperience(source, exp, rawExp, sendText)
if not source or source:isPlayer() then
return exp
end

local level = self:getLevel()

-- Soul regeneration
local vocation = self:getVocation()
if self:getSoul() < vocation:getMaxSoul() and exp >= level then
soulCondition:setParameter(CONDITION_PARAM_SOULTICKS, vocation:getSoulGainTicks() * 1000)
self:addCondition(soulCondition)
end

-- Apply experience stage multiplier
exp = exp * Game.getExperienceStage(level)

-- Stamina modifier
if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
useStamina(self)

local staminaMinutes = self:getStamina()
if staminaMinutes > 2340 and self:isPremium() then
exp = exp * 1.5
elseif staminaMinutes <= 840 then
exp = exp * 0.5
end
end

-- Apply low level bonus
exp = exp * (1 + self:calculateLowLevelBonus(level) / 100)

if sendText then
local pos = self:getPosition()
local expString = exp .. (exp ~= 1 and " experience points." or " experience point.")
self:sendTextMessage(MESSAGE_EXPERIENCE, "You gained " .. expString, pos, exp, TEXTCOLOR_WHITE_EXP)

local spectators = Game.getSpectators(pos, false, true)
for _, spectator in ipairs(spectators) do
if spectator ~= self then
spectator:sendTextMessage(MESSAGE_EXPERIENCE_OTHERS, self:getName() .. " gained " .. expString)
end
end
end

local onGainExperience = EventCallback.onGainExperience
return onGainExperience and onGainExperience(self, source, exp, rawExp, sendText) or exp
end
Expand Down
95 changes: 95 additions & 0 deletions data/scripts/eventcallbacks/player/default_onGainExperience.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
local soulCondition = Condition(CONDITION_SOUL, CONDITIONID_DEFAULT)
soulCondition:setTicks(4 * 60 * 1000)
soulCondition:setParameter(CONDITION_PARAM_SOULGAIN, 1)

local function useStamina(player)
local staminaMinutes = player:getStamina()
if staminaMinutes == 0 then
return
end

local playerId = player:getId()
if not nextUseStaminaTime[playerId] then
nextUseStaminaTime[playerId] = 0
end

local currentTime = os.time()
local timePassed = currentTime - nextUseStaminaTime[playerId]
if timePassed <= 0 then
return
end

if timePassed > 60 then
if staminaMinutes > 2 then
staminaMinutes = staminaMinutes - 2
else
staminaMinutes = 0
end
nextUseStaminaTime[playerId] = currentTime + 120
else
staminaMinutes = staminaMinutes - 1
nextUseStaminaTime[playerId] = currentTime + 60
end
player:setStamina(staminaMinutes)
end

local default = EventCallback

function default.onGainExperience(player, source, exp, rawExp, sendText)
if not source or source:isPlayer() then
return exp
end

local level = player:getLevel()

-- Soul regeneration
local vocation = player:getVocation()
if player:getSoul() < vocation:getMaxSoul() and exp >= level then
soulCondition:setParameter(CONDITION_PARAM_SOULTICKS, vocation:getSoulGainTicks() * 1000)
player:addCondition(soulCondition)
end

-- Apply experience stage multiplier
exp = exp * Game.getExperienceStage(level)

-- Apply low level bonus
exp = exp * (1 + player:calculateLowLevelBonus(level) / 100)

-- Stamina modifier
if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
useStamina(player)

local staminaMinutes = player:getStamina()
if staminaMinutes > 2340 and player:isPremium() then
exp = exp * 1.5
elseif staminaMinutes <= 840 then
exp = exp * 0.5
end
end
return exp
end

default:register()

-- For this event, we use the trigger index math.huge so that this event is called last, thus ensuring that the
-- experience message is sent to the client with the correct value.

local message = EventCallback

function message.onGainExperience(player, source, exp, rawExp, sendText)
if sendText and exp ~= 0 then
local pos = player:getPosition()
local expString = exp .. (exp ~= 1 and " experience points." or " experience point.")
player:sendTextMessage(MESSAGE_EXPERIENCE, "You gained " .. expString, pos, exp, TEXTCOLOR_WHITE_EXP)

local spectators = Game.getSpectators(pos, false, true)
for _, spectator in ipairs(spectators) do
if spectator ~= player then
spectator:sendTextMessage(MESSAGE_EXPERIENCE_OTHERS, player:getName() .. " gained " .. expString)
end
end
end
return exp
end

message:register(math.huge)