Compare commits
3 Commits
641a317873
...
4cf1e550b7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4cf1e550b7 | ||
|
|
66ac81de65 | ||
|
|
ea29136f25 |
@@ -42,7 +42,6 @@ end
|
||||
|
||||
-- Override dofile to load modules into our _ENV so they inherit
|
||||
-- Opus's require/package (CC:Tweaked dofile uses _G instead).
|
||||
local _ccDofile = dofile
|
||||
local function dofile(path) -- luacheck: ignore
|
||||
local fn, err = loadfile(path, nil, _ENV)
|
||||
if fn then return fn()
|
||||
@@ -56,6 +55,7 @@ local CLIENT_CONFIG_FILE = _configPath(".client_config")
|
||||
-------------------------------------------------
|
||||
|
||||
local log = dofile(_path("lib/log.lua"))
|
||||
local ui = dofile(_path("lib/ui.lua"))
|
||||
|
||||
local function loadConfig()
|
||||
if not fs.exists(CLIENT_CONFIG_FILE) then return end
|
||||
@@ -184,46 +184,11 @@ local function getItemTotal(itemName)
|
||||
return 0
|
||||
end
|
||||
|
||||
function ops.getRecipeIngredients(recipe)
|
||||
local ingredients = {}
|
||||
for _, item in ipairs(recipe.grid) do
|
||||
if item then
|
||||
ingredients[item] = (ingredients[item] or 0) + 1
|
||||
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
|
||||
-- Delegate recipe helpers to shared lib/ui.lua with local stock lookup.
|
||||
function ops.getRecipeIngredients(recipe) return ui.getRecipeIngredients(recipe) end
|
||||
function ops.canCraftRecipe(recipe) return ui.canCraftRecipe(recipe, getItemTotal) end
|
||||
function ops.maxCraftBatches(recipe) return ui.maxCraftBatches(recipe, getItemTotal) end
|
||||
function ops.getMissingIngredients(recipe) return ui.getMissingIngredients(recipe, getItemTotal) end
|
||||
|
||||
function ops.orderItem(itemName, amount)
|
||||
sendToMaster({
|
||||
|
||||
@@ -39,7 +39,6 @@ local ok, err = xpcall(function()
|
||||
|
||||
-- Override dofile to load modules into our _ENV so they inherit
|
||||
-- Opus's require/package (CC:Tweaked dofile uses _G instead).
|
||||
local _ccDofile = dofile
|
||||
local function dofile(path) -- luacheck: ignore
|
||||
local fn, err = loadfile(path, nil, _ENV)
|
||||
if fn then return fn()
|
||||
@@ -148,9 +147,12 @@ local function broadcastState()
|
||||
-- Keep ctx in sync so display.lua can check ctx.craftTurtleOk directly
|
||||
ctx.craftTurtleOk = payload.craftTurtleOk
|
||||
|
||||
payload.smeltable = cfg.SMELTABLE
|
||||
payload.craftable = cfg.CRAFTABLE
|
||||
state.configDirty = false
|
||||
-- Only include recipe tables when config has changed (they're large).
|
||||
if state.configDirty then
|
||||
payload.smeltable = cfg.SMELTABLE
|
||||
payload.craftable = cfg.CRAFTABLE
|
||||
state.configDirty = false
|
||||
end
|
||||
|
||||
ctx.networkModem.transmit(cfg.BROADCAST_CHANNEL, cfg.ORDER_CHANNEL, payload)
|
||||
state.lastBroadcastVersion = state.stateVersion
|
||||
|
||||
@@ -102,6 +102,7 @@ local function httpPost(path, body)
|
||||
if ok then
|
||||
return result
|
||||
end
|
||||
print(string.format("[ERR] HTTP POST %s: %s", path, tostring(result)))
|
||||
return nil
|
||||
end
|
||||
|
||||
@@ -121,6 +122,7 @@ local function httpGet(path)
|
||||
if ok then
|
||||
return result
|
||||
end
|
||||
print(string.format("[ERR] HTTP GET %s: %s", path, tostring(result)))
|
||||
return nil
|
||||
end
|
||||
|
||||
@@ -255,13 +257,16 @@ local function modemListener()
|
||||
local resultType = message.type
|
||||
if resultType == "order_result" or resultType == "craft_result"
|
||||
or resultType == "recursive_craft_result" or resultType == "find_item_result" then
|
||||
pcall(httpPost, "/api/bridge/result", {
|
||||
local fwdOk, fwdErr = pcall(httpPost, "/api/bridge/result", {
|
||||
action = resultType,
|
||||
commandId = message.commandId,
|
||||
success = message.success,
|
||||
message = message.message,
|
||||
error = message.error,
|
||||
})
|
||||
if not fwdOk then
|
||||
print(string.format("[ERR] Forward result %s: %s", resultType, tostring(fwdErr)))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -272,7 +277,7 @@ local function stateForwarder()
|
||||
while running do
|
||||
local ok, err = pcall(forwardState)
|
||||
if not ok then
|
||||
-- Connection error, will retry
|
||||
print(string.format("[ERR] State forward: %s", tostring(err)))
|
||||
end
|
||||
sleep(STATE_INTERVAL)
|
||||
end
|
||||
@@ -291,7 +296,10 @@ local function commandPoller()
|
||||
for _, cmd in ipairs(result.commands) do
|
||||
local cmdId = cmd.id or 0
|
||||
if cmdId > lastProcessedId then
|
||||
pcall(processCommand, cmd)
|
||||
local cmdOk, cmdErr = pcall(processCommand, cmd)
|
||||
if not cmdOk then
|
||||
print(string.format("[ERR] Process cmd %s: %s", tostring(cmd.action), tostring(cmdErr)))
|
||||
end
|
||||
if cmdId > maxId then maxId = cmdId end
|
||||
end
|
||||
end
|
||||
@@ -302,6 +310,9 @@ local function commandPoller()
|
||||
end
|
||||
end
|
||||
end)
|
||||
if not ok then
|
||||
print(string.format("[ERR] Command poll: %s", tostring(err)))
|
||||
end
|
||||
sleep(POLL_INTERVAL)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user