Модуль:Entity Sprite/all: различия между версиями

Нет описания правки
Метка: отменено
Нет описания правки
 
(не показаны 4 промежуточные версии этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local JsonPaths = require('Module:JsonPaths')
local JsonPaths = require('Module:JsonPaths')
local prefix = JsonPaths.prefixFile()


local function normalizeSpritePath(path)
local function normalizeSpritePath(path)
Строка 139: Строка 138:
end
end


local function getParents(entry)
local function getParents(parentData, id)
if not entry then
if not parentData or type(parentData) ~= "table" or not id then
return nil
return nil
end
end


if type(entry.parents) == "table" then
local parents = parentData[id]
return entry.parents
if type(parents) ~= "table" then
end
return nil
 
if type(entry.parents) == "string" then
return splitCsv(entry.parents)
end
end


return nil
return parents
end
end


Строка 169: Строка 165:
end
end


local function shouldIncludeEntry(entry, whitelistSet, blacklistSet)
local function shouldIncludeEntry(id, whitelistSet, blacklistSet, parentData)
local parents = getParents(entry)
local parents = getParents(parentData, id)


if whitelistSet and not hasAnyParent(parents, whitelistSet) then
if whitelistSet and not hasAnyParent(parents, whitelistSet) then
Строка 183: Строка 179:
end
end


local function filterSpriteData(spriteData, prototypeData, whitelistSet, blacklistSet, project)
local function filterSpriteData(spriteData, parentData, whitelistSet, blacklistSet, project)
local result = {}
local result = {}


for id, entry in pairs(spriteData) do
for id, entry in pairs(spriteData) do
local protoEntry = prototypeData and prototypeData[id] or entry
if shouldIncludeEntry(id, whitelistSet, blacklistSet, parentData) then
 
if shouldIncludeEntry(protoEntry, whitelistSet, blacklistSet) then
if project == "" or JsonPaths.has(id, project) then
if project == "" or JsonPaths.has(id, project) then
result[id] = entry
result[id] = entry
Строка 199: Строка 193:
end
end


local function collectFileTitles(data)
local function buildFileLink(id, project)
local titles = {}
return "[[:Файл:" .. JsonPaths.prefixFile(id) .. ".png]]"
 
for id, _ in pairs(data) do
table.insert(titles, "Файл:" .. prefix .. id .. ".png")
end
 
return titles
end
 
local function buildFileExistenceMap(data)
local map = {}
 
if mw.title and mw.title.newBatch then
local titlesList = {}
 
for id, _ in pairs(data) do
table.insert(titlesList, "Файл:" .. prefix .. id .. ".png")
end
 
local titleObjs = mw.title.newBatch(titlesList)
:lookupExistence()
:getTitles()
 
for _, title in ipairs(titleObjs) do
if title and title.prefixedText then
map[title.prefixedText] = title.exists and not title.isRedirect
end
end
 
return map
end
 
for id, _ in pairs(data) do
local name = "Файл:" .. prefix .. id .. ".png"
local title = mw.title.new(name)
 
if title then
map[name] = title.exists and not title.isRedirect
else
map[name] = false
end
end
 
return map
end
 
local function buildFileLink(id, existenceMap)
local name = "Файл:" .. prefix .. id .. ".png"
local link = "[[:" .. name .. "]]"
 
if existenceMap[name] then
return "<code>" .. link .. "</code>"
end
 
return link
end
end


Строка 303: Строка 243:
end
end


local function generateRepeatTemplate(data, project, baseUrl, existenceMap)
local function generateRepeatTemplate(data, project, baseUrl)
local spriteGroups = {}
local spriteGroups = {}


Строка 320: Строка 260:


for _, obj in ipairs(group) do
for _, obj in ipairs(group) do
table.insert(idLinks, buildFileLink(obj.id, existenceMap))
table.insert(idLinks, buildFileLink(obj.id, project))
end
end


Строка 329: Строка 269:
table.insert(result, mw.getCurrentFrame():preprocess(
table.insert(result, mw.getCurrentFrame():preprocess(
"{{Entity Sprite/Repeat|файлы=" .. table.concat(idLinks, " ") ..
"{{Entity Sprite/Repeat|файлы=" .. table.concat(idLinks, " ") ..
"|перенаправление=" .. prefix .. firstId ..
"|перенаправление=" .. JsonPaths.prefixFile(firstId) ..
"|id=" .. firstId ..
"|id=" .. firstId ..
"|спрайт=" .. spriteValue ..
"|спрайт=" .. spriteValue ..
Строка 350: Строка 290:


return mw.getCurrentFrame():preprocess(
return mw.getCurrentFrame():preprocess(
"{{Entity Sprite/Image|файл=" .. prefix .. id ..
"{{Entity Sprite/Image|файл=" .. JsonPaths.prefixFile(id) ..
"|id=" .. id ..
"|id=" .. id ..
"|путь=" .. buildSpriteUrl(baseUrl, spritePath) ..
"|путь=" .. buildSpriteUrl(baseUrl, spritePath) ..
Строка 360: Строка 300:
function p.main(frame)
function p.main(frame)
local action = frame.args[1]
local action = frame.args[1]
local json = frame.args.json or "prototype/sprite.json"
local json = frame.args.json or "component/Sprite.json"


local project = JsonPaths.project()
local project = JsonPaths.project()
Строка 366: Строка 306:


local dataPage = JsonPaths.get(json)
local dataPage = JsonPaths.get(json)
local prototypesPage = JsonPaths.get("entity prototypes.json")
local parentPage = JsonPaths.get("entity parent.json")


local spriteDataRaw = mw.loadData(dataPage)
local spriteDataRaw = mw.loadData(dataPage)
local prototypeDataRaw = mw.loadData(prototypesPage)
local parentDataRaw = mw.loadData(parentPage)


local spriteData = normalizeData(spriteDataRaw)
local spriteData = normalizeData(spriteDataRaw)
local prototypeData = normalizeData(prototypeDataRaw)
local parentData = normalizeData(parentDataRaw)


if not spriteData or type(spriteData) ~= "table" then
if not spriteData or type(spriteData) ~= "table" then
Строка 378: Строка 318:
end
end


if not prototypeData or type(prototypeData) ~= "table" then
if not parentData or type(parentData) ~= "table" then
return "Ошибка загрузки JSON: " .. prototypesPage
return "Ошибка загрузки JSON: " .. parentPage
end
end


local whitelistSet = buildSet(frame.args.whitelistParent)
local whitelistSet = buildSet(frame.args.whitelistParent)
local blacklistSet = buildSet(frame.args.blacklistParent)
local blacklistSet = buildSet(frame.args.blacklistParent)
local filteredData = filterSpriteData(spriteData, prototypeData, whitelistSet, blacklistSet, project)
local filteredData = filterSpriteData(spriteData, parentData, whitelistSet, blacklistSet, project)


if action == "repeat" then
if action == "repeat" then
local existenceMap = buildFileExistenceMap(filteredData)
return generateRepeatTemplate(filteredData, project, baseUrl)
return generateRepeatTemplate(filteredData, project, baseUrl, existenceMap)
elseif action == "image" then
elseif action == "image" then
local result = {}
local result = {}