Module:GameData: Difference between revisionsGive feedback
Jump to navigation
Jump to search
Return visible error message when entity/property not found in get_prop (with help from vergir-bot LLM) |
Refactor get_prop: use ResourceLookup type for direct routing, unified find_entity, clearer error messages (with help from vergir-bot LLM) |
||
| Line 75: | Line 75: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Unified property lookup: get_prop | -- Unified property lookup: get_prop | ||
-- | -- Uses ResourceLookup to route directly to the correct dataset. | ||
-- Returns raw values for use in templates and expressions. | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 92: | Line 93: | ||
if not _hero_data then _hero_data = mw.loadJsonData(p.Dataset.HEROES) end | if not _hero_data then _hero_data = mw.loadJsonData(p.Dataset.HEROES) end | ||
return _hero_data | return _hero_data | ||
end | |||
local function load_resource_lookup() | |||
if not _resource_lookup then _resource_lookup = mw.loadJsonData("Data:ResourceLookup.json") end | |||
return _resource_lookup | |||
end | end | ||
-- Find an | -- Find an entity by display name or internal key. | ||
local function | -- Uses ResourceLookup type field to go directly to the right dataset. | ||
local | -- Returns (entity_record, type_string) or (nil, nil). | ||
if | local function find_entity(identifier) | ||
local resource = load_resource_lookup()[identifier:lower()] | |||
if resource then | |||
local data | |||
if resource.type == "ability" then | |||
data = load_ability_data() | |||
elseif resource.type == "hero" then | |||
data = load_hero_data() | |||
elseif resource.type == "item" then | |||
data = load_item_data() | |||
end | |||
if data and data[resource.key] then | |||
return data[resource.key], resource.type | |||
end | |||
end | end | ||
-- | -- Fallback: try as direct internal key | ||
local datasets = { | |||
local | { load_ability_data, "ability" }, | ||
for _, | { load_hero_data, "hero" }, | ||
{ load_item_data, "item" }, | |||
} | |||
for _, ds in ipairs(datasets) do | |||
local data = ds[1]() | |||
if data[identifier] then return data[identifier], ds[2] end | |||
end | end | ||
return nil, nil | |||
end | end | ||
| Line 144: | Line 148: | ||
-- {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY}} | -- {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY}} | ||
-- | -- Finds entity via ResourceLookup, returns the raw value (no formatting). | ||
-- Supports dot notation for nested properties (e.g. "Scale.Value"). | -- Supports dot notation for nested properties (e.g. "Scale.Value"). | ||
-- Tables with a .Value field are automatically unwrapped. | -- Tables with a .Value field are automatically unwrapped. | ||
| Line 152: | Line 156: | ||
if not name or not prop then return "" end | if not name or not prop then return "" end | ||
local entity, etype = find_entity(name) | |||
local entity = | if not entity then | ||
if entity then | return '<span style="color:red;">Entity not found: ' .. name .. '</span>' | ||
end | end | ||
local result = resolve_prop(entity, prop) | |||
if result ~= nil then return result end | |||
if entity.Weapon then | |||
result = resolve_prop(entity.Weapon, prop) | |||
if entity then | |||
if result ~= nil then return result end | if result ~= nil then return result end | ||
end | end | ||
return '<span style="color:red;">Prop not found: ' .. name .. ' | return '<span style="color:red;">Prop not found: ' .. name .. '/' .. prop .. '</span>' | ||
end | end | ||
return p | return p | ||