From f768afa21b840195bd5d900bd9264c64df220a9b Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 9 Dec 2024 15:31:41 -0300 Subject: [PATCH] fix: resolve nil value errors in handleGuildWar function --- data/scripts/creaturescripts/player/death.lua | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/data/scripts/creaturescripts/player/death.lua b/data/scripts/creaturescripts/player/death.lua index 42d143781d4..4c0c9a8b2ca 100644 --- a/data/scripts/creaturescripts/player/death.lua +++ b/data/scripts/creaturescripts/player/death.lua @@ -48,7 +48,7 @@ end local function saveDeathRecord(playerGuid, player, killerName, byPlayer, mostDamageName, byPlayerMostDamage, unjustified, mostDamageUnjustified) local query = string.format( - "INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`, `mostdamage_by`, `mostdamage_is_player`, `unjustified`, `mostdamage_unjustified`) " .. "VALUES (%d, %d, %d, '%s', %d, '%s', %d, %d, %d)", + "INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`, `mostdamage_by`, `mostdamage_is_player`, `unjustified`, `mostdamage_unjustified`) " .. "VALUES (%d, %d, %d, %s, %d, %s, %d, %d, %d)", playerGuid, os.time(), player:getLevel(), @@ -62,23 +62,6 @@ local function saveDeathRecord(playerGuid, player, killerName, byPlayer, mostDam db.query(query) end -local function handleGuildWar(player, killer, mostDamageKiller, killerName, mostDamageName) - local deathRecords = getDeathRecords(player:getGuid()) - - if deathRecords > 0 then - local targetGuildId = player:getGuild() and player:getGuild():getId() or 0 - local killerGuildId = killer:getGuild() and killer:getGuild():getId() or 0 - - if targetGuildId ~= 0 and killerGuildId ~= 0 and targetGuildId ~= killerGuildId then - local warId = checkForGuildWar(targetGuildId, killerGuildId) - if warId then - recordGuildWarKill(killer, player, killerGuildId, targetGuildId, warId) - checkAndUpdateGuildWarScore(warId, targetGuildId, killerGuildId, player:getName(), killerName, mostDamageName) - end - end - end -end - local function getDeathRecords(playerGuid) local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid) local deathRecords = 0 @@ -94,6 +77,27 @@ local function getDeathRecords(playerGuid) return deathRecords end +local function handleGuildWar(player, killer, mostDamageKiller, killerName, mostDamageName) + if not player or not killer or not killer:isPlayer() or not player:getGuild() or not killer:getGuild() then + return + end + + local playerGuildId = player:getGuild():getId() + local killerGuildId = killer:getGuild():getId() + + if playerGuildId == killerGuildId then + return + end + + if getDeathRecords(player:getGuid()) > 0 then + local warId = checkForGuildWar(playerGuildId, killerGuildId) + if warId then + recordGuildWarKill(killer, player, killerGuildId, playerGuildId, warId) + checkAndUpdateGuildWarScore(warId, playerGuildId, killerGuildId, player:getName(), killerName, mostDamageName) + end + end +end + local function checkForGuildWar(targetGuildId, killerGuildId) local resultId = db.storeQuery(string.format("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = %d AND `guild2` = %d) OR (`guild1` = %d AND `guild2` = %d))", killerGuildId, targetGuildId, targetGuildId, killerGuildId))