adrianoswatt

Woe 02 - ClassicBrTibia

Oct 21st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.99 KB | None | 0 0
  1. dofile("./var.woe")
  2. local config = woe_config
  3. local guildGlobalSto = 25666
  4. fileStore = true
  5. infoFile = 'tmp.woe'
  6. infoLua = {}
  7. Woe = {}
  8. Woe.__index = Woe
  9.  
  10. function Woe.setup()
  11.     db.executeQuery("DROP TABLE IF EXISTS `woe`;")
  12.     db.executeQuery("CREATE TABLE `woe` (`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,`started` INT( 11 ) NOT NULL ,`guild` INT( 11 ) NOT NULL ,`breaker` INT( 11 ) NOT NULL ,`time` INT( 11 ) NOT NULL ,PRIMARY KEY ( `id` ) ,UNIQUE (`id`)) ENGINE = MYISAM")
  13.     doBroadcastMessage("DB Added [...]", config.bcType)
  14.     if fileStore then
  15.         local newFile = io.open(infoFile, "w")
  16.         for _, i in ipairs({'started', 'guild', 'breaker', 'time'}) do
  17.             newFile:write(i .. ' = 0 ;end;\n')
  18.         end
  19.         newFile:close()
  20.     else
  21.         db.executeQuery("DROP TABLE IF EXISTS `tmpwoe`;")
  22.         db.executeQuery("CREATE TABLE `tmpwoe` (`started` INT( 11 ) NOT NULL ,`guild` INT( 11 ) NOT NULL ,`breaker` INT( 111 ) NOT NULL ,`time` INT( 1 ) NOT NULL) ENGINE = MYISAM ;")
  23.         db.executeQuery("ALTER TABLE `tmpwoe` ADD `indexer` INT NOT NULL ")
  24.         db.executeQuery("INSERT INTO `tmpwoe` (`started` ,`guild` ,`breaker` ,`time`, `indexer`)VALUES ('0', '0', '0', '0', '1');")
  25.     end
  26. end
  27.  
  28. function Woe.getInfo()
  29.     if fileStore then
  30.         local open = io.open(infoFile, "r")
  31.         if open then
  32.             for i in io.lines(infoFile) do
  33.                 for v, k in ipairs({'started', 'guild', 'breaker', 'time'}) do
  34.                     if (i:find(k)) then
  35.                         n = i:match(k .. '.*')
  36.                         infoLua[v] = tonumber(n:sub(n:find('=') + 2, n:find(';end;') - 2))
  37.                     end
  38.                 end
  39.             end
  40.             open:close()
  41.         end
  42.     else
  43.         for v, k in ipairs({'started', 'guild', 'breaker', 'time'}) do
  44.             local tmp = db.getResult("SELECT " .. k .. " FROM `tmpwoe` WHERE `indexer` = '1';")
  45.             infoLua[v] = tmp:getDataInt(k)
  46.             tmp:free()
  47.         end
  48.     end
  49. end
  50.  
  51. function Woe.updateInfo(tab)
  52.     if fileStore then
  53.         local open = io.open(infoFile, "w")
  54.         if open then
  55.             for k, i in ipairs({'started', 'guild', 'breaker', 'time'}) do
  56.                 open:write(i .. ' = ' .. tab[k] .. ' ;end;\n')
  57.                 setGlobalStorageValue(guildGlobalSto, tab[k])
  58.             end
  59.             open:close()
  60.         end
  61.     else
  62.         for v, k in ipairs({'started', 'guild', 'breaker', 'time'}) do
  63.             db.executeQuery("UPDATE `tmpwoe` SET " .. k .. " =  " .. tab[v] .. " WHERE `indexer` = 1;")
  64.             setGlobalStorageValue(guildGlobalSto, tab[v])
  65.         end
  66.     end
  67. end
  68.  
  69. function Woe.save()
  70.     Woe.getInfo()
  71.     db.executeQuery("INSERT INTO `woe` (`started`, `guild`, `breaker`, `time`) VALUES (" .. infoLua[1] .. ", " .. infoLua[2] .. ", " .. infoLua[3] .. ", " .. infoLua[4] .. ");")
  72. end
  73.  
  74. function Woe.getGuildName(id)
  75.     local res = db.getResult("SELECT `name` FROM `guilds` WHERE `id` = " .. id .. ";")
  76.     if (res:getID() ~= -1) then
  77.         ret = res:getDataString('name')
  78.         res:free()
  79.     end
  80.     return ret
  81. end
  82.  
  83. function Woe.breakerName()
  84.     Woe.getInfo()
  85.     return infoLua[3] ~= 0 and getPlayerNameByGUID(infoLua[3]) or 'None'
  86. end
  87.  
  88. function Woe.guildName()
  89.     Woe.getInfo()
  90.     return infoLua[2] ~= 0 and Woe.getGuildName(infoLua[2]) or 'Nones'
  91. end
  92.  
  93. function Woe.startTime()
  94.     Woe.getInfo()
  95.     return os.date("%d %B %Y %X ", infoLua[1])
  96. end
  97.  
  98. function Woe.breakTime()
  99.     Woe.getInfo()
  100.     return os.date("%d %B %Y %X ", infoLua[4])
  101. end
  102.  
  103. function Woe.timeToEnd()
  104.     Woe.getInfo()
  105.     local myTable = {}
  106.     for k, i in ipairs({"%M", "%S"}) do
  107.         myTable[k] = os.date(i, os.difftime(os.time(), infoLua[1]))
  108.     end
  109.     return {mins = ((config.timeToEnd - 1) - myTable[1]), secs = (60 - myTable[2])}
  110. end
  111.  
  112. function Woe.moveBack(cid, fromPosition, text)
  113.     doTeleportThing(cid, fromPosition, TRUE)
  114.     doPlayerSendCancel(cid, text)
  115. end
  116.  
  117. function Woe.getGuildMembers(id)
  118.     local members = {}
  119.     for _, i in ipairs(getPlayersOnline()) do
  120.         if id == getPlayerGuildId(i) then
  121.             table.insert(members, i)
  122.         end
  123.     end
  124.     return members
  125. end
  126.  
  127. function Woe.deco(text)
  128.     for _, i in ipairs(Castle.decoraciones) do
  129.         doItemSetAttribute(i, "description", text)
  130.     end
  131. end
  132.  
  133. function Woe.removePortals()
  134.     for _, i in ipairs(Castle.PrePortalsPos) do
  135.         if (getThingFromPos(i).itemid > 0) then
  136.             doRemoveItem(getThingFromPos(i).uid)
  137.         end
  138.     end
  139. end
  140.  
  141. function Woe.removePre()
  142.     for _, i in ipairs(Castle.PreEmpes) do
  143.         if (isCreature(getThingFromPos(i).uid) == true) then
  144.             doRemoveCreature(getThingFromPos(i).uid)
  145.         end
  146.     end
  147. end
  148.  
  149. function Woe.checkPre()
  150.     local Count = 0
  151.     for _, i in ipairs(Castle.PreEmpes) do
  152.         if (isCreature(getThingFromPos(i).uid) == false) then
  153.             Count = Count + 1
  154.         end
  155.     end
  156.     return (Count == #Castle.PreEmpes)
  157. end
  158.  
  159. function Woe.isTime()
  160.     return (getGlobalStorageValue(stor.WoeTime) == 1)
  161. end
  162.  
  163. function Woe.isStarted()
  164.     return (getGlobalStorageValue(stor.Started) == 1)
  165. end
  166.  
  167. function Woe.isRegistered(cid)
  168.     return (getPlayerStorageValue(cid, stor.register) == 1)
  169. end
  170.  
  171. function Woe.isInCastle(cid)
  172.     local myPos = getCreaturePosition(cid)
  173.     if (myPos.x >= Castle.salas.a.fromx and myPos.x <= Castle.salas.a.tox) then
  174.         if (myPos.y >= Castle.salas.a.fromy and myPos.y <= Castle.salas.a.toy) then
  175.             if isInArray({Castle.salas.a.z, Castle.salas.b.z, Castle.salas.c.z}, myPos.z) then
  176.                 return true
  177.             end
  178.         end
  179.     end
  180.     return false
  181. end
  182.  
  183. function Woe.expulsar(guild, fromx, tox, fromy, toy, z, outpos)
  184.     for _x = fromx, tox do
  185.         for _y = fromy, toy do
  186.             local player = getThingFromPos({x = _x, y = _y, z = z, stackpos = 253}).uid
  187.             if (isPlayer(player) == true) then
  188.                 if (getPlayerGuildId(player) ~= guild) then
  189.                     doTeleportThing(player, outpos, false)
  190.                 end
  191.             end
  192.         end
  193.     end
  194. end
  195.  
  196. -- extras
  197.  
  198. function doSetItemActionId(uid, action)
  199.     doItemSetAttribute(uid, "aid", action)
  200. end
  201.  
  202. function exhaust(cid, storevalue, exhausttime)
  203. -- Exhaustion function by Alreth, v1.1 2006-06-24 01:31
  204. -- Returns 1 if not exhausted and 0 if exhausted
  205.     newExhaust = os.time()
  206.     oldExhaust = getPlayerStorageValue(cid, storevalue)
  207.     if (oldExhaust == nil or oldExhaust < 0) then
  208.         oldExhaust = 0
  209.     end
  210.     if (exhausttime == nil or exhausttime < 0) then
  211.         exhausttime = 1
  212.     end
  213.     diffTime = os.difftime(newExhaust, oldExhaust)
  214.     if (diffTime >= exhausttime or diffTime < 0) then
  215.         setPlayerStorageValue(cid, storevalue, newExhaust)
  216.         return 1
  217.     else
  218.         return 0
  219.     end
  220. end
  221.  
  222. --Posição dos guardas que vão defender o Castelo.
  223. guard_pos =
  224.     {
  225.         {x = 656, y = 248, z = 5},
  226.         {x = 659, y = 248, z = 5}
  227.     }
  228.    
  229. function Woe.check()
  230.     for storage = 24504, 24511 do
  231.         local pid = getGlobalStorageValue(storage)
  232.         if isCreature(pid) then
  233.             return false
  234.         end
  235.     end
  236.     return true
  237. end
  238.  
  239. function Woe.summon()
  240.     for k, i in ipairs(guard_pos) do
  241.         local pid = doSummonCreature("guard", i)
  242.         setGlobalStorageValue(24503 + k, pid)
  243.     end
  244. end
  245.  
  246. function Woe.remove()
  247.     for storage = 24504, 24511 do
  248.         local pid = getGlobalStorageValue(storage)
  249.         if isCreature(pid) then
  250.             doRemoveCreature(pid)
  251.         end
  252.     end
  253. end
  254.  
  255. function pegavencedor()
  256.     local aa
  257.     local res = db.getResult("SELECT `guild` FROM `woe` ORDER BY `id` DESC;")
  258.     if (res:getID() ~= -1) then
  259.         aa = res:getDataString('guild')
  260.         res:free()
  261.     else
  262.         res:free()
  263.         return 0
  264.     end
  265.     return aa
  266. end
Advertisement
Add Comment
Please, Sign In to add comment