Advertisement
Bolodefchoco_LUAXML

[Function] xml attributes

Jan 27th, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 27/01/2017
  3. --Last update: 27/01/2017
  4. --[[ Notes:
  5.     Example:
  6.         eventNewGame = function()
  7.             local currentXml = xml.addAttrib(tfm.get.room.xmlMapInfo.xml or "",{{tag="BD",value=""},{tag="BN",value="1"}},false)
  8.             xml.attribFunc(currentXml or "",{
  9.                 [1] = {
  10.                     attribute = "meep",
  11.                     func = function()
  12.                         for n in next,tfm.get.room.playerList do
  13.                             tfm.exec.giveMeep(n)
  14.                         end
  15.                     end
  16.                 },
  17.                 [2] = {
  18.                     attribute = "cheese",
  19.                     func = function()
  20.                         for n in next,tfm.get.room.playerList do
  21.                             tfm.exec.giveCheese(n)
  22.                         end
  23.                     end
  24.                 },
  25.                 [3] = {
  26.                     attribute = "vamp",
  27.                     func = function(player)
  28.                         tfm.exec.movePlayer(player,20,30)
  29.                         tfm.exec.setVampirePlayer(player)
  30.                     end
  31.                 },
  32.                 [4] = {
  33.                     attribute = "mapname",
  34.                     func = function(text)
  35.                         ui.setMapName(text)
  36.                     end
  37.                 },
  38.                 [5] = {
  39.                     attribute = "BN",
  40.                     func = function(text)
  41.                         ui.setShamanName(text)
  42.                     end
  43.                 },
  44.             })
  45.         end
  46.  
  47.         xmlmap = '<C><P meep="" vamp="Bolodefchoco" cheese="" l="a" b="123,546;12" teste="" d="213" D="5156,5" /><Z><S><S L="930" H="62" X="426" Y="420" T="0" P="0,0,0.3,0.2,0,0,0,0" /></S><D /><O /></Z></C>'
  48.         tfm.exec.newGame(xmlmap)
  49. ]]--
  50.  
  51. xml = {}
  52. xml.parse = function(currentXml)
  53.     currentXml = currentXml:match("<P (.-)/>") or ""
  54.     local out = {}
  55.     for tag,_,value in currentXml:gmatch("([%-%w]+)=([\"'])(.-)%2") do
  56.         out[tag] = value
  57.     end
  58.     return out
  59. end
  60. xml.attribFunc = function(currentXml,funcs)
  61.     local attributes = xml.parse(currentXml)
  62.     for k,v in next,funcs do
  63.         if attributes[v.attribute] then
  64.             v.func(attributes[v.attribute])
  65.         end
  66.     end
  67. end
  68. xml.addAttrib = function(currentXml,out,launch)
  69.     local parameters = currentXml:match("<P (.-)/>") or ""
  70.     for k,v in next,out do
  71.         if not parameters:find(v.tag) then
  72.             currentXml = currentXml:gsub("<P (.-)/>",function(attribs)
  73.                 return string.format("<P %s=\"%s\" %s/>",v.tag,v.value,attribs)
  74.             end)
  75.         end
  76.     end
  77.     if launch then
  78.         tfm.exec.newGame(currentXml)
  79.     else
  80.         return currentXml
  81.     end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement