Skip to content

Commit

Permalink
fix: resolve nil value errors in handleGuildWar function
Browse files Browse the repository at this point in the history
  • Loading branch information
omarcopires committed Dec 9, 2024
1 parent 3c98b41 commit f768afa
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions data/scripts/creaturescripts/player/death.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -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))

Expand Down

0 comments on commit f768afa

Please sign in to comment.