surferpup

Minecraft Real World Time Display

Jan 11th, 2014
1,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.49 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. --[[    TimeDisplays v 2.0
  3.       by Surferpup
  4.  
  5.     This program displays real world for 3 time zone
  6.     and minecraft time on a computer and (optionally)
  7.     on as many wired monitors as are declared.
  8.  
  9.     The program as configured does not have any monitors declared.
  10.     Simply go to the monitor section and uncomment/add monitor lines
  11.     as necessary.
  12.    
  13.     The program will take advantage of color computers and monitors.
  14.  
  15.     Save this program as startup if you want it to run on block load.
  16.  
  17. ]]
  18. -----------------------------------------------------------------------
  19. local mcInterval = 1 -- the polling interval for Minecraft Time updates
  20. local rwInterval= 60 -- the polling world for real world time updates
  21. -----------------------------------------------------------------------
  22. --  MONITORS
  23. --  You can have as many as you'd like
  24. --  you need to change this to your monitor labels
  25. --  format [n] = peripheral.wrap("monitor_label)
  26. --  Don't forget commas between all BUT the last entry
  27. --
  28. --  I formated three for you and commented them out.
  29. ------------------------------------------------------------------------
  30. local monitors = {
  31. --    [1]=peripheral.wrap("monitor_0");
  32. --    [2]=peripheral.wrap("monitor_1");
  33. --    [3]=peripheral.wrap("monitor_2");
  34. }
  35. -----------------------------------------------------------------------
  36. --  TIME ZONES
  37. --  3 real world and 1 local (minecraft) time zone
  38. --  change zone to be a zone recognized by www.timeapi.org
  39. --  go to timeapi.org and get your own strings using the same exact format
  40. ------------------------------------------------------------------------
  41. local time = {
  42.     [1]={city="Sioux Falls",color=colors.lightBlue,time="NOW",zone = "cst"}, -- central
  43.     [2]={city="Osaka",color=colors.yellow,time="NOW",zone = "utc"}, -- universal
  44.     [3]={city="Livermore",color=colors.lime,time="NOW",zone = "pst"}, -- pacific
  45.     [4]={city="Local",color=colors.pink,time="NOW",zone = ""} -- This is minecraft time
  46. }
  47. function refreshTime()
  48.     for i = 1,3 do
  49.         time[i].time=(http.get("http://www.timeapi.org/"..(time[i].zone).."/now?format=%25l:%25M%20%25p").readAll())
  50.     end
  51.     time[4].time= textutils.formatTime(os.time(), false)
  52. end
  53. ------------------------------------------------------------------------
  54. -- OTHER LOCAL VARIABLES
  55. local minecraftTimer,realWorldTimer,rwTime,mcWorldTime,maxLabelSize
  56. ------------------------------------------------------------------------
  57. -- FUNCTIONS
  58.  
  59. function pad(width,text)
  60.     return (string.rep(" ",width-string.len(text))..text)
  61. end
  62.  
  63. local function clearAllScreens()
  64.     term.clear()
  65.     for i = 1,#monitors do
  66.             monitors[i].clear()
  67.     end
  68. end
  69.  
  70. function calculateMaxLabelSize() -- city names vary in size
  71.     local maxLabelSize = 0
  72.     for i= 1,4 do
  73.     if #(time[i].city)>maxLabelSize then
  74.         maxLabelSize = #(time[i].city)
  75.     end
  76.     end
  77.     return maxLabelSize
  78. end
  79.  
  80. local function setScale(maxLabelSize) -- fit city names to varying size monitors
  81.     for i = 1,#monitors do
  82.     for j=10,1,-1 do
  83.         monitors[i].setTextScale(j/2)
  84.         local mX,mY = monitors[i].getSize()
  85.         if mY>=8 then
  86.         if mX>maxLabelSize then
  87.             break
  88.         end
  89.         end
  90.     end
  91.     end
  92. end
  93.  
  94. function displayRealWorldTime()
  95.     while true do
  96.         local e = {os.pullEvent()} -- this will yield back to main loop
  97.         if (e[1] == "timer" and e[2] == realWorldTimer) or e[1]=="start" then
  98.         refreshTime()
  99.             for t = 1,4 do
  100.            
  101.                     if term.isColor() then
  102.                         term.setTextColor(tonumber(time[t].color))
  103.                     else
  104.                         term.setTextColor(colors.white)
  105.                     end
  106.                     term.setCursorPos(1,t)
  107.             term.clearLine()
  108.                     x,_ = term.getSize()
  109.                     term.write(pad(x,time[t].time))
  110.                     term.setCursorPos(1,t)
  111.                     term.write(time[t].city)
  112.             end
  113.             term.setTextColor(colors.white) -- reset terminal to default color
  114.             for i = 1,#monitors do
  115.                 for t = 1,4 do
  116.             local x,_ = monitors[i].getSize()
  117.                     monitors[i].setCursorPos(1,2*t-1)
  118.             monitors[i].clearLine()
  119.                     if monitors[i].isColor() then
  120.                         monitors[i].setTextColor(tonumber(time[t].color))
  121.                     else
  122.                         monitors[i].setTextColor(colors.white)
  123.                     end
  124.                     monitors[i].write(time[t].city)
  125.                     monitors[i].setCursorPos(1,t*2)
  126.                     monitors[i].write(pad(x,time[t].time))
  127.                 end
  128.             end
  129.             realWorldTimer = os.startTimer(rwInterval)
  130.         end
  131.        
  132.     end
  133. end
  134.  
  135. function displayMinecraftWorldTime()
  136.     while true do
  137.     local e = {os.pullEvent()} -- this will yield back to main loop
  138.         if (e[1] == "timer" and e[2] == minecraftTimer) or e[1]=="start" then
  139.             time[4].time= textutils.formatTime(os.time(), false)
  140.             for i = 1,#monitors do
  141.                 monitors[i].setCursorPos(1,8)
  142.         monitors[i].clearLine()
  143.                 if monitors[i].isColor() then
  144.                     monitors[i].setTextColor(tonumber(time[4].color))
  145.                 end
  146.                 local x,_ = monitors[i].getSize()
  147.                 monitors[i].write(pad(x,time[4].time))
  148.             end
  149.             term.setCursorPos(1,4)
  150.         term.clearLine()
  151.             if term.isColor() then
  152.                 term.setTextColor(tonumber(time[4].color))
  153.             end
  154.             x,_ = term.getSize()
  155.             term.write(pad(x,time[4].time))
  156.             term.setCursorPos(1,4)
  157.             term.write(time[4].city)
  158.             if term.isColor()
  159.                 then term.setTextColor(colors.white)
  160.             end
  161.             minecraftTimer=os.startTimer(mcInterval)
  162.         end
  163.     end
  164. end
  165.  
  166. local function realWorldTime() -- coroutine driver in parallel
  167.     local tFilter = nil
  168.     local eventData = {}
  169.     local r = rwTime -- coroutine
  170.     while true do
  171.         if r then
  172.             if tFilter == nil or tFilter == eventData[1] or eventData[1] == "terminate" then
  173.                 local ok, param = coroutine.resume( r, unpack(eventData) )
  174.                 if not ok then
  175.                     error( param )
  176.                 else
  177.                     tFilter = param
  178.                 end
  179.                 if coroutine.status( r ) == "dead" or eventData[1] == "terminate" then
  180.                     r = nil
  181.                     event = "terminate"
  182.                     return
  183.                 end
  184.             end
  185.         end
  186.         eventData = { os.pullEventRaw() }
  187.     end
  188. end
  189.  
  190. local function mineCraftTime() -- coroutine driver in parallel
  191.     local tFilter = nil
  192.     local eventData = {}
  193.     local r = mcWorldTime --coroutine
  194.     while true do
  195.         if r then
  196.             if tFilter == nil or tFilter == eventData[1] or eventData[1] == "terminate" then
  197.                 local ok, param = coroutine.resume( r, unpack(eventData) )
  198.                 if not ok then
  199.                     error( param )
  200.                 else
  201.                     tFilter = param
  202.                 end
  203.                 if coroutine.status( r ) == "dead" or eventData[1] == "terminate" then
  204.                     r = nil
  205.                     event = "terminate"
  206.                     return
  207.                 end
  208.             end
  209.         end
  210.         eventData = { os.pullEventRaw() }
  211.     end
  212. end  
  213.  
  214. local function start()
  215.     sleep(.5)
  216.     os.queueEvent("start")
  217.     return
  218. end
  219. -----------------------------------------------------------------------
  220. --  MAIN PROGRAM
  221. -----------------------------------------------------------------------
  222. minecraftTimer = 0
  223. realWorldTimer = 0
  224.  
  225. -- create coroutines
  226. rwTime = coroutine.create(displayRealWorldTime)
  227. mcWorldTime = coroutine.create(displayMinecraftWorldTime)
  228.  
  229. -- calculate maxLabelSize
  230. maxLabelSize = calculateMaxLabelSize()
  231.  
  232. -- set Scale for Monitors
  233. setScale(maxLabelSize)
  234. clearAllScreens()
  235.  
  236.  
  237. -- Start Parallel functions
  238. --
  239. --[[
  240.     The program uses coroutines in conjuction with
  241.     parallel functions.  The first function (start) will
  242.     queue a start event.  The next two functions drive
  243.     coroutines which update minecraft time or real world time.
  244.    
  245.     Thanks to Lyqyd on ComputerCraft forums for the
  246.     ideas behind the coroutine driving functions running
  247.     in parallel
  248. ]]
  249.  
  250. parallel.waitForAll(start,mineCraftTime,realWorldTime)
Advertisement
Add Comment
Please, Sign In to add comment