Advertisement
Eihrenjar

Untitled

Jan 28th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.76 KB | None | 0 0
  1. function onSay(cid, words, param)
  2.  
  3.     local player = Player(cid)
  4.     local guild = player:getGuild()
  5.     if(guild == nil) then
  6.         player:sendCancelMessage("You need to be in a guild in order to execute this talkaction.")
  7.         return false
  8.     end
  9.  
  10.     local guild = getPlayerGuildId(cid)
  11.     if not guild or (player:getGuildLevel() < GUILDLEVEL_LEADER) then
  12.         player:sendCancelMessage("You cannot execute this talkaction.")
  13.         return false
  14.     end
  15.  
  16.     local t = string.explode(param, ",")
  17.     if(not t[2]) then  
  18.         player:sendChannelMessage("", "Not enough param(s).", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  19.         return false
  20.     end
  21.  
  22.     local enemy = getGuildId(t[2])
  23.     if(not enemy) then
  24.         player:sendChannelMessage("", "Guild \"" .. t[2] .. "\" does not exists.", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  25.         return false
  26.     end
  27.  
  28.     if(enemy == guild) then
  29.         player:sendChannelMessage("", "You cannot perform war action on your own guild.", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  30.         return false
  31.     end
  32.  
  33.     local enemyName, tmp = "", db.storeQuery("SELECT `name` FROM `guilds` WHERE `id` = " .. enemy)
  34.     if tmp ~= false then
  35.         enemyName = result.getDataString(tmp, "name")
  36.         result.free(tmp)
  37.     end
  38.  
  39.     if(isInArray({"accept", "reject", "cancel"}, t[1])) then
  40.         local query = "`guild1` = " .. enemy .. " AND `guild2` = " .. guild
  41.         if(t[1] == "cancel") then
  42.             query = "`guild1` = " .. guild .. " AND `guild2` = " .. enemy
  43.         end
  44.  
  45.         tmp = db.storeQuery("SELECT `id`, `started`, `ended`, `payment` FROM `guild_wars` WHERE " .. query .. " AND `status` = 0")
  46.         if(tmp == false) then
  47.             player:sendChannelMessage("", "Currently there's no pending invitation for a war with " .. enemyName .. ".", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  48.             return false
  49.         end
  50.  
  51.         if(t[1] == "accept") then
  52.             local _tmp = db.storeQuery("SELECT `balance` FROM `guilds` WHERE `id` = " .. guild)
  53.             local state = result.getDataInt(_tmp, "balance") < result.getDataInt(tmp, "payment")
  54.  
  55.             result.free(_tmp)
  56.             if(state) then
  57.                 player:sendChannelMessage("", "Your guild balance is too low to accept this invitation.", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  58.                 return false
  59.             end
  60.  
  61.             db.query("UPDATE `guilds` SET `balance` = `balance` - " .. result.getDataInt(tmp, "payment") .. " WHERE `id` = " .. guild)
  62.         end
  63.  
  64.         query = "UPDATE `guild_wars` SET "
  65.         local msg = "accepted " .. enemyName .. " invitation to war."
  66.         if(t[1] == "reject") then
  67.             query = query .. "`ended` = " .. os.time() .. ", `status` = 2"
  68.             msg = "rejected " .. enemyName .. " invitation to war."
  69.         elseif(t[1] == "cancel") then
  70.             query = query .. "`ended` = " .. os.time() .. ", `status` = 3"
  71.             msg = "canceled invitation to a war with " .. enemyName .. "."
  72.         else
  73.             query = query .. "`started` = " .. os.time() .. ", `ended` = " .. (result.getDataInt(tmp, "ended") > 0 and (os.time() + ((result.getDataInt(tmp, "started") - result.getDataInt(tmp, "ended")) / 86400)) or 0) .. ", `status` = 1"
  74.         end
  75.  
  76.         query = query .. " WHERE `id` = " .. result.getDataInt(tmp, "id")
  77.         result.free(tmp)
  78.         db.query(query)
  79.         broadcastMessage(getPlayerGuildName(cid) .. " has " .. msg, MESSAGE_EVENT_ADVANCE)
  80.         return false
  81.     end
  82.  
  83.     if(t[1] == "invite") then
  84.         local str = ""
  85.         tmp = db.storeQuery("SELECT `guild1`, `status` FROM `guild_wars` WHERE `guild1` IN (" .. guild .. "," .. enemy .. ") AND `guild2` IN (" .. enemy .. "," .. guild .. ") AND `status` IN (0, 1)")
  86.         if(tmp ~= false) then
  87.            
  88.             if(result.getDataInt(tmp, "status") == 0) then
  89.                 if(result.getDataInt(tmp, "guild1") == guild) then
  90.                     str = "You have already invited " .. enemyName .. " to war."
  91.                 else
  92.                     str = enemyName .. " have already invited you to war."
  93.                 end
  94.             else
  95.                 str = "You are already on a war with " .. enemyName .. "."
  96.             end
  97.  
  98.             result.free(tmp)
  99.         end
  100.  
  101.         if(str ~= "") then
  102.             player:sendChannelMessage("", str, TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  103.             return false
  104.         end
  105.  
  106.         local frags = tonumber(t[3])
  107.         if(frags ~= nil) then
  108.             frags = math.max(10, math.min(1000, frags))
  109.         else
  110.             frags = 100
  111.         end
  112.  
  113.         local payment = tonumber(t[4])
  114.         if(payment ~= nil) then
  115.             payment = math.floor(payment)+1000
  116.             tmp = db.storeQuery("SELECT `balance` FROM `guilds` WHERE `id` = " .. guild)
  117.  
  118.             local state = result.getDataInt(tmp, "balance") < payment
  119.             result.free(tmp)
  120.             if(state) then
  121.                 player:sendChannelMessage("", "Your guild balance is too low for such payment.", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  122.                 return false
  123.             end
  124.  
  125.             db.query("UPDATE `guilds` SET `balance` = `balance` - " .. payment .. " WHERE `id` = " .. guild)
  126.         else
  127.             payment = 0
  128.         end
  129.  
  130.         local begining, ending = os.time(), tonumber(t[5])
  131.         if(ending ~= nil and ending ~= 0) then
  132.             ending = begining + (ending * 86400)
  133.         else
  134.             ending = 0
  135.         end
  136.  
  137.         db.query("INSERT INTO `guild_wars` (`guild1`, `guild2`, `started`, `ended`, `frags`, `payment`) VALUES (" .. guild .. ", " .. enemy .. ", " .. begining .. ", " .. ending .. ", " .. frags .. ", " .. payment .. ");")
  138.         broadcastMessage(getPlayerGuildName(cid) .. " has invited " .. enemyName .. " to war till " .. frags .. " frags.", MESSAGE_EVENT_ADVANCE)
  139.         return false
  140.     end
  141.  
  142.     if(not isInArray({"end", "finish"}, t[1])) then
  143.         return false
  144.     end
  145.  
  146.     local status = (t[1] == "end" and 1 or 4)
  147.     tmp = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `guild1` = " .. guild .. " AND `guild2` = " .. enemy .. " AND `status` = " .. status)
  148.     if(tmp ~= false) then
  149.         local query = "UPDATE `guild_wars` SET `ended` = " .. os.time() .. ", `status` = 5 WHERE `id` = " .. result.getDataInt(tmp, "id")
  150.         result.free(tmp)
  151.  
  152.         db.query(query)
  153.         broadcastMessage(getPlayerGuildName(cid) .. " has " .. (status == 4 and "mend fences" or "ended up a war") .. " with " .. enemyName .. ".", MESSAGE_EVENT_ADVANCE)
  154.         return false
  155.     end
  156.  
  157.     if(status == 4) then
  158.         player:sendChannelMessage("", "Currently there's no pending war truce from " .. enemyName .. ".", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  159.         return false
  160.     end
  161.  
  162.     tmp = db.storeQuery("SELECT `id`, `ended` FROM `guild_wars` WHERE `guild1` = " .. enemy .. " AND `guild2` = " .. guild .. " AND `status` = 1")
  163.     if(tmp ~= false) then
  164.         if(result.getDataInt(tmp, "ended") > 0) then
  165.             result.free(tmp)
  166.             player:sendChannelMessage("", "You cannot request ending for war with " .. enemyName .. ".", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  167.             return false
  168.         end
  169.  
  170.         local query = "UPDATE `guild_wars` SET `status` = 4, `ended` = " .. os.time() .. " WHERE `id` = " .. result.getDataInt(tmp, "id")
  171.         result.free(tmp)
  172.  
  173.         db.query(query)
  174.         broadcastMessage(getPlayerGuildName(cid) .. " has signed an armstice declaration on a war with " .. enemyName .. ".", MESSAGE_EVENT_ADVANCE)
  175.         return false
  176.     end
  177.    
  178.     player:sendChannelMessage("", "Currently there's no active war with " .. enemyName .. ".", TALKTYPE_CHANNEL_R1, CHANNEL_GUILD)
  179.     return false
  180. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement