Jump to content

xml top list


Fury

Recommended Posts

hello guys, i made a xml stats system worked like this;

users.xml

<users> 
        <SERIAL_6218AAAD464C476132E8FC18695D2844 wins="6" points="200" second="2" third="0" nick="[Evo]Flooderne"></SERIAL_6218AAAD464C476132E8FC18695D2844> 
        <SERIAL_B4760ED9F491634C91ACEAF3DB31FCB2 wins="2" points="120" second="6" third="0" nick="[Evo]joshter50"></SERIAL_B4760ED9F491634C91ACEAF3DB31FCB2> 
  
.. 
.. 
..</users> 

is there any way to make a top wins/points/second/third list with nickname?

thanks for reading :)

ps: my gang is not :o it is F u c k N. fix that please :/

Link to comment
Guest Guest4401

--xml

>        wins="6" points="200" second="2" third="0" nick="[Evo]Flooderne">>        wins="2" points="160" second="6" third="0" nick="[Evo]joshter51">>        wins="7" points="120" second="3" third="0" nick="[Evo]joshter52">>        wins="1" points="140" second="9" third="0" nick="[Evo]joshter53">>        wins="9" points="150" second="7" third="0" nick="[Evo]joshter54">>>

--server side

local amountOfTopsToDisplay = 3 -- how many tops you need 
  
addCommandHandler("top", 
    function(player, command, value) 
        local file = xmlLoadFile("users.xml") -- load file 
        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, "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(file) -- unload the file from memory 
        end 
    end 
) 

Tested, works =)

Edited by Guest4401
Link to comment
Guest Guest4401
@karthik184: It would be nice if you would add comments, you know, what does each line, I guess it would be helpful for him.

Okay, done. :)

Link to comment
--server side
local amountOfTopsToDisplay = 3 -- how many tops you need 
  
addCommandHandler("top", 
    function(player, command, value) 
        local file = xmlLoadFile("users.xml") -- load file 
        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, "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 a.data>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(file) -- unload the file from memory 
        end 
    end 
) 

Tested, works =)

thank you so much :) i understand how table.sort works!

but in this code i have got this error;

top.lua:19: atempt to concantenate field 'data' (a boolean value) 

i dont know why i got this error :/

Link to comment
  • 8 months later...

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