Модуль:Prototypes/Объект/Торгомат: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Новая страница: «local p = {} -- Функция для загрузки данных из JSON-файла 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...» |
Pok (обсуждение | вклад) мНет описания правки |
||
Строка 19: | Строка 19: | ||
end | end | ||
-- | -- Функция для получения содержимого автомата | ||
function | local function getInventoryOutput(id) | ||
-- Загрузка данных | -- Загрузка данных | ||
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") | ||
-- Поиск автомата по ID | -- Поиск автомата по ID | ||
local vendingMachine = findDataById(vendingMachines, | local vendingMachine = findDataById(vendingMachines, id) | ||
if not vendingMachine then | if not vendingMachine then | ||
return "Ошибка: автомат с ID \"" .. | return "Ошибка: автомат с ID \"" .. id .. "\" не найден." | ||
end | end | ||
Строка 42: | Строка 35: | ||
local inventory = findDataById(inventories, inventoryId) | local inventory = findDataById(inventories, inventoryId) | ||
if not inventory or not inventory.startingInventory then | if not inventory or not inventory.startingInventory then | ||
return "Ошибка: инвентарь для автомата \"" .. | return "Ошибка: инвентарь для автомата \"" .. id .. "\" не найден." | ||
end | end | ||
Строка 51: | Строка 44: | ||
end | end | ||
-- Поиск пополнителя | return result | ||
local | 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 "Ошибка: автомат с ID \"" .. id .. "\" не найден." | |||
end | |||
-- Получение ID пополнителя | |||
local packId = vendingMachine.pack | |||
for _, restock in ipairs(restockData) do | for _, restock in ipairs(restockData) do | ||
if restock.canRestock | if restock.canRestock and table.concat(restock.canRestock):find(packId) then | ||
return "== Пополнитель ==\n* ID пополнителя: " .. restock.id | |||
end | end | ||
end | end | ||
return "Ошибка: для автомата с ID \"" .. id .. "\" пополнитель не найден." | |||
end | |||
-- Основная функция модуля | |||
function p.main(frame) | |||
local id = frame.args.id | |||
if not id then | |||
return "Ошибка: необходимо указать параметр `id`." | |||
end | |||
-- Определение действия на основе ID | |||
if frame.args.mode == "inventory" then | |||
return getInventoryOutput(id) | |||
elseif frame.args.mode == "restock" then | |||
return getRestockOutput(id) | |||
else | else | ||
return "Ошибка: необходимо указать параметр `mode` (inventory или restock)." | |||
end | end | ||
end | end | ||
return p | return p |
Версия от 17:56, 16 января 2025
Для документации этого модуля может быть создана страница Модуль:Prototypes/Объект/Торгомат/doc
local p = {}
-- Функция для загрузки данных из JSON-файла
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)
-- Загрузка данных
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 "Ошибка: автомат с ID \"" .. id .. "\" не найден."
end
-- Получение инвентаря автомата
local inventoryId = vendingMachine.pack
local inventory = findDataById(inventories, inventoryId)
if not inventory or not inventory.startingInventory then
return "Ошибка: инвентарь для автомата \"" .. id .. "\" не найден."
end
-- Формирование вывода содержимого автомата
local result = "== Содержимое автомата ==\n"
for itemId, count in pairs(inventory.startingInventory) do
result = result .. "* " .. itemId .. " — " .. count .. "\n"
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 "Ошибка: автомат с ID \"" .. id .. "\" не найден."
end
-- Получение ID пополнителя
local packId = vendingMachine.pack
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 "Ошибка: для автомата с ID \"" .. id .. "\" пополнитель не найден."
end
-- Основная функция модуля
function p.main(frame)
local id = frame.args.id
if not id then
return "Ошибка: необходимо указать параметр `id`."
end
-- Определение действия на основе ID
if frame.args.mode == "inventory" then
return getInventoryOutput(id)
elseif frame.args.mode == "restock" then
return getRestockOutput(id)
else
return "Ошибка: необходимо указать параметр `mode` (inventory или restock)."
end
end
return p