Editing Module:GameDataGive feedback
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
| Latest revision | Your text | ||
| Line 12: | Line 12: | ||
CONVARS = "Data:Convars.json", | CONVARS = "Data:Convars.json", | ||
} | } | ||
-- Returns true if a record represents an active, usable entity. | -- Returns true if a record represents an active, usable entity. | ||
| Line 24: | Line 18: | ||
-- - Name must not be nil | -- - Name must not be nil | ||
-- - IsSelectable, if present, must not be false | -- - IsSelectable, if present, must not be false | ||
local function is_active(record) | local function is_active(record) | ||
if record["IsDisabled"] == true then return false end | if record["IsDisabled"] == true then return false end | ||
| Line 38: | Line 29: | ||
-- - a plain value: matches if the string appears as a value anywhere in the record | -- - a plain value: matches if the string appears as a value anywhere in the record | ||
-- Returns true if a match is found, false otherwise. | -- Returns true if a match is found, false otherwise. | ||
local function record_matches_prop(record, prop) | local function record_matches_prop(record, prop) | ||
for k, v in pairs(record) do | for k, v in pairs(record) do | ||
| Line 66: | Line 55: | ||
end | end | ||
-- Returns true if a record matches at least one of the given properties, | |||
-- using the same key-name-or-string-value semantics as get_entities. | |||
-- Returns true if a record matches at least one of the given properties | |||
-- | |||
-- | |||
-- Public so other modules (e.g. Module:AbilityTable membership) can reuse it. | -- Public so other modules (e.g. Module:AbilityTable membership) can reuse it. | ||
function p.entity_matches(record, properties) | function p.entity_matches(record, properties) | ||
for _, prop in ipairs(properties) do | for _, prop in ipairs(properties) do | ||
if record_matches_prop(record, prop) then | |||
if | |||
return true | return true | ||
end | end | ||
| Line 186: | Line 68: | ||
-- Returns all active entities from a dataset that match at least one of the | -- Returns all active entities from a dataset that match at least one of the | ||
-- given properties. A property may be a key name (the property exists with a | -- given properties. A property may be matched either as a key name (the | ||
-- property exists with a non-zero value) or as a plain string value anywhere | |||
-- in the record (e.g. "melee" matching Scale.Type = "melee"). | |||
-- @param dataset string one of the GameData.Dataset constants | -- @param dataset string one of the GameData.Dataset constants | ||
-- @param properties table array of property strings | -- @param properties table array of property strings | ||
| Line 214: | Line 95: | ||
-- Find an entity by display name or internal key. | -- Find an entity by display name or internal key. | ||
-- Uses ResourceLookup type field to go directly to the right dataset. | -- Uses ResourceLookup type field to go directly to the right dataset. | ||
-- Returns (entity_record, type_string | -- Returns (entity_record, type_string) or (nil, nil). | ||
local function find_entity(identifier) | local function find_entity(identifier) | ||
local resource = mw.loadJsonData("Data:ResourceLookup.json")[identifier:lower()] | local resource = mw.loadJsonData("Data:ResourceLookup.json")[identifier:lower()] | ||
| Line 227: | Line 108: | ||
end | end | ||
if data and data[resource.key] then | if data and data[resource.key] then | ||
return data[resource.key], resource.type | return data[resource.key], resource.type | ||
end | end | ||
end | end | ||
| Line 239: | Line 120: | ||
for _, ds in ipairs(datasets) do | for _, ds in ipairs(datasets) do | ||
local data = mw.loadJsonData(ds[1]) | local data = mw.loadJsonData(ds[1]) | ||
if data[identifier] then return data[identifier], ds[2] | if data[identifier] then return data[identifier], ds[2] end | ||
end | end | ||
return | return nil, nil | ||
end | end | ||
| Line 288: | Line 169: | ||
-- {{#invoke:GameData|get_prop|...}} | -- {{#invoke:GameData|get_prop|...}} | ||
-- Two modes: | -- Two modes: | ||
-- Entity: {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY | -- Entity: {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY}} | ||
-- Finds entity via ResourceLookup, returns the raw value (no | -- Finds entity via ResourceLookup, returns the raw value (no | ||
-- formatting). Supports dot notation for nested properties | -- formatting). Supports dot notation for nested properties | ||
-- (e.g. "Scale.Value"); tables with a .Value field are unwrapped. | -- (e.g. "Scale.Value"); tables with a .Value field are unwrapped. | ||
-- Convar: {{#invoke:GameData|get_prop|Convar|CONVAR_NAME}} | -- Convar: {{#invoke:GameData|get_prop|Convar|CONVAR_NAME}} | ||
-- Looks up the convar in Data:Convars.json and returns its value, | -- Looks up the convar in Data:Convars.json and returns its value, | ||
| Line 302: | Line 179: | ||
local arg1 = frame.args[1] | local arg1 = frame.args[1] | ||
local arg2 = frame.args[2] | local arg2 = frame.args[2] | ||
if not arg1 then return "" end | if not arg1 then return "" end | ||
| Line 321: | Line 197: | ||
if not prop then return "" end | if not prop then return "" end | ||
local entity, etype | local entity, etype = find_entity(name) | ||
if not entity then | if not entity then | ||
return '<span style="color:red;">Entity not found: ' .. name .. '</span>' | return '<span style="color:red;">Entity not found: ' .. name .. '</span>' | ||
end | end | ||