Module:GameSkills: Difference between revisions
From SpiritVale Wiki
More actions
No edit summary |
No edit summary |
||
| Line 23: | Line 23: | ||
local skillsCache | local skillsCache | ||
local eventsCache | |||
-- getSkills: lazy-load + cache skill dataset from GameData. | -- getSkills: lazy-load + cache skill dataset from GameData. | ||
local function getSkills() | local function getSkills() | ||
if not skillsCache then | |||
skillsCache = GameData.loadSkills() | |||
end | |||
return skillsCache | |||
end | |||
local function getEvents() | |||
if eventsCache == nil then | |||
if type(GameData.loadEvents) == "function" then | |||
eventsCache = GameData.loadEvents() | |||
else | |||
eventsCache = false | |||
end | |||
end | |||
return eventsCache | |||
end | end | ||
| Line 93: | Line 106: | ||
-- listToText: join an array into a readable string. | -- listToText: join an array into a readable string. | ||
local function listToText(list, sep) | local function listToText(list, sep) | ||
if type(list) ~= "table" or #list == 0 then | |||
return nil | |||
end | |||
return table.concat(list, sep or ", ") | |||
end | |||
local function resolveDisplayName(v, kind) | |||
if v == nil then return nil end | |||
local function firstString(keys, source) | |||
for _, key in ipairs(keys) do | |||
local candidate = source[key] | |||
if type(candidate) == "string" and candidate ~= "" then | |||
return candidate | |||
end | |||
end | |||
return nil | |||
end | |||
if type(v) == "table" then | |||
local primaryKeys = { "External Name", "Display Name", "Name" } | |||
local extendedKeys = { "Skill External Name", "Status External Name" } | |||
local internalKeys = { "Internal Name", "Internal ID", "ID", "InternalID", "Skill Internal Name", "InternalID" } | |||
return firstString(primaryKeys, v) | |||
or firstString(extendedKeys, v) | |||
or firstString(internalKeys, v) | |||
end | |||
if type(v) == "string" then | |||
if kind == "event" then | |||
local events = getEvents() | |||
if events and events.byId and events.byId[v] then | |||
local mapped = resolveDisplayName(events.byId[v], "event") | |||
if mapped then | |||
return mapped | |||
end | |||
end | |||
end | |||
return v | |||
end | |||
return tostring(v) | |||
end | |||
local function resolveEventName(v) | |||
local resolved = resolveDisplayName(v, "event") | |||
if type(resolved) == "string" then | |||
return resolved | |||
end | |||
return (resolved ~= nil) and tostring(resolved) or nil | |||
end | |||
local function resolveSkillNameFromEvent(ev) | |||
if type(ev) ~= "table" then | |||
return resolveDisplayName(ev, "skill") or "Unknown skill" | |||
end | |||
local displayKeys = { | |||
"Skill External Name", | |||
"External Name", | |||
"Display Name", | |||
"Name", | |||
"Skill Name", | |||
} | |||
for _, key in ipairs(displayKeys) do | |||
local candidate = resolveDisplayName(ev[key], "skill") | |||
if candidate then | |||
return candidate | |||
end | |||
end | |||
local internalKeys = { | |||
"Skill Internal Name", | |||
"Skill ID", | |||
"Internal Name", | |||
"Internal ID", | |||
"ID", | |||
} | |||
for _, key in ipairs(internalKeys) do | |||
local candidate = ev[key] | |||
if type(candidate) == "string" and candidate ~= "" then | |||
return candidate | |||
end | |||
end | |||
return "Unknown skill" | |||
end | end | ||
| Line 1,852: | Line 1,951: | ||
-- Users (hide on direct skill page) | -- Users (hide on direct skill page) | ||
if showUsers then | |||
local users = rec.Users or {} | |||
addRow(root, "Classes", listToText(users.Classes), "sv-row-users", "Users.Classes") | |||
addRow(root, "Summons", listToText(users.Summons), "sv-row-users", "Users.Summons") | |||
addRow(root, "Monsters", listToText(users.Monsters), "sv-row-users", "Users.Monsters") | |||
do | |||
local eventsList = {} | |||
if type(users.Events) == "table" then | |||
for _, ev in ipairs(users.Events) do | |||
local name = resolveEventName(ev) or ev | |||
if name ~= nil then | |||
table.insert(eventsList, mw.text.nowiki(tostring(name))) | |||
end | |||
end | |||
end | |||
addRow(root, "Events", listToText(eventsList), "sv-row-users", "Users.Events") | |||
end | |||
end | |||
-- Mechanics (keep small extras only) | -- Mechanics (keep small extras only) | ||
| Line 1,964: | Line 2,074: | ||
end | end | ||
-- Events | |||
local function formatEvents(list) | |||
if type(list) ~= "table" or #list == 0 then return nil end | |||
local parts = {} | |||
for _, ev in ipairs(list) do | |||
if type(ev) == "table" then | |||
local action = resolveDisplayName(ev.Action, "event") or ev.Action or "On event" | |||
local name = resolveSkillNameFromEvent(ev) | |||
table.insert(parts, string.format("%s → %s", mw.text.nowiki(action), mw.text.nowiki(name))) | |||
end | |||
end | |||
return (#parts > 0) and table.concat(parts, "<br />") or nil | |||
end | |||
local eventsText = formatEvents(rec.Events) | local eventsText = formatEvents(rec.Events) | ||