Module:Chem box: Difference between revisions
From Space Station 14 Wiki
(beep boop) |
(boop beep) |
||
Line 165: | Line 165: | ||
end | end | ||
local effects = concat_numberic_table(reagent.effects, " | local effects = concat_numberic_table(reagent.effects, "") | ||
return current_frame:expandTemplate { | return current_frame:expandTemplate { |
Revision as of 20:10, 16 September 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("User:UpAndLeaves/chemData.json")
local current_frame = mw.getCurrentFrame()
local beaker_el = current_frame:expandTemplate {
title = 'Beaker'
}
-- ====================
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
-- ====================
-- 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)
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 reagent.name == query 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)
local recipes_container_el = mw.html.create("div")
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
for reactant_i, reactant in ipairs(recipe.reactants) do
local component_el = current_frame:expandTemplate {
title = 'Recipe Component',
args = {
item = reactant[1],
amount = reactant[2]
-- TODO
-- image = ""
}
}
recipe_box_template_args['component-' .. reactant_i] = component_el
end
recipe_box_template_args.transformer = beaker_el
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
local recipes_el = current_frame:expandTemplate {
title = 'Recipe Box',
args = recipe_box_template_args
}
recipes_container_el:node(recipes_el)
end
local effects = concat_numberic_table(reagent.effects, "")
return current_frame:expandTemplate {
title = 'Manual Chem Box',
args = {
color = reagent.color,
-- textcolor = "",
name = reagent.name,
recipes = tostring(recipes_container_el:allDone()),
metabolisms = effects,
desc = reagent.desc,
physicalDesc = reagent.physicalDesc,
}
}
end
return p