Jump to content

How can I make it working also for offline players...


GTX

Recommended Posts

Hello, I'm wondering how to get all offline players and get them into a top list

(I have no idea where to start)

Code:

  
function tables() 
                    local table1 = {} 
                    local table2 = {} 
                    local players = getElementsByType("player") 
                    local data1 
                    local data2 
                    local data3 
                    for i, v in ipairs(players) do 
                        local account = getPlayerAccount(v) 
                        if not (isGuestAccount(account)) then 
                            local dataValue = tonumber(getAccountData(account, "Points")) 
                            local name = getPlayerName(v) 
                            table.insert(table1, dataValue) 
                            table2[dataValue] = name 
                        end 
                    end 
                    table.sort(table1) 
                    if (#table1 == 1) then 
                        data1 = "1. " .. table2[table1[1]] .. " - " .. table1[1] .. " points." 
                    elseif (#table1 == 2) then 
                        data1 = "1. " .. table2[table1[2]] .. " - " .. table1[2] .. " points." 
                        data2 = "2. " .. table2[table1[1]] .. " - " .. table1[1] .. " points." 
                    elseif (#table1 >= 3) then 
                        data1 = "1. " .. table2[table1[#table1]] .. " - " .. table1[#table1] .. " points." 
                        data2 = "2. " .. table2[table1[#table1 - 1]] .. " - " .. table1[#table1 - 1] .. " points." 
                        data3 = "3. " .. table2[table1[#table1 - 2]] .. " - " .. table1[#table1 - 2] .. " points." 
                    end 
                    setTimer(outputChatBox,50,1,"Top 3 points:", root, 255,255,255) 
                    if (data1) then 
                        setTimer(outputChatBox,60,1, data1, root, 255,255,255) 
                    end 
                    if (data2) then 
                        setTimer(outputChatBox,70,1, data2, root, 255,255,255) 
                    end 
                    if (data3) then 
                        setTimer(outputChatBox,80,1, data3, root, 255,255,255) 
                    end 
                    table.remove( table1 ) 
                    table.remove( table2 ) 
end 
addCommandHandler("top", tables) 

Thanks in advance.

Link to comment
function tables() 
    outputChatBox("Top 3 points:", getRootElement(), 255,255,255) 
    for index, data in ipairs(sortAccounts()) do 
        outputChatBox("#"..tostring(index)..": ".. tostring(data.account) .." - ".. tostring(data.points), getRootElement(), 255, 255, 255) 
        if index == 3 then 
            break 
        end 
    end 
end 
addCommandHandler("top", tables) 
  
function sortAccounts() 
    local rowdata = {} 
    for index, account in pairs(getAccounts()) do 
        rowdata[index] = { 
            account = getAccountName(account), 
            points = getAccountData(account,"Points"), 
        } 
    end 
    local comparator = function (a, b)  
        return (tonumber(a.points) or 0) > (tonumber(b.points) or 0)  
    end 
    table.sort(rowdata, comparator) 
    return rowdata 
end 

Seems to be working.

Edited by Guest
Link to comment
function tables() 
    outputChatBox("Top 3 points:", getRootElement(), 255,255,255) 
    for index, data in pairs(sortAccounts()) do 
        outputChatBox("#"..tostring(index)..": ".. tostring(data.account) .." - ".. tostring(data.points), getRootElement(), 255, 255, 255) 
        if index == 3 then 
            break 
        end 
    end 
end 
addCommandHandler("top", tables) 
  
function sortAccounts() 
    local rowdata = {} 
    for index, account in pairs(getAccounts()) do 
        rowdata[index] = { 
            account = getAccountName(account), 
            points = getAccountData(account,"Points"), 
        } 
    end 
    local comparator = function (a, b)  
        return (tonumber(a.points) or 0) > (tonumber(b.points) or 0)  
    end 
    table.sort(rowdata, comparator) 
    return rowdata 
end 

Seems to be working.

I'd use ipairs in the tables() function since you want to sorted table to be iterated in the index order. pairs doesn't always iterate table as you'd expect.

Anyway, I'd also change the return value in sortAccounts() function to return just the top 3 results instead of returning the entire table. That will also save you some lines in the table() function (unnecessary if statement).

Link to comment
function tables() 
    outputChatBox("Top 3 points:", getRootElement(), 255,255,255) 
    for index, data in ipairs(sortAccounts()) do 
        outputChatBox("#"..tostring(index)..": ".. tostring(data.account) .." - ".. tostring(data.points), getRootElement(), 255, 255, 255) 
        if index == 3 then 
            break 
        end 
    end 
end 
addCommandHandler("top", tables) 
  
function sortAccounts() 
    local rowdata = {} 
    for index, account in pairs(getAccounts()) do 
        rowdata[index] = { 
            account = getAccountName(account), 
            points = getAccountData(account,"Points"), 
        } 
    end 
    local comparator = function (a, b)  
        return (tonumber(a.points) or 0) > (tonumber(b.points) or 0)  
    end 
    table.sort(rowdata, comparator) 
    return rowdata 
end 

Seems to be working.

Thank you! But this only gets account names, can it get player's names too?

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...