SHOW:
|
|
- or go back to the newest paste.
| 1 | -- comandos !rank "[hp][mana] | |
| 2 | ||
| 3 | -- ## configs ## | |
| 4 | local group_id_max = 3 -- group id maximo que entra no rank | |
| 5 | local limit_of_results = 10 -- limite de resultados, ou seja, ate que colocação deve exibir | |
| 6 | local useCache = true -- usar cache? obs.: cache é basicamente uma globalstorage que salva o rank para não ficar fazendo varias consultas na database | |
| 7 | local global_storage_for_cache = {hp = 15243, mana = 15241} -- key da global storage
| |
| 8 | local time_of_cache = 1 * 60-- tempos em segundos (1*60 = 60s = 1min) | |
| 9 | local item_of_dialog = 2112 -- itemid que aparecerá no topo do dialog | |
| 10 | local msg_erro_invalid_parameters = "Invalid parameters, choose 'mana' or 'hp'" -- mensagem que vai aparecer quando o parametro for invalido | |
| 11 | ||
| 12 | -- private functions | |
| 13 | local function getCache(rank_type) | |
| 14 | local str = getStorage(global_storage_for_cache[rank_type]) | |
| 15 | local time_last, String = str:gmatch("(%d+) || (.+)")
| |
| 16 | if str == -1 or not time_last or not String then | |
| 17 | return false | |
| 18 | end | |
| 19 | ||
| 20 | return true, time_last, String | |
| 21 | end | |
| 22 | ||
| 23 | local function isValidCache(rank_type) | |
| 24 | local cache, time_last, str = getCache(rank_type) | |
| 25 | if cache and os.time() < (time_last + time_of_cache) and str ~= "" then | |
| 26 | return true | |
| 27 | end | |
| 28 | return false | |
| 29 | end | |
| 30 | ||
| 31 | local function setCache(String, rank_type) | |
| 32 | if not isValidCache(rank_type) then | |
| 33 | local pattern = string.format("%d || %s", os.time(), String)
| |
| 34 | doSetStorage(global_storage_for_cache[rank_type], pattern) | |
| 35 | end | |
| 36 | end | |
| 37 | ||
| 38 | local function getRank(rank_type) | |
| 39 | ||
| 40 | 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)
| |
| 41 | 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)
| |
| 42 | local query_str = rank_type == "hp" and query_hp_str or query_mana_str | |
| 43 | local ret = nil | |
| 44 | ||
| 45 | ||
| 46 | if useCache then | |
| 47 | if isValidCache(rank_type) then | |
| 48 | _, _, ret = getCache(rank_type) | |
| 49 | return ret | |
| 50 | end | |
| 51 | end | |
| 52 | ||
| 53 | local query = db.getResult(query_hp_str) | |
| 54 | if query:getID() ~= -1 then | |
| 55 | ret = string.format("Rank of %s\n", rank_type == "hp" and "Health" or "Mana") .. "\n"
| |
| 56 | local count = 0 | |
| 57 | repeat | |
| 58 | local name = query:getDataString("name")
| |
| 59 | local value = query:getDataInt(rank_type == "hp" and "healthmax" or "manamax") | |
| 60 | count = count + 1 | |
| 61 | ret = ret .. string.format("%d - %s [ %d ]\n", count, name, value)
| |
| 62 | until not query:next() | |
| 63 | query:free() | |
| 64 | end | |
| 65 | ||
| 66 | return ret | |
| 67 | end | |
| 68 | ||
| 69 | function onSay(cid, words, param) | |
| 70 | ||
| 71 | local rank_type = param | |
| 72 | local rank_str = nil | |
| 73 | ||
| 74 | if isInArray({"mana", "hp"}, rank_type) then
| |
| 75 | rank_str = getRank(rank_type) | |
| 76 | setCache(rank_str, rank_type) | |
| 77 | else | |
| 78 | - | return doPlayerSendCancel(msg_erro_invalid_parameters) |
| 78 | + | return doPlayerSendCancel(cid, msg_erro_invalid_parameters) |
| 79 | end | |
| 80 | ||
| 81 | doShowTextDialog(cid, item_of_dialog, rank_str) | |
| 82 | return true | |
| 83 | end |