Модуль:Песочница/Pok: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 33: | Строка 33: | ||
return "" | return "" | ||
end | end | ||
local MAX_CHECKS = 100 | |||
local checkCount = 0 | |||
local function fileExists(name) | local function fileExists(name) | ||
if checkCount >= MAX_CHECKS then | |||
return false | |||
end | |||
checkCount = checkCount + 1 | |||
local title = mw.title.new(name, "File") | local title = mw.title.new(name, "File") | ||
return title and title.exists | return title and title.exists | ||
Версия от 22:32, 21 марта 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.state and entry.sprite then
table.insert(result, {
state = entry.state,
sprite = entry.sprite
})
end
return (#result > 0) and result or nil
end
local function getTextureBaseUrl(project)
if project == "Goob" then
return "https://github.com/space-syndicate/Goob-Station/blob/master/Resources/Textures/"
end
return "https://github.com/space-syndicate/space-station-14/blob/master/Resources/Textures/"
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(
"{{Entity Sprite/Image|" ..
prefix .. id ..
"|" .. baseUrl .. spritePath ..
"|" .. stateStr ..
"}}"
)
end
function p.main(frame)
local action = frame.args[1]
local checkFile = frame.args.checkFile or ""
if checkFile == "" then checkFile = false end
local project = JsonPaths.project()
local baseUrl = getTextureBaseUrl(project)
local dataPage = JsonPaths.get("prototype/sprite.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