Module:InfoIcon

From Leaf Blower Revolution Wiki
Jump to navigation Jump to search

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

local p = {}

-- These icons come in groups: leaf, pet, unique, coin, essence.
function p.grouped(frame)
    local args = frame.args
    return p._getMarkup(args[1], args[2], args[3], args[4], args[5])
end

-- These icons do not come in groups.
function p.ungrouped(frame)
    local args = frame.args
    return p._getMarkup(nil, args[1], args[2], args[3], args[4])
end

-- group: for example "leaf"
-- infotype: for example "platinum" or "cheese"
-- textmod: empty or "pl" (plural) or "nosuffix" (Platinum instead of Platinum Leaf)
-- quantity: how many of the resource
-- textsuffix: added to text
function p._getMarkup(group, infotype, textmod, quantity, suffix)
    local options = {}
    group = p._cleanLower(group)
    options.infotype = p._cleanLower(infotype)
    options.textmod = p._cleanLower(textmod)
    options.quantity = p._cleanCased(quantity)
    options.textsuffix = p._cleanCased(suffix)
    if group == 'leaf' then
        options.imagesuff = "Leaf"
        options.linksuff = "Leaf"
        options.textgroupsuff = "Leaf"
        options.textgroupsuffpl = "Leaves"
    elseif group == 'pet' then
        options.linkto = "Pets"
        options.textgroupsuff = "Pet"
    elseif group == 'unique' then
        options.linkto = "Unique Leaves"
        options.imagesuff = "Leaf"
        options.textgroupsuff = "Leaf"
        options.textgroupsuffpl = "Leaves"
    elseif group == 'coin' then
        options.imagesuff = "Coin"
        options.textgroupsuff = "Coin"
    elseif group == 'essence' then
        options.linkto = "Essence Types"
        options.imagesuff = "Essence"
        options.textgroupsuff = "Essence"
    elseif group ~= nil then
        return "Unexpected group: " .. group .. " [[Category:Pages with script errors]]"
    end
    return '<span style="white-space: nowrap">' .. p._getInnerMarkup(options) .. '</span>'
end

-- simplify empty values and make caseless
function p._cleanLower(arg)
    if arg == nil or arg == '' then
        return nil
    end
    return string.lower(arg)
end

-- simplify empty values but keep case
function p._cleanCased(arg)
    if arg == '' then
        return nil
    end
    return arg
end

local titleOverrides = {
    ['Alb'] = 'ALB',
    ['Blc'] = 'BLC',
    ['Cursed cheese'] = 'The Cursed Cheese',
    ['Mlc'] = 'MLC',
}
local linkOverrides = {
	['Artifact'] = 'Artifacts',
	['Card Part'] = 'Cards#Card_Parts',
    ['Coin'] = 'Prestige',
    ['Converter'] = 'Converters',
    ['Farming'] = 'Leaf Seeds',
    ['Gem'] = 'Gems',
    ['Gold Token'] = 'Tokens#Gold_Token',
    ['Borb'] = 'Borbs',
    ['Printer'] = 'Printers',
    ['Shard'] = 'Shards',
    ['Silver Token'] = 'Tokens#Silver_Token',
    ['Strange Flask'] = 'Flask Types',
}
local imageOverrides = {
	['Artifact'] = 'Orb Artifact',
    ['Autoblowo'] = 'ALB',
    ['Cheesetor'] = 'Cheese',
    ['Coin Coin'] = 'Prestige coin',
    ['Prestige Coin'] = 'Prestige coin',
    ['BLC Coin'] = 'BLC coin',
    ['MLC Coin'] = 'MLC coin',
    ['Converter'] = 'Converters',
    ['The Cursed Cheese'] = 'Cursed Cheese',
    ['Equipment'] = 'Leaf Shield',
    ['Scrolls'] = 'Damage Scroll',
    ['Trading'] = "Trader's Suitcase",
}
local sizeOverrides = {
    ['Prestige coin'] = '16',
    ['BLC coin'] = '16',
    ['MLC coin'] = '16',
    ['Borb'] = '12',
}

function p._override(str, overrideTable)
    if str == nil then
        return ''
    end
    local ucstr = p._firstToUpper(str)
    local ovr = overrideTable[ucstr]
    if ovr ~= nil then
        return ovr
    end
    return ucstr
end

-- Uppercase each word in string
function p._firstToUpper(str)
    return string.gsub(" "..str, "%W%l", string.upper):sub(2)
end

--Options:
-- infotype: for example "platinum" or "cheese"
-- linkto: override link destination, for example "Pets" or "Unique Leaves"
-- imagesuff: for example "Leaf" or "Coin"
-- linksuff: for example "Leaf" or "Coin"
-- textgroupsuff: for example "Leaf"
-- textgroupsuffpl: for example "Leaves". Defaults to adding "s".
-- textmod: empty or "pl" (plural) or "nogroupsuff" (Platinum instead of Platinum Leaf)
-- quantity: how many of the resource
-- textsuffix: added to text
function p._getInnerMarkup(options)
    local title = p._override(options.infotype, titleOverrides)
    local link = p._override(title, linkOverrides)
    if options.linkto == "Pets" then
    	link = options.linkto .. '#' .. title
    elseif options.linkto ~= nil then
        link = options.linkto
    elseif options.linksuff ~= nil then
        link = p._override(title .. ' '.. options.linksuff, linkOverrides)
    end
    local image = p._override(title, imageOverrides)
    if options.imagesuff ~= nil then
        image = p._override(title .. ' ' .. options.imagesuff, imageOverrides)
    end
    local size = '20'
    if sizeOverrides[image] ~= nil then
        size = sizeOverrides[image]
    end
    local markup = '[[File:'..image..'.png|x'..size..'px|link='..link..']]'
    if (options.textmod == 'notext' or options.textmod == nil) and options.textsuffix == nil then
    	if options.quantity ~= nil then
    		markup = markup..' '..options.quantity
    	end
        return markup
    end
    markup = markup..' '
    if options.quantity ~= nil then
    	markup = markup..options.quantity..' '
    end
    local suffix = ''
	if options.textmod == 'pl' then
	    -- Check if the 'title' ends with 'y'
	    if mw.ustring.sub(title, -1) == 'y' then
	        -- Remove the 'y' and append 'ies'
	        title = mw.ustring.sub(title, 1, -2) .. 'ies'
	    else
	        suffix = 's'
	    end
	end
    if options.textgroupsuff ~= nil and options.textmod ~= 'nogroupsuff' and title ~= 'Coin' then
        if options.textmod == 'pl' then
            if options.textgroupsuffpl ~= nil then
                suffix = ' ' .. options.textgroupsuffpl
            else
                suffix = ' ' .. options.textgroupsuff..'s'
            end
        else
            suffix = ' ' .. options.textgroupsuff
        end
    end
    if title..suffix == "Basic Leaves" then
    	title = ''
    	suffix = "Leaves"
    end
    if options.textsuffix ~= nil then
    	suffix = suffix .. ' ' .. options.textsuffix
    end
    return markup..'[['..link..'|'..title..suffix..']]'
end

return p