LoRData

--

```
local p         = {}
local lib       = require('Module:Feature')
local userError = require('Dev:User error')
local json      = require('Module:JSON')

local regions   = {
    ["BW"]              = "Bilgewater",
    ["DE"]              = "Demacia",
    ["FR"]              = "Freljord",
    ["IO"]              = "Ionia",
    ["NX"]              = "Noxus",
    ["PZ"]              = "Piltover & Zaun",
    ["IS"]              = "Ilha das Sombras",
    ["MT"]              = "Targon",
    ["Bilgewater"]      = "BW",
    ["Demacia"]         = "DE",
    ["Freljord"]        = "FR",
    ["Ionia"]           = "IO",
    ["Noxus"]           = "NX",
    ["Piltover & Zaun"] = "PZ",
    ["Ilha das Sombras"]= "IS",
    ["Targon"]          = "MT"
}

function p.cardData()
    local titleobject = mw.title.new('User:Ninjamask/Sandbox/LoR.json')
    local o           = json.decode(titleobject:getContent())
    local s           = ""
    local cardData    = {}
    local lang          = mw.language.new( "en" )

    for _, data in pairs(o) do
        table.insert(cardData, data)
    end
    table.sort(cardData, function(a, b) return a.cardCode < b.cardCode end)

    -- ============================
    -- = Generate lua data output =
    -- ============================

    s = s .. '-- <pre>\n'
    s = s .. '-- Legends of Runeterra data \n'
    s = s .. '\n'
    s = s .. 'return {\n'

    for i, card in pairs(cardData) do
        -- StatTable data
        local sLoop = ""
        sLoop = sLoop .. '    ["' .. card.cardCode .. '"] = {\n'
        sLoop = sLoop .. '        ["name"]        = "'     .. card.name                  .. '",\n'
        sLoop = sLoop .. '        ["type"]        = "'     .. card.type                  .. '",\n'
        sLoop = sLoop .. '        ["rarity"]      = "'     .. lang:ucfirst(lang:lc(card.rarity))                .. '",\n'

        if card.subtype ~= "" then
            sLoop = sLoop .. '        ["subtype"]     = "' .. lang:ucfirst(lang:lc(card.subtype))               .. '",\n'
        end

        if card.supertype ~= "" then
            sLoop = sLoop .. '        ["supertype"]   = "' .. card.supertype             .. '",\n'
        end

        -- if card.spellSpeed ~= "" then
        --     sLoop = sLoop .. '        ["spellSpeed"]     = "' .. card.spellSpeed            .. '",\n'
        -- end

        local sKeywords = '{"'
        sKeywords = sKeywords .. table.concat(card.keywords, '", "') .. '"}'
        if sKeywords ~= '{""}' then
            sLoop = sLoop .. '        ["keywords"]    = '  .. lib.tbl_debug(card.keywords)     ..  ',\n'
        end

        sLoop = sLoop .. '        ["collectible"] = '      .. tostring(card.collectible) ..  ',\n'
        sLoop = sLoop .. '        ["cost"]        = '      .. card.cost                  ..  ',\n'
        sLoop = sLoop .. '        ["power"]       = '      .. card.attack                ..  ',\n'
        sLoop = sLoop .. '        ["health"]      = '      .. card.health                ..  ',\n'
        if card.descriptionRaw ~= "" then
            sLoop = sLoop .. '        ["desc"]        = "' .. string.gsub(string.gsub(string.gsub(card.descriptionRaw, '\"', '\\"'), "\n", "\\n"), "\r\\n", "\\n") .. '",\n'
        end
        if card.levelupDescriptionRaw ~= "" then
            sLoop = sLoop .. '        ["lvldesc"]     = "' .. string.gsub(string.gsub(string.gsub(card.levelupDescriptionRaw, '\"', '\\"'), "\n", "\\n"), "\r\\n", "\\n") .. '",\n'
        end
        sLoop = sLoop .. '        ["flavor"]      = "'     .. string.gsub(string.gsub(string.gsub(card.flavorText, '\"', '\\"'), "\n", "\\n"), "\r\\n", "\\n") .. '",\n'
        sLoop = sLoop .. '        ["artist"]      = "'     .. card.artistname            .. '"\n'
        sLoop = sLoop .. '    },\n'

        s = s .. sLoop
    end

    s = s .. '}\n'
    s = s .. '-- </' .. 'pre>\n' -- pre needs to be splitted with, because of Lua weirdness
    s = s .. '-- [[Category:Lua]]'

    return '<pre>' .. mw.text.nowiki(s) .. '
```
'

end

