Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Join the Playtest on Steam Now: SpiritVale

Module:GameData: Difference between revisions

From SpiritVale Wiki
m Protected "Module:GameData" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) [cascading]
No edit summary
 
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
if cache[titleText] then
        return cache[titleText]
return cache[titleText]
    end
end
 
    local title = mw.title.new(titleText)
    if not title then
        cache[titleText] = { meta = {}, records = {}, byId = {} }
        return cache[titleText]
    end


    local content = title:getContent()
local function finish(result)
    if not content or content == '' then
cache[titleText] = result
        cache[titleText] = { meta = {}, records = {}, byId = {} }
return result
        return cache[titleText]
end
    end


    local ok, data = pcall(mw.text.jsonDecode, content)
local title = mw.title.new(titleText)
    if not ok or type(data) ~= 'table' then
if not title then
        cache[titleText] = { meta = {}, records = {}, byId = {} }
return finish(newEmptyResult())
        return cache[titleText]
end
    end


    -- Your files use: version, schema_version, generated_at, records
local content = title:getContent()
    local records = data.records
if type(content) ~= "string" or content == "" then
    if type(records) ~= 'table' then
return finish(newEmptyResult())
        records = {}
end
    end


    local byId = {}
local ok, data = pcall(mw.text.jsonDecode, content)
    for _, rec in ipairs(records) do
if not ok or type(data) ~= "table" then
        if type(rec) == 'table' then
return finish(newEmptyResult())
            local internal =
end
                rec["Internal Name"] or
                rec["InternalName"] or
                rec["InternalID"] or
                rec["Name"]


            if internal and internal ~= '' then
-- Your files use: version, schema_version, generated_at, records
                byId[internal] = rec
local records = data.records
            end
if type(records) ~= "table" then
        end
records = {}
    end
end


    local meta = {
local byId = {}
        version        = data.version,
for _, rec in ipairs(records) do
        schema_version = data.schema_version,
local internal = getInternalKey(rec)
        generated_at  = data.generated_at,
if internal then
    }
byId[internal] = rec
end
end


    local result = {
local result = {
        meta   = meta,
meta = {
        records = records,
version        = data.version,
        byId   = byId,
schema_version = data.schema_version,
    }
generated_at  = data.generated_at,
},
records = records,
byId = byId,
}


    cache[titleText] = result
return finish(result)
    return result
end
end


Line 67: Line 86:


function p.loadSkills()
function p.loadSkills()
    return decodeJsonPage('Data:skills.json')
return decodeJsonPage("Data:skills.json")
end
end


function p.loadPassives()
function p.loadPassives()
    return decodeJsonPage('Data:passives.json')
return decodeJsonPage("Data:passives.json")
end
end


function p.loadSummons()
function p.loadSummons()
    return decodeJsonPage('Data:summons.json')
return decodeJsonPage("Data:summons.json")
end
end


function p.loadEffects()
function p.loadEffects()
    return decodeJsonPage('Data:effects.json')
return decodeJsonPage("Data:effects.json")
end
end


return p
return p