Jump to content

Top xml


Memory

Recommended Posts

Hi, please, help me sort players of points in xml.

<users> 
    <user> 
        <SERIAL_XXXXXXXXXXXXXXXXXXXXXXXXXXX points="380" lastPlayerName="Player" money="6000" first="5" second="0" third="0"></SERIAL_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX> 
    </user> 
</users> 

Link to comment

Thanks, but code have error and I can't fix it.

Error ":20 atempt to concantenate field 'data' (a boolean value)"

local amountOfTopsToDisplay = 3 -- how many tops you need 
  
addCommandHandler("top", 
    function(player, command, value) 
        local fileindex = xmlLoadFile("users.xml") -- load file 
        local file = xmlNodeGetChildren(fileindex)  
        if file then -- if file exists 
            if value == "wins" or value == "points" or value == "second" or value == "third" then -- if syntax is right 
                local tableOrder = {} -- make a table 
                local subTable = {} -- make a sub table 
                for i,v in ipairs(xmlNodeGetChildren(fileindex)) do -- get children from file 
                    subTable.name = xmlNodeGetAttribute(v, "nick") -- name goes in subTable 
                    subTable.data = xmlNodeGetAttribute(v, value) -- data goes in subTable 
                    table.insert(tableOrder, subTable) -- subTable is inserted in bigger table 
                    subTable = {} -- empty subTable for next loop 
                end 
                table.sort(tableOrder, function(a,b) return tonumber(a.data) > tonumber(b.data) end) -- put data in order 
                outputChatBox("Displaying top "..value, player, 255,255,255,true) 
                for i,v in ipairs(tableOrder) do -- loop the sorted table  
                    outputChatBox(i..": "..v.name.." - "..v.data, player, 255,255,255,true) -- display in order 
                    if i == amountOfTopsToDisplay then break end -- when enough tops has been shown, break it 
                end 
            else 
                outputChatBox("Syntax : /top [wins|points|second|third]", player, 255,255,255,true) -- show the correct syntax if he got it wrong 
            end 
        xmlUnloadFile(fileindex) -- unload the file from memory 
        end 
    end 
) 

Link to comment

try this

local amountOfTopsToDisplay = 3 -- how many tops you need 
  
addCommandHandler("top", 
    function(player, command, value) 
        local fileindex = xmlLoadFile("users.xml") -- load file 
        local file = xmlNodeGetChildren(fileindex) 
        if file then -- if file exists 
            if value == "wins" or value == "points" or value == "second" or value == "third" then -- if syntax is right 
                local tableOrder = {} -- make a table 
                local subTable = {} -- make a sub table 
                for i,v in ipairs(xmlNodeGetChildren(fileindex)) do -- get children from file 
                    subTable.name = xmlNodeGetAttribute(v, "nick") -- name goes in subTable 
                    subTable.data = xmlNodeGetAttribute(v, value) -- data goes in subTable 
                    table.insert(tableOrder, subTable) -- subTable is inserted in bigger table 
                    subTable = {} -- empty subTable for next loop 
                end 
                table.sort(tableOrder, function(a,b) return tonumber(a.data) > tonumber(b.data) end) -- put data in order 
                outputChatBox("Displaying top "..value, player, 255,255,255,true) 
                for i,v in ipairs(tableOrder) do -- loop the sorted table 
                    outputChatBox(i..": "..v.name.." - "..a,b.data, player, 255,255,255,true) -- display in order 
                    if i == amountOfTopsToDisplay then break end -- when enough tops has been shown, break it 
                end 
            else 
                outputChatBox("Syntax : /top [wins|points|second|third]", player, 255,255,255,true) -- show the correct syntax if he got it wrong 
            end 
        xmlUnloadFile(fileindex) -- unload the file from memory 
        end 
    end 
) 

Link to comment

I fix it.

local amountOfTopsToDisplay = 3 -- how many tops you need 
  
addCommandHandler("top", 
    function(player, command, value) 
        fileindex = xmlLoadFile("users.xml") -- load file 
        file = xmlFindChild ( fileindex, "user", 0 )  
        if file then -- if file exists 
            if value == "wins" or value == "points" or value == "second" or value == "third" then -- if syntax is right 
                local tableOrder = {} -- make a table 
                local subTable = {} -- make a sub table 
                for i,v in ipairs(xmlNodeGetChildren(file)) do -- get children from file 
                    subTable.name = xmlNodeGetAttribute(v, "Name") -- name goes in subTable 
                    subTable.data = xmlNodeGetAttribute(v, value) -- data goes in subTable 
                    table.insert(tableOrder, subTable) -- subTable is inserted in bigger table 
                    subTable = {} -- empty subTable for next loop 
                end 
                table.sort(tableOrder, function(a,b) return tonumber(a.data) > tonumber(b.data) end) -- put data in order 
                outputChatBox("Displaying top "..value, player, 255,255,255,true) 
                for i,v in ipairs(tableOrder) do -- loop the sorted table 
                    outputChatBox(i..": "..v.name.." - "..v.data, player, 255,255,255,true) -- display in order 
                    if i == amountOfTopsToDisplay then break end -- when enough tops has been shown, break it 
                end 
            else 
                outputChatBox("Syntax : /top [wins|points|second|third]", player, 255,255,255,true) -- show the correct syntax if he got it wrong 
            end 
        xmlUnloadFile(fileindex) -- unload the file from memory 
        end 
    end 
) 

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