Jump to content

xml


myyusuf

Recommended Posts

Guest Guest4401

You mean you want to get those values? If yes you have to use this. Sorry if I misunderstood you.

xmlNodeGetAttribute 

and similarly use xmlNodeSetAttribute to save the new values.

Link to comment
You mean you want to get those values? If yes you have to use this. Sorry if I misunderstood you.
xmlNodeGetAttribute 

and similarly use xmlNodeSetAttribute to save the new values.

thank you.

local xml = xmlLoadFile("data/ratings.xml") 
local list = xmlFindChild ( xml, "list", 0 ) 
local map = xmlFindChild ( list, "map", 0 ) 
local rating = xmlNodeGetAttribute(list, "rating") 
local ratedBy = xmlNodeGetAttribute(list, "ratedBy") 
local played = xmlNodeGetAttribute(list, "played") 
outputChatBox ( "rated: " ..rating.. " ratedBy: " ..ratedBy.. " played " ..played, getRootElement(), 255, 0, 0, true ) 

is that right for my xml file?

Link to comment
Guest Guest4401

No. This is the right code

  
local xml = xmlLoadFile("data/ratings.xml") 
local map = xmlFindChild ( xml, "map", 0 ) 
local rating = xmlNodeGetAttribute(map, "rating") 
local ratedBy = xmlNodeGetAttribute(map, "ratedBy") 
local played = xmlNodeGetAttribute(map, "played") 
outputChatBox ( "rated: " ..rating.. " ratedBy: " ..ratedBy.. " played " ..played, getRootElement(), 255, 0, 0, true ) 

Link to comment
No. This is the right code

local xml = xmlLoadFile("data/ratings.xml") 

local map = xmlFindChild ( xml, "map", 0 )

local rating = xmlNodeGetAttribute(map, "rating")

local ratedBy = xmlNodeGetAttribute(map, "ratedBy")

local played = xmlNodeGetAttribute(map, "played")

outputChatBox ( "rated: " ..rating.. " ratedBy: " ..ratedBy.. " played " ..played, getRootElement(), 255, 0, 0, true )

thank you. i have another problem now.

<list> 

    <map id="race-[dd]2012" rating="8.25" ratedBy="4" played="186"></map>

    <map id="race-[dd]ananzaaa" rating="5.25" ratedBy="9" played="25"></map>

    ..

    ..

    ..

</list>

addEventHandler ( 'onGamemodeMapStart', root, 

    function ( uMapStart )    

       local mapname = getResourceName( uMapStart )

    end

)

local xml = xmlLoadFile("data/ratings.xml")

local map = xmlFindChild ( xml, "map", 0 )

local id = xmlNodeGetAttribute(map, "id")

if id == mapname then

local rating = xmlNodeGetAttribute(map, "rating")

local ratedBy = xmlNodeGetAttribute(map, "ratedBy")

local played = xmlNodeGetAttribute(map, "played")

outputChatBox ( "rated: " ..rating.. " ratedBy: " ..ratedBy.. " played " ..played, getRootElement(), 255, 0, 0, true )

else

outputChatBox ( "FAIL", getRootElement(), 255, 0, 0, true )

i wanna do something like that. if id is map folder name, then take data from the map node. is that code right?

Link to comment
  
  
  
addEventHandler ( 'onGamemodeMapStart', root, 
 function ( uMapStart )     
     
     
    local mapname = getResourceName( uMapStart ) 
    local xml = xmlLoadFile("data/ratings.xml") 
    local map = xmlFindChild ( xml, "map", 0 ) 
    local id = xmlNodeGetAttribute(map, "id") 
    if id == mapname then 
    local rating = xmlNodeGetAttribute(map, "rating") 
    local ratedBy = xmlNodeGetAttribute(map, "ratedBy") 
    local played = xmlNodeGetAttribute(map, "played") 
    outputChatBox ( "rated: "..rating.." ratedBy: "..ratedBy.." played "..played, getRootElement(), 255, 0, 0, true ) 
    else 
    outputChatBox ( "FAIL", getRootElement(), 255, 0, 0, true ) 
    end 
end 
) 
  

Link to comment
  
  
  
