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 ade6498
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions data/scripts/creaturescripts/player/death.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ 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 +63,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 +78,34 @@ 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() then
return
end

local deathRecords = getDeathRecords(player:getGuid())

if deathRecords > 0 then
local playerGuild = player:getGuild()
local killerGuild = killer:getGuild()

if not playerGuild or not killerGuild then
return
end

local targetGuildId = playerGuild:getId()
local killerGuildId = killerGuild:getId()

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 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 ade6498

Please sign in to comment.