Module:Collections table

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Collections table/doc. [edit] [history] [purge]
Module:Collections table's function main is invoked by Template:Collections table.
Module:Collections table requires Module:Addcommas.
Module:Collections table requires Module:Currency short.
Module:Collections table requires Module:ExchangeLite.
Module:Collections table requires Module:Skill clickpic.

Creates a table of items, with their sources and materials for archaeology collections.


-- <nowiki>
local Skillpic = require( 'Module:Skill clickpic' )
local commas   = require('Module:Addcommas')._add
local exchange = require( 'Module:ExchangeLite' )
local currency = require( 'Module:Currency short' )._amount

local p = {}

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

function p._main(args)
	local ret = mw.html.create('table')
	ret:addClass('wikitable lighttable')
		:tag('tr')
			:tag('th'):wikitext('Artefact'):done()
			:tag('th'):wikitext(Skillpic._main("Archaeology") .. " Level"):done()
			:tag('th'):wikitext(Skillpic._main("Archaeology") .. " XP"):done()
			:tag('th'):wikitext('Chronotes'):done()
			:tag('th'):wikitext('Hotspot'):done()
			:tag('th'):wikitext('Materials'):done()
			:tag('th'):wikitext('Material costs'):done()
		:done()
	
	local totmats = {}
	local totchro = 0
	local totxp = 0
	local tot_price = 0
	local collectioncompletionchronotesbonus = args.collectioncompletionchronotesbonus or args.collectionCompletionChronotesBonus or 0
	
	for _,i in ipairs(args) do
		i = mw.text.trim(i)
		local query = {
			'[['..i..']]',
			'?=#',
			'?Chronotes',
			'?Production JSON',
			limit = 1,
		}
		local smwdata = mw.smw.ask(query)
		if not smwdata then
			return string.format('Failed to find %s - ensure item name is spelled correctly.', i)
		end
		local chronotes = smwdata[1]['Chronotes'] or '?'
		if tonumber(chronotes, 10) then
			totchro = totchro + tonumber(chronotes, 10)
		end
		local matsjson = smwdata[1]['Production JSON']
		if type(matsjson) == 'string' then
			matsjson = { matsjson }
		end
		local mats = {}
		local level = "?"
		local xp = "?"
		local price = 0
		if matsjson then
			for _,mat_json in ipairs(matsjson) do
				local json_good, mats_t = pcall(mw.text.jsonDecode, mw.text.decode(mat_json))
				if json_good then
					if mats_t.level then
						level = mats_t.level
					end
					if mats_t.exp and tonumber( mats_t.exp ) then
						xp = tonumber(mats_t.exp)
						totxp = totxp + xp
					end
					for _, mat_info in ipairs(mats_t.mats) do
						local qty = string.gsub(mat_info.quantity, '%-', '–')
						qty = tonumber(qty, 10) or 1
						local matnm = mat_info.name
						if totmats[matnm] then
							totmats[matnm] = totmats[matnm] + qty
						elseif not string.find(matnm, 'damaged') then
							totmats[matnm] = qty
						end
						
						-- Skip if it's the damaged one
						if not string.find(matnm, i, nil, true) then
							-- Try using production data (in case added in the future)
							local mat_price = mat_info.price
							if not (mat_info.price and tonumber( mat_info.price )) then
								local noErr, p = pcall(exchange.price, matnm)
								if noErr then
									mat_price = p
								else
									mat_price = 0
								end
							end
                            if not (mat_price) then
                                mat_price = 0
                            end
							price = price + (mat_price * qty)
						end
						
						table.insert(mats, string.format('%s × [[File:%s.png|link=%s]] [[%s]]', qty,matnm,matnm,matnm) )
					end
					
					tot_price = tot_price + price
				end
			end
		else
			table.insert(mats, 'N/A')
		end
		
		local dmgname = i
		if string.find(dmgname, '%)$') then
			dmgname = dmgname..'(damaged)'
		else
			dmgname = dmgname..' (damaged)'
		end
		local query2 = {
			'[[Dropped item::'..i..']] OR [[Dropped item::'..dmgname..']]',
			'?Excavated from#-',
			'?Reward from#-',
			'?Excavated from.Dig site=Dig site',
			'?Is soil screening',
			limit = 5,
		}
		local smwdata2 = mw.smw.ask(query2)
		local sources = {}
		if not smwdata2 then
			table.insert(sources, 'Unknown')
		else
			for _,v in pairs(smwdata2) do
				if v['Excavated from'] then
					if not(v['Is soil screening']) then
						if v['Dig site'] then
							table.insert(sources, '[['..v['Excavated from']..']] ('..v['Dig site']..')')
						else
							table.insert(sources, '[['..v['Excavated from']..']]')
						end
					end
				end
			end
		end
		
		ret:tag('tr')
			:tag('td'):wikitext( string.format('[[File:%s.png|link=%s]] [[%s]]', i,i,i) ):done()
			:tag('td'):wikitext( level ):done()
			:tag('td'):attr('data-sort-value', xp):wikitext( commas(xp) ):done()
			:tag('td'):attr('data-sort-value', chronotes):wikitext( commas(chronotes) ):done()
			:tag('td'):wikitext( table.concat(sources, '<br>') ):done()
			:tag('td'):wikitext( table.concat(mats, '<br>') ):done()
			:tag('td'):attr('data-sort-value', price):wikitext( currency(price, 'coins', true) ):done()
		:done()
	end
	
	local mats = {}
	for m,n in pairs(totmats) do
		table.insert(mats, string.format('%s × [[File:%s.png|link=%s]] [[%s]]', n,m,m,m) )
	end
	
	local chronotes_price = 0
	local noErrChronotes, chronotesp = pcall(exchange.price, 'Chronotes')
	if noErrChronotes then
		chronotes_price = chronotesp
	else
		chronotes_price = 0
	end
	
	local total_chronotes_price = chronotes_price * (totchro + collectioncompletionchronotesbonus)
	
	ret:tag('tr'):addClass('nohighlight')
		:tag('th'):attr("colspan", "2"):wikitext('Totals (XP and chronotes):'):css('font-weight', 'bold'):done()
		:tag('td'):attr('data-sort-value', totxp):wikitext( commas(totxp) ):done()
		:tag('td'):attr('data-sort-value', totchro):wikitext( commas(totchro)..' + '..commas(collectioncompletionchronotesbonus)..'<br><span style="white-space:nowrap">('..currency(total_chronotes_price, 'coins', true)..')</span>' ):done()
		:tag('th'):wikitext('Total materials:'):css('font-weight', 'bold'):done()
		:tag('td'):wikitext( table.concat(mats, '<br>') ):done()
		:tag('td'):attr('data-sort-value', tot_price):wikitext( currency(tot_price, 'coins', true) ):done()
	:done()
	
	return tostring(ret)
end

return p
-- </nowiki>