Открыть меню
Переключить меню настроек
Открыть персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

Модуль:Gases: различия между версиями

Материал из Space Station 14 | Время приключений Вики
м Замена пользователя на себя
мНет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}
 
p.gases = mw.text.jsonDecode(mw.title.new('Участник:WikiHampter/gas_prototypes.json'):getContent())
local function trim(s)
    return s:match('^%s*(.-)%s*$') or s
end
 
local function getJsonData()
    local title = mw.title.new('Участник:WikiHampter/gas_prototypes.json')
    if not title or not title.exists then
        return nil, 'Страница с JSON не найдена (Участник:WikiHampter/gas_prototypes.json)'
    end
    local jsonText = title:getContent()
    if not jsonText or jsonText == '' then
        return nil, 'Страница с JSON пуста'
    end
    local success, data = pcall(mw.text.jsonDecode, jsonText)
    if not success then
        return nil, 'Ошибка парсинга JSON: ' .. data
    end
    return data, nil
end


function p.table(frame)
function p.table(frame)
    local args = frame.args
local out = ""
    local groupFilter = trim(args[1] or args['group'] or '')
local group = frame.args.group
    local additional = trim(args['additional'] or '')
local groups
if group ~= nil and group ~= "" then
groups = mw.text.split(group, ",")
end


    local data, err = getJsonData()
local additional = frame.args.additional
    if err then
if additional ~= nil and additional ~= "" then
        return '<div class="error">' .. mw.text.encode(err) .. '</div>'
local additionalGasIds = mw.text.split(additional, ",")
    end
for _, gasId in ipairs(additionalGasIds) do
out = out .. fillGasRow(gasId, frame)
end
end


    local additionalSet = {}
for _, gasPrototype in pairs(p.gases) do
    if additional ~= '' then
if group == nil or group == "" or contains(groups, gasPrototype.group) then
        for item in mw.text.gsplit(additional, ',') do
out = out .. fillGasRow(gasPrototype.id, frame)
            additionalSet[trim(item)] = true
end
        end
end
    end


    local gases = {}
return out
    for id, gas in pairs(data) do
end
        local matchGroup = false
        if groupFilter ~= '' then
            local groups = mw.text.split(gas.group or '', ',')
            for _, g in ipairs(groups) do
                if trim(g) == groupFilter then
                    matchGroup = true
                    break
                end
            end
        end
        if additionalSet[gas.name] or additionalSet[id] then
            matchGroup = true
        end
        if groupFilter == '' or matchGroup then
            table.insert(gases, gas)
        end
    end


    table.sort(gases, function(a, b)
function fillGasRow(gasId, frame)
        if a.group ~= b.group then
local gasPrototype = p.gases[gasId]
            return (a.group or '') < (b.group or '')
local out = ""
        end
        return (a.name or '') < (b.name or '')
    end)


    local root = mw.html.create('table')
local color = gasPrototype.color or "#000000"
        :addClass('wikitable')
local textColor = gasPrototype.textColor or "#FFFFFF"
        :addClass('sortable')
        :css('width', '100%')
        :css('text-align', 'center')


    root:tag('caption')
out = out .. '|- style="background: ' .. color .. '; color: ' .. textColor .. '"'
        :wikitext('Газы')
out = out .. '\n| ' .. gasPrototype.name
out = out .. '\n| style="text-align: left;" | ' .. gasPrototype.desc
out = out .. '\n| style="text-align: center;" | ' .. 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; font-size: 90%;" | '


    root:tag('tr')
if gasPrototype.metabolisms ~= nil then
        :tag('th'):wikitext('Газ'):done()
local first = true
        :tag('th'):wikitext('Описание'):done()
for metabolismGroupKey, metabolismGroup in pairs(gasPrototype.metabolisms) do
        :tag('th'):wikitext('Цвет'):done()
if not first then
        :tag('th'):wikitext('Удельная теплоёмкость<br/>(Дж/моль·K)'):done()
out = out .. "\n'''"
        :tag('th'):wikitext('Показатель адиабаты'):done()
end
        :tag('th'):wikitext('Молярная масса<br/>(г/моль)'):done()
first = false
        :tag('th'):wikitext('Цена за моль'):done()
out = out .. "'''" .. metabolismGroupKey .. " (" .. tostring(metabolismGroup.rate) .. " ед/сек)'''"
        :tag('th'):wikitext('Эффекты'):done()
for _, effect in ipairs(metabolismGroup.effects) do
if effect.description ~= "" then
out = out .. "\n* " .. effect.description
end
end
end
else
out = out .. "—"
end


    for _, gas in ipairs(gases) do
out = out .. "\n"
        local color = gas.color or '#000000'
