Module:Chem box: Difference between revisions

From Space Station 14 Wiki
(changed data source)
(Added centrifuges, bibles, electrolysers, and temperature)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {} --p stands for package
local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs
local getArgs = require("Module:Arguments").getArgs
local yesNo = require('Module:Yesno')
local yesNo = require("Module:Yesno")


local chem_data = mw.loadJsonData("Module:Chem box/chem data.json")
local chem_data = mw.loadJsonData("Module:Chem box/chem data.json")


local current_frame = mw.getCurrentFrame()
local current_frame = mw.getCurrentFrame()
local beaker_el = current_frame:expandTemplate {
    title = 'Beaker'
}


-- ====================
-- ====================


local function numeric_table_length(t)
local function numeric_table_length(t)
    local count = 0
local count = 0
    for _ in ipairs(t) do count = count + 1 end
for _ in ipairs(t) do
    return count
count = count + 1
end
return count
end
end


local function table_length(t)
local function table_length(t)
    local count = 0
local count = 0
    for _ in pairs(t) do count = count + 1 end
for _ in pairs(t) do
    return count
count = count + 1
end
return count
end
end


local function table_has_value(tab, val)
local function table_has_value(tab, val)
    for _, value in ipairs(tab) do
for _, value in ipairs(tab) do
        if value == val then
if value == val then
            return true
return true
        end
end
    end
end


    return false
return false
end
end


local function assert_value_not_nil(value, error_message)
local function assert_value_not_nil(value, error_message)
    if value == nil then
if value == nil then
        if error_message == nil then
if error_message == nil then
            error("value is nil")
error("value is nil")
        else
else
            error(error_message)
error(error_message)
        end
end
    end
end
end
end


Line 49: Line 48:
-- Source: https://stackoverflow.com/a/2421746
-- Source: https://stackoverflow.com/a/2421746
local function capitalize(str)
local function capitalize(str)
    return (str:gsub("^%l", string.upper))
return (str:gsub("^%l", string.upper))
end
end


local function passthrough_assert_true(value, valueToReturnIfTrue, errorMessageOnFalse)
local function passthrough_assert_true(value, valueToReturnIfTrue, errorMessageOnFalse)
    if value then
if value then
        return valueToReturnIfTrue
return valueToReturnIfTrue
    else
else
        error(errorMessageOnFalse)
error(errorMessageOnFalse)
    end
end
end
end


local function find_first_numeric_table_item_matching_condition(table, condition)
local function find_first_numeric_table_item_matching_condition(table, condition)
    for i, item in ipairs(table) do
for i, item in ipairs(table) do
        if condition(item, i, table) then
if condition(item, i, table) then
            return item
return item
        end
end
    end
end
end
end


-- table concat function because ofcourse fucking table.concat doesn't work
-- table concat function because ofcourse fucking table.concat doesn't work
local function concat_numberic_table(tbl, sep)
local function concat_numberic_table(tbl, sep)
    local temp_table = {}
local temp_table = {}
    for i = 1, numeric_table_length(tbl) do
for i = 1, numeric_table_length(tbl) do
        table.insert(temp_table, tbl[i])
table.insert(temp_table, tbl[i])
    end
end


    return table.concat(temp_table, sep)
return table.concat(temp_table, sep)
end
 
local function ternary_strict(valueToCheck, valueIfTrue, valueIfFalse)
if valueToCheck == true then
return valueIfTrue
else
return valueIfFalse
end
end
end


Line 85: Line 92:
-- Set `no_error` to `true` to return `nil` instead.
-- Set `no_error` to `true` to return `nil` instead.
function p.lookup_reagent(query, no_error)
function p.lookup_reagent(query, no_error)
    local query_lower = string.lower(query)
local query_lower = string.lower(query)


    for _, reagent in ipairs(chem_data) do
for _, reagent in ipairs(chem_data) do
        assert_value_not_nil(reagent.id)
assert_value_not_nil(reagent.id)
        assert_value_not_nil(reagent.name)
assert_value_not_nil(reagent.name)


        if reagent.id == query then return reagent end
if reagent.id == query then
        if string.lower(reagent.name) == query_lower then return reagent end
return reagent
    end
end
if string.lower(reagent.name) == query_lower then
return reagent
end
end


    return passthrough_assert_true(
return passthrough_assert_true(no_error, nil, "failed to lookup reagent: no match was found")
        no_error,
        nil,
        "failed to lookup reagent: no match was found"
    )
end
end


Line 105: Line 112:


function p.generate_chem_box(frame)
function p.generate_chem_box(frame)
    local args = getArgs(frame)
local args = getArgs(frame)
 
local query = args[1]
assert_value_not_nil(query, "failed to generate chem box: query was not provided")
 
-- ===================


    local query = args[1]
local reagent = p.lookup_reagent(query)
    assert_value_not_nil(query, "failed to generate chem box: query was not provided")
assert_value_not_nil(reagent.color)
assert_value_not_nil(reagent.name)
assert_value_not_nil(reagent.recipes)
assert_value_not_nil(reagent.effects)
assert_value_not_nil(reagent.desc)
assert_value_not_nil(reagent.physicalDesc)
assert_value_not_nil(reagent.textColorTheme)


    -- ===================
local recipes_container_el = mw.html.create("div")
if numeric_table_length(reagent.recipes) == 0 then
local no_recipes_text_el = mw.html.create("span"):css("color", "gray"):node("No recipes")


    local reagent = p.lookup_reagent(query)
recipes_container_el:node(no_recipes_text_el)
    assert_value_not_nil(reagent.color)
else
    assert_value_not_nil(reagent.name)
for _, recipe in ipairs(reagent.recipes) do
    assert_value_not_nil(reagent.recipes)
assert_value_not_nil(recipe.id)
    assert_value_not_nil(reagent.effects)
assert_value_not_nil(recipe.reactants)
    assert_value_not_nil(reagent.desc)
assert_value_not_nil(recipe.products)
    assert_value_not_nil(reagent.physicalDesc)


    local recipes_container_el = mw.html.create("div")
local recipe_box_template_args = {}
    if numeric_table_length(reagent.recipes) == 0 then
recipe_box_template_args.name = recipe.id
        local no_recipes_text_el = mw.html.create("span")
            :css("color", "gray")
            :node("No recipes")


        recipes_container_el:node(no_recipes_text_el)
-- a list of catalist reactants.
    else
-- contains "id" and "amount"
        for _, recipe in ipairs(reagent.recipes) do
local catalyst_reactants = {}
            assert_value_not_nil(recipe.id)
            assert_value_not_nil(recipe.reactants)
            assert_value_not_nil(recipe.products)


            local recipe_box_template_args = {}
for reactant_i, reactant in ipairs(recipe.reactants) do
            recipe_box_template_args.name = recipe.id
local reactant_id = reactant[1]
local reactant_amount = reactant[2]
local reactant_is_catalyst = reactant[3] or false


            -- a list of catalist reactants.
local label = reactant_id
            -- contains "id" and "amount"
            local catalyst_reactants = {}


            for reactant_i, reactant in ipairs(recipe.reactants) do
if reactant_is_catalyst then
                local reactant_id = reactant[1]
table.insert(catalyst_reactants, {
                local reactant_amount = reactant[2]
id = reactant_id,
                local reactant_is_catalyst = reactant[3] or false
amount = reactant_amount,
})
end


                local label = reactant_id
local component_el = current_frame:expandTemplate({
title = "Recipe Component",
args = {
item = label,
amount = reactant_amount,
-- TODO
-- image = ""
},
})


                if reactant_is_catalyst then
recipe_box_template_args["component-" .. reactant_i] = component_el
                    table.insert(catalyst_reactants, {
end
                        id = reactant_id,
                        amount = reactant_amount
                    })
                end


                local component_el = current_frame:expandTemplate {
local temperature_args = {}
                    title = 'Recipe Component',
if recipe.minTemp ~= nil or recipe.maxTemp ~= nil then
                    args = {
local temperature_string = ""
                        item = label,
if recipe.minTemp ~= nil then
                        amount = reactant_amount
temperature_string = tostring(recipe.minTemp) .. "k "
                        -- TODO
end
                        -- image = ""
temperature_string = temperature_string .. "<"
                    }
if recipe.maxTemp ~= nil then
                }
temperature_string = temperature_string .. " " .. tostring(recipe.maxTemp) .. "k"
end
temperature_args = {temperature = temperature_string}
end
local required_mixer = "Beaker"
if recipe.requiredMixerCategories ~= nil then
required_mixer = recipe.requiredMixerCategories[1]
end
recipe_box_template_args.transformer = current_frame:expandTemplate({
title = required_mixer,
args = temperature_args
})


                recipe_box_template_args['component-' .. reactant_i] = component_el
