playerdeath.lua

main 136 lines · 5.5 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
35local creatureevent = CreatureEvent("PlayerDeath")
36
37function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
38 local playerId = player:getId()
39 if nextUseStaminaTime[playerId] then
40 nextUseStaminaTime[playerId] = nil
41 end
42
43 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")
44 if not deathListEnabled then
45 return
46 end
47
48 local byPlayer = 0
49 local killerName
50 if killer then
51 if killer:isPlayer() then
52 byPlayer = 1
53 else
54 local master = killer:getMaster()
55 if master and master ~= killer and master:isPlayer() then
56 killer = master
57 byPlayer = 1
58 end
59 end
60 killerName = killer:getName()
61 else
62 killerName = "field item"
63 end
64
65 local byPlayerMostDamage = 0
66 local mostDamageKillerName
67 if mostDamageKiller then
68 if mostDamageKiller:isPlayer() then
69 byPlayerMostDamage = 1
70 else
71 local master = mostDamageKiller:getMaster()
72 if master and master ~= mostDamageKiller and master:isPlayer() then
73 mostDamageKiller = master
74 byPlayerMostDamage = 1
75 end
76 end
77 mostDamageName = mostDamageKiller:getName()
78 else
79 mostDamageName = "field item"
80 end
81
82 local playerGuid = player:getGuid()
83 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) .. ")")
84 local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid)
85
86 local deathRecords = 0
87 local tmpResultId = resultId
88 while tmpResultId ~= false do
89 tmpResultId = result.next(resultId)
90 deathRecords = deathRecords + 1
91 end
92
93 if resultId ~= false then
94 result.free(resultId)
95 end
96
97 local limit = deathRecords - maxDeathRecords
98 if limit > 0 then
99 db.asyncQuery("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT " .. limit)
100 end
101
102 if byPlayer == 1 then
103 local targetGuild = player:getGuild()
104 targetGuild = targetGuild and targetGuild:getId() or 0
105 if targetGuild ~= 0 then
106 local killerGuild = killer:getGuild()
107 killerGuild = killerGuild and killerGuild:getId() or 0
108 if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(playerId, killer:getId()) then
109 local warId = false
110 resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuild .. " AND `guild2` = " .. targetGuild .. ") OR (`guild1` = " .. targetGuild .. " AND `guild2` = " .. killerGuild .. "))")
111 if resultId ~= false then
112 warId = result.getNumber(resultId, "id")
113 result.free(resultId)
114 end
115
116 if warId ~= false then
117 local playerName = player:getName()
118 db.asyncQuery("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(killerName) .. ", " .. db.escapeString(playerName) .. ", " .. killerGuild .. ", " .. targetGuild .. ", " .. os.time() .. ", " .. warId .. ")")
119 addEvent(sendWarStatus, 1000, killerGuild, targetGuild, warId, playerName, killerName)
120 end
121 end
122 end
123 end
124end
125
126creatureevent:register()
127
128local creatureeventLogin = CreatureEvent("creatureeventLogin")
129
130function creatureeventLogin.onLogin(player)
131 player:registerEvent("PlayerDeath")
132 return true
133end
134
135creatureeventLogin:register()
136
Top