Module:Gifted item

From Coral Island Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Gifted item/doc

local p = {}
local lib = require('Module:Feature')
local Icon_list = require('Module:Icon list').buildList
local characters = mw.loadData('Module:Gifted item/data')
local order = { 'love', 'like', 'neutral', 'dislike', 'hate' }
local categories = mw.html.create()
local noDupe = {}
local blankReturn = {
	love	= 'No character loves this item.',
	like	= 'No character likes this item.',
	neutral = 'No character is neutral towards this item.',
	dislike = 'No character dislikes this item.',
	hate	= 'No character hates this item.'
}
local function adj(preference, cap)
	if preference == 'neutral' then
		if cap then return 'Neutral' else return 'neutral' end
	else
		if cap then return preference:gsub("^%l", string.upper) .. 'd' else return preference .. 'd' end
	end
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, { parentFirst = true })
	
	return p._main(args)
end

function p._main(args)
	local data = p.parseCharacters(args)
	local result = mw.html.create('table'):addClass('article-table gift-table tdc1 tdbg1')
	local tr = result:tag('tr')
	tr:tag('th'):css{ width = '5%' }:wikitext('[[File:Gift icon.png|24px|link=Friendship]]')
	tr:tag('th'):wikitext('Characters')
	-- mw.logObject(data)
	--build table
	for _, preference in ipairs(order) do
		local tr = result:tag('tr')
		--type
		if preference == 'neutral' then
			tr:tag('th'):wikitext('Neutral')
		else
			tr:tag('th'):wikitext('[[File:', adj(preference), ' gift.png|16px|link=Gift]] ', adj(preference, true))
		end
		
		--characters
		local td = tr:tag('td')
		if next(data[preference]) == nil then
			td:tag('i'):wikitext(blankReturn[preference])
		else
			local list = {}
			local settings = { suffix = ' icon' }
			for _, character in ipairs(characters) do
				if data[preference][character] and (noDupe[character] == nil or noDupe[character] == preference) then
					categories:wikitext('[[Category:', adj(preference), ' gift by ', character,']]')
					table.insert(list, character)
					if noDupe[character] == nil then noDupe[character] = preference end
				end
			end
			if #list >= 6 then settings.cols = 3 end
			td:node(Icon_list(list, settings))
		end
	end
	
	--add categories
	result:node(require('Module:Namespace detect').main{main=categories})
	
	--return table with categories
	return result
end

function p.parseCharacters(args)
	local data = {
		love	= {},
		like	= {},
		neutral = {},
		dislike = {},
		hate	= {}
	}
	
	for _, preference in ipairs(order) do
		if lib.isNotEmpty(args[preference]) then
			if args[preference] == 'universal' then
				categories:wikitext('[[Category:Item universally ', adj(preference),']]')
				for _, character in ipairs(characters) do
					data[preference][character] = true
				end
			else
				for _, character in ipairs(lib.split(args[preference], ';')) do
					if noDupe[character] == nil then noDupe[character] = preference end
					if noDupe[character] == preference then
						data[preference][character] = true
					else
						categories:wikitext('[[Category:Item with character with multiple preferences]]')
					end
				end
			end
		end
	end
	
	--return parsed characters
	return data
end

return p