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

Нет описания правки
мНет описания правки
 
(не показаны 2 промежуточные версии этого же участника)
Строка 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
Строка 551: Строка 548:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local fieldId = args[3] or ""
local fieldId = p.ucfirst(args[3] or "")


if searchId == "" or fieldId == "" then
if searchId == "" or fieldId == "" then
Строка 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 "[]"
Строка 796: Строка 793:


return ""
return ""
end
local function collect_ids_from_entry(entry)
local out = {}
local seen = {}
local function add(v)
if v == nil then
return
end
local s = tostring(v)
if s ~= "" and not seen[s] then
seen[s] = true
out[#out + 1] = s
end
end
if type(entry) == "table" then
if is_array(entry) then
for _, v in ipairs(entry) do
add(v)
end
else
for _, v in pairs(entry) do
if type(v) == "table" and is_array(v) then
for _, item in ipairs(v) do
add(item)
end
elseif type(v) ~= "table" then
add(v)
end
end
end
else
add(entry)
end
return out
end
end


Строка 812: Строка 847:
end
end


local pagePath
local pagePath = (mode == "component") and "component.json" or "tag.json"
if mode == "component" then
local data = p.loadCachedData(pagePath)
pagePath = "component.json"
if not data then
else
pagePath = "tag.json"
end
 
local data = load_cached_data(pagePath)
if not data or type(data) ~= "table" then
return ""
return ""
end
end


local ids = collect_id_keys(data)
local matches = {}
if #ids == 0 then
local seen = {}
return ""
end


table.sort(ids, function(a, b)
local function add_id(id)
return tostring(a) < tostring(b)
local s = tostring(id)
end)
if s ~= "" and not seen[s] then
 
seen[s] = true
local function entry_has_all_targets(entry, wanted)
matches[#matches + 1] = s
if type(entry) ~= "table" then
return false
end
end
end


local set = {}
for _, key in ipairs(targets) do
for _, v in ipairs(entry) do
local entry = resolve_entry_from_data(data, key)
set[tostring(v)] = true
if entry ~= nil then
end
local ids = collect_ids_from_entry(entry)
 
for _, id in ipairs(ids) do
for i = 1, #wanted do
add_id(id)
if not set[wanted[i]] then
return false
end
end
end
end
return true
end
end


local matches = {}
table.sort(matches, function(a, b)
 
return tostring(a) < tostring(b)
for _, idKey in ipairs(ids) do
end)
local entry = resolve_entry_from_data(data, idKey)
if entry_has_all_targets(entry, targets) then
matches[#matches + 1] = idKey
end
end


local ok, json = pcall(mw.text.jsonEncode, matches)
local ok, json = pcall(mw.text.jsonEncode, matches)
Строка 899: Строка 916:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 977: Строка 994:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local generatorId = args[3] or ""
local generatorId = p.ucfirst(args[3] or "")
local tplPath = mw.text.unstripNoWiki(args[4] or "")
local tplPath = mw.text.unstripNoWiki(args[4] or "")
local tplArgs = args[5] or args.tplArgs or args.templateArgs or ""
local tplArgs = args[5] or args.tplArgs or args.templateArgs or ""
Строка 998: Строка 1015:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 1026: Строка 1043:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local generatorId = args[3] or ""
local generatorId = p.ucfirst(args[3] or "")


if searchId == "" or generatorId == "" then
if searchId == "" or generatorId == "" then
Строка 1049: Строка 1066:
end
end


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


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: Строка 1085:
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: Строка 1096:
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: Строка 1104:
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: Строка 1125:
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: Строка 1141:
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: Строка 1165:
end
end


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


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 1644: Строка 1661:
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)