Module:Sandbox/User:Habblet/Infobox Construction

Revision as of 21:23, 4 November 2021 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/User:Habblet/Infobox Construction/doc

-- <nowiki>
--------------------------
-- Module for [[Template:Infobox Construction]]
-- Please test changes to this module at [[Module:Infobox Construction/sandbox]] first
------------------------
local p = {}

-- "imports"
local onmain = require('Module:Mainonly').on_main
local yesno = require('Module:Yesno')
local paramtest = require('Module:Paramtest')
local editbutton = require('Module:Edit button')
local cleanimg = require('Module:Clean image').clean
local commas = require('Module:Addcommas')
local skillpic = require('Module:Skill_clickpic')._main
local infobox = require('Module:Infobox')

function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)
	
	ret:defineParams{
		{ name = 'name', func = 'name' },
		{ name = 'vanchor', func = { name = 'has_content', params = { 'version' }, flag = 'p' } },
		{ name = 'image', func = { name = imgarg, params = { 'image' }, flag = { 'd' } } },
		{ name = 'release', func = 'release' },
		{ name = 'removal', func = 'removal' },
		{ name = 'members', func = { name = bool, params = { 'members' }, flag = { 'd' } } },
		{ name = 'icon', func = { name = iconarg, params = { 'icon' }, flag = { 'd' } } },
		{ name = 'level', func = { name = lvlarg, params = { 'level' }, flag = { 'd' } } },
		{ name = 'experience', func = 'numbers' },
		{ name = 'room', func = { name = link, params = { 'room' }, flag = { 'd' } } },
		{ name = 'hotspot', func = { name = link, params = { 'hotspot' }, flag = { 'd' } } },
		-- { name = 'actions', func = { name = actionarg, params = { 'actions' }, flag = { 'd' } } },
		-- { name = 'actions_smw', func = { name = actionsmw, params = { 'actions' }, flag = { 'p' } }, dupes = true },
		{ name = 'flatpack', func = { name = bool, params = { 'flatpack' }, flag = { 'd' } } },
		{ name = 'examine', func = 'has_content' },
		{ name = 'id', func = { name = iddisp,  params = { 'id' } } },
		{ name = 'id_smw', func = { name = idsmw, params = { 'id' }, flag = 'p' }, dupes = true },
		{ name = 'chisel_links', func = { name = make_chisel_links, params = { 'id_smw', 'name' }, flag = 'd' } },
	}
	
	local function mat_list(args)
		local ret_list = {}
		for i=1,10,1 do
			local mat = args['mat'..i]
			if mat and paramtest.has_content(mat) then
				local name = mat
				local qty = paramtest.default_to(args['mat'..i..'qty'],'1')
				local img
				local note = ''
				if not ( name:lower() == 'nails' ) then
					img = paramtest.default_to(args['mat'..i..'img'],
										name..'.png')
										:gsub('File:','')
				else
					img = 'Steel nails.png'
					note_text = 'Any type of [[nails]] can be used'
					note = frame:extensionTag{ name = 'ref', content = note_text, args = { group = "nails" } }
				end

				table.insert(ret_list, {
					name = '[['..name..']]'..note,
					name_raw = name,
					quantity = qty,
					image = '[[File:'..img..'|link='..name..']]',
					image_raw = img
				} )
			end
		end
	
		return ret_list
	end
	
	ret:useSMW({
		members = 'Is members only',
	})

	ret:useSMWOne({
		id_smw = 'All Object ID'
	})

	ret:useSMWSubobject({
		id_smw = 'Object ID',
		vanchor = 'Version anchor',
		name = 'Object name',
		members = 'Is members only',
		release = 'Release date',
		update = 'Release update',
		--removal = 'Removal date',
		removalupdate = 'Removal update',
		-- actions_smw = 'Actions'
	})

	
	ret:setMaxButtons(7)
	ret:create()
	ret:cleanParams()
	ret:customButtonPlacement(true)
	
	ret:linkParams{
		{ 'image', 'icon' },
	}
	
	ret:defineLinks({ links = {{ 'Template:%s/doc', 'doc' },
		{ 'Template_talk:%s', 'Talk page' }}, colspan = 2 })
		
	ret:defineName('Infobox Construction')
	ret:addClass('infobox-construction')
	
	if onmain() then
		local a2 = ret:categoryData()
	end
	
	ret:addButtonsCaption()
	
	-- PARAMETER: image
	ret:addRow{
		{ tag = 'argd', content = 'image', class = 'infobox-image', colspan = '2' } }
	
	-- PARAMETER: name
	ret:addRow{
		{ tag = 'argh', content = 'name', class = 'infobox-header', colspan = '2' } }
	
	-- PARAMETER: release
	-- (update included automatically by infobox)
		:addRow{ { tag = 'th', content = 'Release' },
				{ tag = 'argd', content = 'release' } }
	
	-- PARAMETER: removal
	if ret:paramDefined('removal') then
		ret:addRow{ { tag = 'th', content = 'Removal' },
				{ tag = 'argd', content = 'removal' } }
	end
	
	-- PARAMETER: members
	ret:addRow{
		{ tag = 'th', content = '[[Members]]' },
		{ tag = 'argd', content = 'members' } }
	
	-- PARAMETER: icon
	ret:addRow{
		{ tag = 'th', content = 'Icon' },
		{ tag = 'argd', content = 'icon' } }
	
	-- PARAMETER: level
	ret:addRow{
		{ tag = 'th', content = '[[Construction|Level]]' },
		{ tag = 'argd', content = 'level' } }
	
	-- PARAMETER: experience
	ret:addRow{
		{ tag = 'th', content = '[[Experience]]' },
		{ tag = 'argd', content = 'experience' } }
	
	-- PARAMETER: room
	:addRow{
		{ tag = 'th', content = '[[Player-owned house#Rooms|Room]]' },
		{ tag = 'argd', content = 'room' } }

	-- PARAMETER: hotspot
	:addRow{
		{ tag = 'th', content = '[[Hotspot]]' },
		{ tag = 'argd', content = 'hotspot' } }

	-- PARAMETERS: matx, matxqty, matximg
	local table_mats = ret:tag('tr')
						    :tag('th')
							    :wikitext('Materials')
							:tag('td')

	local materials = mat_list(args)
	if #materials > 0 then
		table_mats = table_mats:tag('table')
	end
	for _, v in ipairs(materials) do
		-- Fetch all the variables
		local mat_name = v.name
		local mat_qty = v.quantity
		local mat_img = v.image
		table_mats:tag('tr')
					:tag('td')
						:css({ ['text-align'] = 'right',
							   ['padding'] = 0})
						:wikitext(mat_qty..' × ')
					:tag('td')
						:css({ ['text-align'] = 'center',
							   ['padding'] = 0 })
						:wikitext(mat_img)
					:tag('td')
						:css({ ['text-align'] = 'left',
							   ['padding'] = 0 })
						:wikitext(' '..mat_name)
	end

	-- If no materials found
	-- Indicate nothing listed, add an edit button for the page
	if #materials == 0 then
		table_mats:css({ ['font-style'] = 'italic' })
				      :wikitext('Unknown materials '..editbutton())
				  :done()
	end
	
	-- PARAMETER: actions
	-- :addRow{
	-- 	{ tag = 'th', content = 'Actions' },
	-- 	{ tag = 'argd', content = 'actions' } }

	-- PARAMETER: flatpack
	ret:addRow{
		{ tag = 'th', content = '[[Flatpack|Flatpackable]]' },
		{ tag = 'argd', content = 'flatpack' } }

	-- PARAMETER: examine
	ret:addRow{
		{ tag = 'th', content = '[[Examine]]' },
		{ tag = 'argd', content = 'examine' } }
	
	-- References
	local ref = ''
	for _, v in ipairs(materials) do
		if v.name_raw:lower() == 'nails' then
			ref = '\n' .. frame:extensionTag{ name = 'references', args = { group = "nails" } }
			
		end
	end
	if ref ~= '' then
		ret:tag('tr')
				:tag('th')
					:addClass('infobox-subheader')
					:attr('colspan', '2')
					:wikitext('References')
			:tag('tr')
				:tag('td')
					:attr('colspan', '2')
					-- :css('text-align', 'center')
					:wikitext(ref)
	end
	
	-- Advanced data
	ret:addRow{
		{ tag = 'th', content = 'Advanced data', class = 'infobox-subheader', colspan = '2' },
		meta = {addClass = 'advanced-data'} }
	:addRow{
		{ tag = 'th', content = 'Object ID' },
		{ tag = 'argd', content = 'id' },
		meta = {addClass = 'advanced-data'} }
	:addRow{
		{ tag = 'th', content = 'Links' },
		{ tag = 'argd', content = 'chisel_links' },
		meta = {addClass = 'advanced-data'} }

	ret:finish()
	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1,a2))
		
		-- SMW
		smw = {}
		table.insert(smw,'Required materials:')
		local jsonmats = {}
		for _, v in ipairs(materials) do
			table.insert(jsonmats,{ name = v.name_raw, quantity = v.quantity, image = v.image_raw })
			table.insert(smw,string.format('[[Made from item::%s]]',v.name_raw))
		end

		local tojson = {
			product = args.name,
			link = mw.title.getCurrentTitle().text,
			image = args.image,
			icon = args.icon,
			-- ticks = ticks,
			method = 'construction',
			facility = 'Player-owned house',
			mats = jsonmats,
			skill = 'Construction',
			level = tonumber(level) or '?',
			exp = tonumber(experience) or '?',
			quantity = 1
		}
		mw.logObject(tojson)
		local json = mw.text.nowiki(mw.text.jsonEncode(tojson))

		table.insert(smw,'- - - - - - - - - -')
		table.insert(smw,'Production JSON:')
		table.insert(smw,string.format('[[Production JSON::%s]]',json))
		ret	:tag('div')
				:attr('smw-infobox-data')
				:addClass('hidden')
				:css('display','none')
				:wikitext(table.concat(smw,'\n'))
			:done()
	end

	return ret:tostring()
	
