Module:Item/Mod: Difference between revisions

From Growtopia
Jump to navigationJump to search
>HashJona
m fix nowiki render
>HashJona
m fix safely escape value
Line 145: Line 145:


return {
return {
description = mw.getCurrentFrame():preprocess(joinWithAnd(parsedEffects)),
description = joinWithAnd(parsedEffects),
categories = table.concat(categories)
categories = table.concat(categories)
}
}
Line 176: Line 176:
local label = parent['label'] or parent[1]
local label = parent['label'] or parent[1]
     local parsedEffects =  item.parseDescriptionEffects(parent)
     local parsedEffects =  item.parseDescriptionEffects(parent)
     local baseSentence = "When equipped, the '''<nowiki>" .. title .. "</nowiki>''' grants the ''<nowiki>" .. label .. "</nowiki>'' [[Mods|mod]]"
     local baseSentence = "When equipped, the '''" .. mw.text.nowiki(title) .. "''' grants the ''" .. mw.text.nowiki(label) .. "'' [[Mods|mod]]"
     local fullSentence
     local fullSentence
      
      

Revision as of 10:31, 8 March 2025

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

local effect = {
	['Double Jump'] = 'jump a second time in mid-air',
	['Speedy'] = 'move faster on land',
	['Punch Damage'] = 'break blocks faster',
	['Punch Range'] = 'punch one tile further',
	['Punch Power'] = 'knock back other players further',
	['Skin Color'] = function(args)
		local skin = args['skin'] or 'a specific tone'
		return 'change their skin color to ' .. skin
	end,
	['Miscellaneous'] = function(args)
		return args['misc'] or ""
	end,
	['Light Source'] = 'emit light in [[Dark Background Block (block category)|dark areas]]',
	['Build Range'] = 'build one tile further',
	['Punch Pull'] = 'pull other players towards them upon punching them',
	['High Jump'] = 'jump one tile higher',
	['Slow Fall'] = 'glide slowly to the ground when holding the jump button',
	['Wall Climbing'] = 'cling onto [[Climbing Wall|Climbing Walls]] by pressing the jump button',
	['Fire Hose'] = 'extinguish [[Fire|fires]]',
	['Fireproof'] = 'take 50% less damage from [[Lava Pain Block (block category)|lava pain blocks]]',
	['Ghost Immunity'] = 'immunity against certain types of ghosts',
	['Healing'] = 'heal faster than usual after receiving damage from hitting a [[Pain Block (block category)|pain block]]',
	['XP Buff'] = 'gain more [[Leveling|XP]]',
	['Slippery'] = 'glide on any surface as if it is [[Slippery Block (block category)|slippery]]',
	['Putt Putt Putt'] = 'move slower, but allows the player to harvest trees by walking through them',
	['Damage Reduction'] = 'break blocks more slowly',
	['Knock Back Reduction'] = 'resist a percentage of knockback when [[Punching|punched]] by other players',
	['Speedy in Water'] = 'move faster in water',
	['Float on Water'] = 'float on [[Water Bucket|water]]',
	['Grow Effect'] = 'expand in size',
	['Zombie Weapon'] = 'kill [[G-Virus|zombies]] during a Pandemic',
	['Low Jump'] = 'decrease the player\'s jump height',
	['Card Battle Health'] = 'adds 2 additional health to the player during card battles against a [[Villains|villain]] or another player',
	['Card Battle Strength'] = 'reduce 2 health from the [[Villains|villain]] or another player',
	['Cookie Hunter'] = 'provides a significant chance of doubling fruit drops from [[Fortune Cookie]] and [[Lucky Fortune Cookie|Lucky Fortune Cookie Trees]]',
	['Night Vision'] = 'eliminate the darkness effect created by [[Dark Background Block (block category)|dark background blocks]]',
	['Extra Gems'] = function (args)
		local chance = args['gem_chance'] or ""
		if chance ~= "" then
			chance = chance .. " "	
		end
		return 'get extra ' .. chance .. 'gems from breaking blocks'
	end,
	['Extra Blocks'] = function (args)
		local chance = args['block_chance'] or ""
		if chance ~= "" then
			chance = chance .. " "	
		end
		return 'get an extra ' .. chance .. 'chance for a block broken to drop itself'
	end,
}

local item = {}

function joinWithAnd(list)
    local n = #list
    if n == 0 then
        return ""
    elseif n == 1 then
        return list[1]
    elseif n == 2 then
        return list[1] .. " and " .. list[2]
    elseif n >= 3 then
        local firstPart = table.concat(list, ", ", 1, n - 1)
        return firstPart .. ", and " .. list[n]
    end
end

function parseArgs(args)
	local keys = {}
	for k, _ in pairs(args) do
		if type(k) == 'number' then
			table.insert(keys, k)
		end
	end
	return keys
end

function debugResult(var, debugFlag)
   if debugFlag == "1" then 
       return "<pre>" .. var .. "</pre>"
   else
       return mw.getCurrentFrame():preprocess(var)
   end
end

function startsWith(str, prefix)
	return str:sub(1, #prefix) == prefix
end

function length(obj)
	local count = 0
	for _ in pairs(obj) do 
		count = count + 1 
	end
	return count;
end

function nonEmpty(value, fallback)
	if value == nil or value == "" then
        return fallback
    else
        return value
    end
end

function parseNotes(args)
	local notes = ''
	
	for i = 1, 3 do
		local note = args['note_' .. i] or ""
		if note ~= "" then
			notes = notes .. " | " .. note
		end
	end
	return notes
end

function item.parseDescriptionEffects(args)
	local parsedEffects = {}
	local categories = {}
	local addCat = not args['no_cat']
	local keys = parseArgs(args)

	for i = 2, #keys do
		local argIndex = keys[i]
		local key = args[argIndex]
		
		if effect[key] ~= nil then
			local text = effect[key]
			if type(effect[key]) == "function" then
				text = effect[key](args)
			end
			if addCat then
				table.insert(categories, "[[Category:" .. key .. " Mod Items]]")
			end
			table.insert(parsedEffects, text)
		end
	end
	
	if (#keys - #parsedEffects) ~= 1 and addCat then
		table.insert(categories, "[[Category:ItemModError]]")
	end

	return {
		description = joinWithAnd(parsedEffects),
		categories = table.concat(categories)
	}
end

function item.parseTableCategories(args)
	local categories = {}
	local keys = parseArgs(args)
	local currentPage = mw.title.getCurrentTitle()

	for i = 2, #keys do
		local argIndex = keys[i]
		local key = args[argIndex]
		local modCategory = "Mods/" .. key
		
		if key ~= "Miscellaneous"
		and effect[key] ~= nil
		and currentPage 
		and not startsWith(currentPage.text, modCategory .. "/") then
			table.insert(categories, "''[[" .. modCategory .. "|".. key .."]]''")
		end
	end
	
	return joinWithAnd(categories)
end

function item.description(frame) 
	local parent = frame:getParent().args
	local title = mw.title.getCurrentTitle().text or 'unknown'
	local label = parent['label'] or parent[1]
    local parsedEffects =  item.parseDescriptionEffects(parent)
    local baseSentence = "When equipped, the '''" .. mw.text.nowiki(title) .. "''' grants the ''" .. mw.text.nowiki(label) .. "'' [[Mods|mod]]"
    local fullSentence
    
    if parsedEffects.description == "" then 
    	fullSentence = baseSentence .. "."
    else 
    	fullSentence = baseSentence .. ", which allows the player to " .. parsedEffects.description .. "."
    end
    return fullSentence .. parsedEffects.categories
end

function item.table(frame) 
	local parent = frame:getParent().args
	local label = parent['label'] or parent[1]
	local notes = parseNotes(parent) 
	
	local effects = item.parseTableCategories(parent) or ""
	
	
	if effects ~= "" then
		effects =  " | Also grants " .. effects
	end

	return  label .. effects .. notes
end

function item.documentation(frame)
	local doc = {}
	local args = {
		misc = '<span style="background:#FF9999">misc</span>',
		skin = '<span style="background:#FF9999">skin</span>',
		gem_chance = '<span style="background:#FF9999">gem_chance</span>',
		block_chance = '<span style="background:#FF9999">block_chance</span>'
	}
	table.insert(doc, '{|class="article-table" border="0" cellpadding="1" cellspacing="1" width="100%"')
	table.insert(doc, '|-')
	table.insert(doc, '! scope="col" style="width:15%" |Parameter')
	table.insert(doc, '! scope="col" style="width:10%" |Value')
	table.insert(doc, '! scope="col" style="width:75%" |Meaning')
	table.insert(doc, '|-')
	table.insert(doc, '| rowspan=' .. length(effect) .. ' | <span style="background:#FF9999">Mod</span>')
	
	for k, v in pairs(effect) do
		table.insert(doc, "|'''" .. k .. "'''")
    	if type(v) == "function" then
    		local defaultValue = v({})
    		local value = v(args)
    		if(defaultValue == value) then
    			table.insert(doc, "|" .. nonEmpty(defaultValue, "''No default value''"))
    		else 
    			table.insert(doc, "|" .. "{{Bullet}} " .. nonEmpty(defaultValue, "''No default value''") .. "<br>{{Bullet}} " .. value)
    		end
        else
            table.insert(doc, "|" .. v)
    	end
    table.insert(doc, '|-')
	end
	table.insert(doc, '|}')
	return debugResult(table.concat(doc, '\n'), frame.args['debug'])
end

return item