Модуль:Prototypes/Объект/Торгомат: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
-- | -- Загрузка данных | ||
local function loadData(filePath) | local function loadData(filePath) | ||
local page = mw.title.new(filePath) | local page = mw.title.new(filePath) | ||
Строка 8: | Строка 8: | ||
end | end | ||
-- | -- Поиск данных по ID | ||
local function findDataById(data, id) | local function findDataById(data, id) | ||
if not data then return nil end | if not data then return nil end | ||
Строка 19: | Строка 19: | ||
end | end | ||
-- | -- Получение инвентаря автомата | ||
local function | local function getInventoryOutput(id) | ||
-- Загрузка данных | -- Загрузка данных | ||
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json") | local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json") | ||
Строка 29: | Строка 29: | ||
if not vendingMachine then return "" end | if not vendingMachine then return "" end | ||
-- | -- Получение инвентаря автомата | ||
local inventory = findDataById(inventories, | local inventoryId = vendingMachine.pack | ||
if not inventory then return "" end | local inventory = findDataById(inventories, inventoryId) | ||
if not inventory or not inventory.startingInventory then return "" end | |||
-- Формирование | -- Формирование вывода содержимого автомата | ||
local | local result = "== Содержимое автомата ==\n" | ||
for | for itemId, count in pairs(inventory.startingInventory) do | ||
result = result .. "* " .. itemId .. " — " .. count .. "\n" | |||
end | end | ||
return | return result | ||
end | end | ||
-- | -- Получение информации о пополнителе автомата | ||
local function | local function getRestockOutput(id) | ||
-- Загрузка данных | -- Загрузка данных | ||
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json") | local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json") | ||
local restockData = loadData("User:IanComradeBot/prototypes/vending machines/restock.json") | |||
-- Поиск автомата по ID | -- Поиск автомата по ID | ||
local vendingMachine = findDataById(vendingMachines, id) | local vendingMachine = findDataById(vendingMachines, id) | ||
if not vendingMachine | if not vendingMachine then return "" end | ||
-- | -- Получение ID пополнителя | ||
local | local packId = vendingMachine.pack | ||
for _, | for _, restock in ipairs(restockData) do | ||
if restock.canRestock and table.concat(restock.canRestock):find(packId) then | |||
if | return "== Пополнитель ==\n* ID пополнителя: " .. restock.id | ||
end | end | ||
end | end | ||
return | return "" | ||
end | end | ||
-- | -- Получение данных о доступах | ||
local function | local function getAccessOutput(id) | ||
-- Загрузка данных | -- Загрузка данных | ||
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json") | local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json") | ||
-- Поиск автомата по ID | -- Поиск автомата по ID | ||
Строка 83: | Строка 73: | ||
if not vendingMachine then return "" end | if not vendingMachine then return "" end | ||
-- | -- Получение данных доступа | ||
local | local accessData = vendingMachine.AccessReader and vendingMachine.AccessReader.access | ||
if not | if not accessData then return "" end | ||
-- | -- Использование функции перевода для обработки данных о доступах | ||
local | local frame = {args = {mw.text.jsonEncode(accessData)}} | ||
return | local accessOutput = mw.loadData("Module:Prototypes/Механика/Доступ").parse(frame) | ||
-- Формирование вывода доступов | |||
return "== Доступы ==\n" .. accessOutput | |||
end | end | ||
Строка 95: | Строка 88: | ||
function p.main(frame) | function p.main(frame) | ||
local id = frame.args.id | local id = frame.args.id | ||
if not id then return "" end | |||
if not id then | |||
-- | -- Определение действия на основе ID | ||
if mode == " | if frame.args.mode == "inventory" then | ||
return | return getInventoryOutput(id) | ||
elseif mode == " | elseif frame.args.mode == "restock" then | ||
return | return getRestockOutput(id) | ||
elseif mode == " | elseif frame.args.mode == "access" then | ||
return | return getAccessOutput(id) | ||
else | else | ||
return "" | return "" |
Версия от 18:15, 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)
-- Загрузка данных
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.pack
local inventory = findDataById(inventories, inventoryId)
if not inventory or not inventory.startingInventory then return "" 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 "" 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 ""
end
-- Получение данных о доступах
local function getAccessOutput(id)
-- Загрузка данных
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end
-- Получение данных доступа
local accessData = vendingMachine.AccessReader and vendingMachine.AccessReader.access
if not accessData then return "" end
-- Использование функции перевода для обработки данных о доступах
local frame = {args = {mw.text.jsonEncode(accessData)}}
local accessOutput = mw.loadData("Module:Prototypes/Механика/Доступ").parse(frame)
-- Формирование вывода доступов
return "== Доступы ==\n" .. accessOutput
end
-- Основная функция модуля
function p.main(frame)
local id = frame.args.id
if not id then return "" end
-- Определение действия на основе ID
if frame.args.mode == "inventory" then
return getInventoryOutput(id)
elseif frame.args.mode == "restock" then
return getRestockOutput(id)
elseif frame.args.mode == "access" then
return getAccessOutput(id)
else
return ""
end
end
return p