Module:MonsterVariantsTable

From Brighter Shores Wiki
Revision as of 02:55, 29 December 2024 by Californ1a (talk | contribs) (Created page with "local p = {} function p.main(frame) return p._main(frame:getParent().args) end function p._main(args) local variant = args[1] or args.variant or 'Thug' local query = { string.format('Variant of::%s Has query::+ OR [[Has subobject::<q>Variant of::%s</q>]]', variant, variant), '?Image#64px;x64px=img', '?Has subobject.Image#64px;x64px=img2', '?= page', '?Unlock_level = unlock', '?Has subobject.Unlock_level = u...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:MonsterVariantsTable/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:MonsterVariantsTable/doc. [edit]
Module:MonsterVariantsTable's function main is invoked by Template:MonsterVariantsTable.

local p = {}

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

function p._main(args)
	local variant = args[1] or args.variant or 'Thug'
	local query = {
        string.format('[[Variant of::%s]] [[Has query::+]] OR [[Has subobject::<q>[[Variant of::%s]]</q>]]', variant, variant),
        '?Image#64px;x64px=img',
        '?Has subobject.Image#64px;x64px=img2',
        '?= page',
        '?Unlock_level = unlock',
        '?Has subobject.Unlock_level = unlock2',
        '?Profession_Level_A = combat',
        '?Has subobject.Profession_Level_A = combat2',
    }

    local results = mw.smw.ask(query)
    local sorted = p.sort_by_combat(results)
    local debug_str = '<pre>'..mw.text.jsonEncode(sorted, mw.text.JSON_PRETTY)..'</pre>'
    
    return tostring(p.create_table(sorted))--..debug_str
end

function p.sort_by_combat(data)
    table.sort(data, function(a, b)
        -- Determine combat values
        local combatA = a.combat or (a.combat2 and a.combat2[1]) or math.huge
        local combatB = b.combat or (b.combat2 and b.combat2[1]) or math.huge
        -- Sort in ascending order
        return combatA < combatB
    end)
    return data
end

function p.insert_row(tbl, entry)
	tbl:tag('tr')
		:tag('td')
			:wikitext(entry.img)
		:done()
		:tag('td')
			:wikitext(entry.page)
		:done()
		:tag('td')
			:wikitext(entry.unlock)
		:done()
		:tag('td')
			:wikitext(entry.combat)
		:done()
	:done()
	
	return tbl
end

function p.create_table(results)
	local out = mw.html.create('table')
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th')
				:wikitext('Image')
			:done()
			:tag('th')
				:wikitext('Monster')
			:done()
			:tag('th')
				:wikitext('Unlocked at')
			:done()
			:tag('th')
				:wikitext('Combat level')
			:done()
		:done()
		
	for _,entry in ipairs(results) do
		local built_entry = {
			combat = entry.combat or entry.combat2[1] or entry.combat2 or '',
			img = entry.img or entry.img2[1] or entry.img2 or '',
			unlock = entry.unlock or entry.unlock2[1] or entry.unlock2 or '',
			page = entry.page
		}
		
		out = p.insert_row(out, built_entry)
	end
	
	return out
end

return p