Модуль:GetField: различия между версиями

Нет описания правки
Нет описания правки
Строка 4: Строка 4:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs


local function load_cached_data(pagePath)
function p.loadCachedData(pagePath)
local titleName = JsonPaths.get(pagePath)
local titleName = JsonPaths.get(pagePath)
if not titleName or titleName == "" then
if not titleName or titleName == "" then
Строка 152: Строка 152:
end
end


local function ucfirst(s)
function p.ucfirst(s)
if not s or s == "" then return s end
if not s or s == "" then return s end
return string.upper(s:sub(1, 1)) .. (s:sub(2) or "")
return string.upper(s:sub(1, 1)) .. (s:sub(2) or "")
end
end


local function normalizeComponentName(name)
function p.normalizeComponentName(name)
if type(name) ~= "string" or name == "" then
if type(name) ~= "string" or name == "" then
return nil
return nil
Строка 175: Строка 175:
local function split_data_page_path(pagePath)
local function split_data_page_path(pagePath)
local kind, name = string.match(pagePath or "", "^(component)/([^/]+)%.json$")
local kind, name = string.match(pagePath or "", "^(component)/([^/]+)%.json$")
if not kind then
kind, name = string.match(pagePath or "", "^(prototype)/([^/]+)%.json$")
end
if not kind then
if not kind then
return nil, nil
return nil, nil
end
end
return kind, ucfirst(name)
return kind, p.ucfirst(name)
end
end


Строка 205: Строка 202:


local base
local base
local defaultData = load_cached_data(kind .. "/" .. name .. "/defaultFields.json")
local defaultData = p.loadCachedData(kind .. "/" .. name .. "/defaultFields.json")
if type(defaultData) == "table" then
if type(defaultData) == "table" then
base = deep_copy(defaultData)
base = deep_copy(defaultData)
Строка 211: Строка 208:


local entityPage = "prototype/Entity/" .. id .. ".json"
local entityPage = "prototype/Entity/" .. id .. ".json"
local entity = load_cached_data(entityPage)
local entity = p.loadCachedData(entityPage)
local specific = get_component_from_entity(entity, name)
local specific = get_component_from_entity(entity, name)


Строка 273: Строка 270:
end
end


data = data or load_cached_data(pagePath)
data = data or p.loadCachedData(pagePath)
if allowDefault == nil then
if allowDefault == nil then
allowDefault = true
allowDefault = true
Строка 561: Строка 558:


local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
local data = load_cached_data(storeName)
local data = p.loadCachedData(storeName)
if not data then
if not data then
return ""
return ""
Строка 761: Строка 758:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return "[]"
return "[]"
Строка 819: Строка 816:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data or type(data) ~= "table" then
if not data or type(data) ~= "table" then
return ""
return ""
Строка 899: Строка 896:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 998: Строка 995:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 1049: Строка 1046:
end
end


local function load_entity_data(entityId)
function p.loadEntityData(entityId)
if entityId == "" then
if entityId == "" then
return nil
return nil
Строка 1055: Строка 1052:


local pagePath = "prototype/Entity/" .. entityId .. ".json"
local pagePath = "prototype/Entity/" .. entityId .. ".json"
return load_cached_data(pagePath)
return p.loadCachedData(pagePath)
end
end


local function entity_has_component(entity, compName)
function p.entityHasComponent(entity, compName)
if type(entity) ~= "table" or type(entity.components) ~= "table" then
if type(entity) ~= "table" or type(entity.components) ~= "table" then
return false
return false
Строка 1068: Строка 1065:
end
end


local function collect_entity_components(entity)
function p.collectEntityComponents(entity)
if type(entity) ~= "table" or type(entity.components) ~= "table" then
if type(entity) ~= "table" or type(entity.components) ~= "table" then
return {}
return {}
Строка 1079: Строка 1076:
if #comps > 0 then
if #comps > 0 then
for _, v in ipairs(comps) do
for _, v in ipairs(comps) do
local name = normalizeComponentName(v)
local name = p.normalizeComponentName(v)
if name and not seen[name] then
if name and not seen[name] then
seen[name] = true
seen[name] = true
Строка 1087: Строка 1084:
else
else
for k in pairs(comps) do
for k in pairs(comps) do
local name = normalizeComponentName(k)
local name = p.normalizeComponentName(k)
if name and not seen[name] then
if name and not seen[name] then
seen[name] = true
seen[name] = true
Строка 1108: Строка 1105:
end
end


local entity = load_entity_data(entityId)
local entity = p.loadEntityData(entityId)
if not entity then
if not entity then
return "false"
return "false"
end
end


return entity_has_component(entity, compName) and "true" or "false"
return p.entityHasComponent(entity, compName) and "true" or "false"
end
end


Строка 1124: Строка 1121:
end
end


local entity = load_entity_data(entityId)
local entity = p.loadEntityData(entityId)
if not entity then
if not entity then
return ""
return ""
end
end


local out = collect_entity_components(entity)
local out = p.collectEntityComponents(entity)
local ok, json = pcall(mw.text.jsonEncode, out)
local ok, json = pcall(mw.text.jsonEncode, out)
if ok and json then
if ok and json then
Строка 1148: Строка 1145:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 1198: Строка 1195:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 1644: Строка 1641:
end
end
end
end
p.ucfirst = ucfirst
p.loadCachedData = load_cached_data
p.loadEntityData = load_entity_data
p.normalizeComponentName = normalizeComponentName
p.collectEntityComponents = collect_entity_components
p.entityHasComponent = entity_has_component


function p.flattenFieldSelectiveDirect(id, dataPage, paramNames)
function p.flattenFieldSelectiveDirect(id, dataPage, paramNames)