Module:Item

From Palia Wiki
Jump to navigation Jump to search

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

local common = require('Module:Common')
local l10n   = require('Module:Common/Language')
local files  = require('Module:Common/Files')
local links  = require('Module:LocalizedLink')

local p = {}

local function outputInStyle(data, i, count, link)
	if data['style'] == 0 or data['style'] == nil then
		
		local span = mw.html.create('span')
			:attr({
				['class']     = 'template-item',
				['data-item'] = data['item']
			})
			:wikitext(i .. (count and " '''" .. count .. "'''" or ''))
		
		return tostring(span:allDone()) .. ' ' .. link
		
	elseif data['style'] == 1 then
		
		local tab   = mw.html.create('table')
		tab:attr({
			['class']     = 'template-item',
			['data-item'] = data['item']
		})
		
		local tr = tab:tag('tbody')
			:tag('tr')
			:tag('td')
				:wikitext(i .. ' ' .. link)
			:done()
			:tag('td')
				:wikitext(count)
			:done()
		
		return tostring(tr:allDone())
		
	end
	
	return ''
end

local function getLink(data)
	return links.page(data['item'], data['lang'], {
		['display'] = data['display']
	})
end

local function getImage(data, link, display)
	local png   = nil -- Name of the image we're linking to
	local image = nil -- Image wikitext
	
	if files.hasFile(data['item'] .. '.png') then
		image = data['item']
	end
	
	return '[[File:' .. (image or 'Unknown') .. '.png|link=' .. link .. '|28px|' .. (display or data['item']) .. ']]'
		.. ((data[3] and '[[File:SQ.png|link=|class=quality|28px]]') or '')
end

local function getLinkAndImage(data)
	local link
	local image
	local lowercased = data['item']:lower()
	local any = lowercased:match('^any (.*)$')
	
	if not any then
		local href, display = getLink(data)
		
		image = getImage(data, href, display)
		link  = ('%s|%s'):format(href, display)
	else
		-- Default when no overrides
		link  = data['item']
		image = data['item']
		
		if any == 'arrow' then
			link  = 'Arrows'
			
		elseif any == 'fish' or any == 'catfish' or any == 'trout' or any == 'bass' or any == 'rare fish' then
			link  = 'Fish#list'
			image = ''
		elseif any == 'vegetable' then
			link  = 'Vegetables'
			image = ''
		elseif any == 'mushroom' then
			link  = 'Mushroom'
			image = ''
		elseif any == 'bug' or any == 'uncommon bug' then
			link  = 'Bugs'
			image = ''
		elseif any == 'spice' then
			link  = 'Spices'
			image = ''
		elseif any == 'firework' then
			link  = 'Fireworks'
			image = ''
		elseif any == 'fertilizer' then
			link  = 'Category:Fertilizer'
			image = ''
		elseif any == 'gatherable' then
			link  = 'Category:Gatherable'
			image = ''
		elseif any == 'crop' then
			link  = 'Crops'
			image = ''
		elseif any == 'crab' then
			link  = 'Crabs'
			image = ''
		elseif any == 'fruit' then
			link  = 'Fruit'
			image = ''
		elseif any == 'seed' then
			link  = 'Seeds'
			image = 'Samara'
		elseif any == 'red meat' then
			link  = 'Red Meat'
			image = 'Red Meat'
		elseif any == 'kimchi' then
			link  = 'Bok Choy Kimchi'
			image = 'Bok Choy Kimchi'
		elseif any == 'pickle' or any == 'preserve' or any == 'jam' then
			link  = 'Preserves Jar'
			image = 'Picked Onions'
		elseif any == 'flower' then
			link  = 'Flowers'
			image = 'Sundrop Lily'
		elseif any == 'smoke bomb' then
			link  = 'Smoke Bombs'
			image = 'Sticky Smoke Bomb'
		end
	end
	
	return link, image
end

local function main(data)
	-- Get the link and image using the page
	local link, image = getLinkAndImage(data)
	
	-- Item img and SQ tag
	local i = mw.html.create('i')
		:addClass('template-item-rel')
		:wikitext(image)
		:allDone()
	
	local count = nil
	if data['amount'] then
		local num = tonumber(data['amount'])
		
		-- TODO: Account for string counts such as 'Recipe' instead of a number
		count = (before or '')
			.. (num and mw.getLanguage(data['lang'] or 'en'):formatNum(num) or data['amount'])
			.. (after or '')
	end
	
	return outputInStyle(
		data,
		tostring(i),
		count,
		'[[:' .. link .. ']]'
	)
end

function p.main(frame)
	local args = common.getArgs(frame)
	local lang = l10n.getContentLanguage(frame)
	local page = l10n.untranslatedTitle(args[1], lang)
	
	args['item'] = page:gsub('#', '')
	args['amount'] = args[2]
	
	if not args['lang'] then
		args['lang'] = lang
	end
	
	return main(args)
end

function p.item(item, amount, lang, data)
	if type(item) ~= 'string' then
		error('Item must be provided', 2)
	elseif amount and type(amount) ~= 'number' then
		error('Invalid item quantity ' .. tostring(amount), 2)
	elseif not data then
		data = {}
	end
	
	data['item'] = item
	data['lang'] = lang
	
	if amount then
		data['amount'] = amount
	end
	
	return main(data)
end

function p.items(items, lang)
	if type(items) ~= 'table' then
		error('Items must be provided a list', 2)
	elseif #items <= 0 then
		return ''
	else
		local copies = {}
		local pngs = {}
		local outs = {}
		
		for _, data in ipairs(items) do
			-- Must have an item name
			if data['item'] then
				local copy = common.shallowCopy(data)
				
				copy['lang'] = lang
				
				table.insert(copies, copy)
				table.insert(pngs, data['item'] .. '.png')
			end
		end
		
		-- Check (and cache) if the files exist for the item images
		files.hasFiles(pngs)
		
		-- Generate the set of formatted output
		for _, data in ipairs(copies) do
			table.insert(outs, main(data))
		end
		
		return outs
	end
end

return p