Module:Sandbox/Rainflight24
Jump to navigation
Jump to search
This module implements parsing for Template:Item, specifically: Template:Item/Attribute.
--[[
This module provides processing for item pages.
]]
local p = {}
local String = require( 'Module:String' )
-- Shorthands with add
local add_shorthands = { ['armor'] = true, ['agility'] = true,
['knockback resistance'] = true, ['thorns damage'] = true,
['throw rate'] = true }
-- Shorthands with multiply
local multiply_shorthands = { ['spell power'] = true }
--[[
attribute
Generates a display string (and/or categories) for an Item/Attribute.
Is not compatible with unicode.
Usage:
{{#invoke:Item|attribute|magnitude|isbase|nocategory}}
magnitude|isbase|nocategory
Parameters:
attribute: The attribute's name
magnitude: The magnitude or count of an attribute
Percents should be divided by 100, ie: 0.05 is 5%
isbase: Whether or not this is a base attribute
nocategory: Whether or not to disable categories
]]
function p.attribute ( frame )
local new_args = frame:getParent().args
local attribute_lower = mw.ustring.lower( mw.text.trim( new_args[1] ) or '' )
local attribute = mw.text.split( attribute_lower, '%s' )
local magnitude = tonumber( new_args[2] ) or new_args[2] or ''
local sign = ''
if type(magnitude) == 'number' and magnitude > 0 then
sign = '+'
end
local isbase = String._getBoolean( new_args[3] or false )
local nocategory = String._getBoolean( new_args[4] or false )
local output = {}
local attribute_type = attribute[#attribute]
-- Any case which does not end with "add" or "multiply."
local last = #attribute - 1
if multiply_shorthands[attribute_lower] or add_shorthands[attribute_lower]
or (attribute_type ~= 'add' and attribute_type ~= 'multiply') then
last = #attribute
end
local attribute_formatted = String._titlecase(
table.concat( attribute, ' ', 1, last ), '%s' )
-- + or - magnitude
if attribute_type == 'multiply' or multiply_shorthands[attribute_lower] then
-- Magnitude percent
if magnitude then
table.insert( output, sign .. magnitude * 100 .. '%' )
end
else
-- Magnitude flat (default behavior)
if magnitude then
table.insert( output, sign .. magnitude )
end
-- Special cases.
if attribute and #attribute >= 2 then
if attribute[1] .. attribute[2] == 'attackdamage' then
magnitude = magnitude + 1
end
if attribute[1] .. attribute[2] == 'attackspeed' then
magnitude = magnitude + 4
end
if attribute[1] .. attribute[2] == 'knockbackresistance' then
magnitude = magnitude * 10
end
end
if attribute_type ~= 'add' and not add_shorthands[attribute_lower] then
table.insert( output, '[[Category:Items with Attribute Template Errors]] ' )
end
end
-- Attribute Name
table.insert( output, attribute_formatted )
-- Add categorization if applicable.
if not (isbase or nocategory) then
if attribute_type == 'add' or add_shorthands[attribute_lower] then
table.insert( output, string.format( '[[Category:%s Items]]',
attribute_formatted ) )
elseif attribute_type == 'multiply' or multiply_shorthands[attribute_lower] then
table.insert( output, string.format( '[[Category:%s Percent Items]]',
attribute_formatted ) )
end
end
return table.concat( output, ' ' )
end
return p