• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved TFS 1.2 Znote AAC 1.5 Guild War System playerdeath.lua

Michael Orsino

Premium User
Premium User
Support Team
Joined
Nov 15, 2007
Messages
854
Solutions
10
Reaction score
389
Location
Santiago, Chile (Australian)
See Post #9 for the solution to this issue.

Hi guys,

Znote AAC 1.5 comes with a replacement playerdeath.lua for TFS 1.0 that works with the Znote tables and provides additional 'war mode' features (broadcasting deaths/frags to guildmates, broadcasting end of wars and most importantly actually ending a war mode).
Unfortunately the 1.0 playerdeath.lua seems to be incompatible with 1.2.

Console error when using the TFS 1.0 Znote AAC playerdeath.lua:
Code:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/playerdeath.lua:onDeath
data/creaturescripts/scripts/playerdeath.lua:78: attempt to concatenate local 'mostDamage_unjustified' (a boolean value)
stack traceback:
[C]: in function '__concat'
data/creaturescripts/scripts/playerdeath.lua:78: in function <data/creaturescripts/scripts/playerdeath.lua:35>

I had a brief play around with it but couldn't get it going. I'll continue to try, any assistance is greatly appreciated. The following post will contain the default TFS 1.2 playerdeath.lua and the Znote AAC playerdeath.lua for 1.0.

Thanks,
Michael
 
Last edited:
Default TFS 1.2 playerdeath.lua
Code:
local deathListEnabled = true
local maxDeathRecords = 5

function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local playerId = player:getId()
    if nextUseStaminaTime[playerId] ~= nil then
        nextUseStaminaTime[playerId] = nil
    end

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")
    if not deathListEnabled then
        return
    end

    local byPlayer = 0
    local killerName
    if killer ~= nil then
        if killer:isPlayer() then
            byPlayer = 1
        else
            local master = killer:getMaster()
            if master and master ~= killer and master:isPlayer() then
                killer = master
                byPlayer = 1
            end
        end
        killerName = killer:getName()
    else
        killerName = "field item"
    end

    local byPlayerMostDamage = 0
    local mostDamageKillerName
    if mostDamageKiller ~= nil then
        if mostDamageKiller:isPlayer() then
            byPlayerMostDamage = 1
        else
            local master = mostDamageKiller:getMaster()
            if master and master ~= mostDamageKiller and master:isPlayer() then
                mostDamageKiller = master
                byPlayerMostDamage = 1
            end
        end
        mostDamageName = mostDamageKiller:getName()
    else
        mostDamageName = "field item"
    end

    local playerGuid = player:getGuid()
    db.query("INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`, `mostdamage_by`, `mostdamage_is_player`, `unjustified`, `mostdamage_unjustified`) VALUES (" .. playerGuid .. ", " .. os.time() .. ", " .. player:getLevel() .. ", " .. db.escapeString(killerName) .. ", " .. byPlayer .. ", " .. db.escapeString(mostDamageName) .. ", " .. byPlayerMostDamage .. ", " .. (unjustified and 1 or 0) .. ", " .. (mostDamageUnjustified and 1 or 0) .. ")")
    local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid)

    local deathRecords = 0
    local tmpResultId = resultId
    while tmpResultId ~= false do
        tmpResultId = result.next(resultId)
        deathRecords = deathRecords + 1
    end

    if resultId ~= false then
        result.free(resultId)
    end

    local limit = deathRecords - maxDeathRecords
    if limit > 0 then
        db.asyncQuery("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT " .. limit)
    end

    if byPlayer == 1 then
        local targetGuild = player:getGuild()
        targetGuild = targetGuild and targetGuild:getId() or 0
        if targetGuild ~= 0 then
            local killerGuild = killer:getGuild()
            killerGuild = killerGuild and killerGuild:getId() or 0
            if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(playerId, killer:getId()) then
                local warId = false
                resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuild .. " AND `guild2` = " .. targetGuild .. ") OR (`guild1` = " .. targetGuild .. " AND `guild2` = " .. killerGuild .. "))")
                if resultId ~= false then
                    warId = result.getDataInt(resultId, "id")
                    result.free(resultId)
                end

                if warId ~= false then
                    db.asyncQuery("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(killerName) .. ", " .. db.escapeString(player:getName()) .. ", " .. killerGuild .. ", " .. targetGuild .. ", " .. os.time() .. ", " .. warId .. ")")
                end
            end
        end
    end