addEventHandler ( 'onGamemodeMapStart', root, 
 function ( uMapStart )     
     
     
    local mapname = getResourceName( uMapStart ) 
    local xml = xmlLoadFile("data/ratings.xml") 
    local map = xmlFindChild ( xml, "map", 0 ) 
    local id = xmlNodeGetAttribute(map, "id") 
    if id == mapname then 
    local rating = xmlNodeGetAttribute(map, "rating") 
    local ratedBy = xmlNodeGetAttribute(map, "ratedBy") 
    local played = xmlNodeGetAttribute(map, "played") 
    outputChatBox ( "rated: "..rating.." ratedBy: "..ratedBy.." played "..played, getRootElement(), 255, 0, 0, true ) 
    else 
    outputChatBox ( "FAIL", getRootElement(), 255, 0, 0, true ) 
    end 
end 
) 
  

not worked and no error :/

Link to comment
addEventHandler ( 'onGamemodeMapStart', root, 
    function ( uMapStart ) 
        local mapname = getResourceName( uMapStart ) 
        local xml = xmlLoadFile("data/ratings.xml") 
        if ( xml ) then 
            local index = findIndexByName ( mapname ) 
            if ( index ) then 
                local map = xmlFindChild ( xml, "map", index ) 
                local rating = xmlNodeGetAttribute ( map, "rating" ) 
                local ratedBy = xmlNodeGetAttribute ( map, "ratedBy" ) 
                local played = xmlNodeGetAttribute ( map, "played" ) 
                outputChatBox ( "rated: ".. rating .." ratedBy: ".. ratedBy .." played ".. played, getRootElement(), 255, 0, 0, true ) 
            else 
                outputChatBox ( "Map not found in our ratings list.", getRootElement(), 255, 0, 0, true ) 
            end 
        else 
            outputChatBox ( "Unable to load map ratings XML file.", getRootElement(), 255, 0, 0, true ) 
        end 
    end 
) 
  
function findIndexByName ( mapName ) 
    local ratingsFile = xmlLoadFile("data/ratings.xml") 
    if ( ratingsFile ) then 
        for index, map in ipairs ( xmlNodeGetChildren ( ratingsFile ) ) do 
            local name = xmlNodeGetAttribute ( map, "id" ) 
            if ( name == mapName ) then 
                return index -1 
            end 
        end 
        xmlUnloadFile(ratingsFile) 
    end 
end 

I tested it and it works.

Link to comment
addEventHandler ( 'onGamemodeMapStart', root, 
    function ( uMapStart ) 
        local mapname = getResourceName( uMapStart ) 
        local xml = xmlLoadFile("data/ratings.xml") 
        if ( xml ) then 
            local index = findIndexByName ( mapname ) 
            if ( index ) then 
                local map = xmlFindChild ( xml, "map", index ) 
                local rating = xmlNodeGetAttribute ( map, "rating" ) 
                local ratedBy = xmlNodeGetAttribute ( map, "ratedBy" ) 
                local played = xmlNodeGetAttribute ( map, "played" ) 
                outputChatBox ( "rated: ".. rating .." ratedBy: ".. ratedBy .." played ".. played, getRootElement(), 255, 0, 0, true ) 
            else 
                outputChatBox ( "Map not found in our ratings list.", getRootElement(), 255, 0, 0, true ) 
            end 
        else 
            outputChatBox ( "Unable to load map ratings XML file.", getRootElement(), 255, 0, 0, true ) 
        end 
    end 
) 
  
function findIndexByName ( mapName ) 
    local ratingsFile = xmlLoadFile("data/ratings.xml") 
    if ( ratingsFile ) then 
        for index, map in ipairs ( xmlNodeGetChildren ( ratingsFile ) ) do 
            local name = xmlNodeGetAttribute ( map, "id" ) 
            if ( name == mapName ) then 
                return index -1 
            end 
        end 
        xmlUnloadFile(ratingsFile) 
    end 
end 

I tested it and it works.

thank you so much dude. :)

Link to comment

server

addEventHandler ( 'onGamemodeMapStart', root, 
    function ( uMapStart ) 
        local mapname = getResourceName( uMapStart ) 
        local xml = xmlLoadFile("data/ratings.xml") 
        if ( xml ) then 
            local index = findIndexByName ( mapname ) 
            if ( index ) then 
                local map = xmlFindChild ( xml, "map", index ) 
                local rating = xmlNodeGetAttribute ( map, "rating" ) 
                local ratedBy = xmlNodeGetAttribute ( map, "ratedBy" ) 
                local played = xmlNodeGetAttribute ( map, "played" ) 
                triggerClientEvent(source,"INFO",thePlayer,rating,ratedBy,played) 
                outputChatBox ( "rated: ".. rating .." ratedBy: ".. ratedBy .." played ".. played, getRootElement(), 255, 0, 0, true ) 
            else 
                outputChatBox ( "Map not found in our ratings list.", getRootElement(), 255, 0, 0, true ) 
            end 
        else 
            outputChatBox ( "Unable to load map ratings XML file.", getRootElement(), 255, 0, 0, true ) 
        end 
    end 
) 
  
