summaryrefslogtreecommitdiff
path: root/sorter.lua
blob: 6c50de54df75ddcc91192e348810142af77ed293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local refuel = require("modules.refuel")
local inv_utils = require("modules.inv_utils")

local DATA_PATH = "/data/sorter_data.txt" 


local function runDiagnostics()
	local canStart = true

	print("Running diagnostics...")
	print(string.format("Fuel level: %s", turtle.getFuelLevel()))
	local fuelStorage = peripheral.wrap("front")
	local hasInStorage = refuel.hasFuelInStorage(fuelStorage) and "yes" or "no"
	print(string.format("Fuel in storage: %s", hasInStorage))
	
	local dataExists = fs.exists(DATA_PATH)

	if not dataExists then
		canStart = false
	end

	print(string.format("Data path exists: %s", dataExists and "yes" or "no"))
	
	local hasItemsInUnsorted = inv_utils.hasItemsInStorage(peripheral.wrap("top"))

	if not hasItemsInUnsorted then
		canStart = false
	end

	print(string.format("Items in unsorted storage: %s", hasItemsInUnsorted and "yes" or "no"))

	local hasItemsInLeftover = inv_utils.hasItemsInStorage(peripheral.wrap("bottom"))

	if hasItemsInLeftover then
		canStart = false
	end

	print(string.format("Items in leftover storage: %s", hasItemsInLeftover and "yes" or "no"))
	
	if canStart then
		print("Ready to start sorting.")
	else
		print("Can not start sorting.")
	end

	return canStart
end

local function run()
	local result = runDiagnostics()
	if not result then
		return nil
	end

	write("Start? (y/N) ")
	local response = read()
	local firstChar = string.sub(response, 1, 1)
	if firstChar == "y" or firstChar == "Y" then
		print("Started sorting.")
	else
		print("Aborted.")
		return nil
	end
end

return { run = run }