Модуль:Песочница/Pok: различия между версиями

Материал из Space Station 14 Вики
мНет описания правки
Нет описания правки
Строка 3: Строка 3:
-- Функция для загрузки данных исследований из JSON-файла
-- Функция для загрузки данных исследований из JSON-файла
local function loadResearchData()
local function loadResearchData()
return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/Песочница.json"):getContent())
local content = mw.title.new("User:IanComradeBot/Песочница.json"):getContent()
if not content then
return nil
end
return mw.text.jsonDecode(content)
end
end


Строка 15: Строка 19:
end
end
return results
return results
end
-- Функция для поиска исследования по ID
local function findResearchById(dataCache, id)
for _, research in ipairs(dataCache) do
if research.technology and research.technology.id == id then
return research.technology
end
end
return nil
end
end


Строка 48: Строка 42:
})
})


-- Загрузка данных
local dataCache = loadResearchData()
local dataCache = loadResearchData()
if not dataCache then
return cssLink .. '<div style="color:red;">Ошибка загрузки данных исследований. Проверьте файл JSON.</div>'
end
-- Получение дисциплины
local discipline = frame.args[1] or ""
local discipline = frame.args[1] or ""
if discipline == "" then
return cssLink .. '<div style="color:red;">Не указана дисциплина. Доступные дисциплины: '
.. table.concat(vim.tbl_keys(disciplineMapping), ", ") .. '</div>'
end


if discipline and discipline ~= "" then
-- Инициализация строки вывода
-- Инициализация строки вывода
local out = cssLink .. '<div class="research-group">'
local out = cssLink .. '<div class="research-group">'


-- Получаем список исследований по дисциплине
-- Получаем список исследований по дисциплине
local researches = findResearchByDiscipline(dataCache, discipline)
local researches = findResearchByDiscipline(dataCache, discipline)
if not researches or #researches == 0 then
return out .. '<div style="color:red;">Нет исследований для дисциплины "' .. discipline .. '"</div></div>'
end


if #researches == 0 then
-- Формирование блоков исследований
out = out .. '<div style="color:red;">Нет исследований для дисциплины "' .. discipline .. '"</div>'
for _, tech in ipairs(researches) do
local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
local tierColor = tierColors[tech.tier] or "#FFFFFF"
local iconPath = tech.icon and tech.icon.sprite or nil
 
-- Проверка данных
if not tech.id or not tech.name then
return cssLink .. '<div style="color:red;">Ошибка: отсутствует ID или имя технологии.</div>'
end
end


for _, tech in ipairs(researches) do
-- Основной блок исследования
local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
out = out .. '<div class="research" id="' .. tech.id .. '">'
local tierColor = tierColors[tech.tier] or "#FFFFFF"
out = out .. frame:preprocess('<div class="research__images">[[Файл:{{#invoke:Entity Sprite|main|path|' .. (iconPath or "default_icon.png") .. '}}]]</div>')
local iconPath = tech.icon and tech.icon.sprite or nil
out = out .. frame:preprocess('<div class="research__name">{{#invoke:Ftl|main|translation|' .. tech.name .. '}}[[Файл:' .. tech.discipline .. '.png|16px|link=]]</div>')
out = out .. '<div class="research__type">'
out = out .. '<div>Уровень: <span style="color:' .. tierColor .. ';">' .. (tech.tier or "N/A") .. '</span></div>'
out = out .. '<div class="research__technology">' .. disciplineName .. '</div>'
out = out .. '<div>Стоимость: <span style="color:#DA70D6;">' .. (tech.cost or "N/A") .. '</span></div>'
out = out .. '</div>'


-- Основной блок исследования
-- Блок необходимых исследований
out = out .. '<div class="research" id="' .. tech.id .. '">'
if tech.technologyPrerequisites and #tech.technologyPrerequisites > 0 then
out = out .. frame:preprocess('<div class="research__images">[[Файл:{{#invoke:Entity Sprite|main|path|' .. iconPath .. '}}]]</div>')
out = out .. '<div class="research__technologies-prerequisites">Необходимые исследования:'
out = out .. frame:preprocess('<div class="research__name">{{#invoke:Ftl|main|translation|' .. tech.name .. '}}[[Файл:' .. tech.discipline .. '.png|16px|link=]]</div>')
out = out .. '<ul>'
out = out .. '<div class="research__type">'
out = out .. '<div>Уровень: <span style="color:' .. tierColor .. ';">' .. tech.tier .. '</span></div>'
out = out .. '<div class="research__technology">' .. disciplineName .. '</div>'
out = out .. '<div>Стоимость: <span style="color:#DA70D6;">' .. tech.cost .. '</span></div>'
out = out .. '</div>'


-- Блок необходимых исследований
for _, prerequisite in ipairs(tech.technologyPrerequisites) do
if tech.technologyPrerequisites and #tech.technologyPrerequisites > 0 then
out = out .. frame:preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. prerequisite .. '.png|' .. prerequisite .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. prerequisite .. '}}</li>')
out = out .. '<div class="research__technologies-prerequisites">Необходимые исследования:'
out = out .. '<ul>'
 
for _, prerequisite in ipairs(tech.technologyPrerequisites) do
out = out .. mw.getCurrentFrame():preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. prerequisite .. '.png|' .. prerequisite .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. prerequisite .. '}}</li>')
end
 
out = out .. '</ul>'
out = out .. '</div>'
end
end


-- Блок открываемых рецептов
out = out .. '</ul>'
if tech.recipeUnlocks and #tech.recipeUnlocks > 0 then
out = out .. '</div>'
out = out .. '<div class="research__technologies-unlocks">Разблокирует:'
end
out = out .. '<ul>'


for _, recipe in ipairs(tech.recipeUnlocks) do
-- Блок открываемых рецептов
out = out .. mw.getCurrentFrame():preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. recipe .. '.png|' .. recipe .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. recipe .. '}}</li>')
if tech.recipeUnlocks and #tech.recipeUnlocks > 0 then
end
out = out .. '<div class="research__technologies-unlocks">Разблокирует:'
out = out .. '<ul>'


out = out .. '</ul>'
for _, recipe in ipairs(tech.recipeUnlocks) do
out = out .. '</div>'
out = out .. frame:preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. recipe .. '.png|' .. recipe .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. recipe .. '}}</li>')
end
end
 
out = out .. '</ul>'
out = out .. '</div>'
out = out .. '</div>'
end
end


out = out .. '</div>'
out = out .. '</div>'
return out
else
return cssLink .. '<div style="color:red;">Дисциплина "' .. discipline .. '" не найдена.</div>'
end
end
out = out .. '</div>'
return out
end
end


return p
return p

Версия от 18:27, 25 января 2025

Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc

local p = {}

-- Функция для загрузки данных исследований из JSON-файла
local function loadResearchData()
	local content = mw.title.new("User:IanComradeBot/Песочница.json"):getContent()
	if not content then
		return nil
	end
	return mw.text.jsonDecode(content)
end

-- Функция для поиска исследований по дисциплине
local function findResearchByDiscipline(dataCache, discipline)
	local results = {}
	for _, research in ipairs(dataCache) do
		if research.technology and research.technology.discipline == discipline then
			table.insert(results, research.technology)
		end
	end
	return results
end

-- Таблица для перевода названий дисциплин
local disciplineMapping = {
	Arsenal = "Арсенал",
	Industrial = "Промышленность",
	Experimental = "Экспериментальное",
	CivilianServices = "Обслуживание персонала"
}

-- Таблица для цветов по уровням
local tierColors = {
	[1] = "#54d554",
	[2] = "#ed9000",
	[3] = "#d72a2a"
}

function p.main(frame)
	-- Подключение CSS
	local cssLink = frame:extensionTag('templatestyles', '', {
		src = 'Шаблон:Research/styles.css'
	})

	-- Загрузка данных
	local dataCache = loadResearchData()
	if not dataCache then
		return cssLink .. '<div style="color:red;">Ошибка загрузки данных исследований. Проверьте файл JSON.</div>'
	end

	-- Получение дисциплины
	local discipline = frame.args[1] or ""
	if discipline == "" then
		return cssLink .. '<div style="color:red;">Не указана дисциплина. Доступные дисциплины: ' 
			.. table.concat(vim.tbl_keys(disciplineMapping), ", ") .. '</div>'
	end

	-- Инициализация строки вывода
	local out = cssLink .. '<div class="research-group">'

	-- Получаем список исследований по дисциплине
	local researches = findResearchByDiscipline(dataCache, discipline)
	if not researches or #researches == 0 then
		return out .. '<div style="color:red;">Нет исследований для дисциплины "' .. discipline .. '"</div></div>'
	end

	-- Формирование блоков исследований
	for _, tech in ipairs(researches) do
		local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
		local tierColor = tierColors[tech.tier] or "#FFFFFF"
		local iconPath = tech.icon and tech.icon.sprite or nil

		-- Проверка данных
		if not tech.id or not tech.name then
			return cssLink .. '<div style="color:red;">Ошибка: отсутствует ID или имя технологии.</div>'
		end

		-- Основной блок исследования
		out = out .. '<div class="research" id="' .. tech.id .. '">'
		out = out .. frame:preprocess('<div class="research__images">[[Файл:{{#invoke:Entity Sprite|main|path|' .. (iconPath or "default_icon.png") .. '}}]]</div>')
		out = out .. frame:preprocess('<div class="research__name">{{#invoke:Ftl|main|translation|' .. tech.name .. '}}[[Файл:' .. tech.discipline .. '.png|16px|link=]]</div>')
		out = out .. '<div class="research__type">'
		out = out .. '<div>Уровень: <span style="color:' .. tierColor .. ';">' .. (tech.tier or "N/A") .. '</span></div>'
		out = out .. '<div class="research__technology">' .. disciplineName .. '</div>'
		out = out .. '<div>Стоимость: <span style="color:#DA70D6;">' .. (tech.cost or "N/A") .. '</span></div>'
		out = out .. '</div>'

		-- Блок необходимых исследований
		if tech.technologyPrerequisites and #tech.technologyPrerequisites > 0 then
			out = out .. '<div class="research__technologies-prerequisites">Необходимые исследования:'
			out = out .. '<ul>'

			for _, prerequisite in ipairs(tech.technologyPrerequisites) do
				out = out .. frame:preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. prerequisite .. '.png|' .. prerequisite .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. prerequisite .. '}}</li>')
			end

			out = out .. '</ul>'
			out = out .. '</div>'
		end

		-- Блок открываемых рецептов
		if tech.recipeUnlocks and #tech.recipeUnlocks > 0 then
			out = out .. '<div class="research__technologies-unlocks">Разблокирует:'
			out = out .. '<ul>'

			for _, recipe in ipairs(tech.recipeUnlocks) do
				out = out .. frame:preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. recipe .. '.png|' .. recipe .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. recipe .. '}}</li>')
			end
			out = out .. '</ul>'
			out = out .. '</div>'
		end

		out = out .. '</div>'
	end

	out = out .. '</div>'
	return out
end

return p