• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

RevScripts check msql

alcapone

Member
Joined
Jan 13, 2021
Messages
247
Reaction score
19
in the players table of the database there is a column called top_pontos I would like to check it to work only with the top 3 points but I have no idea
 
otservbr / TFS 1.2+ code:
Lua:
local lastTop3PlayersRefresh = 0
local top3PlayersCache = {}

function getTop3Players()
    if lastTop3PlayersRefresh + 60 < os.time() then
        top3PlayersCache = {}
        lastTop3PlayersRefresh = os.time()
        local resultId = db.storeQuery("SELECT id FROM players ORDER BY top_pontos DESC LIMIT 3")
        if resultId ~= false then
            repeat
                table.insert(top3PlayersCache, result.getDataInt(resultId, "id"))
            until not result.next(resultId)
            result.free(resultId)
        end
    end

    return top3PlayersCache
end

function isTop3Player(player)
    local top3Players = getTop3Players()
    local playerGuid = player:getGuid()

    for _, guid in pairs(top3Players) do
        if playerGuid == guid then
            return true
        end
    end

    return false
end
I don't know how you want to block it in NPC, but you must call function isTop3Player(player) and it will return true/false.
I added cache, to do not send too many queries to database. It updates list of top 3 players every 60 seconds.

If you want to get only players with more than 0 points, replace:
Lua:
local resultId = db.storeQuery("SELECT id FROM players ORDER BY top_pontos DESC LIMIT 3")
with:
Lua:
local resultId = db.storeQuery("SELECT id FROM players WHERE top_pontos > 0 ORDER BY top_pontos DESC LIMIT 3")
 
otservbr / TFS 1.2+ code:
Lua:
local lastTop3PlayersRefresh = 0
local top3PlayersCache = {}

function getTop3Players()
    if lastTop3PlayersRefresh + 60 < os.time() then
        top3PlayersCache = {}
        lastTop3PlayersRefresh = os.time()
        local resultId = db.storeQuery("SELECT id FROM players ORDER BY top_pontos DESC LIMIT 3")
        if resultId ~= false then
            repeat
                table.insert(top3PlayersCache, result.getDataInt(resultId, "id"))
            until not result.next(resultId)
            result.free(resultId)
        end
    end

    return top3PlayersCache
end

function isTop3Player(player)
    local top3Players = getTop3Players()
    local playerGuid = player:getGuid()

    for _, guid in pairs(top3Players) do
        if playerGuid == guid then
            return true
        end
    end

    return false
end
I don't know how you want to block it in NPC, but you must call function isTop3Player(player) and it will return true/false.
I added cache, to do not send too many queries to database. It updates list of top 3 players every 60 seconds.

If you want to get only players with more than 0 points, replace:
Lua:
local resultId = db.storeQuery("SELECT id FROM players ORDER BY top_pontos DESC LIMIT 3")
with:
Lua:
local resultId = db.storeQuery("SELECT id FROM players WHERE top_pontos > 0 ORDER BY top_pontos DESC LIMIT 3")
local function creatureSayCallback(cid, type, msg)
if not npcHandler:isFocused(cid) then
return false
end
local player = Player(cid)
player:setStorageValue(Storage.NPCTable, -1)
for i = 1, #equivalente do
if msgcontains(equivalente, msg) then
player:setStorageValue(Storage.NPCTable, i)
local items = setNewTradeTable(getTable(player))

if isTop3Player(player) == false then
npcHandler:say("has no points", cid)
else
openShopWindow(cid, getTable(player), onBuy, onSell)
npcHandler:say('Alright, here\'s all the ' .. equivalente .. ' I can order for you!', cid)
break
end
end
end

return true
end





for a check on the npc it would be something like this?
if the player who is talking to the npc is not in the top 3 he will receive the message if he is in the top 3 the trade is opened
 
Back
Top