for product_i, product in ipairs(recipe.products) do
            end
local component_el = current_frame:expandTemplate({
title = "Recipe Component",
args = {
item = product[1],
amount = product[2],
-- TODO
-- image = ""
},
})


            recipe_box_template_args.transformer = beaker_el
recipe_box_template_args["result-" .. product_i] = component_el
end


            for product_i, product in ipairs(recipe.products) do
-- add catalysts as products, if any
                local component_el = current_frame:expandTemplate {
for catalyst_product_i, catalyst_product_entry in ipairs(catalyst_reactants) do
                    title = 'Recipe Component',
local product_id = catalyst_product_entry.id
                    args = {
local product_amount = catalyst_product_entry.amount
                        item = product[1],
                        amount = product[2]
                        -- TODO
                        -- image = ""
                    }
                }


                recipe_box_template_args['result-' .. product_i] = component_el
local label = current_frame:expandTemplate({
            end
title = "Tooltip",
args = {
'<sup class="quickbox-catalyst-note">+</sup>',
'<span class="quickbox-catalyst-note-tooltip">This product is a catalyst reactant, remaining after the reaction.</span>',
},
}) .. product_id


            -- add catalysts as products, if any
local component_el = current_frame:expandTemplate({
            for catalyst_product_i, catalyst_product_entry in ipairs(catalyst_reactants) do
title = "Recipe Component",
                local product_id = catalyst_product_entry.id
args = {
                local product_amount = catalyst_product_entry.amount
item = label,
amount = product_amount,
-- TODO
-- image = ""
},
})


                local label = current_frame:expandTemplate {
local index = numeric_table_length(recipe.products) + catalyst_product_i
                    title = 'Tooltip',
recipe_box_template_args["result-" .. index] = component_el
                    args = {
end
                        '<sup class="quickbox-catalyst-note">+</sup>',
                        '<span class="quickbox-catalyst-note-tooltip">This product is a catalyst reactant, remaining after the reaction.</span>'
                    }
                } .. product_id
               
                local component_el = current_frame:expandTemplate {
                    title = 'Recipe Component',
                    args = {
                        item = label,
                        amount = product_amount
                        -- TODO
                        -- image = ""
                    }
                }


                local index = numeric_table_length(recipe.products) + catalyst_product_i
local recipes_el = current_frame:expandTemplate({
                recipe_box_template_args['result-' .. index] = component_el
title = "Recipe Box",
            end
args = recipe_box_template_args,
})


            local recipes_el = current_frame:expandTemplate {
recipes_container_el:node(recipes_el)
                title = 'Recipe Box',
end
                args = recipe_box_template_args
end
            }


            recipes_container_el:node(recipes_el)
local effects = concat_numberic_table(reagent.effects, "")
        end
    end


    local effects = concat_numberic_table(reagent.effects, "")
local text_color = ternary_strict(
reagent.textColorTheme == "light",
"var(--quickbox-header-text-theme-light)",
"var(--quickbox-header-text-theme-dark)"
)


    return current_frame:expandTemplate {
return current_frame:expandTemplate({
        title = 'Manual Chem Box',
title = "Manual Chem Box",
        args = {
args = {
            color = reagent.color,
id = reagent.id,
            -- textcolor = "",
color = reagent.color,
            name = capitalize(reagent.name),
textcolor = text_color,
            recipes = tostring(recipes_container_el:allDone()),
name = capitalize(reagent.name),
            metabolisms = effects,
recipes = tostring(recipes_container_el:allDone()),
            desc = reagent.desc,
metabolisms = effects,
            physicalDesc = reagent.physicalDesc,
desc = reagent.desc,
        }
physicalDesc = reagent.physicalDesc,
    }
},
})
end
end


function p.generate_chem_boxes_for_all_reagents()
function p.generate_chem_boxes_for_all_reagents()
    local container_el = mw.html.create("div")
local container_el = mw.html.create("div"):addClass("reagents-list")
        :addClass("reagents-list")
 
for _, reagent in ipairs(chem_data) do
assert_value_not_nil(reagent.id)
 
container_el:node(p.generate_chem_box({
reagent.id,
}))
end
 
return container_el:allDone()
end


    for _, reagent in ipairs(chem_data) do
function p.generate_chem_boxes_for_group(frame)
        assert_value_not_nil(reagent.id)
