Module:Variant

From WIDEVERSE Wiki
Jump to navigation Jump to search

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

-- Used by [Template:Variant]
-- <nowiki>
local p = {}
local yesno = require('Module:Yesno')
local title = string.lower(mw.title.getCurrentTitle().text)
local info = {undyed=false}

local values = {
	['main hand'] = 'mh',
	['main-hand'] = 'mh',
	['mainhand'] = 'mh',
	['off-hand'] = 'oh',
	['offhand'] = 'oh',
	['superior'] = 'tier',
	['inferior'] = 'tier',
	['achto'] = 'tier',
	['attuned'] = 'tier',
	['enhanced'] = 'tier',
	['refined'] = 'tier',
	['augmented'] = 'augmented',
	['barrows dyed'] = 'dyed',
	['barrows'] = 'dyed',
	['shadow dyed'] = 'dyed',
	['shadow'] = 'dyed',
	['third-age dyed'] = 'dyed',
	['third-age'] = 'dyed',
	['third age dyed'] = 'dyed',
	['third age'] = 'dyed',
	['blood dyed'] = 'dyed',
	['blood'] = 'dyed',
	['ice dyed'] = 'dyed',
	['ice'] = 'dyed',
	['red'] = 'dyed',
	['orange'] = 'dyed',
	['yellow'] = 'dyed',
	['green'] = 'dyed',
	['blue'] = 'dyed',
	['purple'] = 'dyed',
	['black'] = 'dyed',
	['white'] = 'dyed',
	['pink'] = 'dyed',
	['undyed'] = 'dyed',
}

-- as above but specifically tt dyes for correct auto-parsing
local tt_dyes = {
	['barrows dyed'] = true,
	['barrows'] = true,
	['shadow dyed'] = true,
	['shadow'] = true,
	['third-age dyed'] = true,
	['third-age'] = true,
	['third age dyed'] = true,
	['third age'] = true,
	['blood dyed'] = true,
	['blood'] = true,
	['ice dyed'] = true,
	['ice'] = true,
}
local other_dyes = {
	['red'] = true,
	['orange'] = true,
	['yellow'] = true,
	['green'] = true,
	['blue'] = true,
	['purple'] = true,
	['black'] = true,
	['white'] = true,
	['pink'] = true,
	['undyed'] = true,
}
local base_types = {
	['undyed'] = true
}
local msr_oh_replace = {
	'elder rune',
	'bane',
	'necronium',
	'orikalkum',
	'rune',
	'adamant',
	'mithril',
	'steel',
	'iron',
	'bronze',
}

function p.main(frame)
	local args = frame:getParent().args
	local links = {}
	local misc = {}
   	local ret = {}
 	ret.mh = {}
 	ret.oh = {}
	ret.tier = {}
	ret.augmented = {}
	ret.dyed = {}
	ret.plus = {}
	ret.ohplus = {}
	if args['max+'] then
		ret.maxplus = tonumber(args['max+'])
		if yesno(args.msoh) then
			ret.msoh = true
		end
	end
	for key,value in pairs(args) do
		local kind, item  = '', ''
		local kind_t
		if not (key == 'max+' or key == 'msoh') then
			-- numeric key => unnamed param
			if tonumber(key) then
				-- the value is the kind and the item is "kind PAGENAME"
				kind = string.lower(value)
			else
				-- else kind is key and item is value
				kind = string.lower(key)
				item = value
			end
	
			-- apparently linebreaks aren't actually trimmed by the parser (when they really should be)????
			item = mw.text.trim(item)
			kind = mw.text.trim(kind)
	
			if values[kind] then
				links[kind] = getLink(kind, item)
			else
				table.insert(misc, getLink(kind, item))
			end
		end
	end

	-- put links into order
	for i,v in pairs(values) do
		if links[i] then
			table.insert(ret[v],links[i])
		end
	end
	
	if ret.maxplus then
		ret.plus = getPlusLinks(ret.maxplus)
		if ret.msoh then
			ret.ohplus = getOHPlusLinks(ret.maxplus)
		end
	end

	local _ret = mw.html.create()
	
	if ret.plus[1] then
		_ret:tag('div')
			:addClass('seealso')
			:wikitext('For this item\'s upgraded variants, see ')
			:wikitext(table.concat(ret.plus, ', ') .. '.')
		:done()
		if ret.msoh then
			local msss = 'For this item\'s off-hand variants, see '
			if info.isoffhand then
				msss = 'For this item\'s main hand variants, see '
			end
			_ret:tag('div')
				:addClass('seealso')
				:wikitext(msss)
				:wikitext(table.concat(ret.ohplus, ', ') .. '.')
			:done()
		end
	end

	if ret.mh[1] then
		_ret:tag('div')
			:addClass('seealso')
			:wikitext('For this item\'s main hand variant, see ')
			:wikitext(ret.mh[1] .. '.')
		:done()
	end

	if ret.oh[1] then
		local _slot
		if title:find('off%-?hand') then
			_slot = 'main hand'
		else
			_slot = 'off-hand'
		end
		_ret:tag('div')
			:addClass('seealso')
			:wikitext(string.format('For this item\'s %s variant, see ',_slot))
			:wikitext(ret.oh[1] .. '.')
		:done()
	end

	if ret.tier[1] then
		_ret:tag('div')
			:addClass('seealso')
			:wikitext('For other variants of this item, see ')
			:wikitext(table.concat(ret.tier,', ') .. '.')
		:done()
	end

	if ret.augmented[1] then
		local augtext
		if title:find('augmented') then
			augtext = 'For this item\'s standard variant, see '
		else
			augtext = 'For this item\'s augmented variant, see '
		end
		_ret:tag('div')
			:addClass('seealso')
			:wikitext(augtext)
			:wikitext(table.concat(ret.augmented) .. '.')
		:done()
	end

	if ret.dyed[1] then
		local dyedstr = 'dyed'
		if info.undyed then
			dyedstr = '(un)dyed'
		end
		_ret:tag('div')
			:addClass('seealso')
			:wikitext('For this item\'s '..dyedstr..' variants, see ')
			:wikitext(table.concat(ret.dyed,', ') .. '.')
		:done()
	end

	if misc[1] then
		_ret:tag('div')
			:addClass('seealso')
			:wikitext('For other variants of this item, see ')
			:wikitext(table.concat(misc,', ') .. '.')
		:done()
	end

	return _ret
