feat: delegate recipe helper functions to shared lib/ui.lua for improved code organization
This commit is contained in:
@@ -42,7 +42,6 @@ end
|
|||||||
|
|
||||||
-- Override dofile to load modules into our _ENV so they inherit
|
-- Override dofile to load modules into our _ENV so they inherit
|
||||||
-- Opus's require/package (CC:Tweaked dofile uses _G instead).
|
-- Opus's require/package (CC:Tweaked dofile uses _G instead).
|
||||||
local _ccDofile = dofile
|
|
||||||
local function dofile(path) -- luacheck: ignore
|
local function dofile(path) -- luacheck: ignore
|
||||||
local fn, err = loadfile(path, nil, _ENV)
|
local fn, err = loadfile(path, nil, _ENV)
|
||||||
if fn then return fn()
|
if fn then return fn()
|
||||||
@@ -56,6 +55,7 @@ local CLIENT_CONFIG_FILE = _configPath(".client_config")
|
|||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
local log = dofile(_path("lib/log.lua"))
|
local log = dofile(_path("lib/log.lua"))
|
||||||
|
local ui = dofile(_path("lib/ui.lua"))
|
||||||
|
|
||||||
local function loadConfig()
|
local function loadConfig()
|
||||||
if not fs.exists(CLIENT_CONFIG_FILE) then return end
|
if not fs.exists(CLIENT_CONFIG_FILE) then return end
|
||||||
@@ -184,46 +184,11 @@ local function getItemTotal(itemName)
|
|||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
function ops.getRecipeIngredients(recipe)
|
-- Delegate recipe helpers to shared lib/ui.lua with local stock lookup.
|
||||||
local ingredients = {}
|
function ops.getRecipeIngredients(recipe) return ui.getRecipeIngredients(recipe) end
|
||||||
for _, item in ipairs(recipe.grid) do
|
function ops.canCraftRecipe(recipe) return ui.canCraftRecipe(recipe, getItemTotal) end
|
||||||
if item then
|
function ops.maxCraftBatches(recipe) return ui.maxCraftBatches(recipe, getItemTotal) end
|
||||||
ingredients[item] = (ingredients[item] or 0) + 1
|
function ops.getMissingIngredients(recipe) return ui.getMissingIngredients(recipe, getItemTotal) end
|
||||||
end
|
|
||||||
end
|
|
||||||
return ingredients
|
|
||||||
end
|
|
||||||
|
|
||||||
function ops.canCraftRecipe(recipe)
|
|
||||||
local ingredients = ops.getRecipeIngredients(recipe)
|
|
||||||
for itemName, needed in pairs(ingredients) do
|
|
||||||
if (getItemTotal(itemName) or 0) < needed then return false end
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function ops.maxCraftBatches(recipe)
|
|
||||||
local ingredients = ops.getRecipeIngredients(recipe)
|
|
||||||
local minBatches = math.huge
|
|
||||||
for itemName, needed in pairs(ingredients) do
|
|
||||||
local batches = math.floor((getItemTotal(itemName) or 0) / needed)
|
|
||||||
if batches < minBatches then minBatches = batches end
|
|
||||||
end
|
|
||||||
if minBatches == math.huge then return 0 end
|
|
||||||
return minBatches
|
|
||||||
end
|
|
||||||
|
|
||||||
function ops.getMissingIngredients(recipe)
|
|
||||||
local ingredients = ops.getRecipeIngredients(recipe)
|
|
||||||
local missing = {}
|
|
||||||
for itemName, needed in pairs(ingredients) do
|
|
||||||
local have = getItemTotal(itemName) or 0
|
|
||||||
if have < needed then
|
|
||||||
table.insert(missing, { name = itemName, have = have, need = needed })
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return missing
|
|
||||||
end
|
|
||||||
|
|
||||||
function ops.orderItem(itemName, amount)
|
function ops.orderItem(itemName, amount)
|
||||||
sendToMaster({
|
sendToMaster({
|
||||||
|
|||||||
Reference in New Issue
Block a user