Module:Quest overview/steps

From Sun Haven Wiki
Jump to navigation Jump to search

This module is used in the {{Quest overview/steps}} template.


Subpages

local p = {}
local lib = require('Module:Feature')
local Icon = require('Module:Icon')._main
local category = mw.html.create()

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		removeBlanks = false,
		wrapper = { 'Template:Quest overview/steps' }
	})
	return p._main(args)
end

function p._main(args)
	local collectionType = args.colType or mw.title.getCurrentTitle().rootText
	local _table = mw.html.create('table'):addClass('article-table tablemax50 tdwid1 td30wid2 tdc1 thc1 thc2 thc3 thc4 tdbg1')
	local options = {
		delim = args.delim or ';',
		amount_delim = args.amount_delim or '*',
		quality_delim = args.quality_delim or '/',
	}
	
	--headers
	local tr = _table:tag('tr')
	tr:tag('th'):wikitext('Step')
	tr:tag('th'):wikitext('Requires')
	tr:tag('th'):wikitext('Rewards')
	tr:tag('th'):wikitext('Bonus')
	
	local i = '1'
	while args[i .. '_step'] do
		local tr = _table:tag('tr'):attr('id', args[i .. '_step'])
		tr:tag('td'):wikitext('', args[i .. '_step'], '')
		options.str = args[i .. '_requires']
		tr:tag('td'):wikitext('', args[i .. '_requires'], '')
		options.str = args[i .. '_rewards']
		tr:tag('td'):node(p.parseItems(options, false, 'Quest rewards'))
		options.str = args[i .. '_bonus']
		tr:tag('td'):node(p.parseItems(options, false, 'Bonus rewards'))
		i = tostring(tonumber(i)+1)
	end
	_table:node(require('Module:Namespace detect').main{main=category})
	return _table
end

function p.checkQuality(entry, notePattern)
	if notePattern then
		item, quality = entry:match(notePattern)
		if item == nil then -- will be nil if note is not present
			return entry
		end
		return item, quality
	end
	return entry
end

function p.parseItems(data, columns, catPrefix)
	if not tostring(data.str) then return '' end
	local itemDelim = data.delim
	local countDelim = data.amount_delim
	local qualityDelim = data.quality_delim
	
	-- put all text after the first qualityDelim into the second capture
	local qualityPattern
	if lib.isNotEmpty(qualityDelim) then
		qualityPattern = '^(.-)' .. qualityDelim .. '(.*)$'
	end
	
	local items = mw.html.create():tag('div')
	if tonumber(columns) ~= nil then
		items:addClass('columntemplate'):css{ ['-moz-column-count'] = columns, ['-webkit-column-count'] = columns, ['column-count'] = columns }
	end
	-- mw.logObject(data) -- debug
	for i, entry in ipairs(lib.split(data.str, itemDelim, {noTrim=true})) do
		if not lib.isEmpty(entry) then
			local item, quality = p.checkQuality(entry, qualityPattern) --check for item quality
			local item_parts = lib.split(item, countDelim) --check for item amount
			local name = item_parts[1]
			local amount = item_parts[2]
			local br = true
			items:node(Icon{
				name = name,
				amount = amount,
				q = quality or '0',
				br = true
			})
			--if catPrefix then
			--	category:wikitext('[[Category:', catPrefix, ' ', string.lower(name) ,']]')
		--	end
		end
	end
	return items
end

return p