countdown.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. if not minetest.global_exists("countdown") then countdown = {} end
  2. countdown.modpath = minetest.get_modpath("hb4")
  3. countdown.quit = false
  4. local color = minetest.get_color_escape_sequence("#ffff00")
  5. function countdown.step(data)
  6. -- Halt when done.
  7. if countdown.quit then
  8. return
  9. end
  10. data = data or {hour = -1, min = -1, sec = -1}
  11. -- Current timestamp, as a number.
  12. local ct = os.time()
  13. -- Timestamp of next shutdown.
  14. local nt = countdown.time
  15. -- If our restart time is in the past, we're done.
  16. if nt <= ct then
  17. local message = "# Server: RESTART IMMINENT - WAITING FOR OS SIGNAL."
  18. chat_logging.log_server_message(message)
  19. minetest.chat_send_all(color .. message)
  20. countdown.quit = true
  21. return
  22. end
  23. -- Calculate remaining time (subtract future time in seconds from current time, also in seconds).
  24. local rt = nt - ct
  25. local rd = os.date("!*t", rt)
  26. local report = false
  27. local delay = 60
  28. -- Debug.
  29. --delay = 10
  30. --report = true
  31. --minetest.chat_send_all("Seconds remaining: " .. rt)
  32. local msgtype = 0
  33. -- Calculate how long until next public message.
  34. do
  35. -- Report on every hour change.
  36. if data.hour ~= rd.hour then
  37. report = true
  38. end
  39. if rd.hour > 0 then
  40. -- Report the nearest rounded hour.
  41. msgtype = 3
  42. end
  43. if rd.hour == 0 and rd.min <= 60 then
  44. msgtype = 2
  45. end
  46. -- Report every minute once there are 10 or less minutes.
  47. if rd.hour == 0 and rd.min <= 10 and data.min ~= rd.min then
  48. report = true
  49. delay = 5
  50. msgtype = 2
  51. end
  52. -- Report every second of the last 20 seconds.
  53. if rd.hour == 0 and rd.min == 0 and rd.sec <= 20 then
  54. report = true
  55. delay = 1
  56. msgtype = 1
  57. end
  58. -- Record time of last timecheck.
  59. data.hour = rd.hour
  60. data.min = rd.min
  61. data.sec = rd.sec
  62. end
  63. if report then
  64. local message = "# Server: Nightly restart in: " .. string.format("%02d:%02d:%02d", rd.hour, rd.min, rd.sec) .. "."
  65. if msgtype == 1 then
  66. local p = "s"
  67. if rd.sec == 1 then p = "" end
  68. message = "# Server: Restart in " .. rd.sec .. " second" .. p .. "."
  69. elseif msgtype == 2 then
  70. local h = rd.min + math.floor((rd.sec / 60) + 0.5)
  71. local p = "s"
  72. if h == 1 then p = "" end
  73. message = "# Server: Restarting in " .. h .. " minute" .. p .. "."
  74. elseif msgtype == 3 then
  75. local h = rd.hour + math.floor((rd.min / 60) + 0.5)
  76. local p = "s"
  77. if h == 1 then p = "" end
  78. message = "# Server: Nightly restart in " .. h .. " hour" .. p .. "."
  79. end
  80. chat_logging.log_server_message(message)
  81. minetest.chat_send_all(color .. message)
  82. end
  83. -- Wait for next check.
  84. minetest.after(delay, countdown.step, data)
  85. end
  86. if not countdown.registered then
  87. -- Get current timestamp as a datetime table.
  88. local cd = os.date("*t")
  89. -- Should be 1 hour after midnight, CST. 6 AM UTC.
  90. local nd = table.copy(cd)
  91. nd.day = nd.day + 1
  92. nd.hour = 1
  93. nd.min = 0
  94. nd.sec = 0
  95. -- Store shutdown timestamp as time in seconds in the future.
  96. countdown.time = os.time(nd)
  97. local delay = 60
  98. -- Debug.
  99. --delay = 10
  100. minetest.after(delay, countdown.step, {})
  101. countdown.registered = true
  102. end