Модуль:Песочница/Pok: различия между версиями

Нет описания правки
мНет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}
local function trim(s)
if not s then return s end
return (s:gsub("^%s*(.-)%s*$", "%1"))
end


local function split(s, sep)
local function split(s, sep)
Строка 11: Строка 16:
end
end


local function parse_indexed_part(part)
local function parse_keys_from_template(content)
local key, idx = string.match(part, "^(.-)%[(%d+)%]$")
if not content then return {} end
if key then
local keys = {}
return key, tonumber(idx)
local titlePos = string.find(content, "|%s*Title%s*=")
if not titlePos then
return keys
end
end
local num = tonumber(part)
local substr = content:sub(titlePos)
if num then
local endPos = substr:find("}}")
return nil, num
local region = substr
if endPos then
region = substr:sub(1, endPos)
end
end
return part, nil
for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
end
local k = trim(key)
 
if k ~= "" then
local function get_by_path(tbl, path)
table.insert(keys, k)
if not tbl then return nil end
local parts = split(path, "%.")
local cur = tbl
for _, part in ipairs(parts) do
local key, idx = parse_indexed_part(part)
if key and key ~= "" then
local nextCur = nil
if type(cur) == "table" then
nextCur = cur[key]
if nextCur == nil then
nextCur = cur["!type:" .. key]
end
end
cur = nextCur
end
if idx then
if type(cur) ~= "table" then
return nil
end
cur = cur[idx]
end
if cur == nil then
return nil
end
end
end
end
return cur
return keys
end
end


local function is_array(t)
local function load_module_data(page)
if type(t) ~= "table" then return false end
local baseUser = "IanComradeBot/"
local i = 0
local moduleName = "Module:" .. baseUser .. page .. "/data"
for _ in pairs(t) do
local ok, data = pcall(mw.loadData, moduleName)
i = i + 1
if not ok then return nil end
end
return data
local n = 0
for k in pairs(t) do
if type(k) ~= "number" then
return false
end
if k > n then n = k end
end
return n > 0
end
end


local function format_value(v)
local function load_template_content(path)
if v == nil then return "" end
local title = mw.title.new("Template:" .. path)
local t = type(v)
if not title then return nil end
if t == "string" or t == "number" or t == "boolean" then
local ok, content = pcall(function() return title:getContent() end)
return tostring(v)
if not ok then return nil end
elseif t == "table" then
return content
if is_array(v) then
local out = {}
local max = 0
for k in pairs(v) do if type(k) == "number" and k > max then max = k end end
for i = 1, max do
table.insert(out, format_value(v[i]))
end
return table.concat(out, ", ")
else
local out = {}
for k, val in pairs(v) do
table.insert(out, tostring(k) .. ": " .. format_value(val))
end
return table.concat(out, ", ")
end
else
return tostring(v)
end
end
end


Строка 97: Строка 57:
local args = frame.args or {}
local args = frame.args or {}
local id = args[1] or ""
local id = args[1] or ""
local pagePath = args[2] or ""
if id == "" then return "" end
local keyPath = args[3] or ""


local baseUser = "IanComradeBot/"
local componentDefs = load_module_data("component.json")
local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
local prototypeDefs = load_module_data("prototype.json")
local entities = load_module_data("entity_prototypes.json")
if not componentDefs or not prototypeDefs then
return ""
end
if not entities then
return ""
end


local ok, data = pcall(mw.loadData, moduleName)
local entity = entities[id]
if not ok or not data then
if not entity then
return ""
return ""
end
end


local entry = nil
local foundComponents = {}
if id ~= "" then
-- If entity explicitly lists components use that
entry = data[id]
if type(entity.components) == "table" then
for _, v in ipairs(entity.components) do
if type(v) == "string" then
foundComponents[v] = true
elseif type(v) == "table" and v.name then
foundComponents[v.name] = true
end
end
else
-- fallback: detect component keys present in entity
for compName,_ in pairs(componentDefs) do
if entity[compName] ~= nil then
foundComponents[compName] = true
end
end
end
end
if entry == nil then
 
entry = data["default"]
local foundPrototypes = {}
if type(entity.prototypes) == "table" then
for _, v in ipairs(entity.prototypes) do
if type(v) == "string" then
foundPrototypes[v] = true
end
end
else
for protoName,_ in pairs(prototypeDefs) do
if entity[protoName] ~= nil then
foundPrototypes[protoName] = true
end
end
end
end
if entry == nil then
 
return ""
local out = {}
 
for compName,_ in pairs(foundComponents) do
local tplPath = "component/" .. compName
local content = load_template_content(tplPath)
if not content then
table.insert(out, "<h2>" .. mw.text.encode(compName) .. "</h2>")
table.insert(out, "Template: " .. tplPath)
else
local keys = parse_keys_from_template(content)
for _, key in ipairs(keys) do
table.insert(out, "<h2>" .. key .. "</h2>")
table.insert(out, "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
end


if keyPath == "" then
for protoName,_ in pairs(foundPrototypes) do
return format_value(entry)
local tplPath = "prototype/" .. protoName
local content = load_template_content(tplPath)
if not content then
table.insert(out, "<h2>" .. mw.text.encode(protoName) .. "</h2>")
table.insert(out, "Template: " .. tplPath)
else
local keys = parse_keys_from_template(content)
for _, key in ipairs(keys) do
table.insert(out, "<h2>" .. key .. "</h2>")
table.insert(out, "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
end


local value = get_by_path(entry, keyPath)
return table.concat(out, "\n\n")
return format_value(value)
end
end


return p
return p