Advertisement
Derek1017

Test

Feb 27th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. local time_url = "http://www.timeapi.org/pdt/now?\\Q"
  2. local filename_url = "http://www.timeapi.org/pdt/now?\\Y-\\m-\\d%23\\H"
  3. if not fs.exists(".logs") then
  4.     fs.makeDir(".logs")
  5. end
  6. -- Time
  7. local realTime = tonumber(http.get(time_url).readAll())
  8. local osTime = os.clock()
  9. local lagAdjust = 1
  10. local function getTime()
  11.     return realTime/1000 + (os.clock() - osTime)*lagAdjust
  12. end
  13. local logname = ".logs/"..http.get(filename_url).readAll()
  14. local logfile = io.open(logname, "a")
  15. local loghour = math.floor(((realTime/1000)%86400)/3600)
  16. local notLogged = {}
  17. local function log(line)
  18.     local hr = math.floor((getTime()%86400)/3600)
  19.     if hr ~= loghour then
  20.         if #notLogged == 0 then
  21.             http.request(filename_url)
  22.         end
  23.         table.insert(notLogged)
  24.     else
  25.         logfile:write(line.."\n")
  26.     end
  27. end
  28. local function pad(s, n, char)
  29.     char = char or "0"
  30.     return char:rep(n-tostring(s):len())..s
  31. end
  32. term.clear()
  33. term.setCursorPos(1, 1)
  34. local timer = os.startTimer(60)
  35. local terminated = false
  36. while true do
  37.     local e = {os.pullEventRaw()}
  38.     if e[1] == "chat_message" or e[1] == "chatbox_command" then
  39.         if e[1] == "chatbox_command" then e[4] = "##"..e[4] end
  40.         local t = getTime()
  41.   local timestamp = "[" ..pad(math.floor((t%86400)/3600),2)..":"..pad(math.floor((t%3600)/60),2)..":"..pad(math.floor((t%60)),2).."] "
  42.         local sender = e[3]..": "
  43.         local message = e[4]
  44.         term.setTextColor(colors.yellow)
  45.         write(timestamp)
  46.         term.setTextColor(colors.red)
  47.         write(sender)
  48.         term.setTextColor(colors.white)
  49.         print(message)
  50.         log(timestamp..sender..message)
  51.     elseif e[1] == "http_success" then
  52.         if e[2] == time_url then
  53.             local t = tonumber(e[3].readAll())
  54.             if t then
  55.                 now = os.clock()
  56.                 lagAdjust = ((t-realTime)/1000) / (now-osTime)
  57.             realTime = t
  58.                 osTime = now
  59.             end
  60.         elseif e[2] == filename_url then
  61.             logfile:close()
  62.         logname = ".logs/"..e[3].readAll()
  63.             logfile = io.open(logname, "a")
  64.             loghour = math.floor((getTime()%86400)/3600)
  65.    for i=1,#notLogged do
  66.                 logfile:write(notLogged[i].."\n")
  67.             end
  68.             if terminated then break end
  69.             notLogged = {}
  70.    end
  71.     elseif e[1] == "http_failure" and e[2] == filename_url and terminated then
  72.         term.setTextColor(colors.red)
  73.         print("Could not log "..#notLogged.." messages")
  74.         break
  75.     elseif e[1] == "timer" and e[2] == timer then
  76.         http.request(time_url)
  77.         timer = os.startTimer(60)
  78.     elseif e[1] == "terminate" then
  79.         if #notLogged == 0 then
  80.             break
  81.         else
  82.             term.setTextColor(colors.orange)
  83.             print("Waiting for logging to finish...")
  84.             terminated = true
  85.         end
  86.     end
  87. end
  88. print("Exiting...")
  89. logfile:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement