Module:Sandbox/User:Gaz Lloyd/Variant

Documentation for this module may be created at Module:Sandbox/User:Gaz Lloyd/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',
}

local skip_keys = {
	['max+'] = true,
	['msoh'] = true
}

function p.main(frame)
	local args = frame:getParent().args
	title = args['%TITLE%']
	local links = {}
   	local ret = {}
 	ret.mh = {}
 	ret.oh = {}
	ret.tier = {}
	ret.augmented = {}
	ret.dyed = {}
	ret.plus = {}
	ret.ohplus = {}
	ret.misc = {}
	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 skip_keys[key] and not string.find(key, '%%[A-Z]+%%') 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(ret.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 flags = 0
	local out = {}
	for i,v in pairs(ret) do
		if type(v) == 'table' then
			if #v > 0 then
				out[i] = v
			end
		else
			out[i] = v
		end
	end
	local _args = {}
	local arg_counter = true
	for i,v in pairs(args) do
		local _i = mw.text.trim(i)
		if not skip_keys[i] and not string.find(i, '%%[A-Z]+%%') then
			arg_counter = arg_counter and (not (tonumber(i) == nil))
			_args[tostring(_i)] = mw.text.trim(v)
		end
	end
	if arg_counter then
		flags = mw.text.JSON_PRESERVE_KEYS
	end
	out._info = {args=_args, page=args['%PAGE%'], title=args['%TITLE%']}
	return mw.text.jsonEncode(out, flags)
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 {link=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 {link=item, text=kind}
	end
end
p.getLink = getLink
p.setTitle = function(x)
	title = x
end
function getPlusLinks(m)
	local t = title
	local match = {title: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, {link=t, text='base'})
			else
				table.insert(links, {link=string.format('%s + %s', t, i), text=string.format('+%s', i)})
			end
		end
	end
	return links
end

function getOHPlusLinks(m)
	local titlens = title
	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, {link=t, text=t})
		else
			table.insert(links, {link=string.format('%s + %s', t, i), text=string.format('+%s', i)})
		end
	end
	return links
end


return p