feat(tunnels-miner): add onWaitCb on waitForInventory util + prepare for notification
This commit is contained in:
parent
d257aa382c
commit
98bf2b2f31
@ -3,6 +3,8 @@ local turtleUtils = {}
|
||||
local DEFAULT_IDLE_TIME = 2
|
||||
local DEFAULT_MIN_FUEL_NEEDED = 1000
|
||||
|
||||
local function noop() end
|
||||
|
||||
local function isTurtleInventoryFull()
|
||||
for i=1, 16, 1 do
|
||||
if turtle.getItemCount(i) == 0 then
|
||||
@ -86,9 +88,19 @@ local function waitFor(predicate, sleepTime)
|
||||
end
|
||||
end
|
||||
|
||||
turtleUtils.waitForInventory = function(side, sleepTime)
|
||||
turtleUtils.waitForInventory = function(side, sleepTime, onWaitCb)
|
||||
onWaitCb = onWaitCb or noop
|
||||
local counter = 0
|
||||
|
||||
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
|
||||
|
||||
|
||||
@ -21,6 +21,10 @@ local MOVES_BY_DIRECTION = {
|
||||
|
||||
local function noop() end
|
||||
|
||||
local function getComputerLabel()
|
||||
return os.getComputerLabel() or 'Unknown miner'
|
||||
end
|
||||
|
||||
local function getMoves(config)
|
||||
return MOVES_BY_DIRECTION[config.DIRECTION]
|
||||
end
|
||||
@ -47,8 +51,13 @@ local function checkEnoughFuelForTunnel(config)
|
||||
return true
|
||||
end
|
||||
|
||||
local function assertEnoughFuelForTunnel(config)
|
||||
local function assertEnoughFuelForTunnel(config, withNotif)
|
||||
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)
|
||||
end
|
||||
end
|
||||
@ -145,8 +154,6 @@ local function ensureEnoughSpaceInInventory(config)
|
||||
-- 3. and drop the selected item
|
||||
turtle.drop()
|
||||
trySelectEmptySlot()
|
||||
else
|
||||
-- TODO: handle when cannot drop anything (monitoring over network ?)
|
||||
end
|
||||
end
|
||||
|
||||
@ -233,7 +240,11 @@ local function dropAllProcedure(config)
|
||||
local count = turtle.getItemCount(i)
|
||||
|
||||
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
|
||||
os.sleep(IDLE_TIME)
|
||||
@ -242,7 +253,6 @@ local function dropAllProcedure(config)
|
||||
end
|
||||
|
||||
getMoves(config).turnLeft()
|
||||
-- turtleUtils.forceUp()
|
||||
end
|
||||
|
||||
local function goToNextMineProcedure(config)
|
||||
@ -276,7 +286,7 @@ local function main(config)
|
||||
while true do
|
||||
if tunnelNumber > 1 then
|
||||
printFuelReport(config)
|
||||
assertEnoughFuelForTunnel(config)
|
||||
assertEnoughFuelForTunnel(config, true)
|
||||
end
|
||||
|
||||
print('> Start mining tunnel number ' .. tostring(tunnelNumber))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user