SHARE
TWEET

TurtleDigitServerUserCount

fatboychummy May 27th, 2020 (edited) 1,079 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local td = require "turtledigit"
  2.  
  3. local function main()
  4.   local link = require "link"
  5.  
  6.   local tPast = {toggle = 3}
  7.   local iWaitTime = 60
  8.   local iWaitInMins = iWaitTime / 60
  9.   local iNum = "0"
  10.   local iFails = 0
  11.  
  12.   local function loadData(sFilename)
  13.     local h = io.open(sFilename, 'r')
  14.     if not h then
  15.       return {toggle = 3}
  16.     else
  17.       local sTmp = textutils.unserialize(h:read("*a"))
  18.       h:close()
  19.       return sTmp
  20.     end
  21.   end
  22.  
  23.   local function saveData(sFilename, sData)
  24.     local h = io.open(sFilename, 'w')
  25.     if not h then
  26.       error("Failed to open file for writing.")
  27.     else
  28.       h:write(textutils.serialize(sData)):close()
  29.     end
  30.   end
  31.  
  32.   local function wait(iSeconds)
  33.     iSeconds = math.floor(iSeconds)
  34.     local x, y = term.getCursorPos()
  35.     local mx, _ = term.getSize()
  36.     local sClearLine = string.rep(' ', mx - x)
  37.     local function wrt(sData, bClear)
  38.       if not bClear then
  39.         wrt(sClearLine, true)
  40.       end
  41.       term.setCursorPos(x, y)
  42.       term.write(tostring(sData))
  43.     end
  44.  
  45.     for i = iSeconds, 1, -1 do
  46.       wrt(i)
  47.       os.sleep(1)
  48.     end
  49.     wrt(0)
  50.     print()
  51.   end
  52.  
  53.   -- main control loop
  54.   tPast = loadData("/PAST.dat")
  55.   while true do
  56.     if tPast.toggle >= 3 then
  57.       -- get the invite
  58.       local h = http.get(link)
  59.       if h then
  60.         -- read the invite if we acquired it.
  61.         local sData = h.readAll()
  62.         h.close()
  63.  
  64.         -- get the number of users from the invite data
  65.         local sNewNum = table.concat({sData:match("with (%d+),?(%d+)")})
  66.  
  67.         -- if it was valid
  68.         if sNewNum ~= nil then
  69.           -- check which digits changed (if any)
  70.           local sOld = string.rep(' ', 4 - #iNum) .. iNum
  71.           local sNew = string.rep(' ', 4 - #sNewNum) .. sNewNum
  72.           local sDiff = ""
  73.           local bChanged = false
  74.           for i = 1, 4 do
  75.             local c1 = sOld:sub(i, i)
  76.             local c2 = sNew:sub(i, i)
  77.             sDiff = sDiff .. (c1 == c2 and " " or "^")
  78.             bChanged = bChanged or c1 ~= c2
  79.  
  80.             -- send the new digit, even if unchanged.  The turtles will know if they changed or not.
  81.             td.send(i, sNew:sub(i, i))
  82.           end
  83.  
  84.           -- send info
  85.           print()
  86.           print(string.format("Current: %s", sOld))
  87.           print(string.format("New    : %s", sNew))
  88.           print(string.format("Diff   : %s", sDiff))
  89.  
  90.           -- set the old value to the current value if changed
  91.           if bChanged then
  92.             iNum = sNewNum
  93.           end
  94.  
  95.           -- save the value recieved
  96.           table.insert(tPast, 1, tonumber(sNewNum))
  97.           if #tPast > iWaitInMins * 720 then -- if there is more records than one day, delete the extra record.
  98.             table.remove(tPast) -- pop
  99.           end
  100.           saveData("/PAST.dat", tPast)
  101.  
  102.           -- reset the fail counter
  103.           iFails = 0
  104.         elseif sNewNum == nil then
  105.           -- if it is invalid, increase failures
  106.           iFails = iFails + 1
  107.         end
  108.       else
  109.         -- if we failed to acquire the invite link, increase failures
  110.         iFails = iFails + 1
  111.         print("Failed to get count")
  112.       end
  113.  
  114.       -- error if we failed 10 times
  115.       if iFails > 10 then
  116.         error("Failed over ten times to update.", 0)
  117.       end
  118.       print("Waiting...")
  119.       wait(iWaitTime)
  120.     elseif tPast.toggle == 2 then
  121.       print("Show Dif")
  122.       local iLast  = tPast[#tPast]
  123.       local iFirst = tPast[1]
  124.       print(string.format("%d (old) vs %d (new)", iLast, iFirst))
  125.       local iColor = 5 -- if first == last, color is blue
  126.       local sDif   = tostring(math.abs(iFirst - iLast))
  127.       local sRep   = string.rep(' ', 4 - (#sDif + 1))
  128.       local sOp    = iFirst == iLast and "=" or iFirst < iLast and "DN" or "UP"
  129.       local sOut   = sRep .. "%" .. sDif
  130.       if iFirst > iLast then
  131.         iColor = 4 -- if first is greater, color is green
  132.       elseif iFirst < iLast then
  133.         iColor = 3 -- if first is less, color is red
  134.       end
  135.       for i = 1, 4 do
  136.         if sOut:sub(i, i) == "%" then
  137.           td.send(i, sOp, iColor)
  138.         else
  139.           td.send(i, sOut:sub(i, i), iColor)
  140.         end
  141.       end
  142.       wait(iWaitTime / 2)
  143.     elseif tPast.toggle == 1 then
  144.       print("Redisplay")
  145.       td.sendWord(tostring(tPast[1]), 2)
  146.       wait(iWaitTime / 2)
  147.     end
  148.     tPast.toggle = tPast.toggle - 1
  149.     if tPast.toggle < 1 then
  150.       tPast.toggle = 3
  151.     end
  152.   end
  153. end
  154.  
  155. local ok, err = pcall(main)
  156.  
  157. if not ok then
  158.   printError(err)
  159.   local ecode
  160.   os.sleep(3) -- allow turtles to finish redrawing if just redrawn.
  161.   if err == "Terminated" then
  162.     td.sendWord("E000", 3)
  163.     return
  164.   elseif err == "Failed over ten times to update." then
  165.     td.sendWord("E001", 3)
  166.   else
  167.     td.sendWord("E???", 3)
  168.   end
  169. end
  170. os.sleep(30)
  171. os.reboot()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top