Модуль:Prototypes/Хранилище/Предмет: различия между версиями

Отмена правки 62954, сделанной Pok (обсуждение)
Метка: отмена
мНет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}


-- Функция для загрузки данных из страницы
local cache = {
local function loadData(filePath)
    items = nil,
    selectors = nil
}
 
-- Функция для загрузки данных с кешированием
local function loadData(filePath, cacheKey)
    if cache[cacheKey] then
        return cache[cacheKey]
    end
 
     local page = mw.title.new(filePath)
     local page = mw.title.new(filePath)
     local content = page:getContent()
     local content = page:getContent()
     return content and mw.text.jsonDecode(content) or nil
     local data = content and mw.text.jsonDecode(content) or nil
 
    if data then
        cache[cacheKey] = data
    end
    return data
end
end


Строка 29: Строка 43:
         local percentage = content.prob * 100
         local percentage = content.prob * 100
         if percentage >= 1 then
         if percentage >= 1 then
            -- Для значений >= 1%, выводим целое число
             prob = string.format(" <div>%d%%</div>", math.floor(percentage))
             prob = string.format(" <div>%d%%</div>", math.floor(percentage))
         else
         else
            -- Для значений < 1%, убираем лишние нули после запятой
             prob = string.format(" <div>%g%%</div>", percentage)
             prob = string.format(" <div>%g%%</div>", percentage)
         end
         end
Строка 51: Строка 63:
-- Обработка таблиц
-- Обработка таблиц
local function getTableOutput(tableId)
local function getTableOutput(tableId)
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json')
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json', 'selectors')
     local tableData = findDataById(allSelectors, tableId)
     local tableData = findDataById(allSelectors, tableId)
     local children = tableData and tableData['!type:AllSelector'] and tableData['!type:AllSelector'].children
     local children = tableData and tableData['!type:AllSelector'] and tableData['!type:AllSelector'].children
Строка 69: Строка 81:
     if not item then return '' end
     if not item then return '' end


local result = ''
    local result = ''
     if item.StorageFill and item.StorageFill.contents then
     if item.StorageFill and item.StorageFill.contents then
         result = result .. getContentsOutput(item.StorageFill.contents)
         result = result .. getContentsOutput(item.StorageFill.contents)
Строка 109: Строка 121:
     if not id then return 'Не указан ID.' end
     if not id then return 'Не указан ID.' end


     local data = loadData('User:IanComradeBot/prototypes/fills/Item.json')
     local data = loadData('User:IanComradeBot/prototypes/fills/Item.json', 'items')
     if not data then return 'Не удалось загрузить данные.' end
     if not data then return 'Не удалось загрузить данные.' end


     -- При режиме framing
     -- При режиме framing
if mode == 'framing' then
    if mode == 'framing' then
    local subMode = frame.args[2]
        local subMode = frame.args[2]
    local id = frame.args[3]
        local id = frame.args[3]
   
    if not id then
        if not id then
        return 'Не указан ID для режима framing.'
            return 'Не указан ID для режима framing.'
    end
        end
   
    if subMode == 'chem' then
        if subMode == 'chem' then
        return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(data, id) .. '}}')
            return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(data, id) .. '}}')
    elseif subMode == 'contained' then
        elseif subMode == 'contained' then
        return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput(data, id, false) .. '}}')
            return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput(data, id, false) .. '}}')
    else
        else
        return 'Неизвестный подрежим для framing: ' .. subMode
            return 'Неизвестный подрежим для framing: ' .. subMode
    end
        end
end
    end


     -- При нормальном режиме
     -- При нормальном режиме