Модуль:Песочница/Pok: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local cachedData = nil | |||
local function | |||
return | local function loadData(filePath) | ||
if cachedData then | |||
return cachedData | |||
end | |||
local page = mw.title.new(filePath) | |||
local content = page and page:getContent() | |||
cachedData = content and mw.text.jsonDecode(content) or nil | |||
return cachedData | |||
end | end | ||
local function getSpritePath(entry) | |||
local | if entry.Sprite and entry.Sprite.sprite then | ||
return entry.Sprite.sprite | |||
elseif entry.Icon and entry.Icon.sprite then | |||
return entry.Icon.sprite | |||
elseif entry.Sprite and entry.Sprite.layers then | |||
for _, layer in ipairs(entry.Sprite.layers) do | |||
if layer.sprite then | |||
return layer.sprite | |||
end | |||
end | |||
end | |||
return nil | |||
end | |||
function p.main(frame) | function p.main(frame) | ||
local | local param = frame.args[1] | ||
local secondaryParam = frame.args[2] | |||
local | |||
if | local data = loadData('User:IanComradeBot/prototypes/entity sprite.json') | ||
if not data or type(data) ~= 'table' then | |||
return 'Ошибка: Невозможно загрузить данные из JSON.' | |||
end | |||
if param == "image" and secondaryParam then | |||
for _, entry in ipairs(data) do | |||
for _, | if entry.id == secondaryParam then | ||
if | return getSpritePath(entry) or "Ошибка: Спрайт не найден." | ||
end | end | ||
end | end | ||
return "Ошибка: ID не найден." | |||
end | end | ||
end | end | ||
return p | return p | ||
Версия от 12:23, 3 февраля 2025
Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc
local p = {}
local cachedData = nil
local function loadData(filePath)
if cachedData then
return cachedData
end
local page = mw.title.new(filePath)
local content = page and page:getContent()
cachedData = content and mw.text.jsonDecode(content) or nil
return cachedData
end
local function getSpritePath(entry)
if entry.Sprite and entry.Sprite.sprite then
return entry.Sprite.sprite
elseif entry.Icon and entry.Icon.sprite then
return entry.Icon.sprite
elseif entry.Sprite and entry.Sprite.layers then
for _, layer in ipairs(entry.Sprite.layers) do
if layer.sprite then
return layer.sprite
end
end
end
return nil
end
function p.main(frame)
local param = frame.args[1]
local secondaryParam = frame.args[2]
local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
if not data or type(data) ~= 'table' then
return 'Ошибка: Невозможно загрузить данные из JSON.'
end
if param == "image" and secondaryParam then
for _, entry in ipairs(data) do
if entry.id == secondaryParam then
return getSpritePath(entry) or "Ошибка: Спрайт не найден."
end
end
return "Ошибка: ID не найден."
end
end
return p