return out
        local textColor = '#ffffff'
        local r, g, b = color:match('^#(..)(..)(..)$')
        if r then
            local luminance = (tonumber(r, 16) * 0.299 + tonumber(g, 16) * 0.587 + tonumber(b, 16) * 0.114) / 255
            if luminance > 0.5 then
                textColor = '#000000'
            end
        end
 
        local row = root:tag('tr')
 
        local nameCell = row:tag('td')
        nameCell
            :css('background', color)
            :css('color', textColor)
            :css('font-weight', 'bold')
            :wikitext(gas.name or gas.id)
 
        row:tag('td')
            :css('text-align', 'left')
            :wikitext(gas.desc or '')
 
        row:tag('td')
            :css('background', color)
            :css('width', '50px')
            :node(mw.html.create('span')
                :css('color', textColor)
                :wikitext(color))
 
        row:tag('td'):wikitext(tostring(gas.specificHeat or ''))
        row:tag('td'):wikitext(tostring(gas.heatCapacityRatio or ''))
        row:tag('td'):wikitext(tostring(gas.molarMass or ''))
        row:tag('td'):wikitext(tostring(gas.pricePerMole or ''))
 
        local effectsCell = row:tag('td')
            :css('text-align', 'left')
            :css('font-size', '90%')
 
        if gas.metabolisms then
            for metabType, metabData in pairs(gas.metabolisms) do
                effectsCell:tag('div')
                    :css('font-weight', 'bold')
                    :css('margin-top', '4px')
                    :wikitext('Метаболизм: ' .. metabType .. ' (speed: ' .. tostring(metabData.rate or '?') .. ')')
 
                local list = effectsCell:tag('ul')
                    :css('margin', '2px 0')
                    :css('padding-left', '20px')
 
                if metabData.effects then
                    for _, eff in ipairs(metabData.effects) do
                        local desc = eff.description or ''
                        if desc ~= '' then
                            list:tag('li'):wikitext(desc)
                        end
                    end
                end
            end
        else
            effectsCell:wikitext('—')
        end
    end
 
    return tostring(root)
end
end


function p.single(frame)
function p.single(frame)
    local args = frame.args
local gasId = frame.args[1] or frame.args.id or ""
    local gasId = trim(args[1] or args['id'] or '')
if gasId == "" then
return '<div class="error">Не указан ID газа. Использование: {{#invoke:Gases|single|id=Oxygen}}</div>'
end


    if gasId == '' then
local gasPrototype = p.gases[gasId]
        return '<div class="error">Не указан ID газа. Использование: {{#invoke:Gases|single|id=Oxygen}}</div>'
if not gasPrototype then
    end
return '<div class="error">Газ с ID "' .. mw.text.encode(gasId) .. '" не найден</div>'
end


    local data, err = getJsonData()
local color = gasPrototype.color or "#000000"
    if err then
local textColor = gasPrototype.textColor or "#FFFFFF"
        return '<div class="error">' .. mw.text.encode(err) .. '</div>'
    end


    local gas = data[gasId]
local out = ""
    if not gas then
out = out .. '<div style="border: 2px solid ' .. color .. '; border-radius: 8px; padding: 10px; margin: 10px 0; background: #f8f9fa;">'
        return '<div class="error">Газ с ID "' .. mw.text.encode(gasId) .. '" не найден</div>'
out = out .. '<div style="font-size: 1.5em; font-weight: bold; color: ' .. color .. ';">' .. gasPrototype.name .. '</div>'
    end
out = out .. '<div style="margin: 5px 0; font-style: italic;">' .. gasPrototype.desc .. '</div>'


    local color = gas.color or '#000000'
out = out .. '<table style="width: 100%; border-collapse: collapse; margin: 10px 0;">'
    local textColor = '#ffffff'
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>'
    local r, g, b = color:match('^#(..)(..)(..)$')
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>'
    if r then
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>'
        local luminance = (tonumber(r, 16) * 0.299 + tonumber(g, 16) * 0.587 + tonumber(b, 16) * 0.114) / 255
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>'
        if luminance > 0.5 then
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>'
            textColor = '#000000'
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>'
        end
out = out .. '</table>'
    end


    local root = mw.html.create('div')
if gasPrototype.metabolisms ~= nil then
        :addClass('gas-info-box')
out = out .. "<h3>Эффекты метаболизма</h3>"
        :css('border', '2px solid ' .. color)
for metabolismGroupKey, metabolismGroup in pairs(gasPrototype.metabolisms) do
        :css('border-radius', '8px')
out = out .. "<h4>" .. metabolismGroupKey .. " (" .. tostring(metabolismGroup.rate) .. " ед/сек)</h4>"
        :css('padding', '10px')
out = out .. "<ul>"
        :css('margin', '10px 0')
for _, effect in ipairs(metabolismGroup.effects) do
        :css('background', '#f8f9fa')
