Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local td = require "turtledigit"
- local function main()
- local link = require "link"
- local tPast = {toggle = 3}
- local iWaitTime = 60
- local iWaitInMins = iWaitTime / 60
- local iNum = "0"
- local iFails = 0
- local function loadData(sFilename)
- local h = io.open(sFilename, 'r')
- if not h then
- return {toggle = 3}
- else
- local sTmp = textutils.unserialize(h:read("*a"))
- h:close()
- return sTmp
- end
- end
- local function saveData(sFilename, sData)
- local h = io.open(sFilename, 'w')
- if not h then
- error("Failed to open file for writing.")
- else
- h:write(textutils.serialize(sData)):close()
- end
- end
- local function wait(iSeconds)
- iSeconds = math.floor(iSeconds)
- local x, y = term.getCursorPos()
- local mx, _ = term.getSize()
- local sClearLine = string.rep(' ', mx - x)
- local function wrt(sData, bClear)
- if not bClear then
- wrt(sClearLine, true)
- end
- term.setCursorPos(x, y)
- term.write(tostring(sData))
- end
- for i = iSeconds, 1, -1 do
- wrt(i)
- os.sleep(1)
- end
- wrt(0)
- print()
- end
- -- main control loop
- tPast = loadData("/PAST.dat")
- while true do
- if tPast.toggle >= 3 then
- -- get the invite
- local h = http.get(link)
- if h then
- -- read the invite if we acquired it.
- local sData = h.readAll()
- h.close()
- -- get the number of users from the invite data
- local sNewNum = table.concat({sData:match("with (%d+),?(%d+)")})
- -- if it was valid
- if sNewNum ~= nil then
- -- check which digits changed (if any)
- local sOld = string.rep(' ', 4 - #iNum) .. iNum
- local sNew = string.rep(' ', 4 - #sNewNum) .. sNewNum
- local sDiff = ""
- local bChanged = false
- for i = 1, 4 do
- local c1 = sOld:sub(i, i)
- local c2 = sNew:sub(i, i)
- sDiff = sDiff .. (c1 == c2 and " " or "^")
- bChanged = bChanged or c1 ~= c2
- -- send the new digit, even if unchanged. The turtles will know if they changed or not.
- td.send(i, sNew:sub(i, i))
- end
- -- send info
- print()
- print(string.format("Current: %s", sOld))
- print(string.format("New : %s", sNew))
- print(string.format("Diff : %s", sDiff))
- -- set the old value to the current value if changed
- if bChanged then
- iNum = sNewNum
- end
- -- save the value recieved
- table.insert(tPast, 1, tonumber(sNewNum))
- if #tPast > iWaitInMins * 720 then -- if there is more records than one day, delete the extra record.
- table.remove(tPast) -- pop
- end
- saveData("/PAST.dat", tPast)
- -- reset the fail counter
- iFails = 0
- elseif sNewNum == nil then
- -- if it is invalid, increase failures
- iFails = iFails + 1
- end
- else
- -- if we failed to acquire the invite link, increase failures
- iFails = iFails + 1
- print("Failed to get count")
- end
- -- error if we failed 10 times
- if iFails > 10 then
- error("Failed over ten times to update.", 0)
- end
- print("Waiting...")
- wait(iWaitTime)
- elseif tPast.toggle == 2 then
- print("Show Dif")
- local iLast = tPast[#tPast]
- local iFirst = tPast[1]
- print(string.format("%d (old) vs %d (new)", iLast, iFirst))
- local iColor = 5 -- if first == last, color is blue
- local sDif = tostring(math.abs(iFirst - iLast))
- local sRep = string.rep(' ', 4 - (#sDif + 1))
- local sOp = iFirst == iLast and "=" or iFirst < iLast and "DN" or "UP"
- local sOut = sRep .. "%" .. sDif
- if iFirst > iLast then
- iColor = 4 -- if first is greater, color is green
- elseif iFirst < iLast then
- iColor = 3 -- if first is less, color is red
- end
- for i = 1, 4 do
- if sOut:sub(i, i) == "%" then
- td.send(i, sOp, iColor)
- else
- td.send(i, sOut:sub(i, i), iColor)
- end
- end
- wait(iWaitTime / 2)
- elseif tPast.toggle == 1 then
- print("Redisplay")
- td.sendWord(tostring(tPast[1]), 2)
- wait(iWaitTime / 2)
- end
- tPast.toggle = tPast.toggle - 1
- if tPast.toggle < 1 then
- tPast.toggle = 3
- end
- end
- end
- local ok, err = pcall(main)
- if not ok then
- printError(err)
- local ecode
- os.sleep(3) -- allow turtles to finish redrawing if just redrawn.
- if err == "Terminated" then
- td.sendWord("E000", 3)
- return
- elseif err == "Failed over ten times to update." then
- td.sendWord("E001", 3)
- else
- td.sendWord("E???", 3)
- end
- end
- os.sleep(30)
- os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement