Module:Infobox Seed

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Infobox Seed/doc. [edit] [history] [purge]
Module:Infobox Seed's function main is invoked by Template:Databox seed.
Module:Infobox Seed requires Module:Currency.
Module:Infobox Seed requires Module:ExchangeLite.
Module:Infobox Seed requires Module:Infobox.
Module:Infobox Seed requires Module:Mainonly.

Generates {{Infobox Seed}}

Use Module:Infobox Seed/sandbox for testing.


-- <nowiki>
--------------------------
-- Module for [[Template:Infobox Seed]]
-- Please test changes to this module at [[Module:Infobox Seed/sandbox]] first
------------------------
local p = {}

--
-- imports
--
local onmain = require('Module:Mainonly').on_main
local infobox = require('Module:Infobox')
local ge_price = require('Module:ExchangeLite').price
local amount = require('Module:Currency')._amount

local pageTitle = mw.title.getCurrentTitle().text
local NA_PARAM = "N/A"

--
-- main
--
function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)
    local temp
	
	ret:defineParams{
        -- template params
		{ name = 'name', func = { name = 'has_content', params = { 'name', pageTitle }, flag = { 'd', 'r' } } },
        { name = 'level', func = 'has_content' },
        { name = 'patch', func = 'has_content' },
        { name = 'time', func = 'has_content' },
        { name = 'seedsper', func = { name = 'has_content', params = { 'seedsper', '1' }, flag = { 'd', 'r' } } },
        { name = 'plant', func = { name = contentarg, params = { 'plant' }, flag = 'p' } },
        { name = 'check', func = { name = contentarg, params = { 'check' }, flag = 'p' } },
        { name = 'harvest', func = { name = contentarg, params = { 'harvest' }, flag = 'p' } },
        { name = 'payment', func = { name = contentarg, params = { 'payment' }, flag = 'd' } },
        { name = 'seed', func = 'has_content' },
        { name = 'seedling', func = 'has_content' },
        { name = 'sappling', func = 'has_content' },
        { name = 'grown', func = 'has_content' },
        { name = 'output', func = { name = 'has_content', params = { 'output' }, flag = { 'd' } } },
        { name = 'crop', func = contentarg },
        { name = 'cropexchange', func = { name = 'has_content', params = { 'cropexchange', 'gemw' }, flag = { 'd', 'r' } } },
        { name = 'chanceToSave', func = contentarg },
        { name = 'root', func = contentarg },
        { name = 'rootexchange', func = { name = 'has_content', params = { 'rootexchange', 'gemw' }, flag = { 'd', 'r' } } },
        
        -- derived params
        { name = 'cropexchange_gemw', func = { name = gemwarg, params = { 'cropexchange', 'crop' }, flag = 'd' } },
        { name = 'rootexchange_gemw', func = { name = gemwarg, params = { 'rootexchange', 'root' }, flag = 'd' } },
        { name = 'seedsper_disp', func = { name = seedsper_disp, params = { 'seedsper' }, flag = 'd' } },
        
        -- SMW params
        { name = 'name_smw', func = { name = smw_name, params = { pageTitle, 'version' }, flag = { 'r', 'd' } } },
        { name = 'level_smw', func = { name = smwarg, params = { 'level' }, flag = 'p' } },
        { name = 'crop_smw', func = { name = smwarg_strip_link, params = { 'crop' }, flag = 'p' } },
        { name = 'crop_item_smw', func = { name = 'has_content', params = { 'crop_smw' }, flag = 'd' } },
        { name = 'crop_item_text_smw', func = { name = 'has_content', params = { 'crop_smw' }, flag = 'd' } },
        { name = 'crop_qty_smw', func = { name = drop_qty_smwarg, params = { 'output' }, flag = 'd' } },
        { name = 'crop_item_img_smw', func = { name = drop_img_smwarg, params = { 'crop_smw', 'crop_qty_smw', 'name' }, flag = 'd' } },
        { name = 'crop_qty_hi_smw', func = { name = drop_qty_hi_smwarg, params = { 'crop_qty_smw' }, flag = 'd' } },
        { name = 'crop_qty_lo_smw', func = { name = drop_qty_lo_smwarg, params = { 'crop_qty_smw' }, flag = 'd' } },
        { name = 'rarity_smw', func = { name = 'has_content', params = { 'Always' }, flag = 'r' } },
        { name = 'rarity_sort_smw', func = { name = 'has_content', params = { '100' }, flag = 'r' } },
	}

	ret:customButtonPlacement(true)
    ret:setAddRSWInfoboxClass(false)
	ret:setDefaultVersionSMW(true)

    ret:create()
    ret:cleanParams()

	ret:addButtonsCaption()

    ret:useSMWSubobject{
        level_smw = 'Farming level',
    }
	ret:useSMWOne{
		level_smw = 'All Farming level',
	}
    
    -- only add produced item SMW properties for the 'grown' page if present (else always add)
    -- this is meant to prevent seed items from showing up in source lists for the final crop product
    -- NOTE: if root is specified, assume page uses {{DropsLineWC}} and don't set SMW properties to avoid duplication
    local grown = strip_link(ret:param('grown'))
    local root = ret:paramDefined('root') and not ret:paramGrep('root', NA_PARAM)
    if not root and (not ret:paramDefined('grown') or grown == pageTitle) then
	    -- if root is defined, set dropped item SMW properties in subobject
	    if ret:paramDefined('crop') then
		    ret:useSMWSubobject{
		        crop_smw = 'Produce item',
		        crop_item_smw = 'Dropped item',
		        crop_item_text_smw = 'Dropped item text',
		        crop_item_img_smw = 'Dropped item image',
		        crop_qty_smw = 'Drop Quantity',
		        crop_qty_hi_smw = 'Quantity High',
		        crop_qty_lo_smw = 'Quantity Low',
		        name_smw = 'Produce from',
		        rarity_smw = 'Rarity',
		        rarity_sort_smw = 'Rarity sort',
		    }
        end
    end
		
	ret:defineName('Infobox Farming')
    ret:defineLinks{ hide = true }
    ret:addClass('wikitable')
	ret:css{
        width = '50%',
        ['max-width'] = '300px',
    }
    
	-- PARAMETER: name
	ret:addRow{ { tag = 'argh', content = 'name', colspan = '2' } }
	
    -- PARAMETER: level
    ret:addRow{
        { tag = 'th', content = '[[Farming|Farming level]]', css = { width = '1%', ['white-space'] = 'nowrap' } },
        { tag = 'argd', content = 'level' }
    }
    
    -- PARAMETER: patch
    ret:addRow{
        { tag = 'th', content = '[[Farming/Patch locations|Patch]]', css = { ['white-space'] = 'nowrap' } },
        { tag = 'argd', content = 'patch' }
    }
    
    -- PARAMETER: payment
    ret:addRow{
        { tag = 'th', content = 'Payment' },
        { tag = 'argd', content = 'payment' }
    }
    
    -- PARAMETER: time
    ret:addRow{
        { tag = 'th', content = '[[Farming training/Growth guide|Time]]', css = { ['white-space'] = 'nowrap' } },
        { tag = 'argd', content = 'time' }
    }
    
    -- PARAMETER: seedsper
    ret:addRow{
        { tag = 'th', content = 'Seeds per', css = { ['white-space'] = 'nowrap' } },
        { tag = 'argd', content = 'seedsper_disp' }
    }
    
    -- header row: Experience
    ret:addRow{ { tag = 'th', content = 'Experience', colspan = '2' } }

    -- PARAMETER: plant, check, harvest
    temp = { {plant = 'Planting'}, {check = 'Checking'}, {harvest = 'Harvesting'} }
    for _, tbl in ipairs(temp) do
        k, v = next(tbl, nil)
        
        if ret:paramDefined(k) then
            ret:addRow{
                { tag = 'th', content = v, css = { ['white-space'] = 'nowrap' } },
                { tag = 'argd', content = k }
            }
        end
    end
    
    -- header row: Stages
    -- PARAMETER: seed, seedling, sappling, grown
    local headerDone = false
    temp = { {seed = 'Seed'}, {seedling = 'Seedling'}, {sappling = 'Sapling'}, {grown = 'Plant'} }
    for _, tbl in ipairs(temp) do
        k, v = next(tbl, nil)

        if ret:paramDefined(k) then
            if not headerDone then
                ret:addRow{ { tag = 'th', content = 'Stages', colspan = '2' } }
                headerDone = true
            end
            
            ret:addRow{
                { tag = 'th', content = v, css = { ['white-space'] = 'nowrap' } },
                { tag = 'argd', content = k }
            }
        end
    end
    
    -- header row: Produce
    ret:addRow{ { tag = 'th', content = 'Produce', colspan = '2' } }
    
    -- PARAMETER: crop
    local use_gemw = ret:paramDefined('cropexchange_gemw', 'all')
    ret:addRow{
        { tag = 'th', content = 'Crop', rowspan = use_gemw and '2' or '1' },
        { tag = 'argd', content = 'crop' }
    }
    
    -- PARAMETER: cropexchange
    if use_gemw then
        ret:addRow{ { tag = 'argd', content = 'cropexchange_gemw' } }
    end
    
    -- PARAMETER: chanceToSave
    if not ret:paramGrep('chanceToSave', NA_PARAM) then
	    local helpMsg = 'Raw chance of saving a harvest life at specified Farming level with 10% magic secateurs boost'
	    ret:addRow{
	        { tag = 'th', content = 'Chance to save ' .. q_mark(helpMsg), css = { ['white-space'] = 'nowrap' } },
	        { tag = 'argd', content = 'chanceToSave', css = { ['font-size'] = '.8em' } }
	    }
    end
    
    -- PARAMETER: output
    if not ret:paramGrep('output', NA_PARAM) then
	    local helpMsg = 'Base output, assuming no boosts (e.g. compost or greenfingers aura)'
	    ret:addRow{
	        { tag = 'th', content = 'Yield ' .. q_mark(helpMsg), css = { ['white-space'] = 'nowrap' } },
	        { tag = 'argd', content = 'output' }
	    }
    end
    
    -- only display root if defined
    if ret:paramDefined('root') and not ret:paramGrep('root', NA_PARAM) then
	    -- PARAMETER: root
	    local use_gemw = ret:paramDefined('rootexchange_gemw', 'all')
	    ret:addRow{
	        { tag = 'th', content = 'Root', rowspan = use_gemw and '2' or '1' },
	        { tag = 'argd', content = 'root' }
	    }
	    
	    -- PARAMETER: rootexchange
	    if use_gemw then
	        ret:addRow{ { tag = 'argd', content = 'rootexchange_gemw' } }
	    end
    end
	
	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1,a2))
	end
	ret:finish()
	return ret:tostring()
end

--
-- param parsing and cleaning funcs
--
function contentarg(arg)
    if infobox.isDefined(arg) then
        if is_empty(arg) then
            return NA_PARAM
        end
    end
    
    return arg
end

function drop_img_smwarg(crop, output, name)
	if infobox.isDefined(crop) then
		return '[[File:' .. crop .. '.png|link=' .. crop .. '|alt=' .. crop .. '.png: RS3 ' .. name
               .. ' drops ' .. crop .. ' as produce with rarity Always in quantity ' .. output .. ']]'
	end
	return nil
end

function drop_qty_hi_smwarg(output)
    if output then
        return string.match(output, '^%d+ *%- *(%d+)') or output
    end
    return nil
end

function drop_qty_lo_smwarg(output)
    if output then
        return string.match(output, '^%d+') or output
    end
    return nil
end

function drop_qty_smwarg(output)
    if infobox.isDefined(output) then
        return output
    end
    return 'unknown'
end

function gemwarg(cropexchange, crop)
    if infobox.isDefined(cropexchange) and string.lower(cropexchange) ~= 'gemw' then
        return nil
    elseif not infobox.isDefined(crop) or is_empty(crop) then
    	return nil
    end
    
    crop = strip_link(crop)
    local price = ge_price(crop)
    if price ~= nil then
    	return amount(price, 'coins')
	else
		return nil
	end
