--[[
In PHPMyAdmin:
ALTER TABLE `player_deaths` ADD `aol` INT(1) NOT NULL DEFAULT '0', ADD `bless` INT(1) NOT NULL DEFAULT '0'
]]
--[[
In creaturescripts.xml:
<event type="login" name="deathAolBless_login" event="script" value="deathAolBless.lua"/>
<event type="preparedeath" name="deathAolBless_preparedeath" event="script" value="deathAolBless.lua"/>
<event type="logout" name="deathAolBless_logout" event="script" value="deathAolBless.lua"/>
]]
-- file name: data/creaturescripts/scripts/deathAolBless.lua, YES, it's one file, not 3
local storeAOL = 23232
local storeBless = 23233
-- in login we add preparedeath event to 'cid' and set creaturestorages to 0,
-- because we don't want to use old (from deaths before login) informations
function onLogin(cid)
registerCreatureEvent(cid, "deathAolBless_preparedeath")
doSetCreatureStorage(cid, storeAOL, 0)
doSetCreatureStorage(cid, storeBless, 0)
return true
end
-- in this script we check if player wear AoL in neckles slot and if he has any blessing, and store informations in creaturestorage
function onPrepareDeath(cid)
if(getPlayerSlotItem(cid, 2) == 2173) then
doSetCreatureStorage(cid, storeAOL, 1)
end
if(getPlayerBlessing(cid) >= 1) then
doSetCreatureStorage(cid, storeBless, 1)
end
return true
end
-- player logout, server executes preparedeath event before logout event (if player died)
-- so values should be in 'storage' if he had aol/bless
-- ONLY PROBLEM with LUA script is when player do 'relog' in 2 seconds [why 2? read script comments] after death:
-- 1. he dies, server executes prepare death and logout events, it saves if he had aol/bless in `player_deaths` table
-- 2. player see on screen button to login again, he press it and login in temple (server executes onLogin)
-- 3. he logouts very fast (less then 2 seconds from death), server executes again onLogout script,
-- but in creaturestorage are values 0 from onLogin script
--
-- that's why I said it will work with 99.9% deaths and you need C++ code to make it work always
function onLogout(cid)
-- we don't need to run any SQL query if no AoL and no Bless, by default these columns in table contains 0
if(getCreatureStorage(cid, storeAOL) == 1 OR getCreatureStorage(cid, storeBless) == 1) then
-- we try to get death of 'this' (cid) player that happend in last 2 seconds
-- it's must be there (if he died, not just logout) before server logout him
local lastDeathDB = db.getResult("SELECT `id` FROM `player_deaths` WHERE `player_id` = " .. getPlayerGUID(cid) .. " AND `date` >= " .. (os.time()-2) .. " LIMIT 1;")
-- if result of query is -1, then there is no result, when it's not equal -1 we can do something with it..
if(lastDeathDB:getID() ~= -1) then
-- ..in this case we "SELECT `id`" which is number, so we get it from query result by function getDataInt("id")
local lastDeath = lastDeathDB:getDataInt("id")
-- after we did all what we wanted with result of query, we must 'free' memory in which that result is stored
-- if we don't do it, server will use more and more RAM and work slower and slower, it's called 'memory leak'
lastDeathDB:free()
-- REMEMBER that you can't 'free' result that got ID == -1 !!
db.executeQuery("UPDATE `player_deaths` SET `aol` = " .. getCreatureStorage(cid, storeAOL) .. ", `bless` = " .. getCreatureStorage(cid, storeBless) .. ";")
end
end
return true
end