end

function imgarg(arg)
	if infobox.isDefined(arg) then
		return cleanimg{ file = arg, width = 300, height = 300 }
	end
	return nil
end

function iconarg(arg)
	if infobox.isDefined(arg) then
		return '[[File:'..arg..']]'
	end
	return nil
end

function bool(arg)
	if infobox.isDefined(arg) then
		return yesno(arg) and 'Yes' or 'No'
	end
end

function lvlarg(level)
	if infobox.isDefined(level) then
		return level..' '..skillpic('Construction')
	end
	return nil
end

function link(arg)
	if infobox.isDefined(arg) then
		if arg:find('%[%[') then
			return arg
		end
		return '[['..arg..']]'
	end
	return nil
end

function actionarg(arg)
	if infobox.isDefined(arg) then
		local r = string.gsub(arg, ',%s*', '<br />\'\'\'&bull;\'\'\' ')
		r = '\'\'\'&bull;\'\'\' ' .. r
		r = mw.ustring.gsub(r, '%[*[Ww]alk [Hh]ere%]*', '[[Walk here|Walk here]]')
		r = mw.ustring.gsub(r, '%[*[Ee]xamine%]*', '[[Examine]]')
		if not r:find('Examine') then
			r = r .. '<br />\'\'\'&bull;\'\'\' [[Examine]]'
		end
		return r
	else
		return nil
	end
