Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- comandos !rank "[hp][mana]
- -- ## configs ##
- local group_id_max = 3 -- group id maximo que entra no rank
- local limit_of_results = 10 -- limite de resultados, ou seja, ate que colocação deve exibir
- local useCache = true -- usar cache? obs.: cache é basicamente uma globalstorage que salva o rank para não ficar fazendo varias consultas na database
- local global_storage_for_cache = {hp = 15243, mana = 15241} -- key da global storage
- local time_of_cache = 1 * 60-- tempos em segundos (1*60 = 60s = 1min)
- local item_of_dialog = 2112 -- itemid que aparecerá no topo do dialog
- local msg_erro_invalid_parameters = "Invalid parameters, choose 'mana' or 'hp'" -- mensagem que vai aparecer quando o parametro for invalido
- -- private functions
- local function getCache(rank_type)
- local str = getStorage(global_storage_for_cache[rank_type])
- local time_last, String = str:gmatch("(%d+) || (.+)")
- if str == -1 or not time_last or not String then
- return false
- end
- return true, time_last, String
- end
- local function isValidCache(rank_type)
- local cache, time_last, str = getCache(rank_type)
- if cache and os.time() < (time_last + time_of_cache) and str ~= "" then
- return true
- end
- return false
- end
- local function setCache(String, rank_type)
- if not isValidCache(rank_type) then
- local pattern = string.format("%d || %s", os.time(), String)
- doSetStorage(global_storage_for_cache[rank_type], pattern)
- end
- end
- local function getRank(rank_type)
- local query_mana_str = string.format("select name, manamax from players where group_id < %d order by manamax desc limit %d", group_id_max, limit_of_results)
- local query_hp_str = string.format("select name, healthmax from players where group_id < %d order by healthmax desc limit %d", group_id_max, limit_of_results)
- local query_str = rank_type == "hp" and query_hp_str or query_mana_str
- local ret = nil
- if useCache then
- if isValidCache(rank_type) then
- _, _, ret = getCache(rank_type)
- return ret
- end
- end
- local query = db.getResult(query_hp_str)
- if query:getID() ~= -1 then
- ret = string.format("Rank of %s\n", rank_type == "hp" and "Health" or "Mana") .. "\n"
- local count = 0
- repeat
- local name = query:getDataString("name")
- local value = query:getDataInt(rank_type == "hp" and "healthmax" or "manamax")
- count = count + 1
- ret = ret .. string.format("%d - %s [ %d ]\n", count, name, value)
- until not query:next()
- query:free()
- end
- return ret
- end
- function onSay(cid, words, param)
- local rank_type = param
- local rank_str = nil
- if isInArray({"mana", "hp"}, rank_type) then
- rank_str = getRank(rank_type)
- setCache(rank_str, rank_type)
- else
- return doPlayerSendCancel(cid, msg_erro_invalid_parameters)
- end
- doShowTextDialog(cid, item_of_dialog, rank_str)
- return true
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement