Module:HeroClassDataParser: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
HeavyBeber (talk | contribs) Created page with "local p = {} local heroClassDataCache = nil local function getHeroClassData() if not heroClassDataCache then local content = mw.title.new('Data:HeroClassData.json'):getContent() or '{}' heroClassDataCache = mw.text.jsonDecode(content) end return heroClassDataCache end local function findHeroClass(className) local json = getHeroClassData() if not json or not json.heroClasses then return nil end for _, class in ipairs(json.heroClas..." |
HeavyBeber (talk | contribs) No edit summary |
||
| Line 46: | Line 46: | ||
return html | return html | ||
end | end | ||
return p | |||
Revision as of 22:20, 29 October 2025
HeroClassDataParser Module Documentation
This module provides accessors for **hero class data** stored in `Data:HeroClassData.json`. It allows retrieving stats, skill chances, subclasses, and generating formatted stats boxes for display on the wiki.
Data is **cached on first access** for improved performance.
Functions
getStat(frame)
Retrieves the base value of a stat for a given hero class.
- Usage
{{#invoke:HeroClassDataParser|getStat|Knight|attack}}
- Parameters
- frame.args[1] – string: hero class name
- frame.args[2] – string: stat key (`attack`, `defence`, `power`, `knowledge`)
- Returns
- string: stat value, or empty string if not found
---
getPre12(frame)
Retrieves the "pre-12" roll value for a stat.
- Usage
{{#invoke:HeroClassDataParser|getPre12|Knight|attack}}
- Parameters
- frame.args[1] – hero class name
- frame.args[2] – stat key
- Returns
- string: value of statRollPre12 or empty string
---
getPost12(frame)
Retrieves the "post-12" roll value for a stat.
- Usage
{{#invoke:HeroClassDataParser|getPost12|Knight|attack}}
- Parameters
- frame.args[1] – hero class name
- frame.args[2] – stat key
- Returns
- string: value of statRollPost12 or empty string
---
getSkillChance(frame)
Retrieves the chance of learning a secondary skill for a class.
- Usage
{{#invoke:HeroClassDataParser|getSkillChance|Knight|leadershipSkill}}
- Parameters
- frame.args[1] – hero class name
- frame.args[2] – skill key
- Returns
- string: skill chance, or empty string
---
getSubclassName(frame)
Returns the name of a subclass for a class.
- Usage
{{#invoke:HeroClassDataParser|getSubclassName|Necromancer|1}}
- Parameters
- frame.args[1] – hero class name
- frame.args[2] – number/string: subclass index or name
- Returns
- string: subclass name, or empty string
---
getSubclassEffect(frame)
Returns the effect description of a subclass.
- Usage
{{#invoke:HeroClassDataParser|getSubclassEffect|Necromancer|1}}
- Parameters
- frame.args[1] – hero class name
- frame.args[2] – number/string: subclass index or name
- Returns
- string: effect description, or empty string
---
getSubclassSkill(frame)
Returns a specific skill of a subclass.
- Usage
{{#invoke:HeroClassDataParser|getSubclassSkill|Necromancer|Soulweaver|2}}
- Parameters
- frame.args[1] – hero class name
- frame.args[2] – string/number: subclass name or index
- frame.args[3] – number: skill index (1-based)
- Returns
- string: skill name, or empty string
---
statsBox(frame)
Generates an HTML snippet displaying the class's main stats with icons.
- Usage
{{#invoke:HeroClassDataParser|statsBox|Knight}}
- Parameters
- frame.args[1] – hero class name
- Returns
- string: HTML of a vertical stats box
---
Internal Functions
- getHeroClassData() – Loads and caches the hero class JSON data from `Data:HeroClassData.json`.
- findHeroClass(className) – Finds and returns the class table by name.
local p = {}
local heroClassDataCache = nil
local function getHeroClassData()
if not heroClassDataCache then
local content = mw.title.new('Data:HeroClassData.json'):getContent() or '{}'
heroClassDataCache = mw.text.jsonDecode(content)
end
return heroClassDataCache
end
local function findHeroClass(className)
local json = getHeroClassData()
if not json or not json.heroClasses then return nil end
for _, class in ipairs(json.heroClasses) do
if class.name == className then
return class
end
end
return nil
end
function p.statsBox(frame)
local class = frame.args[1]
local cls = findHeroClass(class)
if not cls then return 'Unknown class: ' .. tostring(class) end
-- small helper to stringify values
local function val(v) return tostring(v or '-') end
local icons = {
'[[File:Icon Stats Attack.png|28px]]',
'[[File:Icon Stats Defence.png|28px]]',
'[[File:Icon Stats Spellpower.png|28px]]',
'[[File:Icon Stats Inteligence.png|28px]]',
}
local values = { val(cls.attack), val(cls.defence), val(cls.power), val(cls.knowledge) }
local html = '<div class="stats-vertical" style="display:flex; gap:8px; justify-content:space-between; align-items:center; width:100%;">'
for i=1,4 do
html = html .. string.format('<div style="flex:1; text-align:center;"><div style="font-weight:bold;">%s</div><div style="margin-top:6px;">%s</div></div>', values[i], icons[i])
end
html = html .. '</div>'
return html
end
return p


