Module:ItemSources: Difference between revisions

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
Line 3: Line 3:
local hc = require('Module:Param Parse').has_content
local hc = require('Module:Param Parse').has_content
local pcp = require('Module:Profession clickpic')._main
local pcp = require('Module:Profession clickpic')._main
local params = require('Module:Paramtest')


function p.main(frame)
function p.main(frame)

Revision as of 07:45, 2 December 2024

Module documentation
This documentation is transcluded from Module:ItemSources/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:ItemSources/doc. [edit]
Module:ItemSources's function main is invoked by Template:ItemSources.
Module:ItemSources requires Module:Param Parse.
Module:ItemSources requires Module:Paramtest.
Module:ItemSources requires Module:Profession clickpic.

local p = {}

local hc = require('Module:Param Parse').has_content
local pcp = require('Module:Profession clickpic')._main
local params = require('Module:Paramtest')

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

--class, sort
local frequencies = {
	always = { 'table-bg-blue', 1 },
	common = { 'table-bg-green', 16 },
	uncommon = { 'table-bg-yellow', 64 },
	rare = { 'table-bg-orange', 256 },
	['very rare'] = { 'table-bg-red', 1024 },
	conditional = { 'table-bg-pink', 2048 },
	random = { 'table-bg-pink', 4096 },
	varies = { 'table-bg-pink', 4096 },
	_default = { 'table-bg-gray', 65536 }
}
-- arbitrary numbers
local frequencies2 = {
	{ 1, 'table-bg-blue' },
	{ 1/16, 'table-bg-green' },
	{ 1/64, 'table-bg-yellow' },
	{ 1/256, 'table-bg-orange' },
	{ 1/1024, 'table-bg-red' }
}
-- supporting function for number => colour
local function get_frequency_class(val)
	for i,v in ipairs(frequencies2) do
		curr = v
		if val >= v[1] then
			break
		end
	end
	return curr[2]
end

function p._main(args)
	local item = args[1] or mw.title.getCurrentTitle().fullText
	
	local q = {
		'[[Dropped item::' .. item .. ']]',
		'?Dropped from=source',
		'?Dropped quantity=quantity',
		'?Rarity=rarity',
		'?Drop JSON'
	}
	local sourcessmwdata = mw.smw.ask(q)
	
	local out = mw.html.create('table')
		:addClass('wikitable sortable autosort=4,a align-center-2 align-center-3 align-center-4')
		:tag('tr')
			:tag('th'):wikitext('Source'):done()
			:tag('th'):wikitext('Level'):done()
			:tag('th'):wikitext('Quantity'):done()
			:tag('th'):wikitext('Frequency'):done()
		:done()
		
	for _, source in ipairs(sourcessmwdata) do
		local dropJSON = mw.text.jsonDecode(source['Drop JSON'] or '{}')
		
		local q2 = {
			'[[:' .. dropJSON['Dropped from'] .. ']]',
			'?Profession Requirement A=professionreq',
		}
		local sourcesmwdata = mw.smw.ask(q2)
		
		for _, v in ipairs(sourcesmwdata) do
			local professionreq = v.professionreq or 'Unknown'
			local professionlevel = professionreq
			if professionreq ~= 'Unknown' then
				local profession, level = v.professionreq:match("([^,]+),([^,]+)")
				professionlevel = pcp(profession, level)
			end
			
			local frequency = dropJSON['Frequency']
			local frequency_value
		    if frequencies[string.lower(tostring(frequency))] then
		        frequency = params.ucflc(frequency)
		    else
		        rarity_value = frequency:gsub(',','') --temp place to put this without overriding frequency
		        local rv1, rv2 = string.match(frequency_value, '([%d%.]+)/([%d%.]+)')
		        if rv1 and rv2 then
		            frequency = commas(rv1) .. '/' .. commas(rv2)
		            frequency_value = rv1/rv2
		        else
		            frequency_value = expr(frequency)
		        end
		    end
			
			local freq_class, freq_sort = '', nil
			if frequency_value == nil then
				freq_class, freq_sort = unpack(frequencies[string.lower(tostring(frequency))] or frequencies._default)
			elseif frequency_value == false then
				freq_class, freq_sort = unpack(frequencies._default)
			else
				freq_sort = 1/frequency_value
				freq_class = get_rarity_class(frequency_value)
			end
			
			out:tag('tr')
				:tag('td'):wikitext(source.source):done()
				:tag('td'):wikitext(professionlevel):done()
				:tag('td'):wikitext(dropJSON['Dropped quantity']):done()
				:tag('td'):wikitext(frequency)
					:attr('data-sort-value', freq_sort)
					:addClass(freq_class)
				:done()
			:done()
		end
		
	end
	
	return out
end

return p