Jump to content

Module:Shop Availability: Difference between revisions

From The Petit Planet Wiki
Eleiyas (talk | contribs)
Created page with "local p = {} local Parse = require('Module:Parser').getTemplateArgs local Slib = require('Module:Shop') local lib = require('Module:Feature') function p.main(frame) local args = require('Module:Arguments').getArgs(frame, { parentOnly = false, wrapper = {'Template:Shop Availability'} }) local item = args[1] or mw.title.getCurrentTitle().rootText args._PAGES = {} args._BOOL = {nototal=1} if lib.isNotEmpty(args.pages) then for page in string.gmatch(args.pages..."
 
Eleiyas (talk | contribs)
default to Dough if no currency specified.
 
Line 84: Line 84:
data._SPECFIC = specific
data._SPECFIC = specific
data[1] = nil
data[1] = nil
data.currency = data.currency or call_data.currency or 'Denny'
data.currency = data.currency or call_data.currency or 'Dough'
if not args._BOOL.refresh and data.refresh then args._BOOL.refresh = 1 end
if not args._BOOL.refresh and data.refresh then args._BOOL.refresh = 1 end
if not args._BOOL.note and data.note then args._BOOL.note = 1 end
if not args._BOOL.note and data.note then args._BOOL.note = 1 end

Latest revision as of 22:19, 14 November 2025

Documentation for this module may be created at Module:Shop Availability/doc

local p = {}
local Parse = require('Module:Parser').getTemplateArgs
local Slib = require('Module:Shop')
local lib = require('Module:Feature')

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentOnly = false,
		wrapper = {'Template:Shop Availability'}
	})
	local item = args[1] or mw.title.getCurrentTitle().rootText
	args._PAGES = {}
	args._BOOL = {nototal=1}
	
	if lib.isNotEmpty(args.pages) then
		for page in string.gmatch(args.pages, '(.-)%$%$%$') do
			local data = Parse(page, { only='Shop' } )
			local _ret = {PAGENAME = page, rows = 0}
			if type(data[1]) == 'table' then
				for num, call in pairs(data) do
					local Pdata = p.ParseCall(call, item, args)
					if Pdata then
						_ret.rows = _ret.rows + #Pdata
						table.insert(_ret, Pdata)
					end
				end
			else
				local Pdata = p.ParseCall(data, item, args)
				if Pdata then
					_ret.rows = _ret.rows + #Pdata
					table.insert(_ret, Pdata)
				end
			end
			if #_ret>0 then
				table.insert(args._PAGES, _ret)
			end
		end
		
		if #args._PAGES>0 then
			return p._main(args, frame)
		else
			return args.noresult or ('No shop sells ' .. item .. lib.ternary(item:find('%."?$'), '', '.'))
		end
	else
		return args.noresult or ('No shop sells ' .. item .. lib.ternary(item:find('%."?$'), '', '.'))
	end
end

function p._main(args, frame)
	local _table = mw.html.create('table'):addClass('fandom-table article-table sortable')
	
	-- header row
	local tr = _table:tag('tr')
	tr:tag('th'):wikitext('Source')
	tr:tag('th'):wikitext('Cost')
	tr:tag('th'):wikitext('Stock')
	if lib.isNotEmpty(args._BOOL.refresh) then tr:tag('th'):wikitext('Refresh') end
	if lib.isNotEmpty(args._BOOL.note) then tr:tag('th'):wikitext('Notes') end
	
	-- rest of table
	for _, page in ipairs(args._PAGES) do
		local tr = _table:tag('tr')
		tr:tag('td'):attr('rowspan', page.rows):wikitext('[[', page.PAGENAME, ']]')
		for i, calls in ipairs(page) do
			if i>1 then tr = _table:tag('tr') end
			for y, call in ipairs(calls) do
				if #page==1 and y>1 then tr = _table:tag('tr') end
				tr:node(Slib.ItemRow(call, args._BOOL))
			end
		end
	end
	-- mw.logObject(_table) --debug
	return frame:preprocess(tostring(_table))
end

function p.ParseCall(call_data, item, args)
	local delim = call_data.delim or ';;'
	local ret = {}
	
	for _, row in ipairs(call_data) do
		local data = Slib.splitParams(row, delim)
		local specific = data.specific or lib.ternary(tonumber(data.blueprint)==1, 'Recipe: ' .. data[1], data[1])
		if item == specific then
			data._SPECFIC = specific
			data[1] = nil
			data.currency = data.currency or call_data.currency or 'Dough'
			if not args._BOOL.refresh and data.refresh then args._BOOL.refresh = 1 end
			if not args._BOOL.note and data.note then args._BOOL.note = 1 end
			table.insert(ret, data)
		end
	end
	if #ret > 0 then
		return ret
	end
end

return p