Модуль:Gases
Материал из Space Station 14 | Время приключений Вики
Дополнительные действия
Для документации этого модуля может быть создана страница Модуль:Gases/doc
local p = {}
p.gases = mw.text.jsonDecode(mw.title.new('User:WikiBotADT/gas_prototypes.json'):getContent())
p.reactions = mw.text.jsonDecode(mw.title.new('User:WikiBotADT/gasreact_prototypes.json'):getContent())
local function attachAsNewLine(originalStr, joinStr)
return originalStr .. "\n" .. joinStr
end
local function contains(list, x)
for _, v in ipairs(list) do
if v == x then return true end
end
return false
end
local function getEffects(gasPrototype)
local effects = ""
if gasPrototype.metabolisms == nil then
return effects
end
for metabolismGroupKey, metabolismGroup in pairs(gasPrototype.metabolisms) do
effects = attachAsNewLine(effects, "* " .. metabolismGroupKey .. " (" .. tostring(metabolismGroup.rate) .. " единиц в секунду)")
for _, effect in ipairs(metabolismGroup.effects) do
if effect.description ~= "" then
effects = attachAsNewLine(effects, "** " .. effect.description)
end
end
end
return effects
end
local function gasLink(gasId)
local gasPrototype = p.gases[gasId]
if gasPrototype == nil then
return gasId
end
return "[[#gas_" .. gasId .. "|" .. gasPrototype.name .. "]]"
end
local function sortedReactions()
local list = {}
for _, reaction in pairs(p.reactions) do
table.insert(list, reaction)
end
table.sort(list, function(a, b) return a.name < b.name end)
return list
end
-- Количество 0 в данных значит, что пропорция зависит от условий и описана в примечании.
local function gasAmounts(gases)
local lines = {}
for gasId, amount in pairs(gases) do
if amount == 0 then
table.insert(lines, gasLink(gasId))
else
table.insert(lines, tostring(amount) .. " " .. gasLink(gasId))
end
end
table.sort(lines)
return table.concat(lines, "<br>")
end
local function getConditions(reaction)
local lines = {}
if reaction.minTemp ~= nil and reaction.maxTemp ~= nil then
table.insert(lines, "от " .. reaction.minTemp .. " K до " .. reaction.maxTemp .. " K")
elseif reaction.minTemp ~= nil then
table.insert(lines, "выше " .. reaction.minTemp .. " K")
elseif reaction.maxTemp ~= nil then
table.insert(lines, "ниже " .. reaction.maxTemp .. " K")
end
for gasId, amount in pairs(reaction.requirements) do
table.insert(lines, "не меньше " .. tostring(amount) .. " моль: " .. gasLink(gasId))
end
return table.concat(lines, "<br>")
end
local function getCatalysts(reaction)
local lines = {}
for _, gasId in ipairs(reaction.catalysts) do
table.insert(lines, gasLink(gasId))
end
if #lines == 0 then
return ""
end
return "<br>Не расходуется: " .. table.concat(lines, ", ")
end
local function getRecipes(gasId)
local lines = {}
for _, reaction in ipairs(sortedReactions()) do
if reaction.produces[gasId] ~= nil then
table.insert(lines, "[[#gasreact_" .. reaction.id .. "|" .. reaction.name .. "]]")
end
end
return table.concat(lines, "<br>")
end
local fillGasRow
function p.table(frame)
local out = ""
local group = frame.args.group
local groups
if group ~= nil and group ~= "" then
groups = mw.text.split(group, ",")
end
out = out .. '{| class="wikitable sortable" style="width: 100%; text-align: center;"'
out = out .. "\n! Газ !! Описание !! Цвет !! Удельная теплоёмкость (Дж/моль·K) !! Показатель адиабаты !! Молярная масса (г/моль) !! Цена за моль !! Получение !! Эффекты"
local additional = frame.args.additional
if additional ~= nil and additional ~= "" then
local additionalGasIds = mw.text.split(additional, ",")
for _, gasId in ipairs(additionalGasIds) do
out = out .. fillGasRow(gasId)
end
end
for _, gasPrototype in pairs(p.gases) do
if group == nil or group == "" or contains(groups, gasPrototype.group) then
out = out .. fillGasRow(gasPrototype.id)
end
end
out = out .. "\n|}"
return out
end
fillGasRow = function(gasId)
local gasPrototype = p.gases[gasId]
local out = ""
local color = gasPrototype.color or "#000000"
local textColor = gasPrototype.textColor or "#FFFFFF"
out = out .. '\n|- id="gas_' .. gasId .. '" style="background: ' .. color .. '1E; color: ' .. textColor .. '"'
out = out .. '\n! scope="row" style="font-weight: bold;" | ' .. gasPrototype.name
out = out .. '\n| style="text-align: left;" | ' .. gasPrototype.desc
out = out .. '\n| ' .. color
out = out .. '\n| ' .. tostring(gasPrototype.specificHeat or "")
out = out .. '\n| ' .. tostring(gasPrototype.heatCapacityRatio or "")
out = out .. '\n| ' .. tostring(gasPrototype.molarMass or "")
out = out .. '\n| ' .. tostring(gasPrototype.pricePerMole or "")
out = out .. '\n| style="text-align: left;" | ' .. getRecipes(gasId)
out = out .. '\n| style="text-align: left; font-size: 90%;" | ' .. getEffects(gasPrototype)
return out
end
function p.reactionTable(frame)
local out = '{| class="wikitable sortable" style="width: 100%;"'
out = out .. "\n! Реакция !! Условия !! Расходуется !! Получается !! Примечания"
for _, reaction in ipairs(sortedReactions()) do
out = out .. '\n|- id="gasreact_' .. reaction.id .. '"'
out = out .. '\n! scope="row" | ' .. reaction.name
out = out .. '\n| ' .. getConditions(reaction)
out = out .. '\n| ' .. gasAmounts(reaction.consumes) .. getCatalysts(reaction)
out = out .. '\n| ' .. gasAmounts(reaction.produces)
out = out .. '\n| ' .. table.concat(reaction.notes, " ")
end
out = out .. "\n|}"
return out
end
function p.single(frame)
local gasId = frame.args[1] or frame.args.id or ""
if gasId == "" then
return '<div class="error">Не указан ID газа. Использование: {{#invoke:Gases|single|id=Oxygen}}</div>'
end
local gasPrototype = p.gases[gasId]
if not gasPrototype then
return '<div class="error">Газ с ID "' .. mw.text.encode(gasId) .. '" не найден</div>'
end
local color = gasPrototype.color or "#000000"
local textColor = gasPrototype.textColor or "#FFFFFF"
local out = ""
out = out .. '<div style="border: 2px solid ' .. color .. '; border-radius: 8px; padding: 10px; margin: 10px 0; background: #f8f9fa;">'
out = out .. '<div style="font-size: 1.5em; font-weight: bold; color: ' .. color .. ';">' .. gasPrototype.name .. '</div>'
out = out .. '<div style="margin: 5px 0; font-style: italic;">' .. gasPrototype.desc .. '</div>'
out = out .. '<table style="width: 100%; border-collapse: collapse; margin: 10px 0;">'
out = out .. '<tr style="border-bottom: 1px solid #ddd;"><td style="font-weight: bold; width: 200px; padding: 4px 8px;">Физическое описание</td><td style="padding: 4px 8px;">' .. (gasPrototype.physicalDesc or "-") .. '</td></tr>'
out = out .. '<tr style="border-bottom: 1px solid #ddd;"><td style="font-weight: bold; width: 200px; padding: 4px 8px;">Цвет</td><td style="padding: 4px 8px;"><span style="background: ' .. color .. '; color: ' .. textColor .. '; padding: 2px 8px; border-radius: 3px;">' .. color .. '</span></td></tr>'
out = out .. '<tr style="border-bottom: 1px solid #ddd;"><td style="font-weight: bold; width: 200px; padding: 4px 8px;">Удельная теплоёмкость</td><td style="padding: 4px 8px;">' .. (gasPrototype.specificHeat and (gasPrototype.specificHeat .. " Дж/моль·K") or "-") .. '</td></tr>'
out = out .. '<tr style="border-bottom: 1px solid #ddd;"><td style="font-weight: bold; width: 200px; padding: 4px 8px;">Показатель адиабаты</td><td style="padding: 4px 8px;">' .. tostring(gasPrototype.heatCapacityRatio or "-") .. '</td></tr>'
out = out .. '<tr style="border-bottom: 1px solid #ddd;"><td style="font-weight: bold; width: 200px; padding: 4px 8px;">Молярная масса</td><td style="padding: 4px 8px;">' .. (gasPrototype.molarMass and (gasPrototype.molarMass .. " г/моль") or "-") .. '</td></tr>'
out = out .. '<tr style="border-bottom: 1px solid #ddd;"><td style="font-weight: bold; width: 200px; padding: 4px 8px;">Цена за моль</td><td style="padding: 4px 8px;">' .. (gasPrototype.pricePerMole and (gasPrototype.pricePerMole .. " кредитов") or "-") .. '</td></tr>'
out = out .. '</table>'
out = out .. getEffects(gasPrototype)
out = out .. "</div>"
return out
end
return p