Module:GameData: Difference between revisions
From SpiritVale Wiki
More actions
Created page with "local p = {} -- In-memory cache so we only parse each JSON page once local cache = {} local function decodeJsonPage(titleText) if cache[titleText] then return cache[titleText] end local title = mw.title.new(titleText) if not title then cache[titleText] = { meta = {}, records = {}, byId = {} } return cache[titleText] end local content = title:getContent() if not content or content == '' then cache[titleText]..." |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 3: | Line 3: | ||
-- In-memory cache so we only parse each JSON page once | -- In-memory cache so we only parse each JSON page once | ||
local cache = {} | local cache = {} | ||
local function newEmptyResult() | |||
return { meta = {}, records = {}, byId = {} } | |||
end | |||
local function getInternalKey(rec) | |||
if type(rec) ~= "table" then | |||
return nil | |||
end | |||
-- Prefer the new Structured / Wiki keys, but tolerate legacy variants. | |||
local internal = | |||
rec["Internal Name"] or | |||
rec["InternalName"] or | |||
rec["Internal ID"] or | |||
rec["InternalID"] or | |||
rec["internal_name"] or | |||
rec["Id"] or | |||
rec["ID"] or | |||
rec["Name"] | |||
if type(internal) == "string" and internal ~= "" then | |||
return internal | |||
end | |||
return nil | |||
end | |||
local function decodeJsonPage(titleText) | local function decodeJsonPage(titleText) | ||
if cache[titleText] then | |||
return cache[titleText] | |||
end | |||
local function finish(result) | |||
cache[titleText] = result | |||
return result | |||
end | |||
local title = mw.title.new(titleText) | |||
if not title then | |||
return finish(newEmptyResult()) | |||
end | |||
local content = title:getContent() | |||
if type(content) ~= "string" or content == "" then | |||
return finish(newEmptyResult()) | |||
end | |||
local ok, data = pcall(mw.text.jsonDecode, content) | |||
if not ok or type(data) ~= "table" then | |||
return finish(newEmptyResult()) | |||
end | |||
-- Your files use: version, schema_version, generated_at, records | |||
local records = data.records | |||
if type(records) ~= "table" then | |||
records = {} | |||
end | |||
local byId = {} | |||
for _, rec in ipairs(records) do | |||
local internal = getInternalKey(rec) | |||
if internal then | |||
byId[internal] = rec | |||
end | |||
end | |||
local result = { | |||
meta = { | |||
version = data.version, | |||
schema_version = data.schema_version, | |||
generated_at = data.generated_at, | |||
}, | |||
records = records, | |||
byId = byId, | |||
} | |||
return finish(result) | |||
end | end | ||
| Line 67: | Line 86: | ||
function p.loadSkills() | function p.loadSkills() | ||
return decodeJsonPage("Data:skills.json") | |||
end | end | ||
function p.loadPassives() | function p.loadPassives() | ||
return decodeJsonPage("Data:passives.json") | |||
end | end | ||
function p.loadSummons() | function p.loadSummons() | ||
return decodeJsonPage("Data:summons.json") | |||
end | end | ||
function p.loadEffects() | function p.loadEffects() | ||
return decodeJsonPage("Data:effects.json") | |||
end | end | ||
return p | return p | ||