Module:Offering need

From Coral Island Wiki
Jump to navigation Jump to search

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

local p = {}
local lib = require('Module:Feature')
local Offering = require('Module:Offering')
local Parse = require('Module:Parser').getTemplateArgs
local verifyQuality = require('Module:Icon').verifyQuality

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentOnly = false,
		wrapper = { 'Template:Offering need' }
	})
	return p._main(frame.args)
end

function p._main(args)
	local data = Parse('Lake Temple', { only = 'Offering' })
	local out  = mw.html.create()
	local target = args.item
	local plural = args.item_plural
	local group = args.group
	local matches = {}
	for _, call in ipairs(data) do
		local settings = {
			delim = call.delim or ';',
			amount_delim = call.amount_delim or '*',
			quality_pattern = '^(.-)' .. (call.quality_delim or '/') .. '(.*)$',
		}
		local i = '1'
		while call[i .. '_items'] do
			for item in lib.gsplit(call[i .. '_items'], settings.delim) do
				local pre, quality = Offering.checkQuality(item, settings.quality_pattern)
				quality = verifyQuality(quality)
				local pre_parts = lib.split(pre, settings.amount_delim)
				local name = string.lower(pre_parts[1])
				local amount = pre_parts[2] or 1
				if name == target then
					local matching = mw.html.create()
					matching:tag('b'):wikitext(amount)
					matching:wikitext(' ', lib.ternary(tonumber(amount)>1, plural, name), ' of ')
					if quality then
						matching:wikitext(' at least ', quality)
					else
						matching:wikitext(' any')
					end
					matching:wikitext(' quality', lib.ternary(tonumber(amount)>1, ' are ', ' is '), 'needed')
					matching:wikitext(' for the [[Lake Temple#', call[i .. '_name'], '|', call[i .. '_name'], ' offering]] in the  ', call.altar, ' altar')
					table.insert(matches, matching)
				elseif group ~= nil and name == ('any ' .. group) then
					local matching = mw.html.create()
					matching:tag('b'):wikitext(amount)
					matching:wikitext(' of [[', name, ']]', ' of ')
					if quality then
						matching:wikitext(' at least ', quality)
					else
						matching:wikitext(' any')
					end
					matching:wikitext(' quality', lib.ternary(tonumber(amount)>1, ' are ', ' is '), 'needed')
					matching:wikitext(' for the [[Lake Temple#', call[i .. '_name'], '|', call[i .. '_name'], ' offering]] in the  ', call.altar, ' altar')
					table.insert(matches, matching)
				end
			end
			i = tostring(tonumber(i)+1)
		end
	end
	if #matches == 1 then
		out:node(matches[1]):wikitext('.')
	elseif #matches == 2 then
		out:node(matches[1]):wikitext(', and '):node(matches[2]):wikitext('.')
	elseif #matches > 2 then
		for match_num, match_str in ipairs(matches) do
			if match_num  == 1 then
				out:node(match_str)
			elseif match_num == #matches then
				out:wikitext(', '):node(match_str)
			else
				out:wikitext(', and '):node(match_str):wikitext('.')
			end
		end
	end
	
	return out
end

return p