Модуль:Prototypes/Объект/Торгомат: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 20: | Строка 20: | ||
-- Получение инвентаря автомата | -- Получение инвентаря автомата | ||
local function getInventoryOutput(id) | local function getInventoryOutput(id, mode) | ||
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json") | local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json") | ||
local inventories = loadData("User:IanComradeBot/prototypes/vending machines/inventories.json") | local inventories = loadData("User:IanComradeBot/prototypes/vending machines/inventories.json") | ||
| Строка 35: | Строка 35: | ||
if not inventory then return "" end | if not inventory then return "" end | ||
-- Формирование вывода | -- Формирование вывода | ||
local result = "== Содержимое автомата ==\n" | local result = "== Содержимое автомата ==\n" | ||
-- | -- Основной инвентарь | ||
if inventory.startingInventory then | if mode == "inventory" and inventory.startingInventory then | ||
result = result .. "== Инвентарь ==\n" | result = result .. "== Инвентарь ==\n" | ||
for itemId, count in pairs(inventory.startingInventory) do | for itemId, count in pairs(inventory.startingInventory) do | ||
| Строка 46: | Строка 46: | ||
end | end | ||
-- Содержания | -- Содержания после провода | ||
if inventory.contrabandInventory then | if mode == "contraband" and inventory.contrabandInventory then | ||
result = result .. "== Провод ==\n" | result = result .. "== Провод ==\n" | ||
for itemId, count in pairs(inventory.contrabandInventory) do | for itemId, count in pairs(inventory.contrabandInventory) do | ||
| Строка 54: | Строка 54: | ||
end | end | ||
-- Содержания | -- Содержания после EMAG | ||
if inventory.emaggedInventory then | if mode == "emag" and inventory.emaggedInventory then | ||
result = result .. "== Эмаг ==\n" | result = result .. "== Эмаг ==\n" | ||
for itemId, count in pairs(inventory.emaggedInventory) do | for itemId, count in pairs(inventory.emaggedInventory) do | ||
| Строка 111: | Строка 111: | ||
function p.main(frame) | function p.main(frame) | ||
local id = frame.args.id | local id = frame.args.id | ||
local mode = frame.args.mode | |||
if not id then return "" end | if not id then return "" end | ||
-- Определение действия на основе ID | -- Определение действия на основе ID и выбранного режима | ||
if | if mode == "inventory" or mode == "contraband" or mode == "emag" then | ||
return getInventoryOutput(id) | return getInventoryOutput(id, mode) | ||
elseif | elseif mode == "restock" then | ||
return getRestockOutput(id) | return getRestockOutput(id) | ||
elseif | elseif mode == "access" then | ||
return getAccessOutput(id) | return getAccessOutput(id) | ||
else | else | ||
Версия от 19:48, 16 января 2025
Для документации этого модуля может быть создана страница Модуль:Prototypes/Объект/Торгомат/doc
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
local function findDataById(data, id)
if not data then return nil end
for _, item in ipairs(data) do
if item.id == id then
return item
end
end
return nil
end
-- Получение инвентаря автомата
local function getInventoryOutput(id, mode)
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local inventories = loadData("User:IanComradeBot/prototypes/vending machines/inventories.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end
-- Получение инвентаря автомата
local inventoryId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
if not inventoryId then return "" end
local inventory = findDataById(inventories, inventoryId)
if not inventory then return "" end
-- Формирование вывода
local result = "== Содержимое автомата ==\n"
-- Основной инвентарь
if mode == "inventory" and inventory.startingInventory then
result = result .. "== Инвентарь ==\n"
for itemId, count in pairs(inventory.startingInventory) do
result = result .. "* " .. itemId .. " — " .. count .. "\n"
end
end
-- Содержания после провода
if mode == "contraband" and inventory.contrabandInventory then
result = result .. "== Провод ==\n"
for itemId, count in pairs(inventory.contrabandInventory) do
result = result .. "* " .. itemId .. " — " .. count .. "\n"
end
end
-- Содержания после EMAG
if mode == "emag" and inventory.emaggedInventory then
result = result .. "== Эмаг ==\n"
for itemId, count in pairs(inventory.emaggedInventory) do
result = result .. "* " .. itemId .. " — " .. count .. "\n"
end
end
return result
end
-- Получение информации о пополнителе автомата
local function getRestockOutput(id)
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local restockData = loadData("User:IanComradeBot/prototypes/vending machines/restock.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end
-- Получение ID пополнителя
local packId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
if not packId then return "" end
for _, restock in ipairs(restockData) do
if restock.canRestock and table.concat(restock.canRestock):find(packId) then
return "== Пополнитель ==\n* ID пополнителя: " .. restock.id
end
end
return ""
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
-- Формируем строку для вызова шаблона
local templateCall = "{{#invoke:Prototypes/Механика/Доступ|parse|" .. access .. "}}"
-- Обрабатываем шаблон и возвращаем результат
return mw.getCurrentFrame():preprocess(templateCall)
end
-- Основная функция модуля
function p.main(frame)
local id = frame.args.id
local mode = frame.args.mode
if not id then return "" end
-- Определение действия на основе ID и выбранного режима
if mode == "inventory" or mode == "contraband" or mode == "emag" then
return getInventoryOutput(id, mode)
elseif mode == "restock" then
return getRestockOutput(id)
elseif mode == "access" then
return getAccessOutput(id)
else
return ""
end
end
return p