Skip to content

Commit fbd6228

Browse files
author
Marco
authored
fix: resolve nil value errors in handleGuildWar function (#3172)
I made corrections to the `death.lua` script to resolve nil value errors in the `handleGuildWar` function. I added checks to ensure that both the player and the killer are valid and that both belong to a guild before proceeding with the guild war logic. This helps prevent failures when one of the objects is undefined. Fixes from: 3c98b41
1 parent 74b8ed0 commit fbd6228

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

data/scripts/creaturescripts/player/death.lua

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ end
4848

4949
local function saveDeathRecord(playerGuid, player, killerName, byPlayer, mostDamageName, byPlayerMostDamage, unjustified, mostDamageUnjustified)
5050
local query = string.format(
51-
"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)",
51+
"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)",
5252
playerGuid,
5353
os.time(),
5454
player:getLevel(),
@@ -62,23 +62,6 @@ local function saveDeathRecord(playerGuid, player, killerName, byPlayer, mostDam
6262
db.query(query)
6363
end
6464

65-
local function handleGuildWar(player, killer, mostDamageKiller, killerName, mostDamageName)
66-
local deathRecords = getDeathRecords(player:getGuid())
67-
68-
if deathRecords > 0 then
69-
local targetGuildId = player:getGuild() and player:getGuild():getId() or 0
70-
local killerGuildId = killer:getGuild() and killer:getGuild():getId() or 0
71-
72-
if targetGuildId ~= 0 and killerGuildId ~= 0 and targetGuildId ~= killerGuildId then
73-
local warId = checkForGuildWar(targetGuildId, killerGuildId)
74-
if warId then
75-
recordGuildWarKill(killer, player, killerGuildId, targetGuildId, warId)
76-
checkAndUpdateGuildWarScore(warId, targetGuildId, killerGuildId, player:getName(), killerName, mostDamageName)
77-
end
78-
end
79-
end
80-
end
81-
8265
local function getDeathRecords(playerGuid)
8366
local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid)
8467
local deathRecords = 0
@@ -94,6 +77,27 @@ local function getDeathRecords(playerGuid)
9477
return deathRecords
9578
end
9679

80+
local function handleGuildWar(player, killer, mostDamageKiller, killerName, mostDamageName)
81+
if not player or not killer or not killer:isPlayer() or not player:getGuild() or not killer:getGuild() then
82+
return
83+
end
84+
85+
local playerGuildId = player:getGuild():getId()
86+
local killerGuildId = killer:getGuild():getId()
87+
88+
if playerGuildId == killerGuildId then
89+
return
90+
end
91+
92+
if getDeathRecords(player:getGuid()) > 0 then
93+
local warId = checkForGuildWar(playerGuildId, killerGuildId)
94+
if warId then
95+
recordGuildWarKill(killer, player, killerGuildId, playerGuildId, warId)
96+
checkAndUpdateGuildWarScore(warId, playerGuildId, killerGuildId, player:getName(), killerName, mostDamageName)
97+
end
98+
end
99+
end
100+
97101
local function checkForGuildWar(targetGuildId, killerGuildId)
98102
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))
99103

0 commit comments

Comments
 (0)