function findIndexByName ( mapName ) 
    local ratingsFile = xmlLoadFile("data/ratings.xml") 
    if ( ratingsFile ) then 
        for index, map in ipairs ( xmlNodeGetChildren ( ratingsFile ) ) do 
            local name = xmlNodeGetAttribute ( map, "id" ) 
            if ( name == mapName ) then 
                return index -1 
            end 
        end 
        xmlUnloadFile(ratingsFile) 
    end 
end 
  
  

Client

local screenWidth,screenHeight = guiGetScreenSize() 
  
function drawningText (thePlayer,rating,ratedBy,played) 
    if rating and ratedBy and played then 
        dxDrawText ( "Rate: " ..rating.. "/10",screenWidth/2-150,screenHeight/2+275,684.0,647.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false)  
        dxDrawText ( "(by " ..ratedBy.. " players)",screenWidth/2-150,screenHeight/2+275,684.0,647.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
        dxDrawText ( "Times played: "..played.." times",screenWidth/2-150,screenHeight/2+250,636.0,675.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false)     
    else 
        dxDrawText ( "Never rating before",screenWidth/2-150,screenHeight/2+275,684.0,647.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false)  
        dxDrawText ( "",screenWidth/2-150,screenHeight/2+275,684.0,647.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
        dxDrawText ( "Never played before",screenWidth/2-150,screenHeight/2+250,636.0,675.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    end 
end 
addEvent( "INFO", true ) 
addEventHandler( "INFO", getRootElement(), drawningText ) 

Link to comment
Guest Guest4401

At line 66 of your clientside code, in the function 'drawningText', you have used dxDrawFunctions with an event "INFO".

Shouldn't it be onClientRender if you want to keep showing those dx texts?

EDIT : The simplest solution is to just split the "INFO" event and receive those information. Then use onClientRender to display it using dx functions.

Link to comment

server

local lastTimePlayed 
local month = {"January","February","March","April","May","June","Juli","August","September","October","November","December"} 
  
function timestampToDate(stamp) 
    local time = getRealTime(stamp) 
    return string.format("%d %s %02d:%02d",time.monthday,month[time.month+1],time.hour,time.minute) 
end 
  
addEventHandler ( 'onMapStarting', root, 
    function ( map.Info ) 
        local mapname = getResourceInfo(exports.mapmanager:getRunningGamemodeMap(),"name") 
        local author = getResourceInfo(exports.mapmanager:getRunningGamemodeMap(),"author") 
        local lastTimePlayed = timestampToDate(mapInfo.lastTimePlayed) 
        local xml = xmlLoadFile("data/ratings.xml") 
        if ( xml ) then 
            local index = findIndexByName ( mapname ) 
            if ( index ) then 
                local map = xmlFindChild ( xml, "map", index ) 
                local rating = xmlNodeGetAttribute ( map, "rating" ) 
                local ratedBy = xmlNodeGetAttribute ( map, "ratedBy" ) 
                local played = xmlNodeGetAttribute ( map, "played" ) 
                triggerClientEvent(source,"INFO",getRootElement(),mapname,author,rating,ratedBy,played,lastTimePlayed) 
            else 
                outputChatBox ( "Map not found in our ratings list.", getRootElement(), 255, 0, 0, true ) 
            end 
        else 
            outputChatBox ( "Unable to load map ratings XML file.", getRootElement(), 255, 0, 0, true ) 
        end 
    end 
) 
  
function findIndexByName ( mapName ) 
    local ratingsFile = xmlLoadFile("data/ratings.xml") 
    if ( ratingsFile ) then 
        for index, map in ipairs ( xmlNodeGetChildren ( ratingsFile ) ) do 
            local name = xmlNodeGetAttribute ( map, "id" ) 
            if ( name == mapName ) then 
                return index -1 
            end 
        end 
        xmlUnloadFile(ratingsFile) 
    end 
end 
  
  

Client

local screenWidth, screenHeight = guiGetScreenSize() 
local endPosition = screenHeight-140 
local moveback = false 
  
function getDrawText(mapname,author,rating,ratedBy,played,lastTimePlayed) 
    addEventHandler ( "onClientRender", getRootElement(), drawMapInfo) 
    if mapname then 
        textM = "Map: "..mapname 
    else 
        textM = "Unknown" 
    end 
    if author then 
        textA = "Author: "..author 
    else 
        textA = "Unknown" 
    end 
    if rating then 
        textR = "Rate: " ..rating.. "/10 (by " ..ratedBy.. " players)" 
    else 
        textR = "Not rated yet" 
    end 
    if played then 
        textP = "Times played: "..played.." times" 
    else 
        textP = "Never played before" 
    end 
    if lastTimePlayed then 
        textPL = "Last Time Played\n"..lastTimePlayed 
    else 
        textPL = "Unknown" 
    end 
end 
addEvent( "INFO", true ) 
addEventHandler( "INFO", getRootElement(), getDrawText ) 
  
function drawMapInfo () 
    if position == endPosition then 
        if tickCount == 0 then 
            moveback = true 
        end 
        tickCount = tickCount-1 
        movePosition = 0 
    end 
    if position >= screenHeight+91 then 
        removeEventHandler("onClientRender",getRootElement(),drawMapInfo) 
    end 
    if moveback then 
        movePosition = -2.5 
        alpha = alpha - 10 
    end 
    alpha = alpha + 5 
    if alpha > 255 then 
        alpha = 255 
    elseif alpha < 0 then 
        alpha = 0 
    end 
    local heighResize = dxGetFontHeight(1.15, "default-bold") 
    local widthRezise1 = dxGetTextWidth("Map - "..name,1.15,"default-bold") 
    local widthRezise2 = dxGetTextWidth("Lasttime played - "..lastTimePlayed,1.15,"default-bold") 
    local widthRezise3 = dxGetTextWidth("By - "..author,1.15,"default-bold") 
    if widthRezise1 > widthRezise2 and widthRezise1 > widthRezise3 then 
        widthRezise = widthRezise1 
    elseif  widthRezise2 > widthRezise1 and widthRezise2 > widthRezise3 then 
        widthRezise = widthRezise2 
    elseif  widthRezise3 > widthRezise1 and widthRezise3 > widthRezise2 then 
        widthRezise = widthRezise3 
    end 
    dxDrawText ( textR,screenWidth/2-150,screenHeight/2+275,684.0,647.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    dxDrawText ( textP,screenWidth/2-150,screenHeight/2+250,636.0,675.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false)     
    dxDrawImage(screenWidth/2-200,screenHeight/2+185,420.0,176.0, "images/info_bgr.png",0,0,0,tocolor(200,200,200,alpha)) 
    dxDrawText ( textM,screenWidth/2-150,screenHeight/2+200,636.0,675.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    dxDrawText ( textA,screenWidth/2-150,screenHeight/2+225.0,636.0,619.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    dxDrawText ( textPL,screenWidth/2-150,screenHeight/2+300,684.0,647.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    dxDrawText ( "Mode: Destruction Derby",screenWidth/2-150,screenHeight/2+325,506.0,699.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    position = position - movePosition 
end 
  

Link to comment

Server-side:

local lastTimePlayed 
local month = {"January","February","March","April","May","June","Juli","August","September","October","November","December"} 
  
function timestampToDate(stamp) 
    local time = getRealTime(stamp) 
    return string.format("%d %s %02d:%02d",time.monthday,month[time.month+1],time.hour,time.minute) 
end 
  
addEventHandler ( 'onMapStarting', root, 
    function ( mapInfo ) 
        local mapname = getResourceInfo(exports.mapmanager:getRunningGamemodeMap(),"name") 
        local author = getResourceInfo(exports.mapmanager:getRunningGamemodeMap(),"author") 
        local lastTimePlayed = timestampToDate(mapInfo.lastTimePlayed) 
        local xml = xmlLoadFile("data/ratings.xml") 
        if ( xml ) then 
            local index = findIndexByName ( mapname ) 
            if ( index ) then 
                local map = xmlFindChild ( xml, "map", index ) 
                local rating = xmlNodeGetAttribute ( map, "rating" ) 
                local ratedBy = xmlNodeGetAttribute ( map, "ratedBy" ) 
                local played = xmlNodeGetAttribute ( map, "played" ) 
                triggerClientEvent(source,"INFO",getRootElement(),mapname,author,rating,ratedBy,played,lastTimePlayed) 
            else 
                outputChatBox ( "Map not found in our ratings list.", getRootElement(), 255, 0, 0, true ) 
            end 
        else 
            outputChatBox ( "Unable to load map ratings XML file.", getRootElement(), 255, 0, 0, true ) 
        end 
    end 
) 
  
function findIndexByName ( mapName ) 
    local ratingsFile = xmlLoadFile("data/ratings.xml") 
    if ( ratingsFile ) then 
        for index, map in ipairs ( xmlNodeGetChildren ( ratingsFile ) ) do 
            local name = xmlNodeGetAttribute ( map, "id" ) 
            if ( name == mapName ) then 
                return index -1 
            end 
        end 
        xmlUnloadFile(ratingsFile) 
    end 
end 
  
  

Client-side:

local screenWidth, screenHeight = guiGetScreenSize() 
local endPosition = screenHeight-140 
local moveback = false 
  
function getDrawText(mapname,author,rating,ratedBy,played,lastTimePlayed) 
    addEventHandler ( "onClientRender", getRootElement(), drawMapInfo) 
    if mapname then 
        textM = "Map: "..mapname 
    else 
        textM = "Unknown" 
    end 
    if author then 
        textA = "Author: "..author 
    else 
        textA = "Unknown" 
    end 
    if rating then 
        textR = "Rate: " ..rating.. "/10 (by " ..ratedBy.. " players)" 
    else 
        textR = "Not rated yet" 
    end 
    if played then 
        textP = "Times played: "..played.." times" 
    else 
        textP = "Never played before" 
    end 
    if lastTimePlayed then 
        textPL = "Last Time Played\n"..lastTimePlayed 
    else 
        textPL = "Unknown" 
    end 
end 
addEvent( "INFO", true ) 
addEventHandler( "INFO", getRootElement(), getDrawText ) 
  
function drawMapInfo () 
    if position == endPosition then 
        if tickCount == 0 then 
            moveback = true 
        end 
        tickCount = tickCount-1 
        movePosition = 0 
    end 
    if position >= screenHeight+91 then 
        removeEventHandler("onClientRender",getRootElement(),drawMapInfo) 
    end 
    if moveback then 
        movePosition = -2.5 
        alpha = alpha - 10 
    end 
    alpha = alpha + 5 
    if alpha > 255 then 
        alpha = 255 
    elseif alpha < 0 then 
        alpha = 0 
    end 
    local heighResize = dxGetFontHeight(1.15, "default-bold") 
    local widthRezise1 = dxGetTextWidth("Map - "..name,1.15,"default-bold") 
    local widthRezise2 = dxGetTextWidth("Lasttime played - "..lastTimePlayed,1.15,"default-bold") 
    local widthRezise3 = dxGetTextWidth("By - "..author,1.15,"default-bold") 
    if widthRezise1 > widthRezise2 and widthRezise1 > widthRezise3 then 
        widthRezise = widthRezise1 
    elseif  widthRezise2 > widthRezise1 and widthRezise2 > widthRezise3 then 
        widthRezise = widthRezise2 
    elseif  widthRezise3 > widthRezise1 and widthRezise3 > widthRezise2 then 
        widthRezise = widthRezise3 
    end 
    dxDrawText ( textR,screenWidth/2-150,screenHeight/2+275,684.0,647.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    dxDrawText ( textP,screenWidth/2-150,screenHeight/2+250,636.0,675.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false)     
    dxDrawImage(screenWidth/2-200,screenHeight/2+185,420.0,176.0, "images/info_bgr.png",0,0,0,tocolor(200,200,200,alpha)) 
    dxDrawText ( textM,screenWidth/2-150,screenHeight/2+200,636.0,675.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    dxDrawText ( textA,screenWidth/2-150,screenHeight/2+225.0,636.0,619.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    dxDrawText ( textPL,screenWidth/2-150,screenHeight/2+300,684.0,647.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    dxDrawText ( "Mode: Destruction Derby",screenWidth/2-150,screenHeight/2+325,506.0,699.0,tocolor(255,255,255,255),1.5,"default-bold","left","top",false,false,false) 
    position = position - movePosition 
end 
  

Link to comment

myyusuf, Learn lua again if you not understand.

viewtopic.php?f=148&t=40809

  
local nVar -- nil 
  
print( nVar ) -- nil 
print( nVar + 1 ) -- attempt to perform arithmetic on local 'nVar' (a nil value) 
  

  
print( nNotDefined ) -- nil 
print( nNotDefined + 1 ) -- attempt to perform arithmetic on global 'nNotDefined' (a nil value) 
  

So you need define variable.

Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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