Jump to content

Ajuda com sistema de ranking


Recommended Posts

Olá a alguns dias atrás pedi conselhos no forum para desenvolver um sistema de ranking com accountData, bom digamos que consegui, adaptando um codigo que encontrei aqui no forum de sistema K/D/KDR, porém estou lidando com um erro meio chato já tentei resolver de algumas maneiras e a forma na qual ocorre nem passa pela minha cabeça, quando o jogador passa em cima do marcador ele exibe o painel com o ranking tudo beleza mas ele mostra um ranking desatualizado, se o jogador conseguiu ficar em primeiro no ranking ele so vai mostrar na proxima vez q ele hitar o marker e nao quando o jogador pegou o marker, queria que ele exibisse a informação em "tempo real" e não como um sistema de atualização.

se puderem me ajudar agradeço. Codigo:
Client-side 

function painel(accounts, kills, deaths, ratio)
    if accounts then
        for i, v in ipairs (accounts) do
            function painel_kdr()
               
                kill1 = kills[1] or 0
                kill2 = kills[2] or 0
                kill3 = kills[3] or 0
                kill4 = kills[4] or 0
                kill5 = kills[5] or 0
                      
                conta1 = accounts[1] or "N/A"
                conta2 = accounts[2] or "N/A"
                conta3 = accounts[3] or "N/A"
                conta4 = accounts[4] or "N/A"
                conta5 = accounts[5] or "N/A"
                    
                morte1 = deaths[1] or "N/A"
                morte2 = deaths[2] or "N/A"
                morte3 = deaths[3] or "N/A"
                morte4 = deaths[4] or "N/A"
                morte5 = deaths[5] or "N/A"

                dxDrawText("Pressione 'backspace' para fechar", screenW * 0.2281, screenH * 0.7549, screenW * 0.7586, screenH * 0.7949, tocolor(255, 255, 255, 255), 1.50, "default", "center", "center", false, false, false, false, false)
                dxDrawRoundedRectangle(screenW * 0.2281, screenH * 0.2490, screenW * 0.5312, screenH * 0.5068, tocolor(32, 32, 32, 255), 4)
                dxDrawText("Liders kills", screenW * 0.2641, screenH * 0.2568, screenW * 0.5258, screenH * 0.3086, tocolor(255, 255, 255, 255), 1.50, "default-bold", "left", "center", false, false, false, false, false)
                dxDrawRoundedRectangle(screenW * 0.2641, screenH * 0.3330, screenW * 0.4578, screenH * 0.3535, tocolor(38, 38, 38, 255), 9)
                dxDrawText("Jogador", screenW * 0.2641, screenH * 0.3320, screenW * 0.4602, screenH * 0.3760, tocolor(255, 255, 255, 255), 1.30, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("Kill", screenW * 0.4602, screenH * 0.3330, screenW * 0.5852, screenH * 0.3779, tocolor(255, 255, 255, 255), 1.30, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("Morte", screenW * 0.5852, screenH * 0.3330, screenW * 0.7219, screenH * 0.3779, tocolor(255, 255, 255, 255), 1.30, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawRectangle(screenW * 0.2633, screenH * 0.3779, screenW * 0.4586, screenH * 0.0508, tocolor(32, 32, 32, 255), false)
                dxDrawRectangle(screenW * 0.2641, screenH * 0.4795, screenW * 0.4586, screenH * 0.0508, tocolor(32, 32, 32, 255), false)
                dxDrawRectangle(screenW * 0.2633, screenH * 0.5811, screenW * 0.4586, screenH * 0.0508, tocolor(32, 32, 32, 255), false)
                dxDrawText("1-", screenW * 0.2641, screenH * 0.3779, screenW * 0.2953, screenH * 0.4287, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("2-", screenW * 0.2641, screenH * 0.4287, screenW * 0.2953, screenH * 0.4785, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("3-", screenW * 0.2641, screenH * 0.4785, screenW * 0.2953, screenH * 0.5283, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("4-", screenW * 0.2641, screenH * 0.5283, screenW * 0.2953, screenH * 0.5811, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("5-", screenW * 0.2641, screenH * 0.5811, screenW * 0.2953, screenH * 0.6309, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
     
                dxDrawText(""..conta1, screenW * 0.2953, screenH * 0.3779, screenW * 0.4602, screenH * 0.4277, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..conta2, screenW * 0.2953, screenH * 0.4287, screenW * 0.4602, screenH * 0.4795, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..conta3, screenW * 0.2953, screenH * 0.4795, screenW * 0.4602, screenH * 0.5303, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..conta4, screenW * 0.2953, screenH * 0.5283, screenW * 0.4602, screenH * 0.5811, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..conta5, screenW * 0.2953, screenH * 0.5811, screenW * 0.4602, screenH * 0.6309, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("Você", screenW * 0.2953, screenH * 0.6357, screenW * 0.4602, screenH * 0.6865, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill1, screenW * 0.4602, screenH * 0.3760, screenW * 0.5852, screenH * 0.4277, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill2, screenW * 0.4602, screenH * 0.4277, screenW * 0.5852, screenH * 0.4795, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill3, screenW * 0.4602, screenH * 0.4795, screenW * 0.5852, screenH * 0.5312, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill4, screenW * 0.4602, screenH * 0.5303, screenW * 0.5852, screenH * 0.5820, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill5, screenW * 0.4602, screenH * 0.5811, screenW * 0.5852, screenH * 0.6328, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("N/A", screenW * 0.4602, screenH * 0.6357, screenW * 0.5852, screenH * 0.6875, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte1, screenW * 0.5852, screenH * 0.3770, screenW * 0.7219, screenH * 0.4277, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte2, screenW * 0.5852, screenH * 0.4287, screenW * 0.7219, screenH * 0.4795, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte3, screenW * 0.5852, screenH * 0.4795, screenW * 0.7219, screenH * 0.5303, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte4 , screenW * 0.5852, screenH * 0.5303, screenW * 0.7219, screenH * 0.5811, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte5, screenW * 0.5852, screenH * 0.5820, screenW * 0.7219, screenH * 0.6328, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
            end
        end
    end
end
addEvent("showRank", true)
addEventHandler("showRank", getRootElement(), painel)



------------------------------ MARKER PARA ABRIR O PAINEL ---------------------------------------

local marker = createMarker(1715.93628, -1920.92944, 13.23579, "cylinder", 10.5, 255, 255, 255, 0)
function onMarkerHit(hitElement, matchingDimension)
    local markerPosition = Vector3(1715.93628, -1920.92944, 13.23579)
    
    if hitElement and getElementType(hitElement) == "player" and matchingDimension then
        local playerPosition = Vector3(getElementPosition(hitElement))
            triggerServerEvent("getRank", hitElement)
            setTimer(function() 
                if rankingpanel == false then 
                    addEventHandler("onClientRender", root, painel_kdr)
                    rankingpanel  = true  
                end
            end, 500, 1)     
    end
end
addEventHandler("onClientMarkerHit", marker, onMarkerHit)

 

Server-side

local marker = createMarker(1715.93628, -1920.92944, 13.23579, "cylinder", 10.5, 255, 255, 255, 0)
function recebeKills()
    local data = getAccounts()
    local accTable = {}
    local killsTable = {}
    local deathsTable = {}
    local killDeath = {}

    if not data[1] then
        triggerClientEvent(client, "showRank", client)
        return
    end

    -- Preencher as tabelas apenas para jogadores com kills maiores que 1 ou não vazios
    for i, acc in ipairs(data) do
        local accName = getAccountName(data[i])
        local kills = tonumber(getAccountData(data[i], "kill.s1")) or 0
        local deaths = getAccountData(data[i], "morte.s1") or 0
        if kills > 1 or (kills > 0 and deaths > 0) then
            table.insert(accTable, accName)
            table.insert(killsTable, kills)
            table.insert(deathsTable, deaths)
            
        end
    end

    -- Criar uma tabela temporária com índices classificados
    local indices = {}
    for i = 1, #accTable do
        table.insert(indices, i)
    end

    -- Classificar os índices com base no número de kills (menor vai primeiro)
    table.sort(indices, function(a, b)
        if killsTable[a] == 0 and killsTable[b] == 0 then
            return a < b  -- Mantenha a ordem original para aqueles com 0 kills
        else
            return killsTable[a] < killsTable[b]
        end
    end)

    -- Criar tabelas classificadas usando os índices
    local sortedAccTable = {}
    local sortedKillsTable = {}
    local sortedDeathsTable = {}
    local sortedKillDeath = {}

    for _, index in ipairs(indices) do
        table.insert(sortedAccTable, accTable[index])
        table.insert(sortedKillsTable, killsTable[index])
        table.insert(sortedDeathsTable, deathsTable[index])
        table.insert(sortedKillDeath, killDeath[index])
    end

    -- Enviar dados classificados para o cliente

    triggerClientEvent(client, "showRank", client, sortedAccTable, sortedKillsTable, sortedDeathsTable, sortedKillDeath) 
        
end
addEvent("getRank", true)
addEventHandler("getRank", getRootElement(), recebeKills)

sei que o codigo ta bem bagunçado mas pretendo dar uma "limpada" depois de finalizar se alguem conseguir me ajudar agradeço mt

Link to comment

Acredito que isso possa acontecer por causa do setTimer do lado do cliente. Depois de 500 milisegundos ele começa a renderizar as informações e às vezes não dá tempo do servidor calcular tudo e devolver para o cliente a informação mais atualizada.

O que eu recomendo é remover o setTimer depois do jogador encostar no marker e colocar para renderizar dentro da função painel, veja:
 

1. Remover o setTimer dessa função:

------------------------------ MARKER PARA ABRIR O PAINEL ---------------------------------------

local marker = createMarker(1715.93628, -1920.92944, 13.23579, "cylinder", 10.5, 255, 255, 255, 0)
function onMarkerHit(hitElement, matchingDimension)
    local markerPosition = Vector3(1715.93628, -1920.92944, 13.23579)
    
    if hitElement and getElementType(hitElement) == "player" and matchingDimension and not rankingpanel then
        local playerPosition = Vector3(getElementPosition(hitElement))
    	triggerServerEvent("getRank", hitElement)
    	-- setTimer removido daqui
    end
end
addEventHandler("onClientMarkerHit", marker, onMarkerHit)

 

2. Renderizar o painel só depois de receber as informações mais atualizadas possíveis:

function painel(accounts, kills, deaths, ratio)
    if accounts then
        for i, v in ipairs (accounts) do
            function painel_kdr()
               
                kill1 = kills[1] or 0
                kill2 = kills[2] or 0
                kill3 = kills[3] or 0
                kill4 = kills[4] or 0
                kill5 = kills[5] or 0
                      
                conta1 = accounts[1] or "N/A"
                conta2 = accounts[2] or "N/A"
                conta3 = accounts[3] or "N/A"
                conta4 = accounts[4] or "N/A"
                conta5 = accounts[5] or "N/A"
                    
                morte1 = deaths[1] or "N/A"
                morte2 = deaths[2] or "N/A"
                morte3 = deaths[3] or "N/A"
                morte4 = deaths[4] or "N/A"
                morte5 = deaths[5] or "N/A"

                dxDrawText("Pressione 'backspace' para fechar", screenW * 0.2281, screenH * 0.7549, screenW * 0.7586, screenH * 0.7949, tocolor(255, 255, 255, 255), 1.50, "default", "center", "center", false, false, false, false, false)
                dxDrawRoundedRectangle(screenW * 0.2281, screenH * 0.2490, screenW * 0.5312, screenH * 0.5068, tocolor(32, 32, 32, 255), 4)
                dxDrawText("Liders kills", screenW * 0.2641, screenH * 0.2568, screenW * 0.5258, screenH * 0.3086, tocolor(255, 255, 255, 255), 1.50, "default-bold", "left", "center", false, false, false, false, false)
                dxDrawRoundedRectangle(screenW * 0.2641, screenH * 0.3330, screenW * 0.4578, screenH * 0.3535, tocolor(38, 38, 38, 255), 9)
                dxDrawText("Jogador", screenW * 0.2641, screenH * 0.3320, screenW * 0.4602, screenH * 0.3760, tocolor(255, 255, 255, 255), 1.30, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("Kill", screenW * 0.4602, screenH * 0.3330, screenW * 0.5852, screenH * 0.3779, tocolor(255, 255, 255, 255), 1.30, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("Morte", screenW * 0.5852, screenH * 0.3330, screenW * 0.7219, screenH * 0.3779, tocolor(255, 255, 255, 255), 1.30, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawRectangle(screenW * 0.2633, screenH * 0.3779, screenW * 0.4586, screenH * 0.0508, tocolor(32, 32, 32, 255), false)
                dxDrawRectangle(screenW * 0.2641, screenH * 0.4795, screenW * 0.4586, screenH * 0.0508, tocolor(32, 32, 32, 255), false)
                dxDrawRectangle(screenW * 0.2633, screenH * 0.5811, screenW * 0.4586, screenH * 0.0508, tocolor(32, 32, 32, 255), false)
                dxDrawText("1-", screenW * 0.2641, screenH * 0.3779, screenW * 0.2953, screenH * 0.4287, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("2-", screenW * 0.2641, screenH * 0.4287, screenW * 0.2953, screenH * 0.4785, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("3-", screenW * 0.2641, screenH * 0.4785, screenW * 0.2953, screenH * 0.5283, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("4-", screenW * 0.2641, screenH * 0.5283, screenW * 0.2953, screenH * 0.5811, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("5-", screenW * 0.2641, screenH * 0.5811, screenW * 0.2953, screenH * 0.6309, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
     
                dxDrawText(""..conta1, screenW * 0.2953, screenH * 0.3779, screenW * 0.4602, screenH * 0.4277, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..conta2, screenW * 0.2953, screenH * 0.4287, screenW * 0.4602, screenH * 0.4795, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..conta3, screenW * 0.2953, screenH * 0.4795, screenW * 0.4602, screenH * 0.5303, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..conta4, screenW * 0.2953, screenH * 0.5283, screenW * 0.4602, screenH * 0.5811, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..conta5, screenW * 0.2953, screenH * 0.5811, screenW * 0.4602, screenH * 0.6309, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("Você", screenW * 0.2953, screenH * 0.6357, screenW * 0.4602, screenH * 0.6865, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill1, screenW * 0.4602, screenH * 0.3760, screenW * 0.5852, screenH * 0.4277, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill2, screenW * 0.4602, screenH * 0.4277, screenW * 0.5852, screenH * 0.4795, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill3, screenW * 0.4602, screenH * 0.4795, screenW * 0.5852, screenH * 0.5312, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill4, screenW * 0.4602, screenH * 0.5303, screenW * 0.5852, screenH * 0.5820, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..kill5, screenW * 0.4602, screenH * 0.5811, screenW * 0.5852, screenH * 0.6328, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText("N/A", screenW * 0.4602, screenH * 0.6357, screenW * 0.5852, screenH * 0.6875, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte1, screenW * 0.5852, screenH * 0.3770, screenW * 0.7219, screenH * 0.4277, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte2, screenW * 0.5852, screenH * 0.4287, screenW * 0.7219, screenH * 0.4795, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte3, screenW * 0.5852, screenH * 0.4795, screenW * 0.7219, screenH * 0.5303, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte4 , screenW * 0.5852, screenH * 0.5303, screenW * 0.7219, screenH * 0.5811, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
                dxDrawText(""..morte5, screenW * 0.5852, screenH * 0.5820, screenW * 0.7219, screenH * 0.6328, tocolor(255, 255, 255, 255), 1.20, "default-bold", "center", "center", false, false, false, false, false)
            end
        end
    
        addEventHandler("onClientRender", root, painel_kdr)
        rankingpanel = true
    end
end
addEvent("showRank", true)
addEventHandler("showRank", getRootElement(), painel)

 

Percebo que dá pra fazer algumas melhoras no código na parte de contagem das estatísticas no servidor e cliente, se te interessar, devolva um feedback sobre o problema e o interesse em fazer essas melhoras. Valeu!

Edited by noonbr
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...