Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- modem = require("component").modem
- redstone = require("component").redstone
- term = require("term")
- serialization = require("serialization")
- event = require("event")
- insertSortObj = require("insertSortObj")
- sides = require("sides")
- items = {}
- function main()
- on = true
- _, vHeight = term.getViewport()
- event.listen("key_up", handleKeyUp)
- event.listen("touch", handleTouch)
- if modem.isOpen(30) == false then
- modem.open(30)
- end
- prevText = getHistory()
- prevText = serialization.unserialize(prevText)
- while on do
- _, _, _, _, signal, msg = event.pull('modem')
- slots = serialization.unserialize(msg)
- items = collectUniqItems(slots)
- items = insertSortObj(items, "label")
- printItems(items, prevText)
- term.write("----------")
- term.setCursor(0, 0)
- end
- end
- function handleKeyUp(_, _, btn)
- if btn == 3 then
- on = false
- event.ignore("key_up", handleKeyUp)
- event.ignore("touch", handleTouch)
- end
- if btn == 32 then
- redstone.setOutput(sides.down, 15)
- redstone.setOutput(sides.down, 0)
- end
- if btn == 115 then
- text = serialization.serialize(items)
- file = io.open("logs/log_mining_" .. os.time(), "w")
- file:write(text)
- file:close()
- file = io.open("logs/prev_mining", "w")
- file:write(text)
- file:close()
- end
- end
- function handleTouch()
- redstone.setOutput(sides.down, 15)
- redstone.setOutput(sides.down, 0)
- end
- function collectUniqItems(slots)
- items = {}
- results = {}
- -- make hash table
- for k,v in pairs(slots) do
- if not items[v.label] then items[v.label] = 0 end
- items[v.label] = items[v.label] + v.size
- end
- -- back to obj
- i = 0
- for k, v in pairs(items) do
- i = i + 1
- results[i] = {label = k, size = v}
- end
- return results
- end
- function printItems(items, prevText)
- i = 0
- while i < vHeight do
- i = i + 1
- term.setCursor(1, i)
- term.clearLine()
- if items[i] then
- term.write(items[i].label)
- term.setCursor(40, i)
- term.write(items[i].size)
- end
- if prevText[i] then
- term.setCursor(70, i)
- term.write(prevText[i].label)
- term.setCursor(110, i)
- term.write(prevText[i].size)
- end
- end
- end
- function getHistory()
- file = io.open("logs/prev_mining", "r")
- text = file:read()
- file:close()
- return text
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement