Module:Databox Pet

From WIDEVERSE Wiki
Revision as of 18:58, 3 September 2021 by en>Habblet (set food and growthtime to No automatically depending on the pet category)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Databox Pet/doc

--------------------------
-- Module for [[Template:Databox Pet]]
------------------------
local p = {}

-- "imports"
local onmain = require('Module:Mainonly').on_main
local yesno = require('Module:Yesno')
local infobox = require('Module:Infobox')
local cleanimg = require('Module:Clean image').clean

local intcats = {
	boss = 'Boss pets',
	companion = 'Companion pets',
	follower = 'Follower pets',
	legendary = 'Legendary pets',
	skilling = 'Skilling pets',
}

local overridecats = {
	boss = true,
	companion = true,
	follower = false,
	legendary = true,
	skilling = true,
}

local no_food = {
	boss = true,
}

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

	ret:defineParams{
		{ name = 'name', func = 'name' },
		{ name = 'image', func = has_content },
		{ name = 'caption', func = { name = captionarg, params = { 'image', 'name' } } },
		{ name = 'vanchor', func = { name = 'has_content', params = { 'version' }, flag = 'p' } },
		{ name = 'location', func = 'has_content' },
		{ name = 'members', func = 'has_content' },
		{ name = 'interface', func = 'has_content' },
		{ name = 'intcat', func = intcatarg },
		{ name = 'override', func = { name = overridearg, params = { 'override', 'intcat' }, flag = { 'p', 'p' } } },
		{ name = 'skillrequired', func = 'has_content' },
		{ name = 'questrequired', func = 'has_content' },
		{ name = 'food', func = { name = foodarg, params = { 'food', 'intcat' }, flag = { 'p', 'p' } } },
		{ name = 'growthtime', func = { name = growthtimearg, params = { 'growthtime', 'intcat' }, flag = { 'p', 'p' } } },
		{ name = 'colours', func = 'has_content' },
	}

	ret:useSMW({
		members = 'Is members only'
	})

	ret:useSMWSubobject({
		members = 'Is members only'
	})

	ret:setMaxButtons(4)
	ret:setAddRSWDataboxClass(true)
	ret:create()
	ret:cleanParams()
	ret:customButtonPlacement(true)

	ret:linkParams{
		image = 'imagebackground'
	}
	
	ret:defineLinks({ links = {{ 'Template:%s', 'Databox' },
		{ 'Template_talk:%s', 'Talk page' }}, colspan = '2' })

	ret:defineName('Databox Pet')
	ret:addClass('wikitable')

	ret:addButtonsCaption()

	-- PARAMETER: caption
	ret:addRow{
		{ tag = 'argh', content = 'caption', colspan = '2', class = 'infobox-header' }
	}

	-- PARAMETER: location
	ret:addRow{
		{ tag = 'th', content = 'Source' },
		{ tag = 'argd', content = 'location' }
	}
	
	-- PARAMETER: interface
	:addRow{
		{ tag = 'th', content = '[[Pets#Interface pets|Interface]]' },
		{ tag = 'argd', content = 'interface' }
	}
	
	-- PARAMETER: intcat
	if ret:paramGrep('interface', function(x) return yesno(x) ~= false end) then
		ret:addRow{
			{ tag = 'th', content = 'Category' },
			{ tag = 'argd', content = 'intcat' }
		}
	end
	
	-- PARAMETER: override
	if ret:paramGrep('interface', function(x) return yesno(x) ~= false end) then
		ret:addRow{
			{ tag = 'th', content = 'Override' },
			{ tag = 'argd', content = 'override' }
		}
	end
	
	-- PARAMETER: skillrequired
	ret:addRow{
		{ tag = 'th', content = '[[Skill]] req.' },
		{ tag = 'argd', content = 'skillrequired' }
	}
	
	-- PARAMETER: questrequired
	:addRow{
		{ tag = 'th', content = '[[Quest]] req.' },
		{ tag = 'argd', content = 'questrequired' }
	}
	
	-- PARAMETER: food
	:addRow{
		{ tag = 'th', content = 'Food' },
		{ tag = 'argd', content = 'food' }
	}
	
	-- PARAMETER: growthtime
	:addRow{
		{ tag = 'th', content = 'Growth time' },
		{ tag = 'argd', content = 'growthtime' }
	}
	
	-- PARAMETER: colours
	:addRow{
		{ tag = 'th', content = 'Colours' },
		{ tag = 'argd', content = 'colours' }
	}
	
	ret:finish()
	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end
	return ret:tostring()
end

-- Add [[File: and |link=...]] to image, if it's defined
function captionarg(image, name)
	local link = mw.title.getCurrentTitle().text
	
	if not infobox.isDefined(name) then
		name = link
	end
	
	if infobox.isDefined(image) then
		image = '[[File:' .. image:gsub('File:', '') .. '|link=' .. link .. ']]'
	-- anti-redlink
	elseif link == 'Databox Pet/preload'
			or link == 'PreloadPage/Orphanage' then
		image = ''
	else
		image = '[[File:' .. name .. '.png|link=' .. name ..']]'
	end
	
	return image .. ' ' .. name
end

function intcatarg(intcat)
	if infobox.isDefined(intcat) then
		local cat = intcats[string.lower(intcat)]
		if cat then
			return '[[:Category:' .. cat .. '|' .. intcat .. ']]'
		end
	end
end

function overridearg(override, intcat)
	if infobox.isDefined(override) then
		return override
	end
	if infobox.isDefined(intcat) then
		local overrideable = overridecats[string.lower(intcat)]
		if overrideable == true then
			return 'Yes'
		elseif overrideable == false then
			return 'No'
		else
			return 'Unknown'
		end
	end
end

function foodarg(food, intcat)
	if infobox.isDefined(food) then
		return food
	elseif infobox.isDefined(intcat) then
		if no_food[string.lower(intcat)] then
			return 'No'
		end
	end
end

function growthtimearg(growthtime, intcat)
	if infobox.isDefined(growthtime) then
		if tonumber(growthtime) then
			return growthtime .. ' minutes'
		else
			return growthtime
		end
	elseif infobox.isDefined(intcat) then
		if no_food[string.lower(intcat)] then
			return 'No'
		end
	end
end

function addcategories(args, catargs)
	function is_def(x, y)
		y = y or 'd'
		if infobox.isDefined(x) then
			return infobox.isDefined(x[y])
		end
		return false
	end
	
	local ret = { 'Pets' }
	
	local cat_map = {
		-- Added if the parameter has content
		defined = {
		},
		-- Added if the parameter has no content
		notdefined = {
			members = 'Needs members status',
		},
	}
	
	-- Interface category
	if args.intcat.switches then
		for i,v in ipairs(args.intcat.switches) do
			if infobox.isDefined(v) then
				for k,cat in pairs(intcats) do
					local catmatch = string.lower(v):find(k)
					if catmatch then
						table.insert(ret, cat)
					end
				end
				
			end
		end
	elseif args.intcat.d and infobox.isDefined(args.intcat.d) then
		for k,cat in pairs(intcats) do
			local catmatch = string.lower(args.intcat.d):find(k)
			if catmatch then
				table.insert(ret, cat)
			end
		end
	end
	
	-- 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