Module:Sandbox/User:Jakesterwars/Uses material list

From Old School RuneScape Wiki
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Template:Module sandbox/doc. [edit] [history] [purge]
Module:Sandbox/User:Jakesterwars/Uses material list requires Module:Addcommas.
Module:Sandbox/User:Jakesterwars/Uses material list requires Module:Exchange.
Module:Sandbox/User:Jakesterwars/Uses material list requires Module:SCP.
Module:Sandbox/User:Jakesterwars/Uses material list requires Module:Yesno.

This module is a sandbox for Jakesterwars. It can be used to test changes to existing modules, prototype new modules, or just experimenting with lua features.

Invocations of this sandbox should be kept in userspace; if the module is intended for use in other namespaces, it should be moved out of the sandbox into a normal module and template.

This default documentation can be overridden by creating the /doc subpage of this module, as normal.

-- <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>