if effect.description ~= "" then
out = out .. "<li>" .. effect.description .. "</li>"
end
end
out = out .. "</ul>"
end
end


    root:tag('div')
out = out .. "</div>"
        :css('font-size', '1.5em')
return out
        :css('font-weight', 'bold')
end
        :css('color', color)
        :wikitext(gas.name or gas.id)
 
    root:tag('div')
        :css('margin', '5px 0')
        :css('font-style', 'italic')
        :wikitext(gas.desc or '')
 
    local propsTable = root:tag('table')
        :css('width', '100%')
        :css('border-collapse', 'collapse')
        :css('margin', '10px 0')
 
    local function addProp(label, value)
        local tr = propsTable:tag('tr')
            :css('border-bottom', '1px solid #ddd')
        tr:tag('td')
            :css('font-weight', 'bold')
            :css('width', '200px')
            :css('padding', '4px 8px')
            :wikitext(label)
        local td = tr:tag('td')
            :css('padding', '4px 8px')
        if type(value) == 'table' then
            td:node(value)
        else
            td:wikitext(tostring(value or '—'))
        end
    end
 
    addProp('Физическое описание', gas.physicalDesc or '—')
    addProp('Цвет', mw.html.create('span')
        :css('background', color)
        :css('color', textColor)
        :css('padding', '2px 8px')
        :css('border-radius', '3px')
        :wikitext(color))
    addProp('Удельная теплоёмкость', (gas.specificHeat and (gas.specificHeat .. ' Дж/моль·K') or '—'))
    addProp('Показатель адиабаты', gas.heatCapacityRatio or '—')
    addProp('Молярная масса', (gas.molarMass and (gas.molarMass .. ' г/моль') or '—'))
    addProp('Цена за моль', (gas.pricePerMole and (gas.pricePerMole .. ' кредитов') or '—'))
 
    if gas.metabolisms then
        root:tag('h3'):wikitext('Эффекты метаболизма')
        for metabType, metabData in pairs(gas.metabolisms) do
            root:tag('h4'):wikitext(metabType .. ' (speed: ' .. tostring(metabData.rate or '?') .. ')')
            local list = root:tag('ul')
            if metabData.effects then
                for _, eff in ipairs(metabData.effects) do
                    local desc = eff.description or ''
                    if desc ~= '' then
                        list:tag('li'):wikitext(desc)
                    end
                end
            end
        end
    end


    return tostring(root)
function contains(list, x)
for _, v in ipairs(list) do
if v == x then return true end
end
return false
end
end


return p
return p

Версия от 22:52, 4 июня 2026

Для документации этого модуля может быть создана страница Модуль:Gases/doc

local p = {}
p.gases = mw.text.jsonDecode(mw.title.new('Участник:WikiHampter/gas_prototypes.json'):getContent())

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

	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, frame)
		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, frame)
		end
	end

	return out
end

function fillGasRow(gasId, frame)
	local gasPrototype = p.gases[gasId]
	local out = ""

	local color = gasPrototype.color or "#000000"
	local textColor = gasPrototype.textColor or "#FFFFFF"

	out = out .. '|- style="background: ' .. color .. '; color: ' .. textColor .. '"'
	out = out .. '\n| ' .. gasPrototype.name
	out = out .. '\n| style="text-align: left;" | ' .. gasPrototype.desc
	out = out .. '\n| style="text-align: center;" | ' .. 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; font-size: 90%;" | '

	if gasPrototype.metabolisms ~= nil then
		local first = true
		for metabolismGroupKey, metabolismGroup in pairs(gasPrototype.metabolisms) do
			if not first then
				out = out .. "\n'''"
			end
			first = false
			out = out .. "'''" .. metabolismGroupKey .. " (" .. tostring(metabolismGroup.rate) .. " ед/сек)'''"
			for _, effect in ipairs(metabolismGroup.effects) do
				if effect.description ~= "" then
					out = out .. "\n* " .. effect.description
				end
			end
		end
	else
		out = out .. "—"
	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>'

	if gasPrototype.metabolisms ~= nil then
		out = out .. "<h3>Эффекты метаболизма</h3>"
		for metabolismGroupKey, metabolismGroup in pairs(gasPrototype.metabolisms) do
			out = out .. "<h4>" .. metabolismGroupKey .. " (" .. tostring(metabolismGroup.rate) .. " ед/сек)</h4>"
			out = out .. "<ul>"
			for _, effect in ipairs(metabolismGroup.effects) do
				if effect.description ~= "" then
					out = out .. "<li>" .. effect.description .. "</li>"
				end
			end
			out = out .. "</ul>"
		end
	end

	out = out .. "</div>"
	return out
end

function contains(list, x)
	for _, v in ipairs(list) do
		if v == x then return true end
	end
	return false
end

return p