Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| (не показаны 3 промежуточные версии этого же участника) | |||
| Строка 4: | Строка 4: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
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 | ||
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 | ||
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 | 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 = | 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 = | 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 | 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 = | local data = p.loadCachedData(storeName) | ||
if not data then | if not data then | ||
return "" | return "" | ||
| Строка 761: | Строка 758: | ||
end | end | ||
local data = | 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" | ||
local data = p.loadCachedData(pagePath) | |||
if not data then | |||
local data = | |||
if not data | |||
return "" | return "" | ||
end | end | ||
local | local matches = {} | ||
local seen = {} | |||
local function add_id(id) | |||
local s = tostring(id) | |||
if s ~= "" and not seen[s] then | |||
seen[s] = true | |||
matches[#matches + 1] = s | |||
if | |||
end | end | ||
end | |||
for _, key in ipairs(targets) do | |||
local entry = resolve_entry_from_data(data, key) | |||
if entry ~= nil then | |||
local ids = collect_ids_from_entry(entry) | |||
for _, id in ipairs(ids) do | |||
add_id(id) | |||
end | end | ||
end | end | ||
end | end | ||
table.sort(matches, function(a, b) | |||
return tostring(a) < tostring(b) | |||
end) | |||
end | |||
local ok, json = pcall(mw.text.jsonEncode, matches) | local ok, json = pcall(mw.text.jsonEncode, matches) | ||
| Строка 899: | Строка 916: | ||
end | end | ||
local data = | 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 = | 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 | ||
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 | return p.loadCachedData(pagePath) | ||
end | end | ||
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 | ||
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 = | local entity = p.loadEntityData(entityId) | ||
if not entity then | if not entity then | ||
return "false" | return "false" | ||
end | end | ||
return | return p.entityHasComponent(entity, compName) and "true" or "false" | ||
end | end | ||
| Строка 1124: | Строка 1141: | ||
end | end | ||
local entity = | local entity = p.loadEntityData(entityId) | ||
if not entity then | if not entity then | ||
return "" | return "" | ||
end | end | ||
local out = | 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 = | local data = p.loadCachedData(pagePath) | ||
if not data then | if not data then | ||
return "" | return "" | ||
| Строка 1198: | Строка 1215: | ||
end | end | ||
local data = | local data = p.loadCachedData(pagePath) | ||
if not data then | if not data then | ||
return "" | return "" | ||