Module:ItemSources

From Brighter Shores Wiki
Revision as of 23:34, 3 December 2024 by Californ1a (talk | contribs) (support multiple drop json tables for single item type/frequency)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
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.
Module:ItemSources requires Module:Purge.

local p = {}

local hc = require('Module:Param Parse').has_content
local pcp = require('Module:Profession clickpic')._main
local params = require('Module:Paramtest')
local purge = require('Module:Purge')._purge
local lang = mw.language.getContentLanguage()
local commas = function (n) return lang:formatNum(tonumber(n)) end

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)
	local curr
	for i,v in ipairs(frequencies2) do
		curr = v
		if val >= v[1] then
			break
		end
	end
	return curr[2]
end

local function expr(t)
	local noerr, val = pcall(mw.ext.ParserFunctions.expr, t)
	if noerr then
		return tonumber(val)
	else
		return false
	end
end

local function parseDropJson(out, source, drop)
	local dropJSON = mw.text.jsonDecode(drop 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
	        frequency_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_frequency_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

	return out
end

function p._main(args)
	local item = args[1] or mw.title.getCurrentTitle().fullText

	local q = {
		'[[Dropped item::' .. item .. ']]',
		'?Dropped from=source',
		'?Drop JSON'
	}
	local sourcessmwdata = mw.smw.ask(q)

	if not sourcessmwdata then
		return ":''No sources found. To force an update, click "
				..purge('source-'..mw.uri.anchorEncode(item), 'here', 'span')
				..".''[[Category:Empty source lists]]"
	end

	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 drop = source['Drop JSON']
		if type(drop) == 'string' then
			out = parseDropJson(out, source, drop)
		elseif type(drop) == 'table' then
			for _, v in ipairs(drop) do
				out = parseDropJson(out, source, v)
			end
		end
	end

	return out
end

return p