Модуль:Песочница/Pok: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показано 15 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local JsonPaths = require('Module:JsonPaths') | |||
local function getSpritePath(entry) | |||
return entry.sprite | |||
local function | |||
return | |||
end | end | ||
local function | local function getSpriteStates(entry) | ||
local | local result = {} | ||
if entry.layers and entry.sprite then | |||
if | for _, layer in ipairs(entry.layers) do | ||
if layer.visible ~= false then | |||
table.insert(result, { | |||
state = tostring(layer.state or entry.state or ""), | |||
sprite = entry.sprite | |||
}) | |||
end | end | ||
end | end | ||
elseif entry.state and entry.sprite then | |||
table.insert(result, { | |||
state = tostring(entry.state or ""), | |||
sprite = entry.sprite | |||
}) | |||
end | end | ||
return (#result > 0) and result or nil | |||
end | end | ||
local function | local function getPrefix(id, project) | ||
if project ~= "" and JsonPaths.has(id, project) then | |||
return project .. ":" | |||
end | end | ||
return | return "" | ||
end | end | ||
local | local MAX_CHECKS = 100 | ||
local checkCount = 0 | |||
local | |||
local function | local function fileExists(name) | ||
if | if checkCount >= MAX_CHECKS then | ||
return false | return false | ||
end | end | ||
checkCount = checkCount + 1 | |||
local | local title = mw.title.new(name, "File") | ||
return title and title.exists | |||
return | |||
end | end | ||
local function | local function generateRepeatTemplate(data, project) | ||
local | local spriteGroups = {} | ||
for id, entry in pairs(data) do | |||
local found = false | |||
for _, group in pairs(spriteGroups) do | |||
local g = group[1] | |||
if entry.sprite == g.entry.sprite and entry.state == g.entry.state then | |||
table.insert(group, { id = id, entry = entry }) | |||
found = true | |||
break | |||
end | end | ||
end | end | ||
if not found then | |||
table.insert(spriteGroups, { | |||
{ id = id, entry = entry } | |||
}) | |||
end | end | ||
end | end | ||
local result = {} | |||
for _, group in pairs(spriteGroups) do | |||
if #group > 1 then | |||
local idLinks = {} | |||
for _, obj in pairs(group) do | |||
local id = obj.id | |||
local prefix = getPrefix(id, project) | |||
table.insert(idLinks, "[[:Файл:" .. prefix .. id .. ".png]]") | |||
end | end | ||
table.insert(result, mw.getCurrentFrame():preprocess( | |||
"{{Entity Sprite/Repeat|" .. | |||
table.concat(idLinks, " ") .. | |||
"|" .. group[1].id .. | |||
"}}" | |||
)) | |||
end | end | ||
end | end | ||
return table.concat(result, "\n") | |||
end | end | ||
function | local function generateTemplate(id, entry, baseUrl, project) | ||
local spritePath = getSpritePath(entry) | |||
if not id or not spritePath then | |||
return nil | |||
local | |||
return | |||
end | end | ||
local | local prefix = getPrefix(id, project) | ||
local | local states = getSpriteStates(entry) | ||
local | local stateStr = "" | ||
local | if states then | ||
local links = {} | |||
for _, item in ipairs(states) do | |||
local url = baseUrl .. item.sprite .. "/" .. item.state .. ".png" | |||
table.insert(links, "[" .. url .. " " .. item.state .. "]") | |||
end | end | ||
stateStr = table.concat(links, ", ") | |||
end | end | ||
return mw.getCurrentFrame():preprocess( | |||
"{{Песочница/Pok|файл=" .. prefix .. id .. | |||
"|id=" .. id .. | |||
"|путь=" .. baseUrl .. spritePath .. | |||
"|state=" .. stateStr .. | |||
"}}" | |||
) | |||
end | end | ||
function p. | function p.main(frame) | ||
local action = frame.args[1] | |||
local json = frame.args.json or "prototype/sprite.json" | |||
local checkFile = frame.args.checkFile or "" | |||
if checkFile == "" then checkFile = false end | |||
local | |||
local project = JsonPaths.project() | |||
local | local baseUrl = JsonPaths.git() .. "/Resources/Textures/" | ||
local | local dataPage = JsonPaths.get(json) | ||
local spriteData = mw.loadData(dataPage) | |||
if | |||
local | |||
local | |||
if not spriteData or type(spriteData) ~= "table" then | |||
return "Ошибка загрузки JSON: " .. dataPage | |||
return | |||
end | end | ||
if action == "repeat" then | |||
return generateRepeatTemplate(spriteData, project) | |||
elseif action == "image" then | |||
local result = {} | |||
for id, entry in pairs(spriteData) do | |||
local prefix = getPrefix(id, project) | |||
local skip = false | |||
if checkFile then | |||
local fileName = prefix .. id .. ".png" | |||
if fileExists(fileName) then | |||
skip = true | |||
end | |||
end | |||
if not skip then | |||
local t = generateTemplate(id, entry, baseUrl, project) | |||
if t then | |||
table.insert(result, t) | |||
end | |||
end | |||
end | end | ||
return table.concat(result, "\n") | |||
end | end | ||
return nil | |||
end | end | ||
return p | return p | ||
Текущая версия от 00:39, 22 марта 2026
Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc
local p = {}
local JsonPaths = require('Module:JsonPaths')
local function getSpritePath(entry)
return entry.sprite
end
local function getSpriteStates(entry)
local result = {}
if entry.layers and entry.sprite then
for _, layer in ipairs(entry.layers) do
if layer.visible ~= false then
table.insert(result, {
state = tostring(layer.state or entry.state or ""),
sprite = entry.sprite
})
end
end
elseif entry.state and entry.sprite then
table.insert(result, {
state = tostring(entry.state or ""),
sprite = entry.sprite
})
end
return (#result > 0) and result or nil
end
local function getPrefix(id, project)
if project ~= "" and JsonPaths.has(id, project) then
return project .. ":"
end
return ""
end
local MAX_CHECKS = 100
local checkCount = 0
local function fileExists(name)
if checkCount >= MAX_CHECKS then
return false
end
checkCount = checkCount + 1
local title = mw.title.new(name, "File")
return title and title.exists
end
local function generateRepeatTemplate(data, project)
local spriteGroups = {}
for id, entry in pairs(data) do
local found = false
for _, group in pairs(spriteGroups) do
local g = group[1]
if entry.sprite == g.entry.sprite and entry.state == g.entry.state then
table.insert(group, { id = id, entry = entry })
found = true
break
end
end
if not found then
table.insert(spriteGroups, {
{ id = id, entry = entry }
})
end
end
local result = {}
for _, group in pairs(spriteGroups) do
if #group > 1 then
local idLinks = {}
for _, obj in pairs(group) do
local id = obj.id
local prefix = getPrefix(id, project)
table.insert(idLinks, "[[:Файл:" .. prefix .. id .. ".png]]")
end
table.insert(result, mw.getCurrentFrame():preprocess(
"{{Entity Sprite/Repeat|" ..
table.concat(idLinks, " ") ..
"|" .. group[1].id ..
"}}"
))
end
end
return table.concat(result, "\n")
end
local function generateTemplate(id, entry, baseUrl, project)
local spritePath = getSpritePath(entry)
if not id or not spritePath then
return nil
end
local prefix = getPrefix(id, project)
local states = getSpriteStates(entry)
local stateStr = ""
if states then
local links = {}
for _, item in ipairs(states) do
local url = baseUrl .. item.sprite .. "/" .. item.state .. ".png"
table.insert(links, "[" .. url .. " " .. item.state .. "]")
end
stateStr = table.concat(links, ", ")
end
return mw.getCurrentFrame():preprocess(
"{{Песочница/Pok|файл=" .. prefix .. id ..
"|id=" .. id ..
"|путь=" .. baseUrl .. spritePath ..
"|state=" .. stateStr ..
"}}"
)
end
function p.main(frame)
local action = frame.args[1]
local json = frame.args.json or "prototype/sprite.json"
local checkFile = frame.args.checkFile or ""
if checkFile == "" then checkFile = false end
local project = JsonPaths.project()
local baseUrl = JsonPaths.git() .. "/Resources/Textures/"
local dataPage = JsonPaths.get(json)
local spriteData = mw.loadData(dataPage)
if not spriteData or type(spriteData) ~= "table" then
return "Ошибка загрузки JSON: " .. dataPage
end
if action == "repeat" then
return generateRepeatTemplate(spriteData, project)
elseif action == "image" then
local result = {}
for id, entry in pairs(spriteData) do
local prefix = getPrefix(id, project)
local skip = false
if checkFile then
local fileName = prefix .. id .. ".png"
if fileExists(fileName) then
skip = true
end
end
if not skip then
local t = generateTemplate(id, entry, baseUrl, project)
if t then
table.insert(result, t)
end
end
end
return table.concat(result, "\n")
end
return nil
end
return p