Module:Crafting: Difference between revisions
From Space Station 14 Wiki
No edit summary |
No edit summary |
||
Line 18: | Line 18: | ||
["emagDynamicRecipes"] = "emag dynamic", | ["emagDynamicRecipes"] = "emag dynamic", | ||
} | } | ||
-- =========================================== | |||
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 | |||
-- =========================================== | |||
-- Searches for a station config with given ID. | -- Searches for a station config with given ID. | ||
Line 24: | Line 38: | ||
-- Returns found config or `nil`. | -- Returns found config or `nil`. | ||
function p.find_crafting_station_config(id) | function p.find_crafting_station_config(id) | ||
assert_value_not_nil(id) | |||
local id_lc = string.lower(id) | local id_lc = string.lower(id) | ||
for _, config in ipairs(crafting_stations_configs_json) do | for _, config in ipairs(crafting_stations_configs_json) do | ||
Line 38: | Line 54: | ||
-- Searches for station configs that have given recipe ID in their recipes. | -- Searches for station configs that have given recipe ID in their recipes. | ||
-- Optionally takes in a crafting method to narrow down the results. | |||
-- | -- | ||
-- Returns an array of results, each containing: | -- Returns an array of results, each containing: | ||
Line 46: | Line 63: | ||
-- | -- | ||
-- **Note: This is an expensive function.** | -- **Note: This is an expensive function.** | ||
function p.search_recipe_id(recipe_id_query) | function p.search_recipe_id(recipe_id_query, crafting_method_query) | ||
assert_value_not_nil(recipe_id_query) | |||
local res = {} | local res = {} | ||
for _, cfg in ipairs( | local config_pool = crafting_method_query | ||
and { p.find_crafting_station_config(crafting_method_query) } | |||
or crafting_stations_configs_json | |||
for _, cfg in ipairs(config_pool) do | |||
for _, availabilityPropName in ipairs(p.availability_prop_names) do | for _, availabilityPropName in ipairs(p.availability_prop_names) do | ||
local availability = p.map_of_availability_prop_name_to_availability[availabilityPropName] | local availability = p.map_of_availability_prop_name_to_availability[availabilityPropName] |
Revision as of 06:18, 9 July 2025
Module documentation
|
---|
View or edit this documentation • (about module documentation) |
Uses JSON data
This module uses JSON data pages:
Contains various methods related to crafting. Currently, this only covers item recipes using lathes.
JSON files
JSON files that are updated automatically, syncing with the upstream:
- Module:Crafting/data/auto/crafting_stations_configs.json - contains configs for each crafting station - things like crafting time/resource modifiers, as well as producible IDs of recipes, grouped by availability.
local p = {}
local crafting_stations_configs_json = mw.loadJsonData("Module:Crafting/data/auto/crafting_stations_configs.json")
-- an array of property names containing recipes in station configs.
p.availability_prop_names = {
"staticRecipes",
"dynamicRecipes",
"emagStaticRecipes",
"emagDynamicRecipes",
}
-- maps availability property names to availability.
p.map_of_availability_prop_name_to_availability = {
["staticRecipes"] = "static",
["dynamicRecipes"] = "dynamic",
["emagStaticRecipes"] = "emag static",
["emagDynamicRecipes"] = "emag dynamic",
}
-- ===========================================
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
-- ===========================================
-- Searches for a station config with given ID.
-- Any casing is supported.
--
-- Returns found config or `nil`.
function p.find_crafting_station_config(id)
assert_value_not_nil(id)
local id_lc = string.lower(id)
for _, config in ipairs(crafting_stations_configs_json) do
if string.lower(config.id) == id_lc then
return config
end
end
end
-- Returns all existing configs on crafting stations.
function p.get_all_crafting_stations_configs()
return crafting_stations_configs_json
end
-- Searches for station configs that have given recipe ID in their recipes.
-- Optionally takes in a crafting method to narrow down the results.
--
-- Returns an array of results, each containing:
-- * `station_type` - crafting station type.
-- * `station_id` - crafting station ID.
-- * `availability` - recipe availability, one of: `static`, `dynamic`, `emag static` or `emag dynamic`.
-- * `recipe_id` - recipe ID.
--
-- **Note: This is an expensive function.**
function p.search_recipe_id(recipe_id_query, crafting_method_query)
assert_value_not_nil(recipe_id_query)
local res = {}
local config_pool = crafting_method_query
and { p.find_crafting_station_config(crafting_method_query) }
or crafting_stations_configs_json
for _, cfg in ipairs(config_pool) do
for _, availabilityPropName in ipairs(p.availability_prop_names) do
local availability = p.map_of_availability_prop_name_to_availability[availabilityPropName]
local recipe_ids = cfg[availabilityPropName]
if recipe_ids ~= nil then
for _, recipe_id in ipairs(recipe_ids) do
if recipe_id == recipe_id_query then
table.insert(res, {
station_type = cfg.stationType,
station_id = cfg.id,
availability = availability,
recipe_id = recipe_id
})
end
end
end
end
end
return res
end
return p