5,328 Pages

-- <pre>
local p         = {}
local lib       = require('Module:Feature')
local userError = require('Dev:User error')
local json      = require('Module:JSON')
 
local regions   = {
    ["DE"]              = "Demacia",
    ["FR"]              = "Freljord",
    ["IO"]              = "Ionia",
    ["NX"]              = "Noxus",
    ["PZ"]              = "Piltover & Zaun",
    ["SI"]              = "Shadow Isles",
    ["Demacia"]         = "DE",
    ["Freljord"]        = "FR",
    ["Ionia"]           = "IO",
    ["Noxus"]           = "NX",
    ["Piltover & Zaun"] = "PZ",
    ["Shadow Isles"]    = "SI"
}
 
function p.cardData()
    local titleobject = mw.title.new('User:Ninjamask/Sandbox/LoR.json')
    local o           = json.decode(titleobject:getContent())
    local s           = ""
    local cardData    = {}
 
    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'
 
        local sCardRefs = '{"'
        sCardRefs = sCardRefs .. table.concat(card.associatedCardRefs, '", "') .. '"}'
        if sCardRefs ~= '{""}' then
            sLoop = sLoop .. '        ["cardRefs"]    = '  .. lib.tbl(card.associatedCardRefs)     ..  ',\n'
        end
 
        sLoop = sLoop .. '        ["name"]        = "'     .. card.name                  .. '",\n'
        sLoop = sLoop .. '        ["type"]        = "'     .. card.type                  .. '",\n'
        sLoop = sLoop .. '        ["rarity"]      = "'     .. card.rarity                .. '",\n'
 
        if card.subtype ~= "" then
            sLoop = sLoop .. '        ["subtype"]     = "' .. 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(card.keywords)     ..  ',\n'
        end
 
        sLoop = sLoop .. '        ["collectible"] = '      .. tostring(card.collectible) ..  ',\n'
        sLoop = sLoop .. '        ["power"]      = '      .. card.power                ..  ',\n'
        sLoop = sLoop .. '        ["cost"]        = '      .. card.cost                  ..  ',\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) .. '</pre>'
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
 
    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('Name')
            :done()
            :newline()
            :tag('th')
                :wikitext('Type')
            :done()
            :newline()
            :tag('th')
                :wikitext('Subtype')
            :done()
            :newline()
            :tag('th')
                :wikitext('Region')
            :done()
            :newline()
            :tag('th')
                :css('width','20px')
                :tag('span')
                    :attr('title','Cost')
                    :wikitext('[[File:LoR mana icon.png|40px|link=|]]')
                :done()
            :done()
            :newline()
            :tag('th')
                :css('width','20px')
                :tag('span')
                    :attr('title','Power')
                    :wikitext('[[File:LoR Power.png|40px|link=|]]')
                :done()
            :done()
            :newline()
            :tag('th')
                :css('width','20px')
                :tag('span')
                    :attr('title','Health')
                    :wikitext('[[File:LoR Health.png|40px|link=|]]')
                :done()
            :done()
            :newline()
            :tag('th')
                :wikitext('Rarity')
            :done()
        :done()
        :newline()
 
 
        -- START
        local cardtable = {}
        for cardcode in pairs(lorData) do
            table.insert(cardtable, cardcode)
        end
        table.sort(cardtable)
 
 
        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}
 
                if s_region ~= nil and s_region ~= region then
                    break
                end
 
                if s_set ~= nil and s_set ~= set 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.supertype ~= 'Champion' then
                            break
                        end
                    elseif s_type ~= t.type then
                        break
                    end
                end
 
                -- Image
                cardnode
                    :tag('td')
                        :addClass('lor-tooltip')
                        :attr('data-sort-value', cardcode)
                        :attr('data-param', cardcode)
                        :wikitext('[[File:' .. cardcode .. '.png|75px|link=' .. t.name .. ' (Legends of Runeterra)]]<br>' .. cardcode)
                    :done()
 
                -- Cardname
                cardnode
                    :tag('td')
                        :css('text-align', 'left')
                        :wikitext('[[' .. t.name .. ' (Legends of Runeterra)|' .. t.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
                cardnode
                    :tag('td')
                        :css('text-align', 'left')
                        :wikitext('[[File:' .. region .. ' region.svg|20px|link='.. region .. ' (Legends of Runeterra)]]&nbsp;[[' .. region .. ' (Legends of Runeterra)|' .. region .. ']]')
                    :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 = '[[File:' .. rarity .. ' rarity.svg|20px|link=]]&nbsp;' .. 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
 
    tablenode:allDone()
    return tostring(tablenode)
end
 
function p.findCardByName(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 alt         = args['alt'] or nil
    local artwork     = args['artwork'] or nil
 
    local cardtable   = {}
    local resulttable = {}
 
    for cardcode in pairs(lorData) do
        table.insert(cardtable, cardcode)
    end
    table.sort(cardtable)
 
    for _, cardcode in ipairs(cardtable) do
        if search == lorData[cardcode].name then
            local rCardcode = cardcode
            if alt == 'true' then
                rCardcode = rCardcode .. '-alt'
            end
            if artwork == 'true' then
                rCardcode = rCardcode .. '-full'
            end
            table.insert(resulttable, rCardcode)
        end
    end
 
    if #resulttable == 0 then
        return 'Cardname ' .. 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')
    local cardcode = args['cardcode'] or args[1]
    local datatype = args['datatype'] or args[2]
    local output   = args['output']   or args[3] or nil
 
    local result = get[datatype](cardcode)
 
    if output ~= nil and type(result) == "table" then
        if output == "csv" then
            return table.concat(result, ",")
        elseif output == "tip" then
            table.sort(result)
            return frame:preprocess("{" .. "{TipLoR|style=margin-right:1em;|" .. table.concat(result, "}} {" .. "{TipLoR|style=margin-right:1em;|") .. "}}")
        elseif output == "custom" then 
            return frame:preprocess(lib.tbl_concat{result, prepend = args['prepend'], append = args['append'], separator = args['separator']})
        elseif output == "template" then
            return frame:preprocess(lib.tbl_concat{result, prepend = "{{" .. args['t_name'] .. "|", append = "}}", separator = args['separator']})
        end
    elseif (datatype == "desc" or datatype == "lvldesc" or 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
    local cardcode = args['cardcode'] or args[1] or nil
 
    return regions[mw.ustring.sub(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
    local cardcode = args['cardcode'] or args[1] or nil
 
    return tonumber(mw.ustring.sub(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
 
    args['code'] = args['code'] or p.findCardByName{search=args['name'], alt=args['alt'], artwork=args['artwork']}
    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 link = mw.html.create('span')
        :addClass('lor-tooltip')
        :attr('data-param', args['code'])
        :attr('data-artwork', args['artwork'] or 'false')
        :wikitext(formatnil('[[%s|%s]]', args['link'], args['text']))
 
    return link
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 mw.ustring.format(text, ...)
end
 
return p
-- </pre>
-- [[Category:Lua]]
Community content is available under CC-BY-SA unless otherwise noted.