Module:Uses material list

From RuneScape Classic Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Uses material list/doc

-- <pre>
local p = {}
local lang = mw.getContentLanguage()

function p.main(frame)
	return p._main(frame:getParent())
end

function p._main(frame)
	local args = frame.args
	local material = args[1] or args.material or mw.title.getCurrentTitle().text

    local query = {
        '[[Uses material::'..material..']]',
        '[[Production JSON::+]]',
        '?Production JSON',
        limit = args.limit or 100,
        sort = args.sort,
        order = args.order
    }
    local smwdata = mw.smw.ask(query)
    
    if not smwdata then
        return 'Failed to find products with that material - ensure it is spelled correctly. (ERR: no results from SMW)[[Category:Empty drop lists]]'
    end

    -- local intro = string.format(":''For an exhaustive list of items, click <span class='plainlinks'>[%s here]</span>.''", tostring(mw.uri.fullUrl('Special:Ask', { q = '[[Uses material::'..material..']]', ['p[format]'] = 'table', ['p[limit]'] = 500, ['p[intro]'] = 'All items made from: '..material })))

    local mat = string.lower(material)
    local t = mw.html.create('table')
    t:addClass('wikitable sortable products-list align-right-2 align-right-4 align-right-5')
    if #smwdata > 15 then
    	t:addClass('mw-collapsible mw-collapsed')
    end
    local ttlrow = t:tag('tr')
            :tag('th')
                :wikitext('Item')
            :done()
            :tag('th')
                :wikitext('Skills')
            :done()
            :tag('th')
                :wikitext('Materials')
            :done()

    local rows = 0

    for _,smw_item in ipairs(smwdata) do
        local prod = smw_item['Production JSON']
        if type(prod) == 'string' then
            prod = { prod }
        end
        for _,prod_json in ipairs(prod) do
            local prod_t = mw.text.jsonDecode(prod_json)
            local _found = false
            for _,mat_info in ipairs(prod_t.materials) do
                if mat_info.name:lower() == mat then
                    _found = true
                    break
                end
            end
            if _found then
                local namestr
                if (tonumber(prod_t.output.quantity) or 1) > 1 then
                    namestr = string.format('%s&nbsp;[[%s]] × %s', prod_t.output.image, prod_t.output.name, prod_t.output.quantity)
                else
                    namestr = string.format('%s&nbsp;[[%s]]', prod_t.output.image, prod_t.output.name)
                end
                local skills_cell = mw.html.create('td')
                local skills_ul = skills_cell:tag('ul')
                skills_ul:addClass('skills-list')
                    :css({
                        ['list-style-type'] = 'none',
                        ['list-style-image'] = 'none',
                        margin = 0
                    })
                if #prod_t.skills == 0 then
            		local skill_li = skills_ul:tag('li')
            		skills_cell:addClass('table-na')
            		skill_li:wikitext(string.format('None'))
            	else
            		for _, v in ipairs(prod_t.skills) do
                		local skill_li = skills_ul:tag('li')
                		skill_li:wikitext(string.format('%s&nbsp;[[%s]]', v.lvl, lang:ucfirst(v.name)))
            		end
            	end
                local mats_ul = mw.html.create('ul')
                mats_ul:addClass('products-materials')
                    :css({
                        ['list-style-type'] = 'none',
                        ['list-style-image'] = 'none',
                        margin = 0
                    })
                for _, mat_info in ipairs(prod_t.materials) do
                    local mat_li = mats_ul:tag('li')
                    local qty = string.gsub(mat_info.quantity, '%-', '–')
                    local matnm = mat_info.name
                    if mat_info.name:lower() == mat then
                        mat_li:addClass('production-selected')
                    end
                    mat_li:wikitext(string.format('%s × [[%s]]', qty, matnm))
                end

                local prow = t:tag('tr')
                    :tag('td')
                    	:attr('data-sort-value', prod_t.product)
                        :wikitext(namestr)
                    :done()
                    :node(skills_cell)
                    :tag('td')
                        :node(mats_ul)
                    :done()
                rows = rows + 1
            end
        end
    end

    if rows == 0 then
        return 'Failed to find products with that material - ensure it is spelled correctly. (ERR: no mat found in results)[[Category:Empty drop lists]]'
    end

    return tostring(t)
end

return p
--</pre>