feat(tunnels-miner): add onWaitCb on waitForInventory util + prepare for notification

This commit is contained in:
Guillaume ARM 2024-05-26 19:44:20 +02:00
parent d257aa382c
commit 98bf2b2f31
2 changed files with 30 additions and 8 deletions

View File

@ -3,6 +3,8 @@ local turtleUtils = {}
local DEFAULT_IDLE_TIME = 2 local DEFAULT_IDLE_TIME = 2
local DEFAULT_MIN_FUEL_NEEDED = 1000 local DEFAULT_MIN_FUEL_NEEDED = 1000
local function noop() end
local function isTurtleInventoryFull() local function isTurtleInventoryFull()
for i=1, 16, 1 do for i=1, 16, 1 do
if turtle.getItemCount(i) == 0 then if turtle.getItemCount(i) == 0 then
@ -86,9 +88,19 @@ local function waitFor(predicate, sleepTime)
end end
end end
turtleUtils.waitForInventory = function(side, sleepTime) turtleUtils.waitForInventory = function(side, sleepTime, onWaitCb)
onWaitCb = onWaitCb or noop
local counter = 0
return waitFor(function() return waitFor(function()
return turtleUtils.getInventory(side) local res = turtleUtils.getInventory(side)
counter = counter + 1
if counter == 2 then
onWaitCb()
end
return res
end, sleepTime) end, sleepTime)
end end

View File

@ -21,6 +21,10 @@ local MOVES_BY_DIRECTION = {
local function noop() end local function noop() end
local function getComputerLabel()
return os.getComputerLabel() or 'Unknown miner'
end
local function getMoves(config) local function getMoves(config)
return MOVES_BY_DIRECTION[config.DIRECTION] return MOVES_BY_DIRECTION[config.DIRECTION]
end end
@ -47,8 +51,13 @@ local function checkEnoughFuelForTunnel(config)
return true return true
end end
local function assertEnoughFuelForTunnel(config) local function assertEnoughFuelForTunnel(config, withNotif)
if not checkEnoughFuelForTunnel(config) then if not checkEnoughFuelForTunnel(config) then
if withNotif then
local message = 'Il n\'y a plus assez de fuel sur "' .. getComputerLabel() .. '"'
print(message)
-- TODO: notify.sendChatMessage(o)
end
error('not enough fuel', 0) error('not enough fuel', 0)
end end
end end
@ -145,8 +154,6 @@ local function ensureEnoughSpaceInInventory(config)
-- 3. and drop the selected item -- 3. and drop the selected item
turtle.drop() turtle.drop()
trySelectEmptySlot() trySelectEmptySlot()
else
-- TODO: handle when cannot drop anything (monitoring over network ?)
end end
end end
@ -233,7 +240,11 @@ local function dropAllProcedure(config)
local count = turtle.getItemCount(i) local count = turtle.getItemCount(i)
if count > 0 then if count > 0 then
turtleUtils.waitForInventory('front', IDLE_TIME) turtleUtils.waitForInventory('front', IDLE_TIME, function()
local message = getComputerLabel() .. ' attends un inventaire pour continuer le minage'
print(message)
-- TODO: notify.sendChatMessage(o)
end)
while turtle.getItemCount(i) and not turtleUtils.dropSlot(i, turtle.drop) do while turtle.getItemCount(i) and not turtleUtils.dropSlot(i, turtle.drop) do
os.sleep(IDLE_TIME) os.sleep(IDLE_TIME)
@ -242,7 +253,6 @@ local function dropAllProcedure(config)
end end
getMoves(config).turnLeft() getMoves(config).turnLeft()
-- turtleUtils.forceUp()
end end
local function goToNextMineProcedure(config) local function goToNextMineProcedure(config)
@ -276,7 +286,7 @@ local function main(config)
while true do while true do
if tunnelNumber > 1 then if tunnelNumber > 1 then
printFuelReport(config) printFuelReport(config)
assertEnoughFuelForTunnel(config) assertEnoughFuelForTunnel(config, true)
end end
print('> Start mining tunnel number ' .. tostring(tunnelNumber)) print('> Start mining tunnel number ' .. tostring(tunnelNumber))