Advertisement
sidekick_

CC Watch v2.0

Feb 22nd, 2013
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.82 KB | None | 0 0
  1. local GMT_max = 13
  2. local GMT_min = -12
  3. local GMT_default = -11
  4. local updateTime_delay = 1
  5. local cur_time, cur_date = nil, nil
  6. local h, m, s, d, month, y = nil, nil, nil, nil, nil, nil
  7. local time_URL = "http://test-my-php.freeiz.com/time.php"
  8. local cur_dateFormat = "m/d/y"
  9. local screenX, screenY = term.getSize()
  10.  
  11. -- Tables
  12. local dateFormats = {
  13.     "m/d/y",
  14.     "d/m/y",
  15.     "y/d/m",
  16.     "y/m/d"
  17. }
  18. local boxes = {
  19.     "/------------------------\\",
  20.     "|            |           |",
  21.     "|            |           |",
  22.     "|            |           |",
  23.     "|            |           |",
  24.     "\\------------------------/",
  25. }
  26.  
  27. local watch = {
  28.     "  |----|  ",
  29.     "  |    |  ",
  30.     "  |----|  ",
  31.     "  |    |  ",
  32.     "  |----|  ",
  33.     "  |    |  ",
  34.     "  |----|  ",
  35.     " /      \\ ",
  36.     "|        |",
  37.     "|        |",
  38.     "|        |",
  39.     " \\      /",
  40.     "  |----|  ",
  41.     "  |    |  ",
  42.     "  |----|  ",
  43.     "  |    |  ",
  44.     "  |----|  ",
  45.     "  |    |  ",
  46.     "  |----|  ",
  47. }
  48.  
  49. local background = {
  50.     'bbbbbbbbbbbbbbbbbbbbbbbbbbbbf0000000077777700000000',
  51.     'bbbbbbbbbbbbbbbbbbbbbbbbbbbbf0000000078888700000000',
  52.     'bbbbbbbbbbbbbbbbbbbbbbbbbbbbf0000000077777700000000',
  53.     '3333333333333333333333333333f0000000078888700000000',
  54.     '3eeeeeeeeeeeeeeeeeeeeeeeeee3f0000000077777700000000',
  55.     '3e333333333333e33333333333e3f0000000078888700000000',
  56.     '3e333333333333e33333333333e3f0000000077777700000000',
  57.     '3e333333333333e33333333333e3f0000000744444470000000',
  58.     '3e333333333333e33333333333e3f0000007444444447000000',
  59.     '3eeeeeeeeeeeeeeeeeeeeeeeeee3f0000007444444447000000',
  60.     '3333333333333333333333333333f0000007444444447000000',
  61.     '3333333333333333333333333333f0000000744444470000000',
  62.     '3333333333333333333333333333f0000000077777700000000',
  63.     '3333333333333333333333333333f0000000078888700000000',
  64.     '9999999999999999999999999999f0000000077777700000000',
  65.     '9999999999999999999999999999f0000000078888700000000',
  66.     '9999999999999999999999999999f0000000077777700000000',
  67.     '9999999999999999999999999999f0000000078888700000000',
  68.     '9999999999999999999999999999f0000000077777700000000',
  69. }
  70.  
  71. local tColours = {
  72.     -- Background colours
  73.     ["header"] = term.isColor() and colours.blue or colours.black,
  74.     ["middle"] = term.isColor() and colours.lightBlue or colours.black,
  75.     ["footer"] = term.isColor() and colours.cyan or colours.black,
  76.     ["divider"] = term.isColor() and colours.black or colours.black,
  77.     ["watchArea"] = term.isColor() and colours.white or colours.black,
  78.     ["watchHead"] = term.isColor() and colours.yellow or colours.black,
  79.     ["watchGMT"] = term.isColor() and colours.lightGrey or colours.black,
  80.     ["watchOther"] = term.isColor() and colours.grey or colours.black,
  81.     ["options"] = colours.black,
  82.    
  83.     -- Text colours
  84.     ["info"] = term.isColor() and colours.yellow or colours.white,
  85.     ["left"] = term.isColor() and colours.blue or colours.white,
  86.     ["right"] = term.isColor() and colours.black or colours.white,
  87.     ["UTC"] = term.isColor() and colours.red or colours.white,
  88.     ["heading"] = term.isColor() and colours.red or colours.white,
  89.     ["error"] = term.isColor() and colours.red or colours.white,
  90.     ["other"] = term.isColor() and colours.lime or colours.white
  91. }
  92.  
  93. local tButtons = {
  94.     { text = "Options", x = 3, y = 12, col = tColours["info"] },
  95.     { text = "Help", x = 20, y = 12, col = tColours["info"] },
  96.     { text = "Exit", x = 13, y = 14, col = tColours["error"] },
  97. }
  98.  
  99. local tHelp = {
  100.     bgCol = colours.black,
  101.     txtCol = term.isColor() and colours.yellow or colours.white,
  102.     headCol = term.isColor() and colours.red or colours.white,
  103.     text = {
  104.         "(head)Time Program",
  105.         "This program is for you to check the time for any",
  106.         "given timezone, ranging from -12 to +13 GMT.",
  107.         "",
  108.         "(head)Left side:",
  109.         "On the left hand side of the main screen, you will",
  110.         "find the main heading of the program at the top,",
  111.         "the date and time in a box in the middle area,",
  112.         "buttons surrounded by square brackets and the",
  113.         "current settings at the bottom (default, max",
  114.         "and min GMT).",
  115.         "",
  116.         "(head)Right side:",
  117.         "On the right hand side you will the amazing watch",
  118.         "which allows you to change the current GMT hour.",
  119.         "The GMT hours range from -12 to +13 and you are",
  120.         "able to change the default within the program at",
  121.         "the top. The GMT can be changed by either using",
  122.         "the scroll on your mouse or the up/down arrows on",
  123.         "your keyboard.",
  124.         "",
  125.         "(head)Credits:",
  126.         "jag_e_nummer_ett: idea and background",
  127.         "Mikk809h: idea for date formats"
  128.     }
  129. }
  130.  
  131. -- Functions
  132. local function hexLookup( char )
  133.     local value = tonumber(char, 16)
  134.     return (value and math.pow(2, value) or nil)
  135. end
  136.  
  137. local function colorW(...)
  138.     local curColor
  139.     for i=1, #arg do -- arg is ...
  140.         if type(arg[i]) == 'number' then
  141.             curColor = arg[i]
  142.         else
  143.             if curColor then
  144.                 term.setTextColor(curColor)
  145.             end
  146.             write(arg[i])
  147.         end
  148.     end
  149. end
  150.  
  151. local function drawBG()
  152.     if term.isColor() then
  153.         for i = 1, #background do
  154.             term.setCursorPos( 1, i )
  155.             for z = 1, #background[i] do
  156.                 tmp = string.sub(background[i], z, z)
  157.                 if tmp == " " then
  158.                     term.setBackgroundColour(colours.black)
  159.                     write(' ')
  160.                 else
  161.                     term.setBackgroundColour(hexLookup(tmp))
  162.                     write(' ')
  163.                 end
  164.             end
  165.         end
  166.     else
  167.         term.setBackgroundColour( colours.black )
  168.         term.clear()
  169.        
  170.         for i = 1, #watch do
  171.             term.setCursorPos( 36, i )
  172.             write( watch[i] )
  173.         end
  174.        
  175.         term.setBackgroundColour( tColours["middle"] )
  176.         for i = 1, #boxes do
  177.             term.setCursorPos( 2, i + 4 )
  178.             write( boxes[i] )
  179.         end
  180.     end
  181. end
  182.  
  183. local function drawMain()
  184.     drawBG()
  185.    
  186.     -- Header
  187.     term.setBackgroundColour( tColours["header"] )
  188.     term.setCursorPos( 8, 2 ) colorW( tColours["other"], "WHATS THE TIME?" )
  189.    
  190.     -- Middle
  191.     term.setBackgroundColour( tColours["middle"] )
  192.     term.setCursorPos( 8, 6 ) colorW( tColours["other"], "DATE" )
  193.     term.setCursorPos( 19, 6 ) colorW( tColours["other"], "TIME" )
  194.    
  195.     -- Draw the buttons
  196.     for index, but in pairs( tButtons ) do
  197.         term.setCursorPos( but.x, but.y )
  198.         colorW( tColours["other"], "[", but.col, but.text, tColours["other"], "]" )
  199.     end
  200.    
  201.     -- Footer
  202.     term.setBackgroundColour( tColours["footer"] )
  203.     term.setCursorPos( 5, 16 ) colorW( tColours["info"], "Default GMT: ", tColours["left"], tostring(GMT_default) )
  204.     term.setCursorPos( 5, 17 ) colorW( tColours["info"], "    Max GMT: ", tColours["left"], tostring(GMT_max) )
  205.     term.setCursorPos( 5, 18 ) colorW( tColours["info"], "    Min GMT: ", tColours["left"], tostring(GMT_min) )
  206. end
  207.  
  208. local function getTimeAndDate( txt )
  209.     term.setBackgroundColour( tColours["header"] )
  210.     term.setCursorPos( 3, 2 )
  211.     colorW( tColours["info"], txt .. " current date and" )
  212.     term.setCursorPos( 3, 3 )
  213.     colorW( tColours["info"], "time ..." )
  214.     local res = http.get( time_URL )
  215.     if res then
  216.         local content = res.readAll()
  217.         res.close()
  218.         local _t, _d
  219.         for t in content:gmatch( "<TIME>(.-)</TIME>" ) do
  220.             _t = t
  221.         end
  222.         for d in content:gmatch( "<DATE>(.-)</DATE>" ) do
  223.             _d = d
  224.         end
  225.         return _t, _d
  226.     else
  227.         return nil
  228.     end
  229. end
  230.  
  231. local function split( str, regex )
  232.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  233.     if type( regex ) ~= "string" then error( "Bad argument: String expected, got "..type( regex ), 2 ) end
  234.     local t = { }
  235.     local fpat = "(.-)"..regex
  236.     local last_end = 1
  237.     local s, e, cap = str:find(fpat, 1)
  238.     while s do
  239.         if s ~= 1 or cap ~= "" then
  240.             table.insert(t,cap)
  241.         end
  242.         last_end = e+1
  243.         s, e, cap = str:find(fpat, last_end)
  244.     end
  245.     if last_end <= #str then
  246.         cap = str:sub(last_end)
  247.         table.insert(t, cap)
  248.     end
  249.     return t
  250. end
  251.  
  252. local function writeTimeAndDate( _h, _m, _s, _d, _mo, _y )
  253.     term.setBackgroundColour( tColours["middle"] )
  254.     term.setTextColour( tColours["left"] )
  255.     term.setCursorPos( 4, 8 )
  256.     if cur_dateFormat == "d/m/y" then
  257.         write( ( #tostring(_d) == 1 and "0" or "") .. _d .. '/' .. ( #tostring(_mo) == 1 and "0" or "") .. _mo .. '/' .. _y )
  258.     elseif cur_dateFormat == "m/d/y" then
  259.         write( ( #tostring(_mo) == 1 and "0" or "") .. _mo .. '/' .. ( #tostring(_d) == 1 and "0" or "") .. _d .. '/' .. _y )
  260.     elseif cur_dateFormat == "y/m/d" then
  261.         write( ( #tostring(_y) == 1 and "0" or "") .. _y .. '/' .. ( #tostring(_mo) == 1 and "0" or "") .. _mo .. '/' .. ( #tostring(_d) == 1 and "0" or "") .. _d  )
  262.     elseif cur_dateFormat == "y/d/m" then
  263.         write( _y .. '/' .. ( #tostring(_d) == 1 and "0" or "") .. _d .. '/' .. ( #tostring(_mo) == 1 and "0" or "") .. _mo )
  264.     else
  265.         error("Somehow cur_dateFormat has an invalid date format :(")
  266.     end
  267.     term.setCursorPos( 18, 8 )
  268.     write( ( #tostring(_h) == 1 and "0" or "") .. _h .. ':' .. ( #tostring(_m) == 1 and "0" or "") .. _m .. ':' .. ( #tostring( _s ) == 1 and "0" or "") .. _s )
  269. end
  270.  
  271. local function updateUTC( pos )
  272.     term.setCursorPos( 38, 10 )
  273.     term.setBackgroundColour( tColours["watchHead"] )
  274.     colorW( tColours["UTC"], 'UTC', tColours["right"], (pos > -1 and "+" or ""), tostring(pos), ' ' )
  275.    
  276.     term.setTextColour( tColours["right"] )
  277.     term.setBackgroundColour( tColours["watchGMT"] )
  278.     -- Above the 'UTC'
  279.     term.setCursorPos( 40, 2 )
  280.     write( ( pos + 3 <= GMT_max and ( ( pos + 3 > -1 and  "+" or "" ) .. pos + 3 ) or "   " ) .. string.rep(" ", 2 - #tostring( math.abs( pos + 3 ) ) ) )
  281.     term.setCursorPos( 40, 4 )
  282.     write( ( pos + 2 <= GMT_max and ( ( pos + 2 > -1 and  "+" or "" ) .. pos + 2 ) or "   " ) .. string.rep(" ", 2 - #tostring( math.abs( pos + 2 ) ) ) )
  283.     term.setCursorPos( 40, 6 )
  284.     write( ( pos + 1 <= GMT_max and ( ( pos + 1 > -1 and  "+" or "" ) .. pos + 1 ) or "   " ) .. string.rep(" ", 2 - #tostring( math.abs( pos + 1 ) ) ) )
  285.     -- Below the 'UTC'
  286.     term.setCursorPos( 40, 14 )
  287.     write( ( pos - 1 >= GMT_min and ( ( pos - 1 > -1 and  "+" or "" ) .. pos - 1 ) or "   " ) .. string.rep(" ", 2 - #tostring( math.abs( pos - 1 ) ) ) )
  288.     term.setCursorPos( 40, 16 )
  289.     write( ( pos - 2 >= GMT_min and ( ( pos - 2 > -1 and  "+" or "" ) .. pos - 2 ) or "   " ) .. string.rep(" ", 2 - #tostring( math.abs( pos - 2 ) ) ) )
  290.     term.setCursorPos( 40, 18 )
  291.     write( ( pos - 3 >= GMT_min and ( ( pos - 3 > -1 and  "+" or "" ) .. pos - 3 ) or "   " ) .. string.rep(" ", 2 - #tostring( math.abs( pos - 3 ) ) ) )
  292. end
  293.  
  294. local function drawHelpScreen()
  295.     term.setCursorPos( 1, 1 )
  296.     term.setBackgroundColour( term.isColor() and colours.grey or colours.black )
  297.     term.setTextColour( term.isColor() and colours.lightGrey or colours.white )
  298.     write( " Help Screen" .. string.rep( " ", screenX - 13 ) )
  299.     term.setBackgroundColour( term.isColor() and colours.red or colours.black )
  300.     term.setTextColour( term.isColor() and colours.black or colours.white )
  301.     write( "X" )
  302.  
  303.     local function cleanArea()
  304.         term.setBackgroundColour( term.isColor() and tHelp.bgCol or colours.black )
  305.         for y = 2, screenY do
  306.             term.setCursorPos( 1, y )
  307.             term.clearLine()
  308.         end
  309.     end
  310.    
  311.     yPos = 1
  312.     while true do
  313.         cleanArea()
  314.         for i = 2, screenY do
  315.             term.setCursorPos( 2, i )
  316.             if tHelp.text[yPos + i - 2] then
  317.                 term.setTextColour( tHelp.text[yPos + i - 2]:sub( 1, 6) == "(head)" and tHelp.headCol or tHelp.txtCol )
  318.                 write( tHelp.text[yPos + i - 2]:sub( 1, 6) == "(head)" and tHelp.text[yPos + i - 2]:sub( 7 ) or tHelp.text[yPos + i - 2] )
  319.             else
  320.                 break
  321.             end
  322.         end
  323.        
  324.         local event, but, x, y = os.pullEvent()
  325.         if event == "mouse_scroll" or event == "key" then
  326.             if ( but == -1 or but == keys.up ) and yPos > 1 then
  327.                 yPos = yPos - 1
  328.             elseif ( but == 1 or but == keys.down ) and yPos < #tHelp.text - (screenY - 2) then
  329.                 yPos = yPos + 1
  330.             end
  331.         elseif event == "mouse_click" and but == 1 then
  332.             if x == screenX and y == 1 then
  333.                 drawBG()
  334.                 drawMain()
  335.                 break
  336.             end
  337.         end
  338.     end
  339. end
  340.  
  341. local function optionScreen()
  342.     local function topBar( heading )
  343.         term.setCursorPos( 1, 1 )
  344.         term.setBackgroundColour( tColours["options"] )
  345.         term.clear()
  346.         term.setBackgroundColour( term.isColor() and colours.white or colours.black )
  347.         term.setTextColour( term.isColor() and colours.grey or colours.white )
  348.         write( " " .. heading .. string.rep( " ", screenX - (#heading + 2) ) )
  349.         term.setBackgroundColour( term.isColor() and colours.red or colours.black )
  350.         term.setTextColour( term.isColor() and colours.black or colours.white )
  351.         write( "X" )
  352.     end
  353.    
  354.     local function drawArea( head )
  355.         topBar( head )
  356.         term.setCursorPos( 4, 4 )
  357.         term.setBackgroundColour( tColours["options"] )
  358.         colorW( tColours["other"], "[CHANGE]  ", tColours["info"], "Date format: ", tColours["left"], cur_dateFormat )
  359.     end
  360.    
  361.     local function change( tab, headText, mainHead )
  362.         topBar( headText )
  363.         term.setCursorPos( 3, 5 )
  364.         term.setBackgroundColour( tColours["options"] )
  365.         colorW( tColours["error"], mainHead )
  366.         for i, sFormat in pairs( dateFormats ) do
  367.             term.setCursorPos( 3, i + 5)
  368.             colorW( tColours["info"], '[select] ', tColours["left"], sFormat )
  369.         end
  370.        
  371.         while true do
  372.             term.setCursorPos( 3, 3 )
  373.             colorW( tColours["info"], "Current date format: ", tColours["left"], cur_dateFormat )
  374.             local _, but, x, y = os.pullEvent( "mouse_click" )
  375.             if but == 1 then
  376.                 if x == screenX and y == 1 then
  377.                     break
  378.                 else
  379.                     for i, v in pairs( dateFormats ) do
  380.                         if x >= 3 and x <= 10 and y == i + 5 then
  381.                             cur_dateFormat = v
  382.                             break
  383.                         end
  384.                     end
  385.                 end
  386.             end
  387.         end
  388.     end
  389.    
  390.     while true do
  391.         drawArea( 'Options Screen' )
  392.         local event, but, x, y = os.pullEvent( "mouse_click" )
  393.         if but == 1 then
  394.             if x >= 4 and x <= 11 and y == 4 then
  395.                 change( cur_dateFormat, "Options Screen - changing date format", "Select new date format:" )
  396.             elseif x == screenX and y == 1 then
  397.                 drawBG()
  398.                 drawMain()
  399.                 break
  400.             end
  401.         end
  402.     end
  403. end
  404.  
  405. local function updateTime()
  406.     local updateTimer = os.startTimer( updateTime_delay )
  407.     while true do
  408.         local _, timer = os.pullEvent( "timer" )
  409.         if timer == updateTimer then
  410.             if s < 59 then
  411.                 s = s + 1
  412.             elseif s == 59 then
  413.                 s = 0
  414.                 m = m + 1
  415.             end
  416.             if m == 60 then
  417.                 m = 0
  418.                 h = h + 1
  419.             end
  420.             if h == 24 then
  421.                 h = 0
  422.                 d = d + 1
  423.             end
  424.             updateTimer = os.startTimer( updateTime_delay )
  425.         end
  426.     end
  427. end
  428.  
  429. local function main()
  430.     local cur_gmt_pos = GMT_default
  431.     while true do
  432.         writeTimeAndDate( h, m, s, d, month, y )
  433.         updateUTC( cur_gmt_pos )
  434.         local ev = { os.pullEvent() }
  435.         if ev[1] == "mouse_scroll" or ev[1] == "key" then
  436.             if ( ev[2] == -1 or ev[2] == keys.up ) and cur_gmt_pos < GMT_max then -- Up in GMT
  437.                 cur_gmt_pos = cur_gmt_pos + 1
  438.                 h = h + 1
  439.             elseif ( ev[2] == 1 or ev[2] == keys.down ) and cur_gmt_pos > GMT_min then -- Down in GMT
  440.                 cur_gmt_pos = cur_gmt_pos - 1
  441.                 h = h - 1
  442.             end
  443.             if h >= 24 then h = 24 - h d = d + 1
  444.             elseif h < 0 then h = 23 d = d - 1 end
  445.         elseif ev[1] == "timer" and ev[2] == updateTime then
  446.             if s < 59 then
  447.                 s = s + 1
  448.             elseif s == 59 then
  449.                 s = 0
  450.                 m = m + 1
  451.             end
  452.             if m == 60 then
  453.                 m = 0
  454.                 h = h + 1
  455.             end
  456.             if h == 24 then
  457.                 h = 0
  458.                 d = d + 1
  459.             end
  460.             updateTime = os.startTimer( updateTime_delay )
  461.         elseif ev[1] == "mouse_click" and ev[2] == 1 then
  462.             local opt
  463.             for index, but in pairs( tButtons ) do
  464.                 if ev[3] >= but.x and ev[3] <= but.x + #but.text + 2
  465.                 and ev[4] == but.y then
  466.                     opt = but.text
  467.                 end
  468.             end
  469.             if opt == "Exit" then
  470.                 -- Cleaup
  471.                 term.setBackgroundColour( colours.black )
  472.                 term.setTextColour( colours.white )
  473.                 term.clear()
  474.                 term.setCursorPos( 1, 1 )
  475.                 colorW( tColours["info"], "Last set GMT: ", tColours["left"], tostring( cur_gmt_pos ) )
  476.                 term.setCursorPos( 1, 2 )
  477.                 colorW( tColours["info"], "        DATE: ", tColours["left"], ( #tostring(month) == 1 and "0" or "") .. tostring( month ), tColours["error"], '/', tColours["left"], ( #tostring(d) == 1 and "0" or "") .. tostring( d ), tColours["error"], '/', tColours["left"], tostring( y ) )
  478.                 term.setCursorPos( 1, 3 )
  479.                 colorW( tColours["info"], "        TIME: ", tColours["left"], ( #tostring(h) == 1 and "0" or "") .. tostring( h ), tColours["error"], ':', tColours["left"], ( #tostring(m) == 1 and "0" or "") .. tostring( m ), tColours["error"], ':', tColours["left"], ( #tostring(s) == 1 and "0" or "") .. tostring( s ) )
  480.                 term.setCursorPos( 1, 5 )
  481.                 break
  482.             elseif opt == "Help" then
  483.                 drawHelpScreen()
  484.             elseif opt == "Options" then
  485.                 optionScreen()
  486.             end
  487.         end
  488.     end
  489. end
  490.  
  491. if GMT_default > GMT_max or GMT_default < GMT_min then
  492.     colorW( tColours["error"], "GMT_default value is out of range.\n  Max GMT: +13\n  Min GMT: -12")
  493.     return
  494. end
  495.  
  496. drawBG()
  497.  
  498. -- Get the time from the server for first time
  499. cur_time, cur_date = getTimeAndDate( "Getting" )
  500. -- Split cur_time into hours, minutes, and seconds
  501. local t_time = split( cur_time, ':' )
  502. h, m, s = tonumber(t_time[1]), tonumber(t_time[2]), tonumber(t_time[3])
  503. h = h + GMT_default
  504. -- Split cur_date into day, month and year
  505. local t_date = split( cur_date, '/' )
  506. month, d, y = tonumber(t_date[1]), tonumber(t_date[2]), tonumber(t_date[3])
  507.  
  508. if h >= 24 then
  509.     h = h - 24
  510.     d = d + 1
  511. elseif h < 0 then
  512.     h = 24 + h
  513.     d = d - 1
  514. end
  515.  
  516. -- Main part
  517. drawMain()
  518.  
  519. parallel.waitForAny( updateTime, main )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement