playerdeath.lua

main 123 lines · 5.2 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1local deathListEnabled = true
2local maxDeathRecords = 5
3
4local function sendWarStatus(guildId, enemyGuildId, warId, playerName, killerName)
5 local guild, enemyGuild = Guild(guildId), Guild(enemyGuildId)
6 if not guild or not enemyGuild then
7 return
8 end
9
10 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)
11 if resultId then
12
13 local guild1_kills = result.getNumber(resultId, "guild1_kills")
14 local guild2_kills = result.getNumber(resultId, "guild2_kills")
15 local limit = result.getNumber(resultId, "limit")
16 result.free(resultId)
17
18 local members = guild:getMembersOnline()
19 for i = 1, #members do
20 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)
21 end
22
23 local enemyMembers = enemyGuild:getMembersOnline()
24 for i = 1, #enemyMembers do
25 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)
26 end
27
28 if guild1_kills >= limit or guild2_kills >= limit then
29 db.query("UPDATE `guild_wars` SET `status` = 4, `ended` = " .. os.time() .. " WHERE `status` = 1 AND `id` = " .. warId)
30 Game.broadcastMessage(string.format("%s has just won the war against %s.", guild:getName(), enemyGuild:getName()), MESSAGE_EVENT_ADVANCE)
31 end
32 end
33end
34
35function onDeath(player, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
36 local playerId = player:getId()
37 if nextUseStaminaTime[playerId] then
38 nextUseStaminaTime[playerId] = nil
39 end
40
41 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")
42 if not deathListEnabled then
43 return
44 end
45
46 local byPlayer = 0
47 local killerName
48 if killer then
49 if killer:isPlayer() then
50 byPlayer = 1
51 else
52 local master = killer:getMaster()
53 if master and master ~= killer and master:isPlayer() then
54 killer = master
55 byPlayer = 1
56 end
57 end
58 killerName = killer:getName()
59 else
60 killerName = "field item"
61 end
62
63 local byPlayerMostDamage = 0
64 local mostDamageKillerName
65 if mostDamageKiller then
66 if mostDamageKiller:isPlayer() then
67 byPlayerMostDamage = 1
68 else
69 local master = mostDamageKiller:getMaster()
70 if master and master ~= mostDamageKiller and master:isPlayer() then
71 mostDamageKiller = master
72 byPlayerMostDamage = 1
73 end
74 end
75 mostDamageName = mostDamageKiller:getName()
76 else
77 mostDamageName = "field item"
78 end
79
80 local playerGuid = player:getGuid()
81 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 .. ", " .. (lastHitUnjustified and 1 or 0) .. ", " .. (mostDamageUnjustified and 1 or 0) .. ")")
82 local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid)
83
84 local deathRecords = 0
85 local tmpResultId = resultId
86 while tmpResultId ~= false do
87 tmpResultId = result.next(resultId)
88 deathRecords = deathRecords + 1
89 end
90
91 if resultId ~= false then
92 result.free(resultId)
93 end
94
95 local limit = deathRecords - maxDeathRecords
96 if limit > 0 then
97 db.asyncQuery("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT " .. limit)
98 end
99
100 if byPlayer == 1 then
101 local targetGuild = player:getGuild()
102 targetGuild = targetGuild and targetGuild:getId() or 0
103 if targetGuild ~= 0 then
104 local killerGuild = killer:getGuild()
105 killerGuild = killerGuild and killerGuild:getId() or 0
106 if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(playerId, killer:getId()) then
107 local warId = false
108 resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuild .. " AND `guild2` = " .. targetGuild .. ") OR (`guild1` = " .. targetGuild .. " AND `guild2` = " .. killerGuild .. "))")
109 if resultId ~= false then
110 warId = result.getNumber(resultId, "id")
111 result.free(resultId)
112 end
113
114 if warId ~= false then
115 local playerName = player:getName()
116 db.asyncQuery("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(killerName) .. ", " .. db.escapeString(playerName) .. ", " .. killerGuild .. ", " .. targetGuild .. ", " .. os.time() .. ", " .. warId .. ")")
117 addEvent(sendWarStatus, 1000, killerGuild, targetGuild, warId, playerName, killerName)
118 end
119 end
120 end
121 end
122end
123
Top