Модуль:Ftl
Материал из Space Station 14 Вики
Версия от 22:21, 22 февраля 2025; Pok (обсуждение | вклад)
Для документации этого модуля может быть создана страница Модуль:Ftl/doc
-- Загрузка данных
local data = mw.loadData("Модуль:IanComradeBot/ftl/ru-RU.json/data")
local p = {}
-- Функция для получения таблицы данных
function p.getData()
return data
end
-- Поиск текста по ключу (без учета регистра)
local function findTextByKey(data, key)
local searchKey = string.lower(key)
for categoryName, category in pairs(data) do
for k, value in pairs(category) do
if string.lower(k) == searchKey then
return value
end
end
end
return nil
end
-- Поиск ключа по тексту (без учета регистра для текста)
local function findKeyByText(data, text)
local searchText = string.lower(text)
for categoryName, category in pairs(data) do
for key, value in pairs(category) do
if string.lower(value) == searchText then
return key
end
end
end
return nil
end
-- Получение всех строк из категории (без учета регистра)
local function getCategoryStrings(data, categoryName)
local searchCategory = string.lower(categoryName)
local matchedCategory = nil
for catName, catData in pairs(data) do
if string.lower(catName) == searchCategory then
matchedCategory = catData
break
end
end
if not matchedCategory then
return nil
end
local result = {}
for _, value in pairs(matchedCategory) do
if type(value) == "string" then
table.insert(result, value)
end
end
return result
end
-- Основная функция модуля
function p.main(frame)
local mode = frame.args[1]
local param = frame.args[2]
if not mode or not param then
return "Ошибка: Не указаны все необходимые параметры."
end
if mode == "translation" then
local result = findTextByKey(data, param)
return result or "Ошибка: Ключ не найден."
elseif mode == "key" then
local result = findKeyByText(data, param)
return result or "Ошибка: Текст не найден."
elseif mode == "categories" then
local strings = getCategoryStrings(data, param)
if not strings or #strings == 0 then
return "Ошибка: Категория не найдена или пуста."
end
local output = {}
for _, value in ipairs(strings) do
table.insert(output, "<li>" .. mw.text.encode(value) .. "</li>")
end
return table.concat(output)
else
return "Ошибка: Неизвестный режим работы."
end
end
return p