Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function string.escape(str)
- return str:gsub("([%%%^%$%(%)%.%[%]%*%+%-%?])", "%%%1")
- end
- local config = {
- pageSize = 20, -- Max is forced to be 255
- charText =
- [[Name: %name%
- Vocation: %vocation%
- Level: %level%
- Guild: %guild%]],
- hideGroups = 2, -- Will hide players with group id equal or higher than this
- hideAccess = true, -- Will hide players with group that has access = "1" (groups.xml)
- }
- local patterns = {
- ["%name%"] = Player.getName,
- ["%vocation%"] = function(player)
- local vocation = player:getVocation()
- return vocation:getName()
- end,
- ["%level%"] = Player.getLevel,
- ["%guild%"] = function(player)
- local guild = player:getGuild()
- if guild then
- return guild:getName()
- end
- return "None"
- end,
- }
- --[[
- If you want to add more information about the player in the info you should create a function that will have the player object
- as input and output the text that will replace the pattern, you can see how to do it in the examples above.
- The text will be replaced automatically.
- ]]
- function vocationFilter(player, vocations)
- return isInArray(vocations, player:getVocation():getId())
- end
- local filters = {
- ["knight"] = {f = vocationFilter, param = {4, 8}},
- ["paladin"] = {f = vocationFilter, param = {3, 7}},
- ["druid"] = {f = vocationFilter, param = {2, 6}},
- ["sorcerer"] = {f = vocationFilter, param = {1, 5}},
- }
- --[[
- 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)
- 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.
- Current filters are (you may only use 1 filter at a time):
- Vocation Filters (!online (knight, paladin, druid, sorcerer) )
- Level Filter: Hard coded, will sort the list in descending order by level. (!online level)
- Min Level Filter: Hard coded, will remove all players from the list that are lower than this level. (!online 200)
- 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.)
- All filter: Optional, you can use either !online all or just !online and it will be the same.
- 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.
- The main reason on why some filters were hard coded instead of using the current filter table:
- The filter table is to check each player not to modify the list itself so sorting needs to be hard coded.
- The filter argument does not support patterns so to code something like min level filter you have to hard code it.
- ]]
- function defaultFilter(player, filter)
- if filter == "" then
- return true
- end
- local name = player:getName():lower()
- if name:find("^" .. filter:escape()) then
- return true
- end
- return false
- end
- function getPlayersOnline(filter)
- local ret = {}
- local players = Game.getPlayers()
- if filter == "all" then
- filter = ""
- elseif filter == "level" then
- table.sort(players, function(a, b) return a:getLevel() > b:getLevel() end)
- filter = ""
- elseif filter:match("^%d+$") then
- local level = tonumber(filter:match("^%d+$"))
- for i = #players, 1, -1 do
- if players[i]:getLevel() < level then
- table.remove(players, i)
- end
- end
- filter = ""
- end
- local func = defaultFilter
- local param = filter
- if filters[filter] then
- param = filters[filter].param
- func = filters[filter].f
- end
- for i = 1, #players do
- local tmp = func(players[i], param)
- if tmp then
- if(not config.hideAccess or not players[i]:getGroup():getAccess()) and (not config.hideGroups or players[i]:getGroup():getId() < config.hideGroups) then
- table.insert(ret, players[i])
- end
- end
- end
- return ret
- end
- function evaluateText(name)
- local text = config.charText
- local player = Player(name)
- if not player then
- return "Player Offline."
- end
- for pattern, func in pairs(patterns) do
- text = text:gsub(pattern:escape(), func(player))
- end
- return text
- end
- function Player:sendOnlineModalWindow(filter, players, index)
- filter = filter:trim():lower()
- index = index or 1
- local players = players or getPlayersOnline(filter)
- config.pageSize = math.min(config.pageSize, 255)
- local window = ModalWindow {
- title = "Online List",
- message = "Players online using filter: " .. (filter == "" and "all" or filter),
- }
- if #players == 0 then
- window.message = window.message .. "\n\nNone."
- end
- for i = index, index+config.pageSize-1 do
- if not players[i] then
- break
- end
- window:addChoice(players[i]:getName())
- end
- window:addButton("Cancel")
- if players[index-config.pageSize] then
- window:addButton("Previous",
- function(button, choice)
- if players[index-config.pageSize] then
- self:sendOnlineModalWindow(filter, players, index-config.pageSize)
- end
- end
- )
- end
- if #players > 0 then
- window:addButton("Info",
- function(button, choice)
- self:showTextDialog(1952, evaluateText(choice.text))
- return true
- end
- )
- end
- if players[index+config.pageSize] then
- window:addButton("Next",
- function(button, choice)
- if players[index+config.pageSize] then
- self:sendOnlineModalWindow(filter, players, index+config.pageSize)
- end
- end
- )
- end
- window:setDefaultEscapeButton("Cancel")
- window:setDefaultEnterButton("Info")
- window:sendToPlayer(self)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement