Advertisement
Guest User

GUILDPOINTS tfs1.0

a guest
Sep 20th, 2014
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 0 0
  1. local config = {
  2.         executeInterval = 24,
  3.         minimumLevel = 80,
  4.         membersNeeded = 10,
  5.         minimumDifferentIps = 6,
  6.         pointAmount = 9
  7. }
  8.  
  9. local function getValidAccounts(guild)
  10.         local resultId = db.storeQuery('SELECT a.`id` FROM `accounts` a, `guild_membership` m, `players` p WHERE m.`guild_id` = ' ..guild:getId() .. ' AND p.`id` = m.`player_id` AND p.`level` > ' ..  config.minimumLevel .. ' and a.`id` = p.`account_id` AND a.`guild_points_stats` = 0 GROUP BY a.`id`;')
  11.         if resultId == false then
  12.                 return {}
  13.         end
  14.  
  15.         local accounts = {}
  16.         repeat
  17.                 table.insert(accounts, result.getDataInt(resultId, 'id'))
  18.         until not result.next(resultId)
  19.         result.free(resultId)
  20.  
  21.         return accounts
  22. end
  23.  
  24. function onSay(cid, words, param)
  25.         local player = Player(cid)
  26.         local guild = player:getGuild()
  27.         if not guild or player:getGuildLevel() ~= GUILDLEVEL_LEADER then
  28.                 player:getPosition():sendMagicEffect(CONST_ME_POFF)
  29.                 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, 'Only guild leader can request points.')
  30.                 return false
  31.         end
  32.  
  33.         local resultId = db.storeQuery('SELECT `last_execute_points` FROM `guilds` WHERE id = ' .. guild:getId())
  34.         if resultId == false then
  35.                 player:getPosition():sendMagicEffect(CONST_ME_POFF)
  36.                 player:sendCancelMessage('Error while running database query.')
  37.                 return false
  38.         end
  39.  
  40.         local lastExecution = result.getDataInt(resultId, 'last_execute_points')
  41.         result.free(resultId)
  42.         if lastExecution >= os.time() then
  43.                 player:getPosition():sendMagicEffect(CONST_ME_POFF)
  44.                 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, 'The command can only be run once every ' ..config.executeInterval .. ' hours.')
  45.                 return false
  46.         end
  47.  
  48.         local members = guild:getMembersOnline()
  49.         for i = #members, 1, -1 do
  50.                 if members[i]:getLevel() < config.minimumLevel then
  51.                         table.remove(members, i)
  52.                 end
  53.         end
  54.  
  55.         if #members < config.membersNeeded then
  56.                 player:getPosition():sendMagicEffect(CONST_ME_POFF)
  57.                 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, 'Only ' .. #members .. ' guild members online, you need ' ..config.membersNeeded .. ' guild members with level ' .. config.minimumLevel .. ' or higher.')
  58.                 return false
  59.         end
  60.  
  61.         local ipDictionary, ipCount = {}, 0
  62.         for i = 1, #members do
  63.                 local ip = members[i]:getIp()
  64.                 if not ipDictionary[ip] then
  65.                         ipDictionary[ip] = true
  66.                         ipCount = ipCount + 1
  67.                 end
  68.         end
  69.  
  70.         if ipCount < config.minimumDifferentIps then
  71.                 player:getPosition():sendMagicEffect(CONST_ME_POFF)
  72.                 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, 'Only ' .. ipCount .. ' members are valid, you need ' ..config.minimumDifferentIps .. ' players with different ip addresses.')
  73.                 return false
  74.         end
  75.  
  76.         local validAccounts = getValidAccounts(guild)
  77.         db.query('UPDATE `guilds` SET `last_execute_points` = ' .. (os.time() + config.executeInterval * 3600) .. ' WHERE `guilds`.`id` = ' .. guild:getId() .. ';')
  78.         player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, #validAccounts .. ' guild members received points.')
  79.         if #validAccounts > 0 then
  80.                 db.query('UPDATE `accounts` SET `guild_points` = `guild_points` + ' .. config.pointAmount .. ', `guild_points_stats` = ' .. os.time() .. ' WHERE `id` IN (' .. table.concat(validAccounts, ',') .. ');')
  81.                 for i = 1, #members do
  82.                         local member = members[i]
  83.                         if isInArray(validAccounts, member:getAccountId()) then
  84.                                 member:sendTextMessage(MESSAGE_INFO_DESCR, 'You received ' .. config.pointAmount .. ' guild points.')
  85.                         end
  86.                 end
  87.         end
  88.         return false
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement