Advertisement
Zekrommaster110

[LUA | CC1.4.7] Framer - Master Loop

Apr 4th, 2020
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1. -- least time to shut down at
  2. local STOPAT = "05:00"
  3. -- ammount of minutes before shutdown should happen
  4. -- to ensure shut down will not be skipped in loop
  5. -- cycles
  6. local MARGIN = 10
  7. -- check every x loop cycles
  8. local CHECKEVERY = 20
  9.  
  10. -- An API which returns the current time on GET
  11. -- request in the default strftime format:
  12. -- '%A, %d-%h-%G %T %Z'
  13. local TIMEAPI = "https://api.zekro.de/time"
  14.  
  15. -- The POST webhook to send coordinate data to
  16. local POSITION_WEBHOOK = "https://maker.ifttt.com/trigger/framer_moved/with/key/EXAMPLEKEYUSEYOUROWNHERE"
  17. -- The name to be used for the webhook to
  18. -- identify this computer
  19. local POSITION_NAME = os.getComputerLabel() or "default"
  20. -- The direction vector where the framer is headed
  21. -- to to calculate current position after move
  22. local POSITION_MOVE_VEC = vector.new(-1, 0, 0)
  23. -- The file to save the current position to
  24. local POSITION_FILE = "curr.pos"
  25.  
  26. ---------------------------------------------
  27.  
  28. local function parseTime(s)
  29.     time = s:match('%d%d:%d%d')
  30.     h = tonumber(time:sub(1, 2))
  31.     m = tonumber(time:sub(4, 5))
  32.     return h, m
  33. end
  34.  
  35. local function getTime()
  36.     res = http.get(TIMEAPI)
  37.     if not res then
  38.         sleep(0.5)
  39.         return getTime()
  40.     end
  41.     time = res.readLine()
  42.     return parseTime(time)
  43. end
  44.  
  45. local function checkExit(h, m)
  46.     ch, cm = getTime()
  47.     return ch == h and cm >= m-MARGIN and cm <= m
  48. end
  49.  
  50. local function writePositionToFile(vec)
  51.     f = fs.open(POSITION_FILE, "w")
  52.     f.writeLine(vec.x)
  53.     f.writeLine(vec.y)
  54.     f.writeLine(vec.z)
  55.     f.close()
  56. end
  57.  
  58. local function readPositionFromFile()
  59.     f = fs.open(POSITION_FILE, "r")
  60.     vec = vector.new(0, 0, 0)
  61.  
  62.     if f then
  63.         vec.x = f.readLine()
  64.         vec.y = f.readLine()
  65.         vec.z = f.readLine()
  66.         f.close()
  67.     end
  68.  
  69.     return vec
  70. end
  71.  
  72. local function postPosition(name, x, z)
  73.     if not POSITION_WEBHOOK then return end
  74.     http.post(
  75.         string.format(
  76.             "%s?value1=%s&value2=%d&value3=%d",
  77.             POSITION_WEBHOOK,
  78.             name, x, z
  79.         )
  80.     )
  81. end
  82.  
  83. local function pulse(side)
  84.     redstone.setOutput(side, true)
  85.     sleep(0.5)
  86.     redstone.setOutput(side, false)
  87.     sleep(0.5)
  88. end
  89.  
  90. local function moveFrame()
  91.     pulse("bottom")
  92.     sleep(0.2)
  93.     pulse("bottom")
  94.     sleep(0.2)
  95. end
  96.    
  97. local function dig()
  98.     pulse("right")
  99.     sleep(3)
  100.     while redstone.getInput("left") == true do
  101.         print("Waiting")
  102.         event = os.pullEvent("redstone")
  103.         sleep(0.2)
  104.     end
  105. end
  106.  
  107. local hstop, mstop = parseTime(STOPAT)
  108. local currpos = readPositionFromFile()
  109. local count = 0
  110.  
  111. print("Ready")
  112. while true do
  113.     sleep(0.2)
  114.     if redstone.getInput("back") == true then
  115.         print("Performing")
  116.         dig()
  117.         moveFrame()
  118.         currpos = currpos + POSITION_MOVE_VEC
  119.         print("Pos: ", currpos)
  120.         writePositionToFile(currpos)
  121.        
  122.         count = count + 1
  123.         if count >= CHECKEVERY then
  124.             postPosition(POSITION_NAME, currpos.x, currpos.z)
  125.             if checkExit(hstop, mstop) then
  126.                 print("Scheduled Stop")
  127.                 return
  128.             end
  129.         end
  130.     end
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement