Module:GameData: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
Vergir (talk | contribs)
Create GameData module for generic cross-dataset entity filtering (with help from vergir-bot LLM)
 
Remove Upgrades skip in find_value so abilities gaining charges via upgrades are included (with help from vergir-bot LLM)
Line 25: Line 25:
end
end


-- Recursively searches a record for a given key at any nesting level.
-- Recursively searches a record for a given key at any nesting level,
-- Deliberately skips the "Upgrades" array, which contains delta values
-- including inside the Upgrades array. This means abilities or items that
-- rather than base stats and should not be matched against.
-- only gain a property via an upgrade tier will still be matched.
-- @param  record  table  the data record to search
-- @param  record  table  the data record to search
-- @param  key    string  the internal property name to look for
-- @param  key    string  the internal property name to look for
Line 33: Line 33:
local function find_value(record, key)
local function find_value(record, key)
     for k, v in pairs(record) do
     for k, v in pairs(record) do
         if k == "Upgrades" then
         if k == key then
            -- skip
        elseif k == key then
             return v
             return v
         elseif type(v) == "table" then
         elseif type(v) == "table" then

Revision as of 22:37, 10 May 2026

Documentation for this module may be created at Module:GameData/doc

-- Module:GameData
-- Provides generic access to game data files, with consistent active-entity filtering.
-- Used by ItemTables, and intended for future AbilityTables, HeroTables, etc.

local p = {}

-- Dataset descriptors. Values are the data file paths passed to mw.loadJsonData.
-- Callers should use these constants rather than raw strings.
p.Dataset = {
    ITEMS     = "Data:ItemData.json",
    ABILITIES = "Data:AbilityData.json",
    HEROES    = "Data:HeroData.json",
}

-- Returns true if a record represents an active, usable entity.
-- Applies consistently across all datasets:
--   - IsDisabled must not be true
--   - Name must not be nil
--   - IsSelectable, if present, must not be false
local function is_active(record)
    if record["IsDisabled"] == true then return false end
    if record["Name"] == nil then return false end
    if record["IsSelectable"] ~= nil and record["IsSelectable"] == false then return false end
    return true
end

-- Recursively searches a record for a given key at any nesting level,
-- including inside the Upgrades array. This means abilities or items that
-- only gain a property via an upgrade tier will still be matched.
-- @param   record  table   the data record to search
-- @param   key     string  the internal property name to look for
-- @return          any     the value if found, nil otherwise
local function find_value(record, key)
    for k, v in pairs(record) do
        if k == key then
            return v
        elseif type(v) == "table" then
            local found = find_value(v, key)
            if found ~= nil then return found end
        end
    end
    return nil
end

-- Returns all active entities from a dataset that have a non-nil, non-zero,
-- non-empty value for at least one of the given internal property keys.
-- @param   dataset     string  one of the GameData.Dataset constants
-- @param   properties  table   array of internal property name strings
-- @return              table   array of matching entity records
function p.get_entities(dataset, properties)
    local data = mw.loadJsonData(dataset)
    local results = {}

    for _, record in pairs(data) do
        if is_active(record) then
            for _, prop in ipairs(properties) do
                local value = find_value(record, prop)
                if value ~= nil and value ~= 0 and value ~= "" and value ~= "0" and value ~= "0m" then
                    table.insert(results, record)
                    break
                end
            end
        end
    end

    return results
end

return p