Модуль:Песочница/Pok: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 15: | Строка 15: | ||
local function getSpritePath(entry) | local function getSpritePath(entry) | ||
return entry.Sprite and entry.Sprite.sprite or (entry.Icon and entry.Icon.sprite) | return entry.Sprite and entry.Sprite.sprite or (entry.Icon and entry.Icon.sprite) | ||
end | |||
local indexById = nil | |||
local function buildIndex(data) | |||
local index = {} | |||
for _, entry in ipairs(data) do | |||
if entry.id then | |||
index[entry.id] = entry | |||
end | |||
end | |||
return index | |||
end | end | ||
| Строка 21: | Строка 33: | ||
local secondaryParam = frame.args[2] | local secondaryParam = frame.args[2] | ||
if not cachedData then | |||
if not | cachedData = loadData('User:IanComradeBot/prototypes/entity sprite.json') | ||
if cachedData then | |||
indexById = buildIndex(cachedData) | |||
end | |||
end | |||
if not cachedData then | |||
return 'Ошибка: Невозможно загрузить данные из JSON.' | return 'Ошибка: Невозможно загрузить данные из JSON.' | ||
end | end | ||
if param == "image" and secondaryParam then | if param == "image" and secondaryParam then | ||
local entry = indexById and indexById[secondaryParam] | |||
return entry and getSpritePath(entry) or "Ошибка: ID не найден." | |||
end | end | ||
end | end | ||
return p | return p | ||
Версия от 12:28, 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)
return entry.Sprite and entry.Sprite.sprite or (entry.Icon and entry.Icon.sprite)
end
local indexById = nil
local function buildIndex(data)
local index = {}
for _, entry in ipairs(data) do
if entry.id then
index[entry.id] = entry
end
end
return index
end
function p.main(frame)
local param = frame.args[1]
local secondaryParam = frame.args[2]
if not cachedData then
cachedData = loadData('User:IanComradeBot/prototypes/entity sprite.json')
if cachedData then
indexById = buildIndex(cachedData)
end
end
if not cachedData then
return 'Ошибка: Невозможно загрузить данные из JSON.'
end
if param == "image" and secondaryParam then
local entry = indexById and indexById[secondaryParam]
return entry and getSpritePath(entry) or "Ошибка: ID не найден."
end
end
return p