drpepper240

Timer (ticks only while the chunk is loaded)

Oct 13th, 2021 (edited)
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.83 KB | None | 0 0
  1. local monitorSide = "left"
  2. local refreshInterval = 5 --seconds
  3.  
  4. function secondsToHMS(seconds, leadingZero)
  5.     local h = math.floor(seconds / 3600)
  6.     local m = math.floor(seconds % 3600 / 60)
  7.     local s = seconds % 60
  8.     if leadingZero then
  9.         h = tostring(h)
  10.         m = tostring(m)
  11.         s = tostring(s)
  12.         if string.len(h) < 2 then
  13.             h = "0"..h
  14.         end
  15.         if string.len(m) < 2 then
  16.             m = "0"..m
  17.         end
  18.         if string.len(s) < 2 then
  19.             s = "0"..s
  20.         end
  21.     end
  22.     return h, m, s
  23. end
  24.  
  25. function getIngameEpochAlternative()
  26.     --ingame days in hours plus ingame day hours converted ( * 3600 / 72 ) to real-world millis
  27.     return ( os.time("ingame") + os.day("ingame") * 24 ) * 50 * 1000
  28. end
  29.  
  30. term.clear()
  31. term.setCursorPos(1,1)
  32. local saveData = {}
  33. local utcStarted = os.epoch("utc")
  34. local ingameStarted = getIngameEpochAlternative()
  35. saveData["utcResumed"] = 0
  36. saveData["ingameResumed"] = 0
  37. local file = fs.open("timer.save","r")
  38. if file ~= nil then
  39.     local data = file.readAll()
  40.     file.close()
  41.     saveData = textutils.unserialize(data)
  42.     local h, m, s = secondsToHMS(math.floor(saveData["utcResumed"]/1000), true)
  43.     print(h .. ":" .. m .. ":" .. s .. " since the timer was started, resuming...")
  44. else
  45.     file = fs.open("timer.save","w")
  46.     file.write(textutils.serialize(saveData))
  47.     file.close()
  48. end
  49. local utcResumed = saveData["utcResumed"]
  50. local ingameResumed = saveData["ingameResumed"]
  51. term.setCursorPos(1,3)
  52. term.write("UTC:")
  53. term.setCursorPos(1,5)
  54. term.write("ingame:")
  55. term.setCursorPos(1,17)
  56. term.write("[R] - Reset")
  57.  
  58. local monitor = peripheral.wrap(monitorSide)
  59. if monitor~=nil then
  60.     monitor.setTextColor(colors.black)
  61.     monitor.setBackgroundColor(colors.orange)
  62. end
  63.  
  64. while true do
  65.     --print time(s)
  66.     local utc = os.epoch("utc") - utcStarted + utcResumed
  67.     local ingame = getIngameEpochAlternative() - ingameStarted + ingameResumed
  68.    
  69.     term.setCursorPos(1,4)
  70.     local h, m, s = secondsToHMS(math.floor(utc/1000), true)
  71.     term.write(h .. ":" .. m .. ":" .. s)
  72.    
  73.     term.setCursorPos(1,6)
  74.     local h, m, s = secondsToHMS(math.floor(ingame/1000), true)
  75.     term.write(h .. ":" .. m .. ":" .. s)
  76.    
  77.     if monitor~=nil then
  78.         monitor.clear()
  79.         monitor.setTextScale(5)
  80.         monitor.setCursorPos(1,1)
  81.         monitor.write(h .. ":" .. m)
  82.     end
  83.    
  84.     --update saveData
  85.     saveData["utcResumed"] = utc
  86.     saveData["ingameResumed"] = ingame
  87.     file = fs.open("timer.save","w")
  88.     file.write(textutils.serialize(saveData))
  89.     file.close()
  90.    
  91.     --start refresh timer
  92.     local timer = os.startTimer(refreshInterval)
  93.     local event, b, c, d = os.pullEvent()
  94.     os.cancelTimer(timer)
  95.    
  96.     --listen for events (timer, key press, ...)
  97.     if event == "key" then
  98.         if keys.getName(b) == "r" then
  99.             utcStarted = os.epoch("utc")
  100.             ingameStarted = getIngameEpochAlternative()
  101.             utcResumed = 0
  102.             ingameResumed = 0
  103.             term.setCursorPos(1,1)
  104.             term.clearLine()
  105.         end
  106.     end
  107. end
Add Comment
Please, Sign In to add comment