local args = getArgs(frame)
local query = args[1]
assert_value_not_nil(query)
query = string.lower(query)
local container_el = mw.html.create("div"):addClass("reagents-list")


        container_el:node(
for _, reagent in ipairs(chem_data) do
            p.generate_chem_box {
assert_value_not_nil(reagent.id)
                reagent.id
assert_value_not_nil(reagent.group)
            }
if string.lower(reagent.group) == query then
        )
container_el:node(p.generate_chem_box({
    end
reagent.id,
}))
end
end


    return container_el:allDone()
return container_el:allDone()
end
end


return p
return p

Latest revision as of 14:01, 25 October 2024

Module documentation
View or edit this documentation (about module documentation)
Uses JSON data
This module uses JSON data pages:

Implements {{chem box}}.


local p = {} --p stands for package
local getArgs = require("Module:Arguments").getArgs
local yesNo = require("Module:Yesno")

local chem_data = mw.loadJsonData("Module:Chem box/chem data.json")

local current_frame = mw.getCurrentFrame()

-- ====================

local function numeric_table_length(t)
	local count = 0
	for _ in ipairs(t) do
		count = count + 1
	end
	return count
end

local function table_length(t)
	local count = 0
	for _ in pairs(t) do
		count = count + 1
	end
	return count
end

local function table_has_value(tab, val)
	for _, value in ipairs(tab) do
		if value == val then
			return true
		end
	end

	return false
end

local function assert_value_not_nil(value, error_message)
	if value == nil then
		if error_message == nil then
			error("value is nil")
		else
			error(error_message)
		end
	end
end

-- Makes the first letter uppercase.
-- Source: https://stackoverflow.com/a/2421746
local function capitalize(str)
	return (str:gsub("^%l", string.upper))
end

local function passthrough_assert_true(value, valueToReturnIfTrue, errorMessageOnFalse)
	if value then
		return valueToReturnIfTrue
	else
		error(errorMessageOnFalse)
	end
end

local function find_first_numeric_table_item_matching_condition(table, condition)
	for i, item in ipairs(table) do
		if condition(item, i, table) then
			return item
		end
	end
end

-- table concat function because ofcourse fucking table.concat doesn't work
local function concat_numberic_table(tbl, sep)
	local temp_table = {}
	for i = 1, numeric_table_length(tbl) do
		table.insert(temp_table, tbl[i])
	end

	return table.concat(temp_table, sep)
end

local function ternary_strict(valueToCheck, valueIfTrue, valueIfFalse)
	if valueToCheck == true then
		return valueIfTrue
	else
		return valueIfFalse
	end
end

-- ====================

-- Lookups reagent by ID or name.
--
-- Raises an error if no reagent was found.
-- Set `no_error` to `true` to return `nil` instead.
function p.lookup_reagent(query, no_error)
	local query_lower = string.lower(query)

	for _, reagent in ipairs(chem_data) do
		assert_value_not_nil(reagent.id)
		assert_value_not_nil(reagent.name)

		if reagent.id == query then
			return reagent
		end
		if string.lower(reagent.name) == query_lower then
			return reagent
		end
	end

	return passthrough_assert_true(no_error, nil, "failed to lookup reagent: no match was found")
end

-- ====================

function p.generate_chem_box(frame)
	local args = getArgs(frame)

	local query = args[1]
	assert_value_not_nil(query, "failed to generate chem box: query was not provided")

	-- ===================

	local reagent = p.lookup_reagent(query)
	assert_value_not_nil(reagent.color)
	assert_value_not_nil(reagent.name)
	assert_value_not_nil(reagent.recipes)
	assert_value_not_nil(reagent.effects)
	assert_value_not_nil(reagent.desc)
	assert_value_not_nil(reagent.physicalDesc)
	assert_value_not_nil(reagent.textColorTheme)

	local recipes_container_el = mw.html.create("div")
	if numeric_table_length(reagent.recipes) == 0 then
		local no_recipes_text_el = mw.html.create("span"):css("color", "gray"):node("No recipes")

		recipes_container_el:node(no_recipes_text_el)
	else
		for _, recipe in ipairs(reagent.recipes) do
			assert_value_not_nil(recipe.id)
			assert_value_not_nil(recipe.reactants)
			assert_value_not_nil(recipe.products)

			local recipe_box_template_args = {}
			recipe_box_template_args.name = recipe.id

			-- a list of catalist reactants.
			-- contains "id" and "amount"
			local catalyst_reactants = {}

			for reactant_i, reactant in ipairs(recipe.reactants) do
				local reactant_id = reactant[1]
				local reactant_amount = reactant[2]
				local reactant_is_catalyst = reactant[3] or false

				local label = reactant_id

				if reactant_is_catalyst then
					table.insert(catalyst_reactants, {
						id = reactant_id,
						amount = reactant_amount,
					})
				end

				local component_el = current_frame:expandTemplate({
					title = "Recipe Component",
					args = {
						item = label,
						amount = reactant_amount,
						-- TODO
						-- image = ""
					},
				})

				recipe_box_template_args["component-" .. reactant_i] = component_el
			end

			local temperature_args = {}
			if recipe.minTemp ~= nil or recipe.maxTemp ~= nil then
				local temperature_string = ""
				if recipe.minTemp ~= nil then
					temperature_string = tostring(recipe.minTemp) .. "k "
				end
				temperature_string = temperature_string .. "<"
				if recipe.maxTemp ~= nil then
					temperature_string = temperature_string .. " " .. tostring(recipe.maxTemp) .. "k"
				end
				temperature_args = {temperature = temperature_string}
			end
			local required_mixer = "Beaker"
			if recipe.requiredMixerCategories ~= nil then
				required_mixer = recipe.requiredMixerCategories[1]
			end
			recipe_box_template_args.transformer = current_frame:expandTemplate({
				title = required_mixer,
				args = temperature_args
			})

			for product_i, product in ipairs(recipe.products) do
				local component_el = current_frame:expandTemplate({
					title = "Recipe Component",
					args = {
						item = product[1],
						amount = product[2],
						-- TODO
						-- image = ""
					},
				})

				recipe_box_template_args["result-" .. product_i] = component_el
			end

			-- add catalysts as products, if any
			for catalyst_product_i, catalyst_product_entry in ipairs(catalyst_reactants) do
				local product_id = catalyst_product_entry.id
				local product_amount = catalyst_product_entry.amount

				local label = current_frame:expandTemplate({
					title = "Tooltip",
					args = {
						'<sup class="quickbox-catalyst-note">+</sup>',
						'<span class="quickbox-catalyst-note-tooltip">This product is a catalyst reactant, remaining after the reaction.</span>',
					},
				}) .. product_id

				local component_el = current_frame:expandTemplate({
					title = "Recipe Component",
					args = {
						item = label,
						amount = product_amount,
						-- TODO
						-- image = ""
					},
				})

				local index = numeric_table_length(recipe.products) + catalyst_product_i
				recipe_box_template_args["result-" .. index] = component_el
			end

			local recipes_el = current_frame:expandTemplate({
				title = "Recipe Box",
				args = recipe_box_template_args,
			})

			recipes_container_el:node(recipes_el)
		end
	end

	local effects = concat_numberic_table(reagent.effects, "")

	local text_color = ternary_strict(
		reagent.textColorTheme == "light",
		"var(--quickbox-header-text-theme-light)",
		"var(--quickbox-header-text-theme-dark)"
	)

	return current_frame:expandTemplate({
		title = "Manual Chem Box",
		args = {
			id = reagent.id,
			color = reagent.color,
			textcolor = text_color,
			name = capitalize(reagent.name),
			recipes = tostring(recipes_container_el:allDone()),
			metabolisms = effects,
			desc = reagent.desc,
			physicalDesc = reagent.physicalDesc,
		},
	})
end

function p.generate_chem_boxes_for_all_reagents()
	local container_el = mw.html.create("div"):addClass("reagents-list")

	for _, reagent in ipairs(chem_data) do
		assert_value_not_nil(reagent.id)

		container_el:node(p.generate_chem_box({
			reagent.id,
		}))
	end

	return container_el:allDone()
end

function p.generate_chem_boxes_for_group(frame)
	local args = getArgs(frame)
	local query = args[1]
	assert_value_not_nil(query)
	query = string.lower(query)
	local container_el = mw.html.create("div"):addClass("reagents-list")

	for _, reagent in ipairs(chem_data) do
		assert_value_not_nil(reagent.id)
		assert_value_not_nil(reagent.group)
		if string.lower(reagent.group) == query then
			container_el:node(p.generate_chem_box({
				reagent.id,
			}))
		end
	end

	return container_el:allDone()
end

return p