Last active
January 29, 2016 11:27
-
-
Save dbjorkholm/42a6e3fbdd4f5207bba0 to your computer and use it in GitHub Desktop.
Znote Guild War~
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local CHANNEL_GUILD = 0x00 | |
function Guild.broadcastMessage(self, message) | |
for _, member in ipairs(self:getMembersOnline()) do | |
member:sendTextMessage(MESSAGE_INFO_DESCR, message) | |
member:sendChannelMessage("", message, TALKTYPE_CHANNEL_R1, CHANNEL_GUILD) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local deathListEnabled = true | |
local maxDeathRecords = 5 | |
local function getKillerNames(target, killerGuildId) | |
local killers = { } | |
for creatureId, damage in pairs(target:getDamageMap()) do | |
local tmpPlayer = Player(creatureId) | |
if tmpPlayer then | |
local tmpGuild = tmpPlayer:getGuild() | |
if tmpGuild and tmpGuild:getId() == killerGuildId then | |
killers[#killers + 1] = tmpPlayer:getName() | |
end | |
end | |
end | |
local killerNames = "" | |
for i, killerName in ipairs(killers) do | |
local separator = ", " | |
if i == 1 then | |
separator = "" | |
elseif i == #killers then | |
separator = " and " | |
end | |
killerNames = string.format("%s%s%s", killerNames, separator, killerName) | |
end | |
return killerNames | |
end | |
local function broadcastWarStatus(warInfo) | |
local resultId = db.storeQuery("SELECT (SELECT `limit` FROM `znote_guild_wars` WHERE `znote_guild_wars`.`id` = `guild_wars`.`id`) `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` = " .. warInfo.killerGuild.id .. " OR `guild2` = " .. warInfo.killerGuild.id .. ") AND `status` = 1 AND `id` = " .. warInfo.warId) | |
if not resultId then | |
return | |
end | |
local guild1_kills = result.getNumber(resultId, "guild1_kills") | |
local guild2_kills = result.getNumber(resultId, "guild2_kills") | |
local limit = result.getNumber(resultId, "limit") | |
result.free(resultId) | |
local targetGuild = Guild(warInfo.targetGuild.id) | |
if targetGuild then | |
targetGuild:broadcastMessage(string.format("Guild member %s was killed by %s. The new score is %d:%d frags (limit: %d).", warInfo.targetName, warInfo.killerNames, guild1_kills, guild2_kills, limit)) | |
end | |
local killerGuild = Guild(warInfo.killerGuild.id) | |
if killerGuild then | |
killerGuild:broadcastMessage(string.format("Opponent %s was killed by %s. The new score is %d:%d frags (limit: %d).", warInfo.targetName, warInfo.killerNames, guild1_kills, guild2_kills, limit)) | |
end | |
if guild1_kills >= limit or guild2_kills >= limit then | |
db.query("UPDATE `guild_wars` SET `status` = 4, `ended` = " .. os.time() .. " WHERE `status` = 1 AND `id` = " .. warInfo.warId) | |
Game.broadcastMessage(string.format("%s has just won the war against %s.", warInfo.killerGuild.name, warInfo.targetGuild.name), MESSAGE_EVENT_ADVANCE) | |
end | |
end | |
local function addGuildFrag(target, killer) | |
local targetGuild = target:getGuild() | |
local targetGuildId = targetGuild and targetGuild:getId() or 0 | |
if targetGuildId == 0 then | |
return | |
end | |
local killerGuild = killer:getGuild() | |
local killerGuildId = killerGuild and killerGuild:getId() or 0 | |
if killerGuild == 0 or targetGuildId == killerGuildId then | |
return | |
end | |
if not isInWar(target, killer) then | |
return | |
end | |
local resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuildId .. " AND `guild2` = " .. targetGuildId .. ") OR (`guild1` = " .. targetGuildId .. " AND `guild2` = " .. killerGuildId .. "))") | |
if not resultId then | |
return | |
end | |
local warId = result.getNumber(resultId, "id") | |
result.free(resultId) | |
local targetName = target:getName() | |
db.asyncQuery("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(killer:getName()) .. ", " .. db.escapeString(targetName) .. ", " .. killerGuildId .. ", " .. targetGuildId .. ", " .. os.time() .. ", " .. warId .. ")") | |
local warInfo = { | |
warId = warId, | |
targetName = targetName, | |
killerNames = getKillerNames(target, killerGuildId), | |
targetGuild = {id = targetGuildId, name = targetGuild:getName()}, | |
killerGuild = {id = killerGuildId, name = killerGuild:getName()}, | |
} | |
addEvent(broadcastWarStatus, 500, warInfo) | |
end | |
function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified) | |
local playerId = player:getId() | |
if nextUseStaminaTime[playerId] ~= nil then | |
nextUseStaminaTime[playerId] = nil | |
end | |
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.") | |
if not deathListEnabled then | |
return | |
end | |
local byPlayer = 0 | |
local killerName | |
if killer ~= nil then | |
if killer:isPlayer() then | |
byPlayer = 1 | |
else | |
local master = killer:getMaster() | |
if master and master ~= killer and master:isPlayer() then | |
killer = master | |
byPlayer = 1 | |
end | |
end | |
killerName = killer:getName() | |
else | |
killerName = "field item" | |
end | |
local byPlayerMostDamage = 0 | |
local mostDamageKillerName | |
if mostDamageKiller ~= nil then | |
if mostDamageKiller:isPlayer() then | |
byPlayerMostDamage = 1 | |
else | |
local master = mostDamageKiller:getMaster() | |
if master and master ~= mostDamageKiller and master:isPlayer() then | |
mostDamageKiller = master | |
byPlayerMostDamage = 1 | |
end | |
end | |
mostDamageName = mostDamageKiller:getName() | |
else | |
mostDamageName = "field item" | |
end | |
local playerGuid = player:getGuid() | |
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 .. ", " .. (unjustified and 1 or 0) .. ", " .. (mostDamageUnjustified and 1 or 0) .. ")") | |
local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid) | |
local deathRecords = 0 | |
local tmpResultId = resultId | |
while tmpResultId ~= false do | |
tmpResultId = result.next(resultId) | |
deathRecords = deathRecords + 1 | |
end | |
if resultId ~= false then | |
result.free(resultId) | |
end | |
local limit = deathRecords - maxDeathRecords | |
if limit > 0 then | |
db.asyncQuery("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT " .. limit) | |
end | |
if byPlayer == 1 then | |
addGuildFrag(player, killer) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment