Please note that some information on this wiki is still based on the demo version.
If you notice any errors, feel free to correct them.

Module:Stats

From Super Fantasy Kingdom Official Wiki
Revision as of 23:51, 11 October 2023 by wgg>Cadaei (tonumber fix)

Displays stats for units. Call with Template:Stats, code is in Module:Stats.

Parameters

  • inline: If not empty stats will be shown in one line, else in a table format
  • align: Optional, valid values are left, center or right. It will display the stats in a simple column with the stated alignment.
  • hp: Health
  • ma: Mana
  • sh: Shield
  • pd: Physical Defense
  • md: Magical Defense
  • dm: Damage
  • cr: Critical Chance
  • ra: Range
  • cd: Cooldown
  • ar: Area
  • kb: Knockback
  • sp: Speed
  • du: Duration
  • rg: Rage
  • gr: Greed
  • rr: Reroll
  • ba: Banish
  • cu: Curse

Examples

{{stats|hp=33|dm=8|sh=4|cr=2%|pd=20%|md=10%}}
8
10%
20%
2%
33
4
{{stats|hp=20.5|ra=8.33 m|cd=1.2 s|cr=2%}}
1.2s
8.33m
20.5
2%
{{stats|hp=33|dm=8|sh=4|cr=2%|pd=20%|md=10%|inline=1}}

 8 •  10% •  20% •  2% •  33 •  4

{{stats|hp=33|dm=8|sh=4|cr=2%|pd=20%|md=10%|align=center}}
8
20%
10%
2%
33
4
{{stats
| first = Base stats
| second = Maxed<br/>Base stats
| dm = 5
| dm2 = 6.25
| dmGain = 1
| hp = 11
| hp2 = 14.3
| hpGain = 1
| sh = 13 m
| sh2 = 16.9 m
| shGain = 2
| cr = 5%
| cr2 = 5.63%
| crGain = 1
| pd = 0%
| pd2 = 15%
| pdGain = 3
| md = 0%
| md2 = 15%
| mdGain = 3
}}
Base statsMaxed
Base stats
56.25
1114.3
0%15%
13m16.9m
0%15%
5%5.63%

local p = {}

function p.stats(f)
	local args = f:getParent().args
	
	-- argument name and full name (also used for icon name)
	local possibleStats = {["hp"] = "Health",
						   ["sh"] = "Shield",
						   ["pd"] = "Physical Defense",
						   ["md"] = "Magical Defense",
						   ["dm"] = "Damage",
						   ["cr"] = "Critical Chance",
						   ["ra"] = "Range",
						   ["cd"] = "Cooldown",
						   ["ar"] = "Area",
						   ["kb"] = "Knockback",
						   ["sp"] = "Speed",
						   ["du"] = "Duration",
						   ["gr"] = "Greed",
						   ["rr"] = "Reroll",
						   ["ba"] = "Banish",
						   ["cu"] = "Curse"
						   }
	
	local usedStats = {}
	local useInline = args.inline ~= nil
	local useGain = args.first ~= nil and args.second ~= nil
	
	for a, v in pairs(args) do
		local statName = possibleStats[a]
		if statName ~= nil then
			local secondValue = ''
			local gain = ''
			if useGain then
				secondValue = args[a..'2']
				gain = args[a..'Gain']
				table.insert(usedStats, p.StatEntryFormatGain(statName, v, secondValue, gain))
			else
				table.insert(usedStats, p.StatEntryFormat(statName, v, useInline, true))
			end
		end
	end
	
	if #usedStats == 0 then
	  return ''
	end
	
	if useInline then
	  -- one line, separated by bullet points
	  return table.concat(usedStats, ' &bull; ')
	end
		
	-- table format
	if useGain then
		return '<table style="text-align:left; display:inline-block; border-spacing: 0">'..
			'<tr><td></td><td colspan=3>'..args.first..'</td><td></td><td colspan=3>'..args.second..'</td></tr>'..
			'<tr>'..table.concat(usedStats, '</tr><tr>')..'</tr></table>'
	end
	return '<table style="text-align:left; display:inline-block; border-spacing: 0"><tr>'..table.concat(usedStats, '</tr><tr>')..'</tr></table>'
end

function p.StatEntryFormat(name, val, inline, withIcon)
	if inline then
		return '[[File:'..name..'.png|link='..name..'|16x16px]]&nbsp;'..val
	else
		local wholeNumberPart, decimalPart, unit  = string.match(val, '^(%d*)(%.?%d*) ?(.*)$')
		local iconColumn = ''
		if withIcon then
			iconColumn = '<td style="padding-right: 0.3em;">[[File:'..name..'.png|link='..name..'|16x16px]]</td>'
		end
		return iconColumn..'<td style="text-align: right; padding: 0">'..
			wholeNumberPart..'</td><td style="padding: 0">'..decimalPart..'</td><td style="padding-left: 0.2em">'..unit..'</td>'
	end
end

function p.StatEntryFormatGain(name, val, secondVal, gain)
	local gainCell = ''
	gain = tonumber(gain)
	if gain ~= nil and gain >= 0 then
		gainCell = '<td style="padding: 0 0.5em;">[[File:StatGain'..gain..'.png|16x16px]]</td>'
	end
	return p.StatEntryFormat(name, val, false, true)..
		gainCell..
		p.StatEntryFormat('', secondVal, false, false)
end

return p