local THRONE_POS = {x = 1, y = 2, z = 3}
local STORAGE_EVENT = 83902
local STORAGE_PLAYER = 73289
local REWARDS = {{2160, 10}, {2159, 30}}
local DUR = 30 -- in minutes
function OpenEvent()
for _, tid in ipairs(getPlayersOnline()) do
setPlayerStorageValue(tid, STORAGE_PLAYER, 1)
end
setGlobalStorageValue(STORAGE_EVENT, 1)
doBroadcastMessage("O Evento castle foi aberto.", 25)
end
function getWinnerCastle()
local player = getTopCreature(THRONE_POS).uid
local gid = {}
if getGlobalStorageValue(STORAGE_EVENT) < 0 then return true end
if (isPlayer(player)) then
if (getPlayerStorageValue(player, STORAGE_PLAYER) > 0) then
for _, guild in ipairs(getPlayersOnline()) do
if getPlayerGuildId(guild) == getPlayerGuildId(player) then
table.insert(gid, guild)
end
end
for _, T in pairs(REWARDS) do
doPlayerAddItem(player, T[1], T[2])
for _, players in ipairs(gid) do
doPlayerAddItem(players, T[1], T[2])
end
end
for _, cid in ipairs(getPlayersOnline()) do
setPlayerStorageValue(cid, STORAGE_PLAYER, 0)
doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)))
end
doBroadcastMessage(getCreatureName(player) .. " ganhou o evento ele e da guild " .. getPlayerGuildName(player), 25)
end
else
doBroadcastMessage("Ningúem ganhou o evento.", 27)
for _, pid in ipairs(getPlayersOnline()) do
doPlayerAddItem(pid, 2160, 100)
setPlayerStorageValue(pid, STORAGE_PLAYER, 0)
doTeleportThing(pid, getTownTemplePosition(getPlayerTown(pid)))
end
end
setGlobalStorageValue(STORAGE_EVENT, 0)
return db.executeQuery("DELETE FROM `player_storage` WHERE `key` = " .. STORAGE_PLAYER)
end
function CastleWalls()
local walls = {
{ID, POS},
{ID, POS},
}
for _, K in pairs(walls) do
local wall = getTileItemById(K[2], K[1])
if (wall.uid > 0) then
doRemoveItem(wall.uid)
else
doCreateItem(K[1], 1, K[2])
end
end
end
function onSay(cid, words, param, channel)
if ((param == "abrir") and (getGlobalStorageValue(STORAGE_EVENT) == 1)) then
doPlayerSendTextMessage(cid, 27, "O evento já esta aberto.") return true
elseif ((param == "fechar") and (getGlobalStorageValue(STORAGE_EVENT) == 1)) then
getWinnerCastle()
CastleWalls() return true
end
OpenEvent()
CastleWalls()
addEvent(getWinnerCastle(), DUR * 60 * 1000)
return true
end