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 1: | Line 1: | ||
-- Provides generic access to game data files | -- Provides generic access to game data files | ||
| Line 12: | Line 11: | ||
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 17: | ||
-- - 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 28: | ||
-- - 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 61: | Line 49: | ||
-- Value match: prop string appears as a value (e.g. Scale.Type = "melee") | -- Value match: prop string appears as a value (e.g. Scale.Type = "melee") | ||
if v == prop then return true end | if v == prop then return true end | ||
end | end | ||
end | end | ||
| Line 186: | Line 55: | ||
-- 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 198: | Line 66: | ||
for _, record in pairs(data) do | for _, record in pairs(data) do | ||
if is_active(record) | if is_active(record) then | ||
for _, prop in ipairs(properties) do | |||
if record_matches_prop(record, prop) then | |||
table.insert(results, record) | |||
break | |||
end | |||
end | |||
end | end | ||
end | end | ||
| Line 214: | Line 87: | ||
-- 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 100: | ||
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 112: | ||
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 161: | ||
-- {{#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 171: | ||
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 189: | ||
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 | ||