Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.55 KB | None | 0 0
  1. function string.escape(str)
  2.     return str:gsub("([%%%^%$%(%)%.%[%]%*%+%-%?])", "%%%1")
  3. end
  4.  
  5. local config = {
  6.     pageSize = 20, -- Max is forced to be 255
  7.     charText =
  8. [[Name: %name%
  9. Vocation: %vocation%
  10. Level: %level%
  11. Guild: %guild%]],
  12.     hideGroups = 2, -- Will hide players with group id equal or higher than this
  13.     hideAccess = true, -- Will hide players with group that has access = "1" (groups.xml)
  14. }
  15.  
  16. local patterns = {
  17.     ["%name%"] = Player.getName,
  18.     ["%vocation%"] = function(player)
  19.                         local vocation = player:getVocation()
  20.                         return vocation:getName()
  21.                     end,
  22.     ["%level%"] = Player.getLevel,
  23.     ["%guild%"] = function(player)
  24.                     local guild = player:getGuild()
  25.                     if guild then
  26.                         return guild:getName()
  27.                     end
  28.                     return "None"
  29.                 end,
  30. }
  31. --[[
  32.     If you want to add more information about the player in the info you should create a function that will have the player object
  33.     as input and output the text that will replace the pattern, you can see how to do it in the examples above.
  34.     The text will be replaced automatically.
  35. ]]
  36.  
  37.  
  38. function vocationFilter(player, vocations)
  39.     return isInArray(vocations, player:getVocation():getId())
  40. end
  41.  
  42. local filters = {
  43.     ["knight"] = {f = vocationFilter, param = {4, 8}},
  44.     ["paladin"] = {f = vocationFilter, param = {3, 7}},
  45.     ["druid"] = {f = vocationFilter, param = {2, 6}},
  46.     ["sorcerer"] = {f = vocationFilter, param = {1, 5}},
  47. }
  48.  
  49. --[[
  50.     If you want to add a filter you should create a function that will have the player object as input and optional set parameter (only one)
  51.     The function must output a boolean, if its false the player will be added to the list, if its not he will be removed from it.
  52.     Current filters are (you may only use 1 filter at a time):
  53.     Vocation Filters (!online (knight, paladin, druid, sorcerer) )
  54.     Level Filter: Hard coded, will sort the list in descending order by level. (!online level)
  55.     Min Level Filter: Hard coded, will remove all players from the list that are lower than this level. (!online 200)
  56.     Name Filters: Will return a list with all the players starting with this name (!online m returns the players starting with this letter, !online mka returns all players starting with "mka" and so on.)
  57.     All filter: Optional, you can use either !online all or just !online and it will be the same.
  58.  
  59.     The name filter is the last one to be checked so you can't check for players with starting name "knight" for example. It would return the list of knights online instead of name filtering.
  60.  
  61.     The main reason on why some filters were hard coded instead of using the current filter table:
  62.         The filter table is to check each player not to modify the list itself so sorting needs to be hard coded.
  63.         The filter argument does not support patterns so to code something like min level filter you have to hard code it.
  64.  
  65. ]]
  66.  
  67. function defaultFilter(player, filter)
  68.     if filter == "" then
  69.         return true
  70.     end
  71.  
  72.     local name = player:getName():lower()
  73.     if name:find("^" .. filter:escape()) then
  74.         return true
  75.     end
  76.  
  77.     return false
  78. end
  79.  
  80. function getPlayersOnline(filter)
  81.     local ret = {}
  82.     local players = Game.getPlayers()
  83.     if filter == "all" then
  84.         filter = ""
  85.     elseif filter == "level" then
  86.         table.sort(players, function(a, b) return a:getLevel() > b:getLevel() end)
  87.         filter = ""
  88.     elseif filter:match("^%d+$") then
  89.         local level = tonumber(filter:match("^%d+$"))
  90.         for i = #players, 1, -1 do
  91.             if players[i]:getLevel() < level then
  92.                 table.remove(players, i)
  93.             end
  94.         end
  95.         filter = ""
  96.     end
  97.  
  98.     local func = defaultFilter
  99.     local param = filter
  100.     if filters[filter] then
  101.         param = filters[filter].param
  102.         func = filters[filter].f
  103.     end
  104.  
  105.     for i = 1, #players do
  106.         local tmp = func(players[i], param)
  107.         if tmp then
  108.             if(not config.hideAccess or not players[i]:getGroup():getAccess()) and (not config.hideGroups or players[i]:getGroup():getId() < config.hideGroups) then
  109.                 table.insert(ret, players[i])
  110.             end
  111.         end
  112.     end
  113.  
  114.     return ret
  115. end
  116.  
  117. function evaluateText(name)
  118.     local text = config.charText
  119.     local player = Player(name)
  120.     if not player then
  121.         return "Player Offline."
  122.     end
  123.  
  124.     for pattern, func in pairs(patterns) do
  125.         text = text:gsub(pattern:escape(), func(player))
  126.     end
  127.  
  128.     return text
  129. end
  130.  
  131. function Player:sendOnlineModalWindow(filter, players, index)
  132.     filter = filter:trim():lower()
  133.     index = index or 1
  134.     local players = players or getPlayersOnline(filter)
  135.     config.pageSize = math.min(config.pageSize, 255)
  136.     local window = ModalWindow {
  137.         title = "Online List",
  138.         message = "Players online using filter: " .. (filter == "" and "all" or filter),
  139.     }
  140.  
  141.     if #players == 0 then
  142.         window.message = window.message .. "\n\nNone."
  143.     end
  144.  
  145.     for i = index, index+config.pageSize-1 do
  146.         if not players[i] then
  147.             break
  148.         end
  149.         window:addChoice(players[i]:getName())
  150.     end
  151.  
  152.     window:addButton("Cancel")
  153.     if players[index-config.pageSize] then
  154.         window:addButton("Previous",
  155.             function(button, choice)
  156.                 if players[index-config.pageSize] then
  157.                     self:sendOnlineModalWindow(filter, players, index-config.pageSize)
  158.                 end
  159.             end
  160.         )
  161.     end
  162.     if #players > 0 then
  163.         window:addButton("Info",
  164.             function(button, choice)
  165.                 self:showTextDialog(1952,  evaluateText(choice.text))
  166.                 return true
  167.             end
  168.         )
  169.     end
  170.     if players[index+config.pageSize] then
  171.         window:addButton("Next",
  172.             function(button, choice)
  173.                 if players[index+config.pageSize] then
  174.                     self:sendOnlineModalWindow(filter, players, index+config.pageSize)
  175.                 end
  176.             end
  177.         )
  178.     end
  179.     window:setDefaultEscapeButton("Cancel")
  180.     window:setDefaultEnterButton("Info")
  181.     window:sendToPlayer(self)
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement