Module:Infobox Item: Difference between revisions

From Discord Dungeons Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 120: Line 120:
{ tag = 'th', content = '[[Items#Tradeability|Tradeable]]', colspan = '7' },
{ tag = 'th', content = '[[Items#Tradeability|Tradeable]]', colspan = '7' },
{ tag = 'argd', content = 'tradeable', colspan = '13' }
{ tag = 'argd', content = 'tradeable', colspan = '13' }
}
ret:addRow{
{ tag = 'th', content = '[[Worn Equipment|Equipable]]', colspan = '7' },
{ tag = 'argd', content = 'equipable', colspan = '13' }
}
:addRow{
{ tag = 'th', content = '[[Stackable items|Stackable]]', colspan = '7' },
{ tag = 'argd', content = 'stackable', colspan = '13' }
}
}



Revision as of 14:53, 31 March 2022

Documentation for this module may be created at Module:Infobox Item/doc

--------------------------
-- Module for [[Template:Infobox Item]]
------------------------
local p = {}

local infobox = require('Module:Infobox')
local onmain = require('Module:Mainonly').on_main
local commas = require('Module:Addcommas')._add
VariablesLua = mw.ext.VariablesLua

function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)

	ret:defineParams{
		--- General 
		{ name = 'name', func = 'name' },
		{ name = 'version', func = 'has_content' },
		{ name = 'aka', func = 'has_content' },
		{ name = 'image', func = 'image' },
		{ name = 'image_smw', func = { name = image_smw, params = { 'image' }, flag = 'p' } },

		{ name = 'release', func = 'release' },
		{ name = 'removal', func = 'removal' },
		{ name = 'members', func = 'has_content' },
		{ name = 'quest', func = 'has_content' },

		{ name = 'tradeable', func = tradeablearg },
		{ name = 'equipable', func = 'has_content' },
		{ name = 'usable', func = 'has_content' },

		{ name = 'buyvalue', func = 'has_content' },
		{ name = 'sellvalue', func = 'has_content' },


		-- css class name to hide rows on undefined versions
		{ name = 'market_sell_limit', func = 'has_content'},
		{ name = 'market_buy_limit', func = 'has_content' },


        { name = 'usesinfobox', func = { name = tostring, params = { 'Item' }, flag = 'r' } },
		{ name = 'id', func = 'has_content' },
	}

	ret:setMaxButtons(7)
	ret:create()
	ret:cleanParams()
	ret:customButtonPlacement(true)
	ret:setDefaultVersionSMW(true)


	ret:defineLinks({ hide = true })

	ret:useSMWOne({
		members = 'All Is members only',
		id_smw = 'All Item ID',
		image_smw = 'All Image',
	})

	ret:useSMWSubobject({
		version = 'Version anchor',
		release = 'Release date',
		id_smw = 'Item ID',
		description = 'Description',
		members = 'Is members only',
		buyvalue = 'Buyable for',
		sellvalue = 'Sellable for',
		image_smw = 'Image',
		market_buy_limit = 'Buy limits',
		market_sell_limit = 'Sell limits',
		usesinfobox = 'Uses infobox',
	})

	ret:addButtonsCaption()

	ret:defineName('Infobox Item')
	ret:addClass('infobox-item')

	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'argd', content = 'image', class = 'infobox-image inventory-image infobox-full-width-content', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Released', colspan = '7' },
		{ tag = 'argd', content = 'release', colspan = '13' }
	}

	if ret:paramDefined('removal') then
		ret:addRow{
			{ tag = 'th', content = 'Removal', colspan = '7' },
			{ tag = 'argd', content = 'removal', colspan = '13' }
		}
	end

	if ret:paramDefined('aka') then
		ret:addRow{
			{ tag = 'th', content = 'Also called', colspan = '7' },
			{ tag = 'argd', content = 'aka', colspan = '13' }
		}
	end

	ret:addRow{
		{ tag = 'th', content = '[[Members]]', colspan = '7' },
		{ tag = 'argd', content = 'members', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Quest items|Quest item]]', colspan = '7' },
		{ tag = 'argd', content = 'quest', colspan = '13' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Properties', class = 'infobox-subheader', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = '[[Items#Tradeability|Tradeable]]', colspan = '7' },
		{ tag = 'argd', content = 'tradeable', colspan = '13' }
	}

	
	ret:pad(20)

	ret:addRow{
		{ tag = 'th', content = 'Advanced data', class = 'infobox-subheader', colspan = '20' },
		meta = {addClass = 'advanced-data'}
	}
	:pad(20, 'advanced-data')
	:addRow{
		{ tag = 'th', content = 'Item ID', colspan = '7' },
		{ tag = 'argd', content = 'id',  colspan = '13' },
		meta = {addClass = 'advanced-data'}
	}
	:pad(20, 'advanced-data')

	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end

	return ret:tostring()
end

function tradeablearg(arg)
	if not infobox.isDefined(arg) then
		return nil
	end

	arg = string.lower(arg)
	if arg == 'yes' then
		return 'Yes'
	elseif arg == 'no' then
		return 'No'
	end
	return arg
end

-- Return raw value as a number, or nil if not defined
function valraw(arg)
	if not infobox.isDefined(arg) then
		return nil
	end

	return tonumber(arg)
end

function valuearg(value)
	if not infobox.isDefined(value) then
		return nil
	end

	return plural('coin', value)
end

-- split items with multiple images for smw (e.g. [[File:Arrow 1.png]] [[File:Arrow 2.png]])
function image_smw(arg)
	local _img = {}
	for i in string.gmatch(arg, "[Ff]ile:.-%.png") do
		table.insert(_img, i)
	end
	if #_img == 0 then
		return nil
	end
	return table.concat(_img, '&&SPLITPOINT&&')
end


function idsmw(id)
	if not infobox.isDefined(id) then
		return nil
	end
	return string.gsub(id, ',', '&&SPLITPOINT&&')
end

-- red ERR span with title hover for explanation
function badarg(argname, argmessage)
	return '<span '..
			'title="The parameter «'..argname..'» '..argmessage..'" '..
			'style="color:red; font-weight:bold; cursor:help; border-bottom:1px dotted red;">'..
			'ERR</span>'
end

function plural(word, amount, alt_plural_word)
	local output_amount = commas(tonumber(amount) or 1)
	if tonumber(amount) == 1 then
		return string.format('%s %s', output_amount, word)
	elseif alt_plural_word then
		return string.format('%s %s', output_amount, alt_plural_word)
	else
		return string.format('%s %ss', output_amount, word)
	end
end

function has_three_decimals(weight)
	local decimals = string.match(weight, "%.(.*)")
	if not decimals then
		return false
	end
	return string.len(decimals) == 3
end

function addcategories(args, catargs)
	local ret = { 'Items' }
	local cat_map = {
		-- Added if the parameter has content
		defined = {
			aka = 'Pages with AKA'
		},
		-- Added if the parameter has no content
		notdefined = {
			image = 'Needs image',
			members = 'Needs members status',
			release = 'Needs release date',
			description = 'Needs description added',
			level = 'Needs level',
			buyvalue = 'Items missing buy price',
			sellvalue = 'Items missing sell price',
			quest = 'Items missing quest',
			id = 'Needs ID',
			market_sell_limit = 'Needs sell limit',
			market_buy_limit = 'Needs buy limit',
		},
		-- Parameters that have text
		-- map a category to a value
		matches = {
			members = { yes = 'Members\' items', no = 'Free-to-play items' },
			equipable = { yes = 'Equipable items' },
			edible = { yes = 'Edible items' },
			tradeable = { yes = 'Tradeable items', no = 'Untradeable items' },
		}
	}

	-- defined 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

	-- undefined categories
	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

	-- searches
	for n, v in pairs(cat_map.matches) do
		for m, w in pairs(v) do
			if args[n] then
				if string.lower(tostring(args[n].d) or '') == m then
					table.insert(ret, w)
				end
				if args[n].switches then
					for _, x in ipairs(args[n].switches) do
						if string.lower(tostring(x)) == m then
							table.insert(ret, w)
						end
					end
				end
			end
		end
	end

	-- quest items
	-- just look for a link
	if args.quest.d:find('%[%[') then
		table.insert(ret, 'Quest items')
	elseif args.quest.switches then
		for _, v in ipairs(args.quest.switches) do
			if v:find('%[%[') then
				table.insert(ret, 'Quest items')
				break
			end
		end
	end

	-- ids
	if not catargs.id.all_defined then
		table.insert(ret, 'Needs ID')
	end


	-- combine table and format category wikicode
	for i, v in ipairs(ret) do
		ret[i] = string.format('[[Category:%s]]', v)
	end

	return table.concat(ret, '')
end

return p