function p.getRosters(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   local lorData   = mw.loadData('Module:LoRData/data')
   local lang      = mw.language.new( "en" )
   local tablenode = mw.html.create('table')
   local s_set  = tonumber(args["set"]) or nil
   local s_region  = args["region"] or nil

   -- START
   local cardtable = {}
   for cardcode in pairs(lorData) do
       table.insert(cardtable, cardcode)
   end
   table.sort(cardtable)

   local championtable = {}

   for i, cardcode in pairs(cardtable) do
       repeat
           local t        = lorData[cardcode]

           if t.rarity ~= 'Champion' then
               break
           end

           table.insert(championtable, t.name)

           break
       until true
   end
   -- END
   return championtable

```

end

function p.cardlist(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   local lorData       = mw.loadData('Module:LoRData/data')
   local lang          = mw.language.new( "en" )
   local tablenode     = mw.html.create('table')
   local s_set         = tonumber(args["set"]) or nil
   local s_region      = args["region"] or nil
   local s_type        = args["type"] or nil
   local s_keyword     = args["keyword"] or nil
   local s_entire      = not not args['entire'] or nil
   local s_collectible = args['collectible']
   local s_alternate   = not not args['alternate'] or nil
   local s_category    = args["category"] or nil

   if s_collectible ~= nil then
       if s_collectible == 'any' then
           s_collectible = nil
       else
           local s_collectible_string = tostring(s_collectible)
           if s_collectible_string == "false" then
               s_collectible = false
           elseif s_collectible_string == "true" then
               s_collectible = true
           else
               return 'Collectible "' .. s_collectible_string .. '" is not in the data.'
           end
       end
   end

   tablenode
       :addClass('sortable article-table nopadding sticky-header')
       :css('width','100%')
       :css('text-align','center')
       :newline()
       :tag('tr')
           :tag('th')
           :done()
           :newline()
           :tag('th')
               :wikitext('Nome')
           :done()
           :newline()
           :tag('th')
               :wikitext('Tipo')
           :done()
           :newline()
           :tag('th')
               :wikitext('Subtipo')
           :done()
           :newline()
           :tag('th')
               :wikitext('Região')
           :done()
           :newline()
           :tag('th')
               :css('width','20px')
               :tag('span')
                   :attr('title','Cost')
                   :wikitext('')
               :done()
           :done()
           :newline()
           :tag('th')
               :css('width','20px')
               :tag('span')
                   :attr('title','Power')
                   :wikitext('')
               :done()
           :done()
           :newline()
           :tag('th')
               :css('width','20px')
               :tag('span')
                   :attr('title','Health')
                   :wikitext('')
               :done()
           :done()
           :newline()
           :tag('th')
               :wikitext('Raridade')
           :done()
       :done()
       :newline()

       -- START
       local cardtable = {}
       for cardcode in pairs(lorData) do
           table.insert(cardtable, cardcode)
       end
       table.sort(cardtable)

       local hasResult = false

       for i, cardcode in pairs(cardtable) do
           repeat
               local t        = lorData[cardcode]
               local cardnode = mw.html.create('tr')
               local region   = p.getRegionFromCardcode{cardcode}
               local set      = p.getSetFromCardcode{cardcode}

               local art_cardcode = cardcode

               if s_entire ~= true and t.removed == true then
                   break
               end

```

```
               if s_region ~= nil and s_region ~= region then
                   break
               end

               if s_set ~= nil and s_set ~= set then
                   break
               end

               -- Set 0 (Tutorial) is only taken when the "set" param is set
               if s_set == nil and set == 0  then
                   break
               end

               if s_type ~= nil then
                   if s_type == 'Follower' then
                       if t.type ~= 'Unit' or t.supertype == 'Champion' then
                           break
                       end
                   elseif s_type == 'Champion' then
                       if t.type ~= 'Unit' or t.supertype ~= 'Champion' then
                           break
                       end
                   elseif s_type ~= t.type then
                       break
                   end
               end

               -- Using keywordRefs instead of keywords attribute
               if s_keyword ~= nil then
                   local k_found = false

                   if not t.keywordRefs then
                       break
                   else
                       for _, keyword in pairs(t.keywordRefs) do
                           if s_keyword == keyword then
                               k_found = true
                               break
                           end
                       end
                   end
                   if k_found == false then
                       break
                   end
               end

               if s_collectible ~= nil and s_collectible ~= t.collectible then
                   break
               end

               if s_alternate ~= nil and s_alternate ~= t.alternate then
                   break
               elseif s_alternate == true and t.alternate == true then
                   art_cardcode = art_cardcode .. '-alt'
               end

               if s_category ~= nil then
                   local c_found = false

                   if not t.categoryRefs then
                       break
                   else
                       for _, category in pairs(t.categoryRefs) do
                           if s_category == category then
                               c_found = true
                               break
                           end
                       end
                   end
                   if c_found == false then
                       break
                   end
               end

               hasResult = true

```

```
               -- Image
               cardnode
                   :tag('td')
                       :addClass('lor-tooltip')
                       :attr('data-sort-value', art_cardcode)
                       :attr('data-param', art_cardcode)
                       :wikitext('Arquivo:' .. art cardcode .. '.png
' .. cardcode)
                   :done()

               -- Cardname
               cardnode
                   :tag('td')
                       :css('text-align', 'left')
                       :wikitext('' .. name .. '')
                   :done()

               -- Type
               cardnode
                   :tag('td')
                       :css('text-align', 'left')
                       :wikitext(t.type)
                   :done()

               -- Subtype
               cardnode
                   :tag('td')
                       :css('text-align', 'left')
                       :wikitext(t.subtype)
                   :done()

               -- Region
               local regionText =
               if region ~= nil then
                   regionText = ' .. region .. ''
               end
               cardnode
                   :tag('td')
                       :css('text-align', 'left')
                       :wikitext(regionText)
                   :done()

               -- Cost
               cardnode
                   :tag('td')
                       :css('text-align', 'left')
                       :wikitext(t.cost)
                   :done()

               -- Power
               cardnode
                   :tag('td')
                       :css('text-align', 'left')
                       :wikitext(t.power)
                   :done()

               -- Health
               cardnode
                   :tag('td')
                       :css('text-align', 'left')
                       :wikitext(t.health)
                   :done()

               -- Rarity
               local rarity = t.rarity
               if rarity ~= 'None' then
                   rarity = 'Arquivo:' .. rarity .. ' rarity.svg ' .. rarity
               else
                   rarity =
               end
               cardnode
                   :tag('td')
                       :css('text-align', 'left')
                       :wikitext(rarity)
                   :done()

               -- Add card row to the table
               tablenode
                   :newline()
                   :node(cardnode)

               break
           until true
       end
       -- END

       if hasResult == false then
           local cardnode = mw.html.create('tr')
           cardnode
               :tag('td')
               :attr('colspan', 9)
               :wikitext('No cards found')

           tablenode
               :newline()
               :node(cardnode)
       end

   tablenode:allDone()
   return tostring(tablenode)

```

end

function p.findCardsByname(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   local lorData     = mw.loadData('Module:LoRData/data')
   local search      = args['search'] or args[1]
   local entire      = args['entire'] or false
   local alt         = args['alt'] or nil
   local artwork     = args['artwork'] or nil
   local firstOnly   = args['firstOnly'] or nil

   local cardtable   = {}
   local resulttable = {}

```

```
   for cardcode in pairs(lorData) do
       table.insert(cardtable, cardcode)
   end
   table.sort(cardtable)

   local tutorialtable = {};

   for _, cardcode in ipairs(cardtable) do
       local rCardcode = cardcode
       if alt == 'true' then
           rCardcode = rCardcode .. '-alt'
       end
       if artwork == 'true' then
           rCardcode = rCardcode .. '-full'
       end
       -- Set 0 (Tutorial) is only taken when there is an "entire" param set
       if entire == false and p.getSetFromCardcode{cardcode=cardcode} == 0 then
           if search == lorData[cardcode].name then
               table.insert(tutorialtable, rCardcode)
           end
       else
           if search == lorData[cardcode].name then
               table.insert(resulttable, rCardcode)
               if firstOnly then
                   break
               end
           end
       end
   end

   -- If it is a tutorial-only card of the tutorial, it will be taken
   if #resulttable == 0 and #tutorialtable ~= 0 then
       resulttable = tutorialtable
   end

   if #resulttable == 0 then
       return 'Card with name "' .. search .. '" is not in the data.'
   elseif #resulttable == 1 then
       return resulttable[1]
   else
       return table.concat(resulttable, ",")
   end

```

end

function p.get(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   local get        = require ('Module:LoRData/getter')

   args['cardcode'] = args['cardcode'] or args[1]
   args['datatype'] = args['datatype'] or args[2]
   args['output']   = args['output']   or args[3] or nil

   local result = get[args['datatype']](args['cardcode'])

   if args['output'] ~= nil and type(result) == "table" then
       if args['output'] == "csv" then
           return table.concat(result, ",")
       elseif args['output'] == "tip" then
           table.sort(result)
       elseif args['output'] == "custom" then
           return frame:preprocess(lib.tbl_concat{result, prepend = args['prepend'], append = args['append'], separator = args['separator']})
       elseif args['output'] == "template" then
           return frame:preprocess(lib.tbl_concat{result, prepend = "{{" .. args['t_name'] .. "|", append = "}}", separator = args['separator']})
       end
   elseif (args['datatype'] == "desc" or args['datatype'] == "lvldesc" or args['datatype'] == "flavor") and result ~= nil then
       return frame:preprocess(result)
   elseif result == nil then
       return ""
   else
       return result
   end

```

end

function p.getRegion(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   return regions[args['region'] or args[1]]

```

end

function p.getRegionFromCardcode(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   args['cardcode'] = args['cardcode'] or args[1] or nil

   return regions[mw.ustring.sub(args['cardcode'], 3, 4)]

```

end

function p.getSetFromCardcode(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   args['cardcode'] = args['cardcode'] or args[1] or nil

   return tonumber(mw.ustring.sub(args['cardcode'], 0, 2))

```

end

function p.link(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   local cardCode = args['code']

   if cardCode == nil then
       if args['name'] then
           cardCode = p.findCardsByname{search=args['name'], entire=args['entire'], alt=args['alt'], artwork=args['artwork']}
       end
   else
       if args['alt'] == 'true' then
           cardCode = cardCode .. '-alt'
       end
       if args['artwork'] == 'true' then
           cardCode = cardCode .. '-full'
       end
   end

   args['name'] = args['name'] or p.get{args['code'], "name"}
   args['text'] = args['text'] or args['name']
   args['link'] = args['link'] or args['name'] .. ' (Legends of Runeterra)'

   local cardLink = formatnil('%s', args['link'], args['text'])
   if args['nolink'] then
       cardLink = args['text']
   end

   local link = mw.html.create('span')
       :addClass('lor-tooltip')
       :attr('data-param', cardCode)
       :wikitext(cardLink)

   return link

```

end

function p.splashartistpage(frame)

```
   local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end

   local lorData = mw.loadData('Module:LoRData/data')
   local s = ""

   args['artist'] = args['artist'] or args[1] or mw.title.getCurrentTitle().text

   local cardtable = {}
   for cardcode in pairs(lorData) do
       if p.getSetFromCardcode{cardcode} ~= 0 and lorData[cardcode].artist == args['artist'] then
           table.insert(cardtable, {['name'] = lorData[cardcode].name, ['code'] = cardcode})
       end
   end

   table.sort(cardtable, function(a, b) return a['name'] < b['name'] end)

```

{{{2}}}"

```
   return frame:preprocess(s)

```

end

function p.fileCategories()

```
   local lorData    = mw.loadData('Module:LoRData/data')
   local filename   = mw.title.getCurrentTitle().text
   local cardcode   = string.sub(filename, 1, string.find(filename, "%p") - 1)
   local s          = ""

   local cardset = p.getSetFromCardcode{cardcode}
   s = s .. "" .. "Category:LoR Set " .. cardset .. ""

   local cardregion = p.getRegionFromCardcode{cardcode}
   if cardregion then
       s = s .. "" .. "Category:LoR " .. tostring(cardregion) .. ""
   end

   local cardtype = lorData[cardcode].type
   if cardtype == "Unit" then
       s = s .. "" .. "Category:LoR Units"
       if lorData[cardcode].supertype then
           s = s .. "" .. "Category:LoR Champions"
       else
           s = s .. "" .. "Category:LoR Followers"
       end
   elseif cardtype == "Trap" then
       s = s .. "" .. "Category:LoR Traps"
   elseif cardtype == "Spell" or cardtype == "Ability" then
       s = s .. "" .. "Category:LoR Spells"
       if cardtype == "Ability" then
           s = s .. "" .. "Category:LoR Abilities"
       end
   end

   local cardsubtype = lorData[cardcode].subtype
   if cardsubtype then
       s = s .. "" .. "Category:LoR " .. cardsubtype .. ""
   end

   if string.find(filename, "-full") then
       s = s .. "" .. "Category:LoR Card artwork"
   end

   return s

```

end

--// Helper functions //
function formatnil(text, ...)

```
   if select("#", ...) == 0 then return text end
   for i=1, select("#", ...) do
       if select(i, ...) == nil then return nil end
   end
   return string.format(text, ...)

```

end

function p.meta()

```
   local lorData   = mw.loadData('Module:LoRData/data')

   local cardtable = {}
   for cardcode in pairs(lorData) do
       table.insert(cardtable, cardcode)
   end
   table.sort(cardtable)

   for i, cardcode in pairs(cardtable) do
       local t        = lorData[cardcode]
       local region   = p.getRegionFromCardcode{cardcode}
       local set      = p.getSetFromCardcode{cardcode}

       -- if set == 2  then
           mw.log("File:" .. cardcode .. ".png")
           -- mw.log(cardcode .. " (Legends of Runeterra)")
           -- mw.log(t.name)
       -- end
   end
   return ""

```

end

return p

--

--