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

Fix Event Scheduler #239

Merged
merged 9 commits into from
May 27, 2022
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
9 changes: 5 additions & 4 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ globalServerSaveTime = "06:00:00"
sortLootByChance = false

-- Rates
-- NOTE: rateExp, rateSkill and rateMagic is used as a fallback only
-- NOTE: rateExp, rateSkill and rateMagic are only used when 'rateUseStages = false'
-- 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="Canary example 2" startdate="10/2/2020" enddate="11/7/2020" script="example.lua" >
<ingame exprate="50" lootrate="300" spawnrate="150" skillrate="100" />
<event name="Canary example 2" startdate="2/2/2022" enddate="11/7/2022" script="example.lua" >
<ingame exprate="100" lootrate="100" spawnrate="100" skillrate="100" />
<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
101 changes: 84 additions & 17 deletions data/events/scripts/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,43 @@ local function useStamina(player)
player:setStamina(staminaMinutes)
end

function Player:onGainExperience(source, exp, rawExp)
if not source or source:isPlayer() then
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
return
end

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

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

Expand All @@ -285,31 +320,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
local expStage = getRateFromTable(experienceStages, self:getLevel(), configManager.getNumber(configKeys.RATE_EXPERIENCE))

-- Event scheduler
if SCHEDULE_EXP_RATE ~= 100 then
expStage = math.max(0, (expStage * SCHEDULE_EXP_RATE)/100)
end

-- Store Bonus
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)

-- Stamina modifier
-- Stamina Bonus
local staminaBoost = 1
if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
useStamina(self)

local staminaMinutes = self:getStamina()
if staminaMinutes > 2400 and self:isPremium() then
exp = exp * 1.5
elseif staminaMinutes <= 840 then
exp = exp * 0.5
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 target:getName():lower() == (Game.getBoostedCreature()):lower() then
exp = exp * 2
end

-- Prey system
if configManager.getBoolean(configKeys.PREY_ENABLED) then
local monsterType = source:getType()
local monsterType = target:getType()
if monsterType and monsterType:raceId() > 0 then
exp = math.ceil((exp * self:getPreyExperiencePercentage(monsterType:raceId())) / 100)
end
end

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

function Player:onLoseExperience(exp)
Expand All @@ -321,10 +374,24 @@ function Player:onGainSkillTries(skill, tries)
return tries
end

if skill == SKILL_MAGLEVEL then
return tries * configManager.getNumber(configKeys.RATE_MAGIC)
-- Event scheduler skill rate
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
return tries * configManager.getNumber(configKeys.RATE_SKILL)

skillOrMagicRate = getRateFromTable(STAGES_DEFAULT, SKILL_DEFAULT, RATE_DEFAULT)

if SCHEDULE_SKILL_RATE ~= 100 then
skillOrMagicRate = math.max(0, (skillOrMagicRate * SCHEDULE_SKILL_RATE) / 100)
end

return tries / 100 * (skillOrMagicRate * 100)
end

function Player:onChangeZone(zone)
Expand Down
18 changes: 1 addition & 17 deletions data/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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 @@ -65,23 +66,6 @@ if damageImpact == nil then
damageImpact = {}
end

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
11 changes: 7 additions & 4 deletions data/lib/core/functions/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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 All @@ -81,9 +82,11 @@ debug.sethook(function(event, line)
end, "l")

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
1 change: 1 addition & 0 deletions data/lib/core/storages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Storage = {
}

GlobalStorage = {
XpDisplayMode = 65006,
ExampleQuest = {
Example = 60000
}
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 @@ -477,7 +477,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 @@ -501,22 +501,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
39 changes: 39 additions & 0 deletions data/scripts/creaturescripts/advance_save.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local config = {
heal = true,
save = true,
effect = false
}

local advanceSave = CreatureEvent("AdvanceSave")

function advanceSave.onAdvance(player, skill, oldLevel, newLevel)
if skill ~= SKILL_LEVEL or newLevel <= oldLevel then
return true
end

if config.effect then
player:getPosition():sendMagicEffect(math.random(CONST_ME_FIREWORK_YELLOW, CONST_ME_FIREWORK_BLUE))
player:say('LEVEL UP!', TALKTYPE_MONSTER_SAY)
end

if config.heal then
player:addHealth(player:getMaxHealth())
end

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()
Loading