Skip to content

Commit

Permalink
improve: upgrade from storage to kv in data/ (#1979)
Browse files Browse the repository at this point in the history
- upgrade from storage to kv

---------

Co-authored-by: Luan Santos <[email protected]>
  • Loading branch information
Luan Luciano and luan authored Dec 29, 2023
1 parent 458eb99 commit a5dcb65
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 82 deletions.
5 changes: 3 additions & 2 deletions data-canary/scripts/actions/other/exercise_training.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function exerciseTraining.onUse(player, item, fromPosition, target, toPosition,
end
end

if player:getStorageValue(Storage.IsTraining) > os.time() then
local hasExhaustion = player:kv():get("training-exhaustion") or 0
if hasExhaustion > os.time() then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "This exercise dummy can only be used after a 30 second cooldown.")
return true
end
Expand All @@ -48,7 +49,7 @@ function exerciseTraining.onUse(player, item, fromPosition, target, toPosition,
_G.OnExerciseTraining[playerId].event = addEvent(ExerciseEvent, 0, playerId, targetPos, item.itemid, targetId)
_G.OnExerciseTraining[playerId].dummyPos = targetPos
player:setTraining(true)
player:setStorageValue(Storage.IsTraining, os.time() + 30)
player:kv():set("training-exhaustion", os.time() + 30)
end
return true
end
Expand Down
2 changes: 1 addition & 1 deletion data-canary/scripts/creaturescripts/familiar/on_death.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function familiarOnDeath.onDeath(creature, corpse, lasthitkiller, mostdamagekill
local vocation = FAMILIAR_ID[player:getVocation():getBaseId()]

if table.contains(vocation, creature:getName()) then
player:setStorageValue(Global.Storage.FamiliarSummon, os.time())
player:kv():set("familiar-summon-time", os.time())
for sendMessage = 1, #FAMILIAR_TIMER do
stopEvent(player:getStorageValue(FAMILIAR_TIMER[sendMessage].storage))
player:setStorageValue(FAMILIAR_TIMER[sendMessage].storage, -1)
Expand Down
26 changes: 5 additions & 21 deletions data-canary/scripts/creaturescripts/familiar/on_login.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ function familiarOnLogin.onLogin(player)
local vocation = FAMILIAR_ID[player:getVocation():getBaseId()]

local familiarName
local familiarTimeLeft = player:getStorageValue(Global.Storage.FamiliarSummon) - player:getLastLogout()
local familiarSummonTime = player:kv():get("familiar-summon-time") or 0
local familiarTimeLeft = familiarSummonTime - player:getLastLogout()

if vocation then
if (not player:isPremium() and player:hasFamiliar(vocation.id)) or player:getLevel() < 200 then
Expand All @@ -26,27 +27,10 @@ function familiarOnLogin.onLogin(player)
end
end

if familiarName then
local position = player:getPosition()
local familiarMonster = Game.createMonster(familiarName, position, true, false, player)
if familiarMonster then
familiarMonster:setOutfit({ lookType = player:getFamiliarLooktype() })
familiarMonster:registerEvent("FamiliarDeath")
position:sendMagicEffect(CONST_ME_MAGIC_BLUE)

local deltaSpeed = math.max(player:getSpeed() - familiarMonster:getSpeed(), 0)
familiarMonster:changeSpeed(deltaSpeed)

player:setStorageValue(Global.Storage.FamiliarSummon, os.time() + familiarTimeLeft)
addEvent(RemoveFamiliar, familiarTimeLeft * 1000, familiarMonster:getId(), player:getId())

for sendMessage = 1, #FAMILIAR_TIMER do
if player:getStorageValue(FAMILIAR_TIMER[sendMessage].storage) == -1 and familiarTimeLeft >= FAMILIAR_TIMER[sendMessage].countdown then
player:setStorageValue(FAMILIAR_TIMER[sendMessage].storage, addEvent(SendMessageFunction, (familiarTimeLeft - FAMILIAR_TIMER[sendMessage].countdown) * 1000, player:getId(), FAMILIAR_TIMER[sendMessage].message))
end
end
end
if not familiarName then
return true
end
player:createFamiliar(familiarName, familiarTimeLeft)
return true
end

Expand Down
6 changes: 0 additions & 6 deletions data-otservbr-global/lib/core/storages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ Reserved player action storage key ranges (const.h)

Storage = {
-- General storages
IsTraining = 30000,
-- Reserved in Global.Storage.NpcExhaust = 30001
Dragonfetish = 30003,
EdronRopeQuest = 30004,
GhostShipQuest = 30005,
Expand All @@ -102,10 +100,7 @@ Storage = {
RookgaardDestiny = 30020,
EruaranGreeting = 30021,
MaryzaCookbook = 30022,
-- Reserved in Global.Storage.CombatProtectionStorage = 30023
Factions = 30024,
-- Reserved in Global.Storage.BlockMovementStorage = 30025
-- Reserved in Global.Storage.FamiliarSummon = 30026
-- unused TrainerRoom = 30027,
-- unused NpcSpawn = 30028,
ExerciseDummyExhaust = 30029,
Expand All @@ -130,7 +125,6 @@ Storage = {
Navigator = 30048,
DwarvenLegs = 30049,
PrinceDrazzakTime = 30050,
-- Reserved in Global.Storage.StoreExaust = 30051
LemonCupcake = 30052,
BlueberryCupcake = 30053,
-- Reserved in Global.Storage.FamiliarSummonEvent10 = 30054
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ function exerciseTraining.onUse(player, item, fromPosition, target, toPosition,
end
end

if player:getStorageValue(Storage.IsTraining) > os.time() then
local hasExhaustion = player:kv():get("training-exhaustion") or 0
if hasExhaustion > os.time() then
player:sendTextMessage(MESSAGE_FAILURE, "You are already training!")
return true
end
Expand All @@ -64,7 +65,7 @@ function exerciseTraining.onUse(player, item, fromPosition, target, toPosition,
_G.OnExerciseTraining[playerId].event = addEvent(ExerciseEvent, 0, playerId, targetPos, item.itemid, targetId)
_G.OnExerciseTraining[playerId].dummyPos = targetPos
player:setTraining(true)
player:setStorageValue(Storage.IsTraining, os.time() + cooldown)
player:kv():set("training-exhaustion", os.time() + cooldown)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have started training on an exercise dummy.")
end
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function familiarOnDeath.onDeath(creature, corpse, lasthitkiller, mostdamagekill
local vocation = FAMILIAR_ID[player:getVocation():getBaseId()]

if table.contains(vocation, creature:getName()) then
player:setStorageValue(Global.Storage.FamiliarSummon, os.time())
player:kv():set("familiar-summon-time", os.time())
for sendMessage = 1, #FAMILIAR_TIMER do
stopEvent(player:getStorageValue(FAMILIAR_TIMER[sendMessage].storage))
player:setStorageValue(FAMILIAR_TIMER[sendMessage].storage, -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ function familiarOnLogin.onLogin(player)
local vocation = FAMILIAR_ID[player:getVocation():getBaseId()]

local familiarName
local familiarTimeLeft = player:getStorageValue(Global.Storage.FamiliarSummon) - player:getLastLogout()
local familiarSummonTime = player:kv():get("familiar-summon-time") or 0
local familiarTimeLeft = familiarSummonTime - player:getLastLogout()

if vocation then
if (not isPremium(player) and player:hasFamiliar(vocation.id)) or player:getLevel() < 200 then
if (not player:isPremium() and player:hasFamiliar(vocation.id)) or player:getLevel() < 200 then
player:removeFamiliar(vocation.id)
elseif isPremium(player) and player:getLevel() >= 200 then
elseif player:isPremium() and player:getLevel() >= 200 then
if familiarTimeLeft > 0 then
familiarName = vocation.name
end
Expand Down
13 changes: 7 additions & 6 deletions data-otservbr-global/scripts/creaturescripts/others/login.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
local function onMovementRemoveProtection(cid, oldPos, time)
local player = Player(cid)
local function onMovementRemoveProtection(playerId, oldPos, time)
local player = Player(playerId)
if not player then
return true
end

local playerPos = player:getPosition()
if (playerPos.x ~= oldPos.x or playerPos.y ~= oldPos.y or playerPos.z ~= oldPos.z) or player:getTarget() then
player:setStorageValue(Global.Storage.CombatProtectionStorage, 0)
player:kv():remove("combat-protection")
return true
end

addEvent(onMovementRemoveProtection, 1000, cid, oldPos, time - 1)
addEvent(onMovementRemoveProtection, 1000, playerId, oldPos, time - 1)
end

local function protectionZoneCheck(playerName)
Expand Down Expand Up @@ -250,8 +250,9 @@ function playerLogin.onLogin(player)
stats.playerId = player:getId()
end

if player:getStorageValue(Global.Storage.CombatProtectionStorage) < 1 then
player:setStorageValue(Global.Storage.CombatProtectionStorage, 1)
local isProtected = player:kv():get("combat-protection") or 0
if isProtected < 1 then
player:kv():set("combat-protection", 1)
onMovementRemoveProtection(playerId, player:getPosition(), 10)
end

Expand Down
11 changes: 6 additions & 5 deletions data/chatchannels/scripts/help.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local CHANNEL_HELP = 7
local storage = 456112

local muted = Condition(CONDITION_CHANNELMUTEDTICKS, CONDITIONID_DEFAULT)
muted:setParameter(CONDITION_PARAM_SUBID, CHANNEL_HELP)
Expand All @@ -12,7 +11,8 @@ function onSpeak(player, type, message)
return false
end

if player:getStorageValue(storage) > os.time() then
local hasExhaustion = player:kv():get("channel-help-exhaustion") or 0
if hasExhaustion > os.time() then
player:sendCancelMessage("You are muted from the Help channel for using it inappropriately.")
return false
end
Expand All @@ -25,7 +25,7 @@ function onSpeak(player, type, message)
if playerGroupType > target:getAccountType() then
if not target:getCondition(CONDITION_CHANNELMUTEDTICKS, CONDITIONID_DEFAULT, CHANNEL_HELP) then
target:addCondition(muted)
target:setStorageValue(storage, os.time() + 180)
target:kv():set("channel-help-exhaustion", os.time() + 180) -- 3 minutes
sendChannelMessage(CHANNEL_HELP, TALKTYPE_CHANNEL_R1, target:getName() .. " has been muted by " .. player:getName() .. " for using Help Channel inappropriately.")
else
player:sendCancelMessage("That player is already muted.")
Expand All @@ -42,10 +42,11 @@ function onSpeak(player, type, message)
local target = Player(targetName)
if target then
if playerGroupType > target:getAccountType() then
if target:getStorageValue(storage) > os.time() then
local hasExhaustionTarget = target:kv():get("channel-help-exhaustion") or 0
if hasExhaustionTarget > os.time() then
target:removeCondition(CONDITION_CHANNELMUTEDTICKS, CONDITIONID_DEFAULT, CHANNEL_HELP)
sendChannelMessage(CHANNEL_HELP, TALKTYPE_CHANNEL_R1, target:getName() .. " has been unmuted.")
target:setStorageValue(storage, -1)
target:kv():remove("channel-help-exhaustion")
else
player:sendCancelMessage("That player is not muted.")
end
Expand Down
14 changes: 7 additions & 7 deletions data/events/scripts/creature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ local function removeCombatProtection(playerUid)
time = 30
end

player:setStorageValue(Global.Storage.CombatProtectionStorage, 2)
player:kv():set("combat-protection", 2)
addEvent(function(playerFuncUid)
local playerEvent = Player(playerFuncUid)
if not playerEvent then
return
end

playerEvent:setStorageValue(Global.Storage.CombatProtectionStorage, 0)
playerEvent:kv():remove("combat-protection")
playerEvent:remove()
end, time * 1000, playerUid)
end
Expand All @@ -32,24 +32,24 @@ function Creature:onTargetCombat(target)

if target:isPlayer() then
if self:isMonster() then
local protectionStorage = target:getStorageValue(Global.Storage.CombatProtectionStorage)
local isProtected = target:kv():get("combat-protection") or 0

if target:getIp() == 0 then -- If player is disconnected, monster shall ignore to attack the player
if target:isPzLocked() then
return true
end
if protectionStorage <= 0 then
if isProtected <= 0 then
addEvent(removeCombatProtection, 30 * 1000, target.uid)
target:setStorageValue(Global.Storage.CombatProtectionStorage, 1)
elseif protectionStorage == 1 then
target:kv():set("combat-protection", 1)
elseif isProtected == 1 then
self:searchTarget()
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
end

return true
end

if protectionStorage >= os.time() then
if isProtected >= os.time() then
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
end
end
Expand Down
3 changes: 2 additions & 1 deletion data/events/scripts/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ function Player:onLookInBattleList(creature, distance)
local master = creature:getMaster()
local summons = { "sorcerer familiar", "knight familiar", "druid familiar", "paladin familiar" }
if master and table.contains(summons, creature:getName():lower()) then
local familiarSummonTime = master:kv():get("familiar-summon-time") or 0
description = description .. " (Master: " .. master:getName() .. "). \z
It will disappear in " .. getTimeInWords(master:getStorageValue(Global.Storage.FamiliarSummon) - os.time())
It will disappear in " .. getTimeInWords(familiarSummonTime - os.time())
end
end
if self:getGroup():getAccess() then
Expand Down
5 changes: 0 additions & 5 deletions data/libs/core/global_storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ Reserved player action storage key ranges (const.hpp)

Global = {
Storage = {
NpcExhaust = 30001,
CombatProtectionStorage = 30023,
BlockMovementStorage = 30025,
FamiliarSummon = 30026,
StoreExaust = 30051,
FamiliarSummonEvent10 = 30054,
FamiliarSummonEvent60 = 30055,
},
Expand Down
7 changes: 4 additions & 3 deletions data/libs/functions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ function Player.getCookiesDelivered(self)
end

function Player.allowMovement(self, allow)
return self:setStorageValue(Global.Storage.BlockMovementStorage, allow and -1 or 1)
return allow and self:kv():remove("block-movement") or self:kv():set("block-movement", 1)
end

function Player.hasAllowMovement(self)
return self:getStorageValue(Global.Storage.BlockMovementStorage) ~= 1
local blockMovement = self:kv():get("block-movement") or 0
return blockMovement ~= 1
end

function Player.checkGnomeRank(self)
Expand Down Expand Up @@ -431,7 +432,7 @@ function Player:createFamiliar(familiarName, timeLeft)
playerPosition:sendMagicEffect(CONST_ME_MAGIC_BLUE)
myFamiliar:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
-- Divide by 2 to get half the time (the default total time is 30 / 2 = 15)
self:setStorageValue(Global.Storage.FamiliarSummon, os.time() + timeLeft)
self:kv():set("familiar-summon-time", os.time() + timeLeft)
addEvent(RemoveFamiliar, timeLeft * 1000, myFamiliar:getId(), self:getId())
for sendMessage = 1, #FAMILIAR_TIMER do
self:setStorageValue(
Expand Down
39 changes: 21 additions & 18 deletions data/npclib/npc_system/modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,27 +223,30 @@ if Modules == nil then
npcHandler:say("First get rid of those blood stains! You are not going to ruin my vehicle!", npc, player)
elseif not player:removeMoneyBank(cost) then
npcHandler:say("You don't have enough money.", npc, player)
elseif os.time() < player:getStorageValue(Global.Storage.NpcExhaust) then
npcHandler:say("Sorry, but you need to wait three seconds before travel again.", player)
playerPosition:sendMagicEffect(CONST_ME_POFF)
else
npcHandler:removeInteraction(npc, player)
npcHandler:say(parameters.text or "Set the sails!", npc, player)

local destination = parameters.destination
if type(destination) == "function" then
destination = destination(player)
end
local hasExhaustion = player:kv():get("npc-exhaustion") or 0
if hasExhaustion > os.time() then
npcHandler:say("Sorry, but you need to wait three seconds before travel again.", player)
playerPosition:sendMagicEffect(CONST_ME_POFF)
else
npcHandler:removeInteraction(npc, player)
npcHandler:say(parameters.text or "Set the sails!", npc, player)

player:setStorageValue(Global.Storage.NpcExhaust, 3 + os.time())
player:teleportTo(destination)
playerPosition:sendMagicEffect(CONST_ME_TELEPORT)
local destination = parameters.destination
if type(destination) == "function" then
destination = destination(player)
end

-- What a foolish Quest - Mission 3
if Storage.WhatAFoolish.PieBoxTimer ~= nil then
if player:getStorageValue(Storage.WhatAFoolish.PieBoxTimer) > os.time() then
if destination ~= Position(32660, 31957, 15) then -- kazordoon steamboat
player:setStorageValue(Storage.WhatAFoolish.PieBoxTimer, 1)
player:kv():set("npc-exhaustion", os.time() + 3) -- 3 seconds
player:teleportTo(destination)
playerPosition:sendMagicEffect(CONST_ME_TELEPORT)

-- What a foolish Quest - Mission 3
if Storage.WhatAFoolish.PieBoxTimer ~= nil then
if player:getStorageValue(Storage.WhatAFoolish.PieBoxTimer) > os.time() then
if destination ~= Position(32660, 31957, 15) then -- kazordoon steamboat
player:setStorageValue(Storage.WhatAFoolish.PieBoxTimer, 1)
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion data/scripts/eventcallbacks/player/on_look.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function callback.playerOnLook(player, thing, position, distance)
if thing:isMonster() then
local master = thing:getMaster()
if master and table.contains({ "sorcerer familiar", "knight familiar", "druid familiar", "paladin familiar" }, thing:getName():lower()) then
description = string.format("%s (Master: %s). \z It will disappear in %s", description, master:getName(), getTimeInWords(master:getStorageValue(Global.Storage.FamiliarSummon) - os.time()))
local familiarSummonTime = master:kv():get("familiar-summon-time") or 0
description = string.format("%s (Master: %s). \z It will disappear in %s", description, master:getName(), getTimeInWords(familiarSummonTime - os.time()))
end
end
end
Expand Down

0 comments on commit a5dcb65

Please sign in to comment.