Module:Sandbox/User:Jakesterwars/Uses material list

From Illerai

This is an old revision of this page, as edited by illerai>Jakesterwars at 02:48, 8 August 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

-- <pre>
local p = {}
local geprice = require('Module:Exchange')._price
local commas = require('Module:Addcommas')
local skillpic = require('Module:SCP')._main
local yesno = require('Module:Yesno')
local lang = mw.getContentLanguage()

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

function p._main(frame)
	local args = frame.args
	local materials = {}
	for material in string.gmatch(args[1] or mw.title.getCurrentTitle().text, "[^,]+") do
    	local trimmed = material:gsub("^%s*(.-)%s*$", "%1")
		table.insert(materials, trimmed)
	end
	
	local materialsLowered = {}
	for index, material in pairs(materials) do
	    materialsLowered[material:lower()] = true
	end
	
    local query = {
        '[[Uses material::'..table.concat(materials, '||')..']]',
        '[[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 those materials - ensure they are spelled correctly. (ERR: no results from SMW)[[Category:Empty drop lists]]'
    end
    
    local t = mw.html.create('table')
    t:addClass('wikitable sortable products-list align-center-1 align-right-3 align-right-4')
    local ttlrow = t:tag('tr')
            :tag('th')
            	:attr('colspan', '2')
                :wikitext('Item')
            :done()
            :tag('th')
            	:attr('data-sort-type', 'number')
                :wikitext('Skills')
            :done()
            :tag('th')
            	:attr('data-sort-type', 'number')
            	:wikitext('XP')
            :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 materialsLowered[mat_info.name:lower()] == true 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]] × %s', prod_t.output.name, prod_t.output.quantity)
                else
                    namestr = string.format('[[%s]]', prod_t.output.name)
                end
                local experience_cell = mw.html.create('td')
                local experience_ul = experience_cell:tag('ul')
                experience_ul:addClass('skills-list')
                    :css({
                        ['list-style-type'] = 'none',
                        ['list-style-image'] = 'none',
                        margin = 0
                    })
                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')
            		local experience_li = experience_ul:tag('li')
            		experience_cell:addClass('table-na')
            		experience_li:wikitext(string.format('None'))
            		skills_cell:addClass('table-na')
            		skill_li:wikitext(string.format('None'))
            		
            	else
            		for index, v in ipairs(prod_t.skills) do
                		local skill_li = skills_ul:tag('li')
                		local experience_li = experience_ul:tag('li')
                		local stripped_experience = string.gsub(v.experience, ',', '')
                		mw.log(index)
                		skill_li:attr('data-sort-value', index == 1 and v.level or ''):wikitext(skillpic(lang:ucfirst(v.name), v.level))
                		experience_li:attr('data-sort-value', index == 1 and v.experience or ''):wikitext(skillpic(lang:ucfirst(v.name), v.experience))
            		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
	            	for _,mat in ipairs(materialsLowered) do
	            		if mat_info.name:lower() == mat then
	            			mat_li:addClass('production-selected')
	            		end
	            	end
                    if materialsLowered[mat_info.name:lower()] == true then
                        mat_li:addClass('production-selected')
                    end
                    mat_li:wikitext(string.format('%s × [[%s]]', qty, matnm))
                end

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

    if rows == 0 then
        return 'Failed to find products with those materials - ensure they are spelled correctly. (ERR: no mats found in results)[[Category:Empty drop lists]]'
    end

    return tostring(t)
end

return p
--</pre>