end

function actionsmw(arg)
	if infobox.isDefined(arg) then
		local r = string.gsub(arg, ',%s*', infobox.splitpoint)
		r = mw.ustring.gsub(r, '%[*[Ww]alk [Hh]ere%]*', 'Walk here')
		r = mw.ustring.gsub(r, '%[*[Ee]xamine%]*', 'Examine')
		if not r:find('Examine') then
			r = r .. infobox.splitpoint .. 'Examine'
		end
		return r
	end
	return nil
end

function iddisp(id)
	if infobox.isDefined(id) then
		local r = string.gsub(id, ', *', ', ')
		return r
	end
	return nil
end
function idsmw(id)
	if infobox.isDefined(id) then
		local r = string.gsub(id, ', *', infobox.splitpoint)
		return r
	end
	return nil
end

function make_chisel_links(id, name)
	local link = 'https://chisel.weirdgloop.org/gazproj/mrod'
	if infobox.isDefined(id) then
		local ids = mw.text.split(id, infobox.splitpoint)
		if #ids == 0 then
			local _name = name:gsub(' ', '%%20')
			link = string.format('[%s#%s MROD]', link, _name)
		elseif #ids == 1 then
			link = string.format('[%sid?%s#%s-%s MROD%s]', link, ids[1], ids[1]-15, ids[1]+15, ids[1])
		else
			link = string.format('[%sid?%s#%s', link, ids[1], ids[1])
			for i,j in ipairs(ids) do
				if i > 1 then
					link = link .. '@' .. j
				end
			end
			link = link .. ' MROD]'
		end
	else
		local _name = name:gsub(' ', '%%20')
		link = string.format('[%s#%s MROD]', link, _name)
	end
	return link
end

-- Categories
function addcategories(args,catargs)
	local ret = { 'Built items' }
	
	local cat_map = {
		-- Added if the parameter has content
		defined = {
			-- parameter = 'category name',
		},
		-- Added if the parameter has no content
		notdefined = {
			image = 'Needs image',
			members = 'Needs members status',
			release = 'Needs release date',
			actions = 'Needs actions added',
			examine = 'Needs examine added',
			id = 'Needs ID'
			},
	}
	
	-- Run and add mapped categories
	for n, v in pairs(cat_map.defined) do
		if catargs[n] and catargs[n].one_defined then
			table.insert(ret,v)
		end
	end
	for n, v in pairs(cat_map.notdefined) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret,v)
		end
	end
	
	-- combine table and format category wikicode
	for i, v in ipairs(ret) do
		if (v ~= '') then
			ret[i] = string.format('[[Category:%s]]',v)
		end
	end

	return table.concat(ret,'')
end

return p
-- </nowiki>