Dimencia

Clock67

Jul 21st, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1.  
  2.  
  3. function getDate()
  4. local result = http.get("http://worldtimeapi.org/api/timezone/America/New_York.txt").readAll() -- This is a synchronous request. Seems easier. Change to async if necessary
  5. print(result)
  6. -- Third line...
  7. lines = {}
  8. for s in result:gmatch("[^\r\n]+") do
  9. table.insert(lines, s)
  10. end
  11. local date = lines[3] -- LUA starts at 1 index...
  12. -- Now we have: datetime: 2020-07-21T00:07:29.748572-04:00
  13. datePieces = {}
  14. for s in date:gmatch("[^ ]+") do
  15. table.insert(datePieces, s)
  16. end
  17. return datePieces[2]; -- Should just get the ISO date bit
  18. end
  19.  
  20.  
  21. -- This is super simple and had 0 upvotes, but, I don't need much. I don't care about timezone or anything.
  22.  
  23. -- Parse date given in any of supported forms.
  24.  
  25. -- Note! For unrecognised format will return now.
  26.  
  27. -- @param str ISO date. Formats:
  28. -- Y-m-d
  29. -- Y-m -- this will assume January
  30. -- Y -- this will assume 1st January
  31.  
  32.  
  33.  
  34. -- We're looking for: datetime: 2020-07-20T23:50:33.619682-04:00
  35.  
  36. function parseDate(str)
  37. local y,m,d,hour, minute, second, ms = str:match("(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)%a(%d%d):(%d%d):(%d%d).(%d%d%d)")
  38. -- Lua doesn't do ms... but we can make it work
  39. -- fallback to now
  40. if y == nil then
  41. return os.time()
  42. end
  43. -- defaults
  44. if m == '' then
  45. m = 1
  46. end
  47. if d == '' then
  48. d = 1
  49. end
  50. -- create time
  51. return os.time({year=tonumber(y), month=tonumber(m), day=tonumber(d), hour=tonumber(hour), min = tonumber(minute), sec = tonumber(second)}),tonumber(ms) -- Returning a datetime and a number for the ms
  52. end
  53.  
  54.  
  55. local function StartCounting()
  56. ticks = ticks+1
  57. end
  58.  
  59.  
  60.  
  61. rednet.open("front")
  62. targetid = 3 -- You still gotta set this if you move.
  63.  
  64. ticks = 0
  65.  
  66. ISODate = getDate()
  67. t, ms = parseDate(ISODate)
  68. starttime = t
  69. startms = ms
  70. endtime = starttime -- set this as we go
  71. endms = ms
  72. ratio = 1
  73.  
  74. local lastEvent = nil
  75.  
  76. local routine = coroutine.create(StartCounting)
  77. while true do
  78. http.request("http://worldtimeapi.org/api/timezone/America/New_York.txt")
  79. event ={os.pullEvent()}
  80. if event != lastEvent and event[1] == "http_success" then
  81. result = event[3]
  82. lines = {}
  83. for s in result:gmatch("[^\r\n]+") do
  84. table.insert(lines, s)
  85. end
  86. local date = lines[3] -- LUA starts at 1 index...
  87. -- Now we have: datetime: 2020-07-21T00:07:29.748572-04:00
  88. datePieces = {}
  89. for s in date:gmatch("[^ ]+") do
  90. table.insert(datePieces, s)
  91. end
  92. t,ms = parseDate(datePieces[2]); -- Should just get the ISO date bit
  93. print("2"..t)
  94. endtime = t
  95. endms = ms
  96. -- Easier said than done.
  97. -- I think we have to wait for an event
  98. -- But, our URL can maybe be http://worldtimeapi.org/api/timezone/America/New_York.txt
  99.  
  100. -- Find the third line, datetime: 2020-07-20T23:50:33.619682-04:00
  101. -- Can lua parse that?
  102.  
  103.  
  104. second = endtime - starttime -- I think this works...
  105. second = second + (endms - startms)/1000
  106. tps = ticks/second
  107. -- Find ratio of this to the expected 20
  108. -- We want where 10 tps / 20expected = 0.5 speed (because of the way speed works, it's kinda backwards)
  109.  
  110. -- Ummm apparently on this server we're hitting near 60. Let's try that.
  111. ratio = tps/60
  112. -- We need to cap this or things might get crazy?
  113. print(ratio)
  114. -- Then send this new ratio out on rednet
  115. rednet.send(targetid,"speed " .. ratio)
  116. -- Reset
  117. ticks = 0
  118. starttime = endtime
  119. startms = endms
  120. end
  121. lastEvent = event
  122. coroutine.resume(routine)
  123. end
Advertisement
Add Comment
Please, Sign In to add comment