Модуль:Prototypes/Объект/Торгомат: различия между версиями

мНет описания правки
мНет описания правки
 
(не показано 36 промежуточных версий этого же участника)
Строка 1: Строка 1:
-- Загрузка данных
local vendingMachinesData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines.json/data")
local restockData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines/restock.json/data")
local inventoriesData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines/inventories.json/data")
local p = {}
local p = {}
-- Загрузка данных
local function loadData(filePath)
local page = mw.title.new(filePath)
local content = page:getContent()
return content and mw.text.jsonDecode(content) or nil
end


-- Поиск данных по ID
-- Поиск данных по ID
Строка 17: Строка 15:
end
end
return nil
return nil
end
-- Формирование вывода инвентаря
local function formatInventoryOutput(inventoryData)
local result = ""
for itemId, count in pairs(inventoryData) do
result = result .. mw.getCurrentFrame():preprocess("{{#invoke:Предмет|main|" .. itemId .. "|[" .. count .. "]|repository=|wrapper=}}")
end
return result ~= "" and result or "Инвентарь пуст."
end
end


-- Получение инвентаря автомата
-- Получение инвентаря автомата
local function getInventoryOutput(id, mode)
local function getInventoryOutput(id, mode)
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local vendingMachine = findDataById(vendingMachinesData, id)
local inventories = loadData("User:IanComradeBot/prototypes/vending machines/inventories.json")
if not vendingMachine then return "Автомат с таким ID не найден." end
 
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end


-- Получение инвентаря автомата
local inventoryId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
local inventoryId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
if not inventoryId then return "" end
if not inventoryId then return "Инвентарь не найден." end


local inventory = findDataById(inventories, inventoryId)
local inventory = findDataById(inventoriesData, inventoryId)
if not inventory then return "" end
if not inventory then return "Данные об инвентаре отсутствуют." end


-- Формирование вывода
local result = ""
-- Основной инвентарь
if mode == "inventory" and inventory.startingInventory then
if mode == "inventory" and inventory.startingInventory then
for itemId, count in pairs(inventory.startingInventory) do
return formatInventoryOutput(inventory.startingInventory)
result = result .. mw.getCurrentFrame():preprocess("{{LinkСard|SideStyle=1|image=" .. itemId .. "|name={{#invoke:Entity Lookup|getname|" .. itemId .. "}} " .. count .. "}}")
elseif mode == "contraband" and inventory.contrabandInventory then
end
return formatInventoryOutput(inventory.contrabandInventory)
end
elseif mode == "emag" and inventory.emaggedInventory then
 
return formatInventoryOutput(inventory.emaggedInventory)
-- Содержания после провода
if mode == "contraband" and inventory.contrabandInventory then
for itemId, count in pairs(inventory.contrabandInventory) do
result = result .. mw.getCurrentFrame():preprocess("{{LinkСard|SideStyle=1|image=" .. itemId .. "|name={{#invoke:Entity Lookup|getname|" .. itemId .. "}} " .. count .. "}}")
end
end
end


-- Содержания после EMAG
return "Не имеет дополнительного ассортимента."
if mode == "emag" and inventory.emaggedInventory then
for itemId, count in pairs(inventory.emaggedInventory) do
result = result .. mw.getCurrentFrame():preprocess("{{LinkСard|SideStyle=1|image=" .. itemId .. "|name={{#invoke:Entity Lookup|getname|" .. itemId .. "}} " .. count .. "}}")
end
end
 
return result
end
end


-- Получение информации о пополнителе автомата
-- Получение информации о пополнителе автомата
local function getRestockOutput(id)
local function getRestockOutput(id)
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local vendingMachine = findDataById(vendingMachinesData, id)
local restockData = loadData("User:IanComradeBot/prototypes/vending machines/restock.json")
 
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end
if not vendingMachine then return "" end


-- Получение ID пополнителя
local packId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
local packId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
if not packId then return "" end


for _, restock in ipairs(restockData) do
for _, restock in ipairs(restockData) do
if restock.canRestock and table.concat(restock.canRestock):find(packId) then
    local restockInfo = restock.VendingMachineRestock
return mw.getCurrentFrame():preprocess("{{LinkСard|image=" .. restock.id .. "|name={{#invoke:Entity Lookup|getname|" .. restock.id .. "}}}}")
    if restockInfo and restockInfo.canRestock then
end
        for _, restockPack in ipairs(restockInfo.canRestock) do
end
            if restockPack == packId then
 
                return mw.getCurrentFrame():preprocess("{{Предмет|" .. restock.id .. "|size=64px|link=|repository=|wrapper=}}")
return ""
            end
end
        end
 
    end
-- Получение данных о доступах
local function getAccessOutput(id)
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
 
-- Получение доступа из данных автомата
local access = vendingMachine.AccessReader and vendingMachine.AccessReader.access
 
if not access then
return "Доступ не требуется"
end
end


-- Формируем строку для вызова шаблона
return "Не имеет пополнителя."
local templateCall = "{{#invoke:Prototypes/Механика/Доступ|parse|" .. access .. "}}"
-- Обрабатываем шаблон и возвращаем результат
return mw.getCurrentFrame():preprocess(templateCall)
end
end


-- Основная функция модуля
-- Основная функция модуля
function p.main(frame)
function p.main(frame)
local id = frame.args.id
local mode = frame.args[1]
local mode = frame.args.mode
local id = frame.args[2]


if not id then return "" end
if not id then return "" end


-- Определение действия на основе ID и выбранного режима
if mode == "inventory" or mode == "contraband" or mode == "emag" then
if mode == "inventory" or mode == "contraband" or mode == "emag" then
return getInventoryOutput(id, mode)
return getInventoryOutput(id, mode)
elseif mode == "restock" then
elseif mode == "restock" then
return getRestockOutput(id)
return getRestockOutput(id)
elseif mode == "access" then
return getAccessOutput(id)
else
else
return ""
return ""