end

function getLink(kind, item)
	local strippedtitle = string.lower(title)
	local strippedkind = string.gsub(kind, '%-', '%%-?')
	
	if item == '' then
		if string.find(strippedtitle, strippedkind) ~= nil or base_types[strippedkind] then
			item = string.gsub(strippedtitle, strippedkind, '')
			if tt_dyes[kind] then
				item = string.gsub(title, ' %(' .. strippedkind .. '%)', '')
			end
			
			if kind == 'off-hand' then
				kind = 'main hand'
			elseif kind == 'superior' then
				kind = 'inferior'
			elseif tt_dyes[kind] or other_dyes[kind] then
				kind = 'undyed'
				info.undyed=true
			else
				kind = 'non-'..kind
			end
		else
			if tt_dyes[kind] then
				local replnum
				item, replnum = string.gsub(title, '%(.*%)$', string.format('(%s)', kind))
				if replnum == 0 then
					item = string.format('%s (%s)', item, kind)
				end
			else
				item = kind..' '..title
			end
		end
	else
		if string.find(strippedtitle, strippedkind) ~= nil or base_types[strippedkind] then
		    if tt_dyes[kind] or other_dyes[kind] then
			    kind = 'undyed'
				info.undyed=true
			else
			    kind = 'non-'..kind
			end
		end
	end

	if kind:find('hand') or kind:find('augment') then
		return string.format('[[%s]]',item)
	else
	    if tt_dyes[kind] then
	        if kind ~= 'blood' and kind ~= 'blood dyed' then
	            if kind:find('third') then
	                kind = 'Third Age'
	            else
	                kind = string.lower(string.sub(kind,1,1))..string.lower(string.sub(kind,2,-1))
	            end
	        end
        end
    
	    return string.format('[[%s|%s]]',item,kind)
	end
end
p.getLink = getLink
p.setTitle = function(x)
	title = x
end
function getPlusLinks(m)
	local titlens = mw.title.getCurrentTitle().fullText
	local t = titlens
	local match = {titlens:match('^(.-) %+ (%d+)')}
	local curr = 0
	local links = {}
	if match[1] then
		t = match[1]
		curr = tonumber(match[2])
	end
	for i=0,m,1 do
		if i ~= curr then
			if i == 0 then
				table.insert(links, string.format('[[%s|%s]]', t, 'base'))
			else
				table.insert(links, string.format('[[%s + %s|+%s]]', t, i, i))
			end
		end
	end
	return links
end

function getOHPlusLinks(m)
	local titlens = mw.title.getCurrentTitle().fullText:lower()
	if titlens:find(' off hand ') then
		info.isoffhand = true
	end
	local t = titlens:gsub('off hand ', '')
	if not info.isoffhand then
		for i,v in ipairs(msr_oh_replace) do 
			if t:find(v) then
				t = t:gsub(v, v..' off hand')
				break
			end
		end
	end
	local match = {t:match('^(.-) %+ (%d+)')}
	local links = {}
	if match[1] then
		t = match[1]
	end
	for i=0,m,1 do
		if i == 0 then
			table.insert(links, string.format('[[%s|%s]]', t, t))
		else
			table.insert(links, string.format('[[%s + %s|+%s]]', t, i, i))
		end
	end
	return links
end


return p