end

TFS 1.0 Znote AAC supplied playerdeath.lua
Code:
local deathListEnabled = true
local maxDeathRecords = 5

local function sendWarStatus(guildId, enemyGuildId, warId, playerName, killerName)
    local guild, enemyGuild = Guild(guildId), Guild(enemyGuildId)
    if not guild or not enemyGuild then
        return
    end

    local resultId = db.storeQuery("SELECT `guild_wars`.`id`, (SELECT `limit` FROM `znote_guild_wars` WHERE `znote_guild_wars`.`id` = `guild_wars`.`id`) AS `limit`, (SELECT COUNT(1) FROM `guildwar_kills` WHERE `guildwar_kills`.`warid` = `guild_wars`.`id` AND `guildwar_kills`.`killerguild` = `guild_wars`.`guild1`) guild1_kills, (SELECT COUNT(1) FROM `guildwar_kills` WHERE `guildwar_kills`.`warid` = `guild_wars`.`id` AND `guildwar_kills`.`killerguild` = `guild_wars`.`guild2`) guild2_kills FROM `guild_wars` WHERE (`guild1` = " .. guildId .. " OR `guild2` = " .. guildId .. ") AND `status` = 1 AND `id` = " .. warId)
    if resultId then

        local guild1_kills = result.getDataInt(resultId, "guild1_kills")
        local guild2_kills = result.getDataInt(resultId, "guild2_kills")
        local limit = result.getDataInt(resultId, "limit")
        result.free(resultId)

        local members = guild:getMembersOnline()
        for i = 1, #members do
            members[i]:sendChannelMessage("", string.format("%s was killed by %s. The new score is %d:%d frags (limit: %d)", playerName, killerName, guild1_kills, guild2_kills, limit), TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
        end

        local enemyMembers = enemyGuild:getMembersOnline()
        for i = 1, #enemyMembers do
            enemyMembers[i]:sendChannelMessage("", string.format("%s was killed by %s. The new score is %d:%d frags (limit: %d)", playerName, killerName, guild1_kills, guild2_kills, limit), TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
        end

        if guild1_kills >= limit or guild2_kills >= limit then
            db.query("UPDATE `guild_wars` SET `status` = 4, `ended` = " .. os.time() .. " WHERE `status` = 1 AND `id` = " .. warId)
            Game.broadcastMessage(string.format("%s has just won the war against %s.", guild:getName(), enemyGuild:getName()), MESSAGE_EVENT_ADVANCE)
        end
    end
end

function onDeath(cid, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
    local player = Player(cid)

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")
    if not deathListEnabled then
        return
    end

    local byPlayer = 0
    local killerCreature = Creature(killer)
    if not killerCreature then
        killerName = "field item"
    else
        if killerCreature:isPlayer() then
            byPlayer = 1
        else
            local master = killerCreature:getMaster()
            if master and master ~= killerCreature and master:isPlayer() then
                killerCreature = master
                byPlayer = 1
            end
        end
        killerName = killerCreature:isMonster() and killerCreature:getType():getNameDescription() or killerCreature:getName()
    end

    local byPlayerMostDamage = 0
    if mostDamage == 0 then
        mostDamageName = "field item"
    else
        local mostDamageKiller = Creature(mostDamage)
        if mostDamageKiller:isPlayer() then
            byPlayerMostDamage = 1
        else
            local master = mostDamageKiller:getMaster()
            if master and master ~= mostDamageKiller and master:isPlayer() then
                mostDamageKiller = master
                byPlayerMostDamage = 1
            end
        end
        mostDamageName = mostDamageKiller:isMonster() and mostDamageKiller:getType():getNameDescription() or mostDamageKiller:getName()
    end

    local playerGuid = player:getGuid()
    db.query("INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`, `mostdamage_by`, `mostdamage_is_player`, `unjustified`, `mostdamage_unjustified`) VALUES (" .. playerGuid .. ", " .. os.time() .. ", " .. player:getLevel() .. ", " .. db.escapeString(killerName) .. ", " .. byPlayer .. ", " .. db.escapeString(mostDamageName) .. ", " .. byPlayerMostDamage .. ", " .. unjustified .. ", " .. mostDamage_unjustified .. ")")
    local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid)

    local deathRecords = 0
    local tmpResultId = resultId
    while tmpResultId ~= false do
        tmpResultId = result.next(resultId)
        deathRecords = deathRecords + 1
    end

    if resultId ~= false then
        result.free(resultId)
    end

    while deathRecords > maxDeathRecords do
        db.query("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT 1")
        deathRecords = deathRecords - 1
    end

    if byPlayer == 1 then
        local targetGuild = player:getGuild()
        targetGuild = targetGuild and targetGuild:getId() or 0
        if targetGuild ~= 0 then
            local killerGuild = killerCreature:getGuild()
            killerGuild = killerGuild and killerGuild:getId() or 0
            if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(cid, killerCreature) then
                local warId = false
                resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuild .. " AND `guild2` = " .. targetGuild .. ") OR (`guild1` = " .. targetGuild .. " AND `guild2` = " .. killerGuild .. "))")
                if resultId ~= false then
                    warId = result.getDataInt(resultId, "id")
                    result.free(resultId)
                end

                if warId ~= false then
                    local playerName = player:getName()
                    db.query("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(killerName) .. ", " .. db.escapeString(playerName) .. ", " .. killerGuild .. ", " .. targetGuild .. ", " .. os.time() .. ", " .. warId .. ")")
                    addEvent(sendWarStatus, 1000, killerGuild, targetGuild, warId, playerName, killerName)
                end
            end
        end
    end
end
 
@xscas had a related support thread awhile ago - https://otland.net/threads/znote-aac.234095/#post-2257438
Nobody in that thread realized that playerdeath.lua was key, but even if they had - they would have come up against the issue I am experiencing now.

I'll give this script another bash when I get home from work this afternoon. Any clues would be appreciated!

Thanks,
Michael
 
This is what I have so far.
Code:
local deathListEnabled = true
local maxDeathRecords = 5

local function sendWarStatus(guildId, enemyGuildId, warId, playerName, killerName)
broadcastMessage("sendWarStatus has executed", MESSAGE_STATUS_WARNING)

    local guild, enemyGuild = Guild(guildId), Guild(enemyGuildId)
    if not guild or not enemyGuild then
        return
    end

    local resultId = db.storeQuery("SELECT `guild_wars`.`id`, (SELECT `limit` FROM `znote_guild_wars` WHERE `znote_guild_wars`.`id` = `guild_wars`.`id`) AS `limit`, (SELECT COUNT(1) FROM `guildwar_kills` WHERE `guildwar_kills`.`warid` = `guild_wars`.`id` AND `guildwar_kills`.`killerguild` = `guild_wars`.`guild1`) guild1_kills, (SELECT COUNT(1) FROM `guildwar_kills` WHERE `guildwar_kills`.`warid` = `guild_wars`.`id` AND `guildwar_kills`.`killerguild` = `guild_wars`.`guild2`) guild2_kills FROM `guild_wars` WHERE (`guild1` = " .. guildId .. " OR `guild2` = " .. guildId .. ") AND `status` = 1 AND `id` = " .. warId)
    if resultId then
broadcastMessage("resultId returned true and the script continues", MESSAGE_STATUS_WARNING)
        local guild1_kills = result.getDataInt(resultId, "guild1_kills")
        local guild2_kills = result.getDataInt(resultId, "guild2_kills")
        local limit = result.getDataInt(resultId, "limit")
        result.free(resultId)

        local members = guild:getMembersOnline()
        for i = 1, #members do
            members[i]:sendChannelMessage("", string.format("%s was killed by %s. The new score is %d:%d frags (limit: %d)", playerName, killerName, guild1_kills, guild2_kills, limit), TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
        end

        local enemyMembers = enemyGuild:getMembersOnline()
        for i = 1, #enemyMembers do
            enemyMembers[i]:sendChannelMessage("", string.format("%s was killed by %s. The new score is %d:%d frags (limit: %d)", playerName, killerName, guild1_kills, guild2_kills, limit), TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
        end

        if guild1_kills >= limit or guild2_kills >= limit then
            db.query("UPDATE `guild_wars` SET `status` = 4, `ended` = " .. os.time() .. " WHERE `status` = 1 AND `id` = " .. warId)
            Game.broadcastMessage(string.format("%s has just won the war against %s.", guild:getName(), enemyGuild:getName()), MESSAGE_EVENT_ADVANCE)
        end
    end
end

function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local playerId = player:getId()
    if nextUseStaminaTime[playerId] ~= nil then
        nextUseStaminaTime[playerId] = nil
    end

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")
    if not deathListEnabled then
        return
    end

    local byPlayer = 0
    local killerName
    if killer ~= nil then
        if killer:isPlayer() then
            byPlayer = 1
        else
            local master = killer:getMaster()
            if master and master ~= killer and master:isPlayer() then
                killer = master
                byPlayer = 1
            end
        end
        killerName = killer:getName()
    else
        killerName = "field item"
    end

    local byPlayerMostDamage = 0
    local mostDamageKillerName
    if mostDamageKiller ~= nil then
        if mostDamageKiller:isPlayer() then
            byPlayerMostDamage = 1
        else
            local master = mostDamageKiller:getMaster()
            if master and master ~= mostDamageKiller and master:isPlayer() then
                mostDamageKiller = master
                byPlayerMostDamage = 1
            end
        end
        mostDamageName = mostDamageKiller:getName()
    else
        mostDamageName = "field item"
    end

    local playerGuid = player:getGuid()
    db.query("INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`, `mostdamage_by`, `mostdamage_is_player`, `unjustified`, `mostdamage_unjustified`) VALUES (" .. playerGuid .. ", " .. os.time() .. ", " .. player:getLevel() .. ", " .. db.escapeString(killerName) .. ", " .. byPlayer .. ", " .. db.escapeString(mostDamageName) .. ", " .. byPlayerMostDamage .. ", " .. (unjustified and 1 or 0) .. ", " .. (mostDamageUnjustified and 1 or 0) .. ")")
    local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid)

    local deathRecords = 0
    local tmpResultId = resultId
    while tmpResultId ~= false do
        tmpResultId = result.next(resultId)
        deathRecords = deathRecords + 1
    end

    if resultId ~= false then
        result.free(resultId)
    end

    local limit = deathRecords - maxDeathRecords
    if limit > 0 then
        db.asyncQuery("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT " .. limit)
    end

    if byPlayer == 1 then
        local targetGuild = player:getGuild()
        targetGuild = targetGuild and targetGuild:getId() or 0
        if targetGuild ~= 0 then
            local killerGuild = killer:getGuild()
            killerGuild = killerGuild and killerGuild:getId() or 0
            if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(playerId, killer:getId()) then
                local warId = false
                resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuild .. " AND `guild2` = " .. targetGuild .. ") OR (`guild1` = " .. targetGuild .. " AND `guild2` = " .. killerGuild .. "))")
                if resultId ~= false then
                    warId = result.getDataInt(resultId, "id")
                    result.free(resultId)
                end

                if warId ~= false then
                    db.asyncQuery("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(killerName) .. ", " .. db.escapeString(player:getName()) .. ", " .. killerGuild .. ", " .. targetGuild .. ", " .. os.time() .. ", " .. warId .. ")")
                    addEvent(sendWarStatus, 1000, killerGuild, targetGuild, warId, player:getName(), killerName)
                end
            end
        end
    end
end
The script is failing on this line:
Code:
    local guild, enemyGuild = Guild(guildId), Guild(enemyGuildId)
    if not guild or not enemyGuild then
        return
    end

I don't know why it is failing yet
 
Last edited:
What is the error?
You posted an error which is inconsistent with the script you provided, I don't feel like back tracking through the code, so just post the error and stop adjusting the script...
 
The error I posted is consistent with the unedited Znote AAC playerdeath.lua as seen in post #2.
There is no console error with my adjusted script. By printing/broadcasting after each section runs I have determined where the script is failing. (both my adjusted script and where it is failing are posted above.)
As for why it is failing at that point, I am not sure.

I would have thought you of all people @Codex NG would appreciate somebody actually trying to learn and get the script working themselves.
 
The error I posted is consistent with the unedited Znote AAC playerdeath.lua as seen in post #2.
There is no console error with my adjusted script. By printing/broadcasting after each section runs I have determined where the script is failing. (both my adjusted script and where it is failing are posted above.)
As for why it is failing at that point, I am not sure.

I would have thought you of all people @Codex NG would appreciate somebody actually trying to learn and get the script working themselves.
I didn't look at Default TFS 1.2 playerdeath.lua, I looked at the error and then I looked at what you had so far plus where you said it was not working.
 
This issue has been solved by Ninja. For anybody searching, I have included the working TFS 1.2 playerdeath.lua for Znote AAC 1.5.

lib.lua:
Code:
local CHANNEL_GUILD = 0x00

function Guild.broadcastMessage(self, message)
    for _, member in ipairs(self:getMembersOnline()) do
        member:sendTextMessage(MESSAGE_INFO_DESCR, message)
        member:sendChannelMessage("", message, TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
    end
end
playerdeath.lua:
Code:
local deathListEnabled = true
local maxDeathRecords = 5

local function getKillerNames(target, killerGuildId)
    local killers = { }
    for creatureId, damage in pairs(target:getDamageMap()) do
        local tmpPlayer = Player(creatureId)
        if tmpPlayer then
            local tmpGuild = tmpPlayer:getGuild()
            if tmpGuild and tmpGuild:getId() == killerGuildId then
                killers[#killers + 1] = tmpPlayer:getName()
            end
        end
    end

    local killerNames = ""
    for i, killerName in ipairs(killers) do
        local separator = ", "
        if i == 1 then
            separator = ""
        elseif i == #killers then
            separator = " and "
        end

        killerNames = string.format("%s%s%s", killerNames, separator, killerName)
    end

    return killerNames
end

local function broadcastWarStatus(warInfo)
    local resultId = db.storeQuery("SELECT (SELECT `limit` FROM `znote_guild_wars` WHERE `znote_guild_wars`.`id` = `guild_wars`.`id`) `limit`, (SELECT COUNT(1) FROM `guildwar_kills` WHERE `guildwar_kills`.`warid` = `guild_wars`.`id` AND `guildwar_kills`.`killerguild` = `guild_wars`.`guild1`) guild1_kills, (SELECT COUNT(1) FROM `guildwar_kills` WHERE `guildwar_kills`.`warid` = `guild_wars`.`id` AND `guildwar_kills`.`killerguild` = `guild_wars`.`guild2`) guild2_kills FROM `guild_wars` WHERE (`guild1` = " .. warInfo.killerGuild.id .. " OR `guild2` = " .. warInfo.killerGuild.id .. ") AND `status` = 1 AND `id` = " .. warInfo.warId)
    if not resultId then
        return
    end

    local guild1_kills = result.getNumber(resultId, "guild1_kills")
    local guild2_kills = result.getNumber(resultId, "guild2_kills")
    local limit = result.getNumber(resultId, "limit")
    result.free(resultId)

    local targetGuild = Guild(warInfo.targetGuild.id)
    if targetGuild then
        targetGuild:broadcastMessage(string.format("Guild member %s was killed by %s. The new score is %d:%d frags (limit: %d).", warInfo.targetName, warInfo.killerNames, guild1_kills, guild2_kills, limit))
    end

    local killerGuild = Guild(warInfo.killerGuild.id)
    if killerGuild then
        killerGuild:broadcastMessage(string.format("Opponent %s was killed by %s. The new score is %d:%d frags (limit: %d).", warInfo.targetName, warInfo.killerNames, guild1_kills, guild2_kills, limit))
    end

    if guild1_kills >= limit or guild2_kills >= limit then
        db.query("UPDATE `guild_wars` SET `status` = 4, `ended` = " .. os.time() .. " WHERE `status` = 1 AND `id` = " .. warInfo.warId)
        Game.broadcastMessage(string.format("%s has just won the war against %s.", warInfo.killerGuild.name, warInfo.targetGuild.name), MESSAGE_EVENT_ADVANCE)
    end
end

local function addGuildFrag(target, killer)
    local targetGuild = target:getGuild()
    local targetGuildId = targetGuild and targetGuild:getId() or 0
    if targetGuildId == 0 then
        return
    end

    local killerGuild = killer:getGuild()
    local killerGuildId = killerGuild and killerGuild:getId() or 0
    if killerGuild == 0 or targetGuildId == killerGuildId then
        return
    end

    if not isInWar(target, killer) then
        return
    end

    local resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuildId .. " AND `guild2` = " .. targetGuildId .. ") OR (`guild1` = " .. targetGuildId .. " AND `guild2` = " .. killerGuildId .. "))")
    if not resultId then
        return
    end

    local warId = result.getNumber(resultId, "id")
    result.free(resultId)

    local targetName = target:getName()
    db.asyncQuery("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(killer:getName()) .. ", " .. db.escapeString(targetName) .. ", " .. killerGuildId .. ", " .. targetGuildId .. ", " .. os.time() .. ", " .. warId .. ")")

    local warInfo = {
        warId = warId,
        targetName = targetName,
        killerNames = getKillerNames(target, killerGuildId),
        targetGuild = {id = targetGuildId, name = targetGuild:getName()},
        killerGuild = {id = killerGuildId, name = killerGuild:getName()},
    }

    addEvent(broadcastWarStatus, 500, warInfo)
end

function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local playerId = player:getId()
    if nextUseStaminaTime[playerId] ~= nil then
        nextUseStaminaTime[playerId] = nil
    end

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")
    if not deathListEnabled then
        return
    end

    local byPlayer = 0
    local killerName
    if killer ~= nil then
        if killer:isPlayer() then
            byPlayer = 1
        else
            local master = killer:getMaster()
            if master and master ~= killer and master:isPlayer() then
                killer = master
                byPlayer = 1
            end
        end
        killerName = killer:getName()
    else
        killerName = "field item"
    end

    local byPlayerMostDamage = 0
    local mostDamageKillerName
    if mostDamageKiller ~= nil then
        if mostDamageKiller:isPlayer() then
            byPlayerMostDamage = 1
        else
            local master = mostDamageKiller:getMaster()
            if master and master ~= mostDamageKiller and master:isPlayer() then
                mostDamageKiller = master
                byPlayerMostDamage = 1
            end
        end
        mostDamageName = mostDamageKiller:getName()
    else
        mostDamageName = "field item"
    end

    local playerGuid = player:getGuid()
    db.query("INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`, `mostdamage_by`, `mostdamage_is_player`, `unjustified`, `mostdamage_unjustified`) VALUES (" .. playerGuid .. ", " .. os.time() .. ", " .. player:getLevel() .. ", " .. db.escapeString(killerName) .. ", " .. byPlayer .. ", " .. db.escapeString(mostDamageName) .. ", " .. byPlayerMostDamage .. ", " .. (unjustified and 1 or 0) .. ", " .. (mostDamageUnjustified and 1 or 0) .. ")")
    local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid)

    local deathRecords = 0
    local tmpResultId = resultId
    while tmpResultId ~= false do
        tmpResultId = result.next(resultId)
        deathRecords = deathRecords + 1
    end

    if resultId ~= false then
        result.free(resultId)
    end

    local limit = deathRecords - maxDeathRecords
    if limit > 0 then
        db.asyncQuery("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT " .. limit)
    end

    if byPlayer == 1 then
        addGuildFrag(player, killer)
    end
end
 
Back
Top