Skip to content

Commit

Permalink
Fix Event Scheduler (opentibiabr#471)
Browse files Browse the repository at this point in the history
This works directly with: opentibiabr/canary#239
  • Loading branch information
beats-dh authored and JoaoCRDias committed Jul 27, 2022
1 parent a130227 commit 1c6abc5
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 99 deletions.
9 changes: 5 additions & 4 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,13 @@ globalServerSaveTime = "06:00:00"
sortLootByChance = false

-- Rates
-- NOTE: rateExp, rateSkill and rateMagic is used as a fallback only
-- NOTE: rateExp, rateSkill and rateMagic is used when 'rateUseStages = false' - or a fallback only
-- To configure rates see file data/stages.lua
rateUseStages = false
rateExp = 1
rateSkill = 50
rateLoot = 3
rateMagic = 25
rateSkill = 1
rateLoot = 1
rateMagic = 1
rateSpawn = 1

-- Today regeneration condition over an loop every 1 second,
Expand Down
4 changes: 2 additions & 2 deletions data/XML/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<colors colordark="#235c00" colorlight="#2d7400" />
<details displaypriority="6" isseasonal="0" specialevent="0" />
</event>
<event name="Otservbr example 2" startdate="10/2/2020" enddate="11/7/2020" script="example.lua" >
<ingame exprate="50" lootrate="300" spawnrate="150" skillrate="100" />
<event name="Otservbr example 2" startdate="2/2/2022" enddate="11/7/2022" script="example.lua" >
<ingame exprate="500" lootrate="500" spawnrate="500" skillrate="500" />
<description description="Otserver br example 2 description 50% less exp, triple loot !chance!, 50% faster spawn and regular skill" />
<colors colordark="#735D10" colorlight="#8B6D05" />
<details displaypriority="6" isseasonal="0" specialevent="0" />
Expand Down
96 changes: 52 additions & 44 deletions data/events/scripts/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,20 @@ soulCondition:setTicks(4 * 60 * 1000)
soulCondition:setParameter(CONDITION_PARAM_SOULGAIN, 1)

local function useStamina(player)
if not player then
return false
end

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

local playerId = player:getId()
if not playerId then
return false
end

local currentTime = os.time()
local timePassed = currentTime - nextUseStaminaTime[playerId]
if timePassed <= 0 then
Expand All @@ -621,13 +629,21 @@ local function useStamina(player)
player:setStamina(staminaMinutes)
end

local function useStaminaXp(player)
local function useStaminaXpBoost(player)
if not player then
return false
end

local staminaMinutes = player:getExpBoostStamina() / 60
if staminaMinutes == 0 then
return
end

local playerId = player:getId()
if not playerId then
return false
end

local currentTime = os.time()
local timePassed = currentTime - nextUseXpStamina[playerId]
if timePassed <= 0 then
Expand All @@ -648,8 +664,8 @@ local function useStaminaXp(player)
player:setExpBoostStamina(staminaMinutes * 60)
end

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

Expand All @@ -661,62 +677,49 @@ function Player:onGainExperience(source, exp, rawExp)
end

-- Experience Stage Multiplier
local expStage = getRateFromTable(experienceStages, self:getLevel(), configManager.getNumber(configKeys.RATE_EXP))
exp = exp * expStage
baseExp = rawExp * expStage
if Game.getStorageValue(GlobalStorage.XpDisplayMode) > 0 then
displayRate = expStage
else
displayRate = 1
end
local expStage = getRateFromTable(experienceStages, self:getLevel(), configManager.getNumber(configKeys.RATE_EXPERIENCE))

-- Prey system
if configManager.getBoolean(configKeys.PREY_ENABLED) then
local monsterType = source:getType()
if monsterType and monsterType:raceId() > 0 then
exp = math.ceil((exp * self:getPreyExperiencePercentage(monsterType:raceId())) / 100)
end
-- Event scheduler
if SCHEDULE_EXP_RATE ~= 100 then
expStage = math.max(0, (expStage * SCHEDULE_EXP_RATE)/100)
end

-- Store Bonus
useStaminaXp(self) -- Use store boost stamina
useStaminaXpBoost(self) -- Use store boost stamina

local Boost = self:getExpBoostStamina()
local stillHasBoost = Boost > 0
local storeXpBoostAmount = stillHasBoost and self:getStoreXpBoost() or 0

self:setStoreXpBoost(storeXpBoostAmount)

if (storeXpBoostAmount > 0) then
exp = exp + (baseExp * (storeXpBoostAmount/100)) -- Exp Boost
end

-- Stamina Bonus
local staminaBoost = 1
if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
useStamina(self)
local staminaMinutes = self:getStamina()
if staminaMinutes > 2340 and self:isPremium() then
exp = exp * 1.5
self:setStaminaXpBoost(150)
elseif staminaMinutes <= 840 then
exp = exp * 0.5 --TODO destroy loot of people with 840- stamina
self:setStaminaXpBoost(50)
else
self:setStaminaXpBoost(100)
end
if staminaMinutes > 2340 and self:isPremium() then
staminaBoost = 1.5
elseif staminaMinutes <= 840 then
staminaBoost = 0.5 --TODO destroy loot of people with 840- stamina
end
self:setStaminaXpBoost(staminaBoost * 100)
end

-- Boosted creature
if source:getName():lower() == (Game.getBoostedCreature()):lower() then
if target:getName():lower() == (Game.getBoostedCreature()):lower() then
exp = exp * 2
end

-- Event scheduler
if SCHEDULE_EXP_RATE ~= 100 then
exp = (exp * SCHEDULE_EXP_RATE)/100
-- Prey system
if configManager.getBoolean(configKeys.PREY_ENABLED) then
local monsterType = target:getType()
if monsterType and monsterType:raceId() > 0 then
exp = math.ceil((exp * self:getPreyExperiencePercentage(monsterType:raceId())) / 100)
end
end
self:setBaseXpGain(displayRate * 100)
return exp

return math.max((exp * expStage + (exp * (storeXpBoostAmount/100))) * staminaBoost)
end

function Player:onLoseExperience(exp)
Expand All @@ -733,18 +736,23 @@ function Player:onGainSkillTries(skill, tries)
end

-- Event scheduler skill rate
if SCHEDULE_SKILL_RATE ~= 100 then
tries = (tries * SCHEDULE_SKILL_RATE)/100
local STAGES_DEFAULT = skillsStages or nil
local SKILL_DEFAULT = self:getSkillLevel(skill)
local RATE_DEFAULT = configManager.getNumber(configKeys.RATE_SKILL)

if(skill == SKILL_MAGLEVEL) then -- Magic Level
STAGES_DEFAULT = magicLevelStages or nil
SKILL_DEFAULT = self:getBaseMagicLevel()
RATE_DEFAULT = configManager.getNumber(configKeys.RATE_MAGIC)
end

local skillRate = configManager.getNumber(configKeys.RATE_SKILL)
local magicRate = configManager.getNumber(configKeys.RATE_MAGIC)
skillOrMagicRate = getRateFromTable(STAGES_DEFAULT, SKILL_DEFAULT, RATE_DEFAULT)

if(skill == SKILL_MAGLEVEL) then -- Magic getLevel
return tries * getRateFromTable(magicLevelStages, self:getBaseMagicLevel(), magicRate)
if SCHEDULE_SKILL_RATE ~= 100 then
skillOrMagicRate = math.max(0, (skillOrMagicRate * SCHEDULE_SKILL_RATE) / 100)
end

return tries * getRateFromTable(skillsStages, self:getSkillLevel(skill), skillRate)
return tries / 100 * (skillOrMagicRate * 100)
end

function Player:onRemoveCount(item)
Expand Down
18 changes: 1 addition & 17 deletions data/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ weatherConfig = {
SCHEDULE_LOOT_RATE = 100
SCHEDULE_EXP_RATE = 100
SCHEDULE_SKILL_RATE = 100
SCHEDULE_SPAWN_RATE = 100

-- MARRY
PROPOSED_STATUS = 1
Expand Down Expand Up @@ -84,23 +85,6 @@ startupGlobalStorages = {
GlobalStorage.FerumbrasAscendant.Elements.Done
}

do -- Event Schedule rates
local lootRate = Game.getEventSLoot()
if lootRate ~= 100 then
SCHEDULE_LOOT_RATE = lootRate
end

local expRate = Game.getEventSExp()
if expRate ~= 100 then
SCHEDULE_EXP_RATE = expRate
end

local skillRate = Game.getEventSSkill()
if skillRate ~= 100 then
SCHEDULE_SKILL_RATE = skillRate
end
end

table.contains = function(array, value)
for _, targetColumn in pairs(array) do
if targetColumn == value then
Expand Down
2 changes: 1 addition & 1 deletion data/lib/compat/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ function doPlayerAddExp(cid, exp, useMult, ...)
end

if useMult then
exp = exp * getRateFromTable(experienceStages, player:getLevel(), configManager.getNumber(configKeys.RATE_EXP))
exp = exp * getRateFromTable(experienceStages, player:getLevel(), configManager.getNumber(configKeys.RATE_EXPERIENCE))
end
return player:addExperience(exp, ...)
end
Expand Down
11 changes: 7 additions & 4 deletions data/lib/core/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function getFormattedWorldTime()
end

function getLootRandom()
return math.random(0, MAX_LOOTCHANCE) * 100 / (configManager.getNumber(configKeys.RATE_LOOT) * SCHEDULE_LOOT_RATE)
local multi = (configManager.getNumber(configKeys.RATE_LOOT) * SCHEDULE_LOOT_RATE)
return math.random(0, MAX_LOOTCHANCE) * 100 / math.max(1, multi)
end

local start = os.time()
Expand Down Expand Up @@ -50,9 +51,11 @@ function getJackLastMissionState(player)
end

function getRateFromTable(t, level, default)
for _, rate in ipairs(t) do
if level >= rate.minlevel and (not rate.maxlevel or level <= rate.maxlevel) then
return rate.multiplier
if (t ~= nil) then
for _, rate in ipairs(t) do
if level >= rate.minlevel and (not rate.maxlevel or level <= rate.maxlevel) then
return rate.multiplier
end
end
end
return default
Expand Down
30 changes: 15 additions & 15 deletions data/modules/scripts/daily_reward/daily_reward.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,21 @@ DailyReward.retrieveHistoryEntries = function(playerId)
return entries
end

DailyReward.loadDailyReward = function(playerId, source)
DailyReward.loadDailyReward = function(playerId, target)
local player = Player(playerId)
if not player then
return false
end
if source ~= 0 then
source = REWARD_FROM_SHRINE
if target ~= 0 then
target = REWARD_FROM_SHRINE
else
source = REWARD_FROM_PANEL
target = REWARD_FROM_PANEL
end

player:sendCollectionResource(ClientPackets.JokerResource, player:getJokerTokens())
player:sendCollectionResource(ClientPackets.CollectionResource, player:getCollectionTokens())
player:sendDailyReward()
player:sendOpenRewardWall(source)
player:sendOpenRewardWall(target)
player:sendDailyRewardCollectionState(DailyReward.isRewardTaken(player:getId()) and DAILY_REWARD_COLLECTED or DAILY_REWARD_NOTCOLLECTED)
return true
end
Expand All @@ -273,8 +273,8 @@ DailyReward.pickedReward = function(playerId)
return true
end

DailyReward.isShrine = function(source)
if source ~= 0 then
DailyReward.isShrine = function(target)
if target ~= 0 then
return false
else
return true
Expand Down Expand Up @@ -349,9 +349,9 @@ DailyReward.init = function(playerId)
player:loadDailyRewardBonuses()
end

DailyReward.processReward = function(playerId, source)
DailyReward.processReward = function(playerId, target)
DailyReward.pickedReward(playerId)
DailyReward.loadDailyReward(playerId, source)
DailyReward.loadDailyReward(playerId, target)
local player = Player(playerId)
if player then
player:loadDailyRewardBonuses()
Expand Down Expand Up @@ -405,8 +405,8 @@ function Player.selectDailyReward(self, msg)
return false
end

local source = msg:getByte() -- 0 -> shrine / 1 -> tibia panel
if not DailyReward.isShrine(source) then
local target = msg:getByte() -- 0 -> shrine / 1 -> tibia panel
if not DailyReward.isShrine(target) then
if self:getCollectionTokens() < 1 then
self:sendError("You do not have enough collection tokens to proceed.")
return false
Expand Down Expand Up @@ -476,7 +476,7 @@ function Player.selectDailyReward(self, msg)
-- Registering history
DailyReward.insertHistory(self:getGuid(), self:getDayStreak(), "Claimed reward no. \z
" .. self:getDayStreak() + 1 .. ". Picked items: " .. description)
DailyReward.processReward(playerId, source)
DailyReward.processReward(playerId, target)
end

local reward = nil
Expand All @@ -500,22 +500,22 @@ function Player.selectDailyReward(self, msg)
-- end
-- DailyReward.insertHistory(self:getGuid(), self:getDayStreak(), "Claimed reward no. \z
-- " .. self:getDayStreak() + 1 .. ". Picked reward: " .. description)
-- DailyReward.processReward(playerId, source)
-- DailyReward.processReward(playerId, target)
-- end

if (dailyTable.type == DAILY_REWARD_TYPE_XP_BOOST) then
self:setExpBoostStamina(self:getExpBoostStamina() + (reward * 60))
self:setStoreXpBoost(50)
DailyReward.insertHistory(self:getGuid(), self:getDayStreak(), "Claimed reward no. \z
" .. self:getDayStreak() + 1 .. ". Picked reward: XP Bonus for " .. reward .. " minutes.")
DailyReward.processReward(playerId, source)
DailyReward.processReward(playerId, target)
end

if (dailyTable.type == DAILY_REWARD_TYPE_PREY_REROLL) then
self:addPreyCards(reward)
DailyReward.insertHistory(self:getGuid(), self:getDayStreak(), "Claimed reward no. \z
" .. self:getDayStreak() + 1 .. ". Picked reward: " .. reward .. "x Prey bonus reroll(s)")
DailyReward.processReward(playerId, source)
DailyReward.processReward(playerId, target)
end

return true
Expand Down
14 changes: 13 additions & 1 deletion data/scripts/creaturescripts/others/advance_save.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local config = {
}

local advanceSave = CreatureEvent("AdvanceSave")

function advanceSave.onAdvance(player, skill, oldLevel, newLevel)
if skill ~= SKILL_LEVEL or newLevel <= oldLevel then
return true
Expand All @@ -22,6 +23,17 @@ function advanceSave.onAdvance(player, skill, oldLevel, newLevel)
if config.save then
player:save()
end

if Game.getStorageValue(GlobalStorage.XpDisplayMode) > 0 then
local baseRate = getRateFromTable(experienceStages, player:getLevel(), configManager.getNumber(configKeys.RATE_EXPERIENCE))
-- Event scheduler
if SCHEDULE_EXP_RATE ~= 100 then
baseRate = math.max(0, (baseRate * SCHEDULE_EXP_RATE)/100)
end
player:setBaseXpGain(baseRate * 100)
end

return true
end
advanceSave:register()

advanceSave:register()
Loading

0 comments on commit 1c6abc5

Please sign in to comment.