end

function seedsper_disp(seedsper)
    if infobox.isDefined(seedsper) then
        local numSeeds = tonumber(seedsper)
        if type(numSeeds) == 'number' and numSeeds > 0 then
            if numSeeds > 1 then
                return seedsper .. ' seeds'
            else
                return seedsper .. ' seed'
            end
        end
        return nil
    end
    return nil
end

function smw_name(name, version)
	if infobox.isDefined(version) and s ~= '' then
		return name .. "#" .. version
	else
		return name
	end
end

function smwarg(arg)
	if infobox.isDefined(arg) then
		if string.lower(tostring(arg)) == 'no' then
			return nil
		end
		return arg
	end
	return nil
end

function smwarg_strip_link(arg)
	if infobox.isDefined(arg) then
		if string.lower(tostring(arg)) == 'no' then
			return nil
		end
		return strip_link(arg)
	end
	return nil
end

--
-- util funcs
--
function is_empty(arg)
    local strarg = string.lower(tostring(arg))
    
    if strarg == 'no'
    or strarg == 'none'
    or strarg == '0'
    or strarg == 'na'
    or strarg == 'n/a'
    or strarg == 'hide' then
        return true
    end
    
    return false
end

function q_mark(text)
    text = text:gsub( '"', '&quot;' )
    return '<sup class="noprint small" style="border-bottom: 1px dotted; cursor: help" title="' .. text .. '">[?]</sup>'
end

function strip_link(text)
	if text == nil then
		return nil
	end
	
    -- returns "Foo" for: Foo, [[Foo]], [[Foo|Bar]]
    return string.match(text, "[%[]*(.-)[%]%|]") or text
end

--
-- category funcs
--
function addcategories(args, catargs)
	local ret = {}
	
    -- always added
	table.insert(ret, 'Farming')
	
	local cat_map = {
		-- Added if the parameter has content
		defined = {
		},
		-- Added if the parameter has no content
		notdefined = {
			level = 'Needs Farming level',
			patch = 'Needs Farming patch type',
			['time'] = 'Needs Farming grow time',
			seedsper = 'Needs Farming seeds per patch',
			plant = 'Needs Farming XP',
            check = 'Needs Farming XP',
            harvest = 'Needs Farming XP',
			crop = 'Needs Farming crop produced',
        },
	}
	
	-- Run and add mapped categories
	for n, v in pairs(cat_map.defined) do
		if catargs[n] and catargs[n].one_defined then
			table.insert(ret, v)
		end
	end
	for n, v in pairs(cat_map.notdefined) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret, v)
		end
	end
	
	-- combine table and format category wikicode
	for i, v in ipairs(ret) do
		if v ~= '' then
			ret[i] = string.format('[[Category:%s]]', v)
		end
	end

	return table.concat(ret, '')
end

return p
-- </nowiki>