Advertisement
kd8lvt

Plastic, as fixed by Kd8lvt

Apr 13th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 78.54 KB | None | 0 0
  1.    
  2.  
  3.     -- Plastic Beta v0.12 As edited by: Kd8lvt
  4.     -- [0.12] Bug fixes
  5.     -- [0.11] Support for latest version of OpenPeripheral
  6.     -- A Google Glass like OS/Programs/Whatever you want to call it
  7.     -- That runs on Terminal Glasses in OpenPeripheral
  8.     -- Just stick in a computer with a Terminal Glass Peripheral and run it!
  9.     -- Have fun using it!
  10.     -- Feel free to use my code in anyway you want!
  11.     -- HOWEVER as stated by the policies of the APIs I used
  12.     -- Please do not use my API keys. Just register for them
  13.     -- Yourself! It's free and World Weather Online is instant!
  14.    
  15.     function sync()
  16.       plastic.sync()
  17.     end
  18.      
  19.     local tArgs = {...}
  20.      
  21.     local function searchArgs(text)
  22.             for k,v in pairs(tArgs) do
  23.                     if v == text then return true end
  24.             end
  25.             return false
  26.     end
  27.      
  28.     if not http then print("Sorry, Plastic requires HTTP to run!") return end
  29.      
  30.     local plasticInstallation = shell.getRunningProgram()
  31.      
  32.     -- Find glass bridge
  33.     local plastic = false
  34.     local worldSensor = false
  35.      
  36.     if fs.exists("/ocs/apis/sensor") then
  37.             os.loadAPI("ocs/apis/sensor")
  38.     end
  39.      
  40.     for k,v in pairs(rs.getSides()) do
  41.             if (peripheral.getType(v) == "openperipheral_glassesbridge") then
  42.                     plastic = peripheral.wrap(v)
  43.                     break
  44.             elseif (peripheral.getType(v) == "sensor") and (not worldSensor) then
  45.                     worldSensor = sensor.wrap(v)
  46.                     if not (worldSensor.getSensorName() == "worldCard") then
  47.                             worldSensor = false
  48.                     end
  49.             end
  50.     end
  51.      
  52.     if not plastic then print("Could not find Glass Bridge!") error() end
  53.      
  54.     local rootApps = nil
  55.      
  56.     if searchArgs("root") then
  57.             rootApps = {}
  58.             if term.isColor() then
  59.                     term.setTextColor(colors.red)
  60.             end
  61.             print("-- [[ ROOT MODE ENABLED ]] --")
  62.             print("PLASTIC MAY BECOME UNSTABLE AND")
  63.             print("MAY CRASH. USE AT YOUR OWN RISK!")
  64.             if fs.isDir("/plasticApps") then
  65.      
  66.             else
  67.                     print("Creating /plasticApps folder")
  68.                     fs.delete("/plasticApps")
  69.                     fs.makeDir("/plasticApps")
  70.             end
  71.             -- Example plastic root app
  72.             --       --[[Plastic Root App]]--
  73.             --       --NAME: Example App
  74.             --       if cmd == "hello" then
  75.             --       print("HELLO!")
  76.             --       end
  77.      
  78.             print("Searching for apps in /plasticApps")
  79.             print("")
  80.             for k,v in pairs(fs.list("/plasticApps")) do
  81.                     if not(fs.isDir("/plasticApps/"..v)) then
  82.                             local f = io.open("/plasticApps/"..v, "r")
  83.                             if f:read("*l") == "--[[Plastic Root App]]--" then
  84.                                     local name = f:read("*l"):match("%-%-NAME%: (.+)")
  85.                                     dofile("/plasticApps/"..v)
  86.                                     print("-- Application registered: ", name, " --")
  87.                                     local data = f:read("*a")
  88.                                     rootApps[name] = data
  89.                             end
  90.                             f:close()
  91.                     end
  92.             end
  93.             print("")
  94.             print("Search completed!")
  95.             print("Starting Plastic...")
  96.     end
  97.      
  98.     if not(searchArgs("nodaemon")) and not(searchArgs("root")) then
  99.             term.clear()
  100.             term.setCursorPos(1,1)
  101.      
  102.             print("Plastic daemon now running. Use exit to exit")
  103.     end
  104.      
  105.     local function reliableSleep(time)
  106.             sleep(time)
  107.     end
  108.      
  109.      
  110.     -- Setup the actual "plastic"
  111.     local gColors = {}
  112.     gColors.red = 0xff3333
  113.     gColors.blue = 0x7dd2e4
  114.     gColors.yellow = 0xffff4d
  115.     gColors.green = 0x4dff4d
  116.     gColors.gray = 0xe0e0e0
  117.     gColors.textGray = 0x818181
  118.     gColors.text = 0x5a5a5a
  119.     gColors.rain = 0x2e679f
  120.      
  121.     plastic.clear()
  122.     local mainBox = plastic.addBox(20, 20, 1, 48, gColors.gray, 0.7)
  123.     local outlineT = plastic.addBox(18,18,2,2,gColors.blue,0.7)
  124.     local outlineB = plastic.addBox(18,68,2,2,gColors.blue,0.7)
  125.     sync()
  126.     --Startup Animation
  127.     if not(searchArgs("lite")) then
  128.             -- 120, Edge is 140, Center is 80
  129.             for i = 0, 17 do
  130.                     mainBox.setWidth(i*8)
  131.                     outlineT.setWidth(i*8+4)
  132.                     outlineB.setWidth(i*8+4)
  133.                     reliableSleep(0.01)
  134.             sync()
  135.             end
  136.     else
  137.             mainBox.setWidth(136)
  138.             outlineT.setWidth(140)
  139.             outlineB.setWidth(140)
  140.             mainBox.setHeight(48)
  141.         sync()
  142.     end
  143.     local header = plastic.addText(75, 25, "", gColors.textGray)
  144.     local secondText = plastic.addText(50, 40, "", gColors.blue)
  145.     local thirdText = plastic.addText(40, 55, "", gColors.blue)
  146.     local mainText = plastic.addText(40, 27, "", gColors.blue)
  147.     local forthText = plastic.addText(40, 55, "", gColors.text)
  148.     local tempText = plastic.addText(40, 70, "", gColors.text)
  149.     sync()
  150.     header.setZ(5)
  151.     mainText.setZ(5)
  152.     secondText.setZ(5)
  153.     thirdText.setZ(5)
  154.     tempText.setZ(5)
  155.     forthText.setZ(5)
  156.     sync()
  157.      
  158.     local function closeAnimation()
  159.             if mainBox then
  160.                     pcall(mainText.delete)
  161.                     pcall(secondText.delete)
  162.                     pcall(header.delete)
  163.                     pcall(thirdText.delete)
  164.                     pcall(tempText.delete)
  165.                     pcall(forthText.delete)
  166.                     os.queueEvent("plastic_clock_manager", "kill")
  167.             sync()
  168.                     reliableSleep(0.1)
  169.                     if not(searchArgs("lite")) then
  170.                             pcall(function()
  171.                             for i = 17, 0, -1 do
  172.                                     mainBox.setWidth(i*8)
  173.                                     outlineT.setWidth(i*8+2)
  174.                                     outlineB.setWidth(i*8+2)
  175.                     sync()
  176.                                     reliableSleep(0.01)
  177.                             end
  178.                             end)
  179.                     end
  180.                     pcall(outlineT.delete)
  181.                     pcall(outlineB.delete)
  182.                     pcall(mainBox.delete)
  183.             sync() 
  184.             end
  185.     end
  186.      
  187.     local oldShutdown = os.shutdown
  188.     local oldReboot = os.reboot
  189.      
  190.     function os.shutdown()
  191.             closeAnimation()
  192.             return oldShutdown()
  193.     end
  194.      
  195.     function os.reboot()
  196.             closeAnimation()
  197.             return oldReboot()
  198.     end
  199.      
  200.     local function runPlastic()
  201.             -- Variables & Stuff
  202.             local corruption = false
  203.             local firstTime = true
  204.             local showClock = false
  205.      
  206.             -- Fancy functions
  207.             local gWidth = 24
  208.             local extraSupport = 0
  209.      
  210.             local function trimText(s)
  211.                     return s:match("^%s*(.-)%s*$")
  212.             sync()
  213.             end
  214.      
  215.             local function plasticGet(url, noCancel)
  216.                     http.request(url)
  217.                     while true do
  218.                             local e, rUrl, rmsg = os.pullEvent()
  219.                             if (e == "http_success") and (rUrl == url) then
  220.                                     if rmsg then
  221.                                             local data = rmsg.readAll()
  222.                                             rmsg.close()
  223.                                             if data then
  224.                                                     return "success", data
  225.                                             else
  226.                                                     sleep(1)
  227.                                                     http.request(url)
  228.                                             end
  229.                                     else
  230.                                             sleep(1)
  231.                                             http.request(url)
  232.                                     end
  233.                             elseif (e == "http_failure") and (rUrl == url) then
  234.                                     return "failure"
  235.                             elseif (e == "chat_command") and ((trimText(rUrl:lower()) == "cancel") or (trimText(rUrl:lower()) == "home")) and not(noCancel) then
  236.                                     return "cancel"
  237.                             end
  238.                     end
  239.             end
  240.      
  241.             local function slowText(text, object)
  242.                     if not(searchArgs("lite")) then
  243.                             object.setText("")
  244.                             for i = 1, #text do
  245.                                     object.setText(string.sub(text, 1, i))
  246.                     sync()
  247.                                     reliableSleep(0.01)
  248.                             end
  249.                     else
  250.                             object.setText(text)
  251.                 sync()
  252.                     end
  253.             end
  254.      
  255.             local function getCenter(text)
  256.                     return math.ceil(((136/2)-(plastic.getStringWidth(text)*(0.65)/2))+20-0.5)
  257.             sync()
  258.             end
  259.      
  260.             local function centerText(text, object)
  261.                     object.setText("")
  262.                     object.setX(getCenter(text))
  263.                     slowText(text, object)
  264.             sync()
  265.             end
  266.      
  267.             local function copyTable(tb)
  268.                     local newTable = {}
  269.                     for k,v in pairs(tb) do
  270.                             newTable[k] = v
  271.                     end
  272.                     return newTable
  273.             end
  274.      
  275.             -- Load settings
  276.             local settings = {}
  277.             if fs.exists("/plasticOptions") and not(fs.isDir("/plasticOptions")) then
  278.                     local f = io.open("/plasticOptions", "r")
  279.                     local data = f:read("*a")
  280.                     settings = textutils.unserialize(data)
  281.                     f:close()
  282.                     firstTime = false
  283.             end
  284.      
  285.             if not(settings) or not((type(settings["name"]) == "string") and (type(settings["use12hour"]) == "boolean") and
  286.                     (type(settings["city"]) == "string") and (type(settings["showtime"]) == "string") and
  287.                     ((settings["temperature"] == "c") or (settings["temperature"] == "f"))) and not(firstTime) then
  288.                     corruption = true
  289.                     header.setY(25)
  290.                     header.setColor(gColors.red)
  291.                     centerText("Error: Corruption", header)
  292.                     mainText.setY(37)
  293.                     mainText.setColor(gColors.yellow)
  294.                     centerText("Options data is", mainText)
  295.                     secondText.setY(47)
  296.                     secondText.setColor(gColors.yellow)
  297.                     centerText("corrupted.", secondText)
  298.                     thirdText.setY(57)
  299.                     thirdText.setColor(gColors.yellow)
  300.                     thirdText.setX(40)
  301.                     slowText("Resetting Plastic...", thirdText)
  302.                     reliableSleep(2)
  303.                     fs.delete("/plasticOptions")
  304.                     closeAnimation()
  305.                     reliableSleep(1)
  306.                     shell.run(plasticInstallation, "nodaemon", unpack(tArgs))
  307.                     error()
  308.             end
  309.      
  310.             local function getWT(city, canceller)
  311.                     local function getRawWT(city, canceller) --f or c for unit
  312.                             local unit = settings["temperature"]
  313.                             local use12hour = settings["use12hour"]
  314.                             local months = {"Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"}
  315.                             local resp, webData = nil
  316.                             if canceller then
  317.                                     resp, webData = plasticGet("http://api.worldweatheronline.com/free/v1/weather.ashx?q="..textutils.urlEncode(city).."&format=xml&extra=localObsTime&num_of_days=1&includelocation=yes&key=uyp4r5ekjns64pmpvv3qkynj", true)
  318.                             else
  319.                                     resp, webData = plasticGet("http://api.worldweatheronline.com/free/v1/weather.ashx?q="..textutils.urlEncode(city).."&format=xml&extra=localObsTime&num_of_days=1&includelocation=yes&key=uyp4r5ekjns64pmpvv3qkynj")
  320.                             end
  321.                             if resp == "cancel" then
  322.                                     return "cancel"
  323.                             elseif resp == "success" then
  324.                             else
  325.                                     error()
  326.                             end
  327.                             if webData:find("<error>") then
  328.                                     return "invalid"
  329.                             end
  330.                             local city, country = webData:match([[<areaName><!%[CDATA%[([^>]+)%]%]></areaName><country><!%[CDATA%[([^>]+)%]%]></country>]])
  331.                             if trimText(country) == "United States Of America" then
  332.                                     country = "USA"
  333.                             end
  334.                             city = trimText(city):gsub(" City", "")
  335.                             local resolvedLocation = city .. ", " .. country
  336.      
  337.                             local currentTemp = nil
  338.                             if unit == "c" then
  339.                                     currentTemp = webData:match([[<current_condition>.+<temp_C>([^<]+)</temp_C>.+</current_condition>]])
  340.                             elseif unit == "f" then
  341.                                     currentTemp = webData:match([[<current_condition>.+<temp_F>([^<]+)</temp_F>.+</current_condition>]])
  342.                             end
  343.                             local currentHumidity = webData:match([[<current_condition>.+<humidity>([^<]+)</humidity>.+</current_condition>]])
  344.                         local currentWeather = trimText(webData:match([[<current_condition>.+<weatherDesc><!%[CDATA%[([^>]+)%]%]></weatherDesc>.+</current_condition>]]))
  345.                         local lowTemp, highTemp = nil
  346.                             if unit == "c" then
  347.                                     highTemp = webData:match([[<weather>.+<tempMaxC>([^<]+)</tempMaxC>.+</weather>]])
  348.                                     lowTemp = webData:match([[<weather>.+<tempMinC>([^<]+)</tempMinC>.+</weather>]])
  349.                             elseif unit == "f" then
  350.                                     highTemp = webData:match([[<weather>.+<tempMaxF>([^<]+)</tempMaxF>.+</weather>]])
  351.                                     lowTemp = webData:match([[<weather>.+<tempMinF>([^<]+)</tempMinF>.+</weather>]])
  352.                             end
  353.                             dayWeather = trimText(webData:match([[<weather>.+<weatherDesc><!%[CDATA%[([^<]+)%]%]></weatherDesc>.+</weather>]]))
  354.      
  355.                             local time = nil
  356.                             local year, month, day, rawTime, timeFormat = webData:match("<localObsDateTime>(%d+)%-(%d+)%-(%d+) (%d+:%d+) (%u+)</localObsDateTime>")
  357.                             local resolvedDate = day .. " " .. months[tonumber(month)] .. " " .. year
  358.                             if not use12hour then
  359.                                     if timeFormat == "AM" then
  360.                                             if rawTime:sub(1,2) == "12" then
  361.                                                     time = "0" .. rawTime:sub(3,-1)
  362.                                             else
  363.                                                     time = rawTime
  364.                                             end
  365.                                     elseif timeFormat == "PM" then
  366.                                             if rawTime:sub(1,2) == "12" then
  367.                                                     time = rawTime
  368.                                             else
  369.                                                     time = tostring(tonumber(rawTime:sub(1,2))+12) .. rawTime:sub(3,-1)
  370.                                             end
  371.                                     else
  372.                                             error()
  373.                                     end
  374.                             else
  375.                                     time = rawTime .. " " .. timeFormat
  376.                             end
  377.                             if time:sub(1,1) == "0" then time = time:sub(2,-1) end
  378.                             local current = {}
  379.                             current["temp"] = currentTemp
  380.                             current["weather"] = currentWeather
  381.                             current["humidity"] = currentHumidity
  382.                             current["time"] = time
  383.                             current["date"] = resolvedDate
  384.                             current["url"] = timeURL
  385.                             current["location"] = resolvedLocation
  386.      
  387.                             local day = {}
  388.                             day["high"] = highTemp
  389.                             day["low"] = lowTemp
  390.                             day["weather"] = dayWeather
  391.      
  392.                             return "success", current, day
  393.                     end
  394.                     local result = nil
  395.                     result = {pcall(getRawWT, city, canceller)}
  396.                     if result[1] then
  397.                             table.remove(result, 1)
  398.                     end
  399.                     if result[1] then
  400.                             return unpack(result)
  401.                     else
  402.                             return "failure"
  403.                     end
  404.             end
  405.      
  406.             -- Get the time and weather!
  407.             local function getTime(city)
  408.                     --local resp, time = getTime(settings["city"])
  409.                     local resp, cur, today = getWT(city)
  410.                     if resp == "success" then
  411.                             return "success", cur["time"]
  412.                     else return "failure" end
  413.             end
  414.      
  415.             local function displayTime(time)
  416.                     if time then
  417.                             if #time == 4 then
  418.                                     mainText.setX(60)
  419.                     sync()
  420.                             elseif #time == 5 then
  421.                                     mainText.setX(50)
  422.                                     sync()
  423.                             elseif #time == 7 then
  424.                                     mainText.setX(36)
  425.                     sync()
  426.                             elseif #time == 8 then
  427.                                     mainText.setX(27)
  428.                     sync()
  429.                             end
  430.                             mainText.setText(time)
  431.                 sync()
  432.                     end
  433.             end
  434.      
  435.             local function resetScreen()
  436.                     thirdText.setText("")
  437.                     header.setText("")
  438.                     forthText.setText("")
  439.                     tempText.setText("")
  440.                     mainText.setText("")
  441.                     mainText.setColor(gColors.text)
  442.                     mainText.setScale(3)
  443.                     mainText.setY(32)
  444.                     secondText.setText("")
  445.                     secondText.setColor(gColors.blue)
  446.                     secondText.setY(57)
  447.                     os.queueEvent("plastic_clock_manager", "show")
  448.                     centerText("Welcome, " .. settings["name"], secondText)
  449.             sync()
  450.             end
  451.      
  452.             local function textScreen()
  453.                     thirdText.setText("")
  454.                     header.setText("")
  455.                     mainText.setText("")
  456.                     secondText.setText("")
  457.                     os.queueEvent("plastic_clock_manager", "hide")
  458.                     reliableSleep(0.05)
  459.                     header.setScale(1)
  460.                     mainText.setScale(1)
  461.                     secondText.setScale(1)
  462.                     thirdText.setScale(1)
  463.                     header.setColor(gColors.text)
  464.                     mainText.setColor(gColors.text)
  465.                     secondText.setColor(gColors.text)
  466.                     thirdText.setColor(gColors.text)
  467.                     header.setY(25)
  468.                     mainText.setY(37)
  469.                     secondText.setY(47)
  470.                     thirdText.setY(57)
  471.             sync()
  472.                     reliableSleep(0.1)
  473.             end
  474.      
  475.             local function squeezeScreen()
  476.                     thirdText.setText("")
  477.                     header.setText("")
  478.                     mainText.setText("")
  479.                     secondText.setText("")
  480.                     forthText.setText("")
  481.                     os.queueEvent("plastic_clock_manager", "hide")
  482.                     reliableSleep(0.05)
  483.                     header.setScale(1)
  484.                     mainText.setScale(1)
  485.                     secondText.setScale(1)
  486.                     thirdText.setScale(1)
  487.                     forthText.setScale(1)
  488.                     header.setColor(gColors.text)
  489.                     mainText.setColor(gColors.text)
  490.                     secondText.setColor(gColors.text)
  491.                     thirdText.setColor(gColors.text)
  492.                     forthText.setColor(gColors.text)
  493.                     header.setY(22)
  494.                     mainText.setY(31)
  495.                     secondText.setY(40)
  496.                     thirdText.setY(49)
  497.                     forthText.setY(58)
  498.             sync()
  499.                     reliableSleep(0.1)
  500.             end
  501.      
  502.             -- Setup functions
  503.             local function setupName()
  504.                     thirdText.setY(57)
  505.                     secondText.setText("")
  506.                     mainText.setText("")
  507.                     thirdText.setText("")
  508.                     centerText("What's your name?", mainText)
  509.                     secondText.setY(47)
  510.                     secondText.setScale(1)
  511.                     secondText.setColor(gColors.text)
  512.                     centerText("Ex: $$John", secondText)
  513.                     local e, msg = os.pullEvent("chat_command")
  514.                     local name = msg
  515.                     secondText.setText("")
  516.                     centerText("Your name is", mainText)
  517.                     centerText(name .. "?", secondText)
  518.                     thirdText.setColor(gColors.blue)
  519.                     centerText("Options: $$y/n", thirdText)
  520.             sync()
  521.                     while true do
  522.                             local e, msg = os.pullEvent("chat_command")
  523.                             msg = trimText(msg:lower())
  524.                             if msg:find("y") then
  525.                                     return name
  526.                             elseif msg:find("n") then
  527.                                     return setupName()
  528.                             end
  529.                     end
  530.             end
  531.      
  532.      
  533.             local function setupTime()
  534.                     thirdText.setY(57)
  535.                     secondText.setText("")
  536.                     thirdText.setText("")
  537.                     centerText("Use 12h Time Format?", mainText)
  538.                     secondText.setColor(gColors.blue)
  539.                     centerText("Options: $$y/n", secondText)
  540.             sync()
  541.                     while true do
  542.                             local e, msg = os.pullEvent("chat_command")
  543.                             msg = trimText(msg:lower())
  544.                             if msg:find("y") then
  545.                                     return true
  546.                             elseif msg:find("n") then
  547.                                     return false
  548.                             end
  549.                     end
  550.             end
  551.      
  552.             local function setupTemperature()
  553.                     thirdText.setY(57)
  554.                     secondText.setText("")
  555.                     thirdText.setText("")
  556.                     centerText("Celsius or Farenheit?", mainText)
  557.                     secondText.setColor(gColors.blue)
  558.                     centerText("Options: $$c/f", secondText)
  559.             sync()
  560.                     while true do
  561.                             local e, msg = os.pullEvent("chat_command")
  562.                             msg = trimText(msg:lower())
  563.                             if msg:find("c") then
  564.                                     return "c"
  565.                             elseif msg:find("f") then
  566.                                     return "f"
  567.                             end
  568.                     end
  569.             end
  570.      
  571.             local function setupLocation()
  572.                     thirdText.setY(57)
  573.                     secondText.setText("")
  574.                     thirdText.setText("")
  575.                     secondText.setColor(gColors.text)
  576.                     thirdText.setColor(gColors.text)
  577.                     centerText("What's your city?", mainText)
  578.                     centerText("(To get time/weather)", secondText)
  579.                     centerText("Ex: $$New York", thirdText)
  580.             sync()
  581.                     while true do
  582.                             local e, city = os.pullEvent("chat_command")
  583.                             thirdText.setColor(gColors.yellow)
  584.                             centerText("Connecting...", thirdText)
  585.                             local resp, cur = getWT(city)
  586.                             if resp == "success" then
  587.                                     mainText.setText("")
  588.                                     secondText.setText("")
  589.                                     thirdText.setText("")
  590.                                     centerText("You're in", mainText)
  591.                                     centerText(cur["location"] .. "?", secondText)
  592.                                     thirdText.setColor(gColors.blue)
  593.                                     centerText("Options: $$y/n", thirdText)
  594.                                     while true do
  595.                                             local e, msg = os.pullEvent("chat_command")
  596.                                             msg = trimText(msg:lower())
  597.                                             if msg:find("y") then
  598.                                                     return city
  599.                                             elseif msg:find("n") then
  600.                                                     return setupLocation()
  601.                                             end
  602.                                     end
  603.                             else
  604.                                     thirdText.setColor(gColors.red)
  605.                                     centerText("Invalid city!", thirdText)
  606.                     sync()
  607.                             end
  608.                     end
  609.             end
  610.      
  611.             local function mainThread()    
  612.                     local function start()
  613.                             local function welcome()
  614.                                     centerText("Welcome to", mainText)
  615.                                     secondText.setScale(3)
  616.                                     secondText.setX(40)
  617.                                     slowText("Plastic", secondText)
  618.                                     reliableSleep(2)
  619.                                     secondText.setText("")
  620.                                     mainText.setText("")
  621.                                     centerText("Setup", header)
  622.                                     mainText.setText("")
  623.                                     mainText.setColor(gColors.text)
  624.                                     mainText.setY(37)
  625.                                     thirdText.setY(57)
  626.                     sync()
  627.                                     return setupName()
  628.                             end
  629.      
  630.                             if firstTime then
  631.                                     settings["name"] = welcome()
  632.                                     settings["use12hour"] = setupTime()
  633.                                     settings["city"] = setupLocation()
  634.                                     settings["temperature"] = setupTemperature()
  635.                                     settings["showtime"] = "ingame"
  636.      
  637.                                     local f = io.open("/plasticOptions", "w")
  638.                                     f:write(textutils.serialize(settings))
  639.                                     f:close()
  640.                                     closeAnimation()
  641.                                     reliableSleep(1)
  642.                                     shell.run(plasticInstallation, "nodaemon", unpack(tArgs))
  643.                                     error()
  644.                             else
  645.                                     return
  646.                             end
  647.                     end
  648.      
  649.                     local function convert(query)
  650.                             secondText.setX(25)
  651.                             slowText("Powered by STANDS4 APIs", secondText)
  652.                 sync()
  653.                             --reliableSleep(1)
  654.                             --secondText.setX(35)
  655.                             --slowText("Converting... | cancel", secondText)
  656.                             local resp, msg = plasticGet("http://www.stands4.com/services/v2/conv.php?uid=2464&tokenid=lQAygI15b9x34e2L&expression=" .. textutils.urlEncode(query))
  657.                             if resp == "success" then
  658.                                     if tonumber(msg:match("<errorCode>(%d+)")) > 0 then
  659.                                             centerText(trimText(msg:match("<errorMessage>([^<]+)</errorMessage>")), secondText)
  660.                                             return
  661.                                     else
  662.                                             local response = msg:match("<result>([^<]+)</result>")
  663.                                             local replaceable = {["kilogram"] = "kg", ["nautical mile"] = "nmile" , ["megabyte"] = "mb", ["gigabyte"] = "gb", ["kilobyte"] = "kb"
  664.                                                     ,["millimeter"] = "mm", ["centimeter"] = "cm", ["micrometer"] = "Um", ["nanometer"] = "nm", ["terrabyte"] = "tb", ["exabyte"] = "eb",
  665.                                                     ["British"] = "gb", ["kilometer"] = "km", ["hour"] = "h", [" / "] = "/", ["&amp;"] = "", ["deg;"] = ""}
  666.                                             for k,v in pairs(replaceable) do
  667.                                                     response = response:lower():gsub(k,v)
  668.                                                     response = response:lower():gsub(k .. "s",v)
  669.                                             end
  670.                                             if #response >24 then
  671.                                                     local startSearch = response:find("%=")
  672.                                                     local trim = response:find("%.", startSearch)
  673.                                                     local units = response:find("%s", trim)
  674.                                                     local nresponse = response:sub(1, trim+3) .. response:sub(units,-1)
  675.                                                     response = trimText(nresponse)
  676.                                             end
  677.                                             secondText.setX(25)
  678.                                             if #response > 22 then
  679.                                                     slowText(response, secondText)
  680.                                             else
  681.                                                     centerText(response, secondText)
  682.                                             end
  683.                                             return
  684.                                     end
  685.                             elseif resp == "failure" then
  686.                                     centerText("Service not available", secondText)
  687.                                     return
  688.                             elseif resp == "cancel" then
  689.                                     centerText("Request Cancelled", secondText)
  690.                                     return
  691.                             end
  692.                     end
  693.      
  694.                     local function renderWeather(city)
  695.                             local resp, cur, day = nil
  696.                             centerText("Src: World Weather Online",secondText)
  697.                             if not city then
  698.                                     resp, cur, day = getWT(settings["city"], true)
  699.                             else
  700.                                     resp, cur, day = getWT(city, true)
  701.                             end
  702.                             if resp == "success" then
  703.                                             squeezeScreen()
  704.                                             header.setColor(gColors.blue)
  705.                                             centerText(cur["location"], header)
  706.                                             mainText.setColor(gColors.textGray)
  707.                                             centerText(cur["weather"], mainText)
  708.                                             tempText.setX(97)
  709.                                             tempText.setScale(3)
  710.                                             tempText.setY(43)
  711.                                             tempText.setColor(gColors.blue)
  712.                                             slowText(cur["temp"]..settings["temperature"]:upper(), tempText)
  713.                                             secondText.setX(23)
  714.                                             slowText("Humidity: " .. cur["humidity"] .. "%", secondText)
  715.                                             thirdText.setX(23)
  716.                                             slowText("Current", thirdText)
  717.                                             forthText.setX(23)
  718.                                             slowText("$$back/day", forthText)
  719.                                     local function loadCurrent()
  720.                                             centerText(cur["weather"], mainText)
  721.                                             slowText(cur["temp"]..settings["temperature"]:upper(), tempText)
  722.                                             slowText("Current", thirdText)
  723.                                             slowText("$$back/day", forthText)
  724.                                             while true do
  725.                                                     local e, msg = os.pullEvent("chat_command")
  726.                                                     msg = trimText(msg:lower())
  727.                                                     if (msg == "back") or msg == "home" then
  728.                                                             return resetScreen()
  729.                                                     elseif (msg == "day") then
  730.                                                             return loadDay()
  731.                                                     end
  732.                                             end
  733.                                     end
  734.                                     local function loadDay()
  735.                                             centerText(day["weather"], mainText)
  736.                                             slowText(day["high"]..settings["temperature"]:upper(), tempText)
  737.                                             slowText("Low: " .. day["low"] .. (settings["temperature"]:upper()), thirdText)
  738.                                             slowText("$$back/cur", forthText)
  739.                                             while true do
  740.                                                     local e, msg = os.pullEvent("chat_command")
  741.                                                     msg = trimText(msg:lower())
  742.                                                     if (msg == "back") or msg == "home" then
  743.                                                             return resetScreen()
  744.                                                     elseif (msg:find("cur")) then
  745.                                                             return loadCurrent()
  746.                                                     end
  747.                                             end
  748.                                     end
  749.                                     while true do
  750.                                             local e, msg = os.pullEvent("chat_command")
  751.                                             msg = trimText(msg:lower())
  752.                                             if (msg == "back") or msg == "home" then
  753.                                                     return resetScreen()
  754.                                             elseif (msg == "day") then
  755.                                                     return loadDay()
  756.                                             end
  757.                                     end
  758.                             elseif resp == "cancel" then
  759.                                     centerText("Request Cancelled", secondText)
  760.                                     return
  761.                             elseif resp == "invalid" then
  762.                                     centerText("Invalid location!", secondText)
  763.                                     return
  764.                             else
  765.                                     centerText("Service not available", secondText)
  766.                                     return
  767.                             end
  768.                     end
  769.      
  770.                     local function renderDate(city)
  771.                             centerText("Src: World Weather Online",secondText)
  772.                             local resp, cur = getWT(city, true)
  773.                             if resp == "success" then
  774.                                     textScreen()
  775.                                     header.setColor(gColors.blue)
  776.                                     centerText("Current Time/Date", header)
  777.                                     centerText(cur["location"], mainText)
  778.                                     centerText(cur["date"] .. " - " .. cur["time"], secondText)
  779.                                     centerText("$$back", thirdText)
  780.                                     while true do
  781.                                             local e, msg = os.pullEvent("chat_command")
  782.                                             msg = trimText(msg:lower())
  783.                                             if (msg == "back") or (msg == "home") then
  784.                                                     resetScreen()
  785.                                                     return
  786.                                             end
  787.                                     end
  788.                             elseif resp == "cancel" then
  789.                                     centerText("Request Cancelled", secondText)
  790.                                     return
  791.                             elseif resp == "invalid" then
  792.                                     centerText("Invalid location!", secondText)
  793.                                     return
  794.                             else
  795.                                     centerText("Service not available", secondText)
  796.                                     return
  797.                             end
  798.                     end
  799.      
  800.                     local function getEnvs()
  801.                             local newEnv = getfenv(0)
  802.                             newEnv.secondText = secondText
  803.                             newEnv.thirdText = thirdText
  804.                             newEnv.forthText = forthText
  805.                             newEnv.tempText = tempText
  806.                             newEnv.mainText = mainText
  807.                             newEnv.resetScreen = resetScreen
  808.                             newEnv.squeezeScreen = squeezeScreen
  809.                             newEnv.textScreen = textScreen
  810.                             newEnv.slowText = slowText
  811.                             newEnv.centerText = centerText
  812.                             newEnv.plasticGet = plasticGet
  813.                             newEnv.trimText = trimText
  814.                             newEnv.header = header
  815.                             newEnv.plastic = plastic
  816.                             return newEnv
  817.                     end
  818.      
  819.                     local function home()
  820.                             local function resetSecond()
  821.                                     secondText.setText("")
  822.                                     secondText.setColor(gColors.blue)
  823.                                     secondText.setY(57)
  824.                                     secondText.setScale(1)
  825.                             end
  826.                             thirdText.setText("")
  827.                             header.setText("")
  828.                             mainText.setText("")
  829.                             mainText.setColor(gColors.text)
  830.                             mainText.setScale(3)
  831.                             mainText.setY(32)
  832.                             secondText.setText("")
  833.                             secondText.setColor(gColors.blue)
  834.                             secondText.setY(57)
  835.                             displayTime("00:00 --")
  836.                             os.queueEvent("plastic_clock_manager", "show")
  837.                             centerText("Welcome, " .. settings["name"], secondText)
  838.                             while true do
  839.                                     local skipAll = false
  840.                                     local e, msg = os.pullEvent()
  841.                                     if e == "chat_command" then
  842.                                             msg = trimText(msg:lower())
  843.                                             if searchArgs("root") then
  844.                                                     for k,v in pairs(rootApps) do
  845.                                                             local a = loadstring("local cmd = [[" .. msg .. "]]\n" .. v)
  846.                                                             if a then
  847.                                                                     local env = getEnvs()
  848.                                                                     setfenv(a, env)
  849.                                                                     if a() then
  850.                                                                             skipAll = true
  851.                                                                     end
  852.                                                             else
  853.                                                                     print("Failed to run application: ", k)
  854.                                                             end
  855.                                                     end
  856.                                             end
  857.                                             if not(skipAll) then
  858.                                                     if (msg:sub(1,1) == "=") then
  859.                                                             local banned = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
  860.                                                             "p", "q", "r", "s", "t", "u", "v", "w", "x","y", "z"}
  861.                                                             local formula = msg:sub(2, -1):lower()
  862.                                                             for k,v in pairs(banned) do
  863.                                                                     formula = formula:gsub(v, "")
  864.                                                             end
  865.                                                             local func = loadstring("return " .. formula)
  866.                                                             local e, resp = pcall(func)
  867.                                                             if e and ((type(resp) == "number") or (type(resp) == "string")) then
  868.                                                                     centerText("=" .. resp, secondText)
  869.                                                             else
  870.                                                                     centerText("Syntax Error", secondText)
  871.                                                             end
  872.                                                     elseif msg == "help" then
  873.                                                             squeezeScreen()
  874.                                                             header.setColor(gColors.blue)
  875.                                                             centerText("Help, Try: $$", header)
  876.                                                             centerText("=12*3, home, irl, restart,", mainText)
  877.                                                             centerText("igl, quit, update, weather,", secondText)
  878.                                                             centerText("time/weather for city,", thirdText)
  879.                                                             centerText("convert x to y (back)", forthText)
  880.                                 sync()
  881.                                                             while true do
  882.                                                                     local _, msg = os.pullEvent("chat_command")
  883.                                                                     msg = trimText(msg:lower())
  884.                                                                     if (msg == "back") or (msg == "home") then
  885.                                                                             resetScreen()
  886.                                                                             break
  887.                                                                     end
  888.                                                             end
  889.                                                     elseif msg == "about" then
  890.                                                             secondText.setX(32)
  891.                                                             slowText("Plastic B v0.11 by 1lann", secondText)
  892.                                 sync()
  893.                                                     elseif (msg:sub(1,8) == "weather ") or msg == "weather" then
  894.                                                             local location = nil
  895.                                                             if msg:sub(1,12) == "weather for " then
  896.                                                                     location = trimText(msg:sub(13, -1))
  897.                                                             elseif msg:sub(1,11) == "weather in " then
  898.                                                                     location = trimText(msg:sub(12, -1))
  899.                                                             elseif msg == "weather" then
  900.                                                                     location = ""
  901.                                                             else
  902.                                                                     location = trimText(msg:sub(9, -1))
  903.                                                             end
  904.                                                             if location == "" then
  905.                                                                     renderWeather()
  906.                                                             else
  907.                                                                     renderWeather(location)
  908.                                                             end
  909.                                                     elseif (msg:sub(1,8) == "time in ") or (msg:sub(1,9) == "time for ") or (msg:sub(1,9) == "date for ") then
  910.                                                             local location = nil
  911.                                                             if (msg:sub(1,8) == "time in ") then
  912.                                                                     location = msg:sub(9,-1)
  913.                                                             else
  914.                                                                     location = msg:sub(10, -1)
  915.                                                             end
  916.                                                             if location == "" then
  917.                                                                     centerText("No city speicifed!", secondText)
  918.                                     sync()
  919.                                                             else
  920.                                                                     renderDate(location)
  921.                                                             end
  922.                                                     elseif msg == "settings" then
  923.                                                             local exit = false
  924.                                                             while true do
  925.                                                                     local noSave = false
  926.                                                                     textScreen()
  927.                                                                     header.setColor(gColors.blue)
  928.                                                                     centerText("Change Settings", header)
  929.                                                                     centerText("Options: temperature,", mainText)
  930.                                                                     centerText("time format, name,", secondText)
  931.                                                                     centerText("location, back", thirdText)
  932.                                     sync()
  933.                                                                     while true do
  934.                                                                             local _, msg = os.pullEvent("chat_command")
  935.                                                                             msg = trimText(msg:lower())
  936.                                                                             if (msg == "back") or (msg == "home") then
  937.                                                                                     resetScreen()
  938.                                                                                     exit = true
  939.                                                                                     break
  940.                                                                             elseif (msg == "temperature") then
  941.                                                                                     mainText.setText("")
  942.                                                                                     secondText.setText("")
  943.                                                                                     thirdText.setText("")
  944.                                                                                     header.setText("")
  945.                                                                                     header.setColor(gColors.textGray)
  946.                                                                                     centerText("Setup", header)
  947.                                                                                     settings["temperature"] = setupTemperature()
  948.                                             sync()
  949.                                                                                     noSave = false
  950.                                                                             elseif (msg == "time format") then
  951.                                                                                     mainText.setText("")
  952.                                                                                     secondText.setText("")
  953.                                                                                     thirdText.setText("")
  954.                                                                                     header.setText("")
  955.                                                                                     header.setColor(gColors.textGray)
  956.                                                                                     centerText("Setup", header)
  957.                                                                                     settings["use12hour"] = setupTime()
  958.                                             sync()
  959.                                                                                     noSave = false
  960.                                                                             elseif (msg == "name") then
  961.                                                                                     mainText.setText("")
  962.                                                                                     secondText.setText("")
  963.                                                                                     thirdText.setText("")
  964.                                                                                     header.setText("")
  965.                                                                                     header.setColor(gColors.textGray)
  966.                                                                                     centerText("Setup", header)
  967.                                                                                     settings["name"] = setupName()
  968.                                                 sync()
  969.                                                                                     noSave = false
  970.                                                                             elseif (msg == "location") then
  971.                                                                                     mainText.setText("")
  972.                                                                                     secondText.setText("")
  973.                                                                                     thirdText.setText("")
  974.                                                                                     header.setText("")
  975.                                                                                     header.setColor(gColors.textGray)
  976.                                                                                     centerText("Setup", header)
  977.                                                                                     settings["city"] = setupLocation()
  978.                                             sync()
  979.                                                                                     noSave = false
  980.                                                                             else
  981.                                                                                     noSave = true
  982.                                                                             end
  983.                                                                             if not(noSave) then
  984.                                                                                     local f = io.open("/plasticOptions", "w")
  985.                                                                                     f:write(textutils.serialize(settings))
  986.                                                                                     f:close()
  987.                                                                                     mainText.setText("")
  988.                                                                                     secondText.setText("")
  989.                                                                                     thirdText.setText("")
  990.                                                                                     header.setText("")
  991.                                                                                     mainText.setColor(gColors.green)
  992.                                                                                     centerText("Settings saved!", mainText)
  993.                                             sync()
  994.                                                                                     sleep(2)
  995.                                                                                     break
  996.                                                                             end
  997.                                                                     end
  998.                                                                     if exit then break end
  999.                                                             end
  1000.                                                     elseif (msg:sub(1,8) == "convert ") then
  1001.                                                             os.queueEvent("plastic_clock_manager", "show")
  1002.                                                             resetSecond()
  1003.                                                             convert(msg)
  1004.                                                     elseif msg == "irl" then
  1005.                                                             if not(settings["showtime"] == "irl") then
  1006.                                                                     secondText.setX(30)
  1007.                                                                     slowText("Changing time... | cancel", secondText)
  1008.                                                                     local resp, time = getTime(settings["city"])
  1009.                                                                     if resp == "success" then
  1010.                                                                             settings["showtime"] = "irl"
  1011.                                                                             os.queueEvent("plastic_clock_manager", "force", time)
  1012.                                                                             os.queueEvent("plastic_clock_manager", "show")
  1013.                                                                             os.queueEvent("plastic_clock_manager", "irl")
  1014.                                                                             displayTime(time)
  1015.                                                                             local f = io.open("/plasticOptions", "w")
  1016.                                                                             f:write(textutils.serialize(settings))
  1017.                                                                             f:close()
  1018.                                                                             centerText("Time is now IRL", secondText)
  1019.    
  1020.                                                                     else
  1021.                                                                             centerText("Could not get IRL time!", secondText)
  1022.                                                                     end
  1023.                                                             else
  1024.                                                                     centerText("Time is already IRL!", secondText)
  1025.                                                             end
  1026.                                                     elseif msg == "ingame" or (msg == "igl") then
  1027.                                                             if not(settings["showtime"] == "ingame") then
  1028.                                                                     settings["showtime"] = "ingame"
  1029.                                                                     os.queueEvent("plastic_clock_manager", "force", textutils.formatTime(os.time(), not(settings["use12hour"])))
  1030.                                                                     os.queueEvent("plastic_clock_manager", "show")
  1031.                                                                     os.queueEvent("plastic_clock_manager", "ingame")
  1032.                                                                     local f = io.open("/plasticOptions", "w")
  1033.                                                                     f:write(textutils.serialize(settings))
  1034.                                                                     f:close()
  1035.                                                                     centerText("Time is now in-game", secondText)
  1036.                                                             else
  1037.                                                                     secondText.setX(30)
  1038.                                                                     slowText("Time is already in-game!", secondText)
  1039.                                                             end
  1040.                                                     elseif msg == "update" then
  1041.                                                             secondText.setX(35)
  1042.                                                             centerText("Updating... | cancel", secondText)
  1043.                                                             local resp, data = plasticGet("http://pastebin.com/raw.php?i=43nHyKXU")
  1044.                                                             if resp == "success" then
  1045.                                                                     local f = io.open(plasticInstallation, "w")
  1046.                                                                     f:write(data)
  1047.                                                                     f:close()
  1048.                                                                     closeAnimation()
  1049.                                                                     reliableSleep(1)
  1050.                                                                     shell.run(plasticInstallation, "nodaemon", unpack(tArgs))
  1051.                                                                     error()
  1052.                                                             elseif resp == "failure" then
  1053.                                                                     centerText("Failed to update!", secondText)
  1054.                                                             elseif resp == "cancel" then
  1055.                                                                     centerText("Request Cancelled", secondText)
  1056.                                                             end
  1057.                                                     elseif (msg == "home") or (msg == "clock") or (msg == "time") then
  1058.                                                             resetScreen()
  1059.                                                     elseif (msg == "restart") then
  1060.                                                             closeAnimation()
  1061.                                                             reliableSleep(1)
  1062.                                                             shell.run(plasticInstallation, "nodaemon", unpack(tArgs))
  1063.                                                             error()
  1064.                                                     elseif (msg == "exit") or (msg == "quit") or (msg == "stop") then
  1065.                                                             closeAnimation()
  1066.                                                             error()
  1067.                                                     else
  1068.                                                             centerText("Unknown Command!", secondText)
  1069.                                                     end
  1070.                                             end
  1071.                                     end
  1072.                             end
  1073.                     end
  1074.      
  1075.                     start()
  1076.                     home()
  1077.             end
  1078.      
  1079.             local function updateWeather()
  1080.                     if sensor then
  1081.                             if worldSensor then
  1082.                                     header.setY(22)
  1083.                                     local data = worldSensor.getTargets()["CURRENT"]
  1084.                                     if data["Thundering"] then
  1085.                                             header.setColor(gColors.textGray)
  1086.                                             header.setX(53)
  1087.                                             header.setText("Thunderstorm")
  1088.                                     elseif data["Raining"] then
  1089.                                             header.setColor(gColors.rain)
  1090.                                             header.setX(80)
  1091.                                             header.setText("Rain")
  1092.                                     elseif data["Daytime"] then
  1093.                                             header.setColor(gColors.yellow)
  1094.                                             header.setX(75)
  1095.                                             header.setText("Sunny")
  1096.                                     else
  1097.                                             header.setColor(gColors.rain)
  1098.                                             header.setX(59)
  1099.                                             header.setText("Clear Night")
  1100.                                     end
  1101.                             else
  1102.                                     header.setColor(gColors.textGray)
  1103.                                     header.setX(35)
  1104.                                     header.setText("Missing world sensor!")
  1105.                             end
  1106.                     end
  1107.             end
  1108.      
  1109.             local function weatherThread()
  1110.                     local timer = os.startTimer(5)
  1111.                     while true do
  1112.                             local e, id = os.pullEvent()
  1113.                             if e == "plastic_clock_manager" then
  1114.                                     if id == "hide" then
  1115.                                             showClock = false
  1116.                                     elseif id == "show" then
  1117.                                             updateWeather()
  1118.                                             timer = os.startTimer(5)
  1119.                                             showClock = true
  1120.                                     end
  1121.                             elseif (e == "timer") and (timer == id) and showClock then
  1122.                                     updateWeather()
  1123.                                     timer = os.startTimer(5)
  1124.                             end
  1125.                     end
  1126.             end
  1127.      
  1128.             local function backgroundThread()
  1129.                     local webUpdate = 0
  1130.                     local resp, time = nil
  1131.                     local lastTimeUpdate = os.clock()
  1132.                     local function updateTime(prevTime, city)
  1133.                             if os.clock() >= lastTimeUpdate+60 then
  1134.                                     webUpdate  = webUpdate + 1
  1135.                                     if webUpdate < 60 then
  1136.                                             if prevTime then
  1137.                                                     local hour,minute,ampm = prevTime:match("^(%d+):(%d+)(.-)$")
  1138.                                                     if ampm == " AM" then
  1139.                                                             if settings["use12hour"] == false then
  1140.                                                                     return getTime(city)
  1141.                                                             end
  1142.                                                             lastTimeUpdate = os.clock()
  1143.                                                             if minute == "59" then
  1144.                                                                     if hour == "11" then
  1145.                                                                             return "success","12:00 PM"
  1146.                                                                     elseif hour == "12" then
  1147.                                                                             return "success","1:00 AM"
  1148.                                                                     else
  1149.                                                                             return "success",tostring(tonumber(hour)+1)..":00 AM"
  1150.                                                                     end
  1151.                                                             else
  1152.                                                                     if #tostring(tonumber(minute)+1) > 1 then
  1153.                                                                             return "success",hour..":"..tostring(tonumber(minute)+1).." AM"
  1154.                                                                     else
  1155.                                                                             return "success",hour..":0"..tostring(tonumber(minute)+1).." AM"
  1156.                                                                     end
  1157.                                                             end
  1158.                                                     elseif ampm == " PM" then
  1159.                                                             if settings["use12hour"] == false then
  1160.                                                                     return getTime(city)
  1161.                                                             end
  1162.                                                             lastTimeUpdate = os.clock()
  1163.                                                             if minute == "59" then
  1164.                                                                     if hour == "11" then
  1165.                                                                             return "success","12:00 AM"
  1166.                                                                     elseif hour == "12" then
  1167.                                                                             return "success","1:00 PM"
  1168.                                                                     else
  1169.                                                                             return "success",tostring(tonumber(hour)+1)..":00 PM"
  1170.                                                                     end
  1171.                                                             else
  1172.                                                                     if #tostring(tonumber(minute)+1) > 1 then
  1173.                                                                             return "success",hour..":"..tostring(tonumber(minute)+1).." PM"
  1174.                                                                     else
  1175.                                                                             return "success",hour..":0"..tostring(tonumber(minute)+1).." PM"
  1176.                                                                     end
  1177.                                                             end
  1178.                                                     else
  1179.                                                             if settings["use12hour"] == true then
  1180.                                                                     return getTime(city)
  1181.                                                             end
  1182.                                                             lastTimeUpdate = os.clock()
  1183.                                                             if minute == "59" then
  1184.                                                                     if hour == "23" then
  1185.                                                                             return "success", "0:00"
  1186.                                                                     else
  1187.                                                                             return "success", tostring(tonumber(hour)+1)..":".."00"
  1188.                                                                     end
  1189.                                                             else
  1190.                                                                     if #tostring(tonumber(minute)+1) > 1 then
  1191.                                                                             return "success",hour..":"..tostring(tonumber(minute)+1)
  1192.                                                                     else
  1193.                                                                             return "success",hour..":0"..tostring(tonumber(minute)+1)
  1194.                                                                     end
  1195.                                                             end
  1196.                                                     end
  1197.                                             else
  1198.                                                     return "failure"
  1199.                                             end
  1200.                                     else
  1201.                                             return getTime(city)
  1202.                                     end
  1203.                             else
  1204.                                     return "success",prevTime
  1205.                             end
  1206.                     end
  1207.                     local clockType = settings["showtime"]
  1208.                     local timerID = nil
  1209.                     local updateTimer = nil
  1210.                     local previousClock = nil
  1211.                     local dynamicSleep = nil
  1212.                     local resp = nil
  1213.                     if settings["showtime"] == "irl" then
  1214.                             resp, time = getTime(settings["city"])
  1215.                             if resp == "success" then
  1216.                                     settings["showtime"] = "irl"
  1217.                                     os.queueEvent("plastic_clock_manager", "force", time)
  1218.                                     os.queueEvent("plastic_clock_manager", "show")
  1219.                                     os.queueEvent("plastic_clock_manager", "irl")
  1220.                                     displayTime(time)
  1221.                                     local f = io.open("/plasticOptions", "w")
  1222.                                     f:write(textutils.serialize(settings))
  1223.                                     f:close()
  1224.      
  1225.                             else
  1226.      
  1227.                             end
  1228.                     end
  1229.                     if clockType == "ingame" then
  1230.                             dynamicSleep = 0.83
  1231.                     else
  1232.                             dynamicSleep = 60
  1233.                     end
  1234.                     timerID = os.clock() + dynamicSleep
  1235.                     os.startTimer(0.83)
  1236.                     updateTimer = 0.83+os.clock()
  1237.                     while true do
  1238.                             if os.clock() >= updateTimer then
  1239.                                             os.startTimer(0.83)
  1240.                                             updateTimer = 0.83+os.clock()
  1241.                             end
  1242.                             local e, command, param =  os.pullEvent()
  1243.                             if e == "plastic_clock_manager" then
  1244.                                     if command == "show" then
  1245.                                             if time then
  1246.                                                     displayTime(time)
  1247.                                             end
  1248.                                             mainText.setColor(gColors.text)
  1249.                                             mainText.setScale(3)
  1250.                                             mainText.setY(32)
  1251.                                             header.setText("")
  1252.                                             showClock = true
  1253.                                             timerID = os.clock()
  1254.                                     elseif command == "hide" then
  1255.                                             header.setText("")
  1256.                                             mainText.setText("")
  1257.                                             timerID = os.clock()+2
  1258.                                             showClock = false
  1259.                                     elseif command == "irl" then
  1260.                                             clockType = "irl"
  1261.                                             dynamicSleep = 60
  1262.                                             timerID = os.clock() + dynamicSleep
  1263.                                     elseif command == "ingame" then
  1264.                                             clockType = "ingame"
  1265.                                             dynamicSleep = 0.83
  1266.                                             timerID = os.clock() + dynamicSleep
  1267.                                     elseif command == "force" then
  1268.                                             if param then
  1269.                                                     time = param
  1270.                                             end
  1271.                                     elseif command == "kill" then
  1272.                                             sleep(100)
  1273.                                     end
  1274.                             elseif os.clock() >= timerID then
  1275.                                     if showClock then
  1276.                                             if clockType == "irl" then
  1277.                                                     resp, time = updateTime(time,settings["city"])
  1278.                                                     if resp ~= "success" then
  1279.                                                             displayTime("ERROR")
  1280.                                                             webUpdate = 60
  1281.                                                     else
  1282.                                                             displayTime(time)
  1283.                                                     end
  1284.                                             elseif clockType == "ingame" then
  1285.                                                     time = textutils.formatTime(os.time(), not(settings["use12hour"]))
  1286.                                                     displayTime(time)
  1287.                                             end
  1288.                                     end
  1289.                                     timerID = os.clock() + dynamicSleep
  1290.                             end
  1291.                     end
  1292.             end
  1293.      
  1294.             local eError, eResp = pcall(function() parallel.waitForAny(mainThread,backgroundThread,weatherThread) end)
  1295.             if eError then
  1296.                     return
  1297.             elseif eResp then
  1298.                     if not(fs.exists("/plasticLog")) then
  1299.                             local f = io.open("/plasticLog", "w") f:write("-- Plastic Error Logs --\n") f:close()
  1300.                     end
  1301.                     local f = io.open("/plasticLog", "a")
  1302.                     f:write(eResp .. "\n")
  1303.                     f:close()
  1304.                     mainText.setText("")
  1305.                     secondText.setText("")
  1306.                     thirdText.setText("")
  1307.                     forthText.setText("")
  1308.                     tempText.setText("")
  1309.                     header.setText("")
  1310.                     header.setY(25)
  1311.                     header.setColor(gColors.red)
  1312.                     centerText("Plastic has crashed", header)
  1313.                     mainText.setY(37)
  1314.                     mainText.setColor(gColors.yellow)
  1315.                     secondText.setColor(gColors.yellow)
  1316.                     thirdText.setColor(gColors.yellow)
  1317.                     mainText.setScale(1)
  1318.                     secondText.setScale(1)
  1319.                     thirdText.setScale(1)
  1320.                     centerText("and will now restart.", mainText)
  1321.                     secondText.setY(47)
  1322.                     centerText("See /plasticLog for", secondText)
  1323.                     thirdText.setY(57)
  1324.                     centerText("more information.", thirdText)
  1325.                     reliableSleep(3)
  1326.                     closeAnimation()
  1327.                     reliableSleep(1)
  1328.                     shell.run(plasticInstallation, "nodaemon", unpack(tArgs))
  1329.                     error()
  1330.             end
  1331.     end
  1332.      
  1333.     local function plasticWrapper()
  1334.             pcall(runPlastic)
  1335.     end
  1336.      
  1337.     --[[function os.pullEvent(lookfor)
  1338.             local data = {os.pullEventRaw()}
  1339.             --if not(data[1] == "timer") and not(data[1] == "ocs_success") then
  1340.                     print(os.clock(), data[1], tostring(data[2]))
  1341.             --end
  1342.             if not lookfor then
  1343.                     return unpack(data)
  1344.             elseif data[1] == lookfor then
  1345.                     return unpack(data)
  1346.             end
  1347.     end]]
  1348.      
  1349.     if not(searchArgs("nodaemon")) then
  1350.             parallel.waitForAny(plasticWrapper, function() shell.run("/rom/programs/shell") end)
  1351.             closeAnimation()
  1352.             term.clear()
  1353.             term.setCursorPos(1,1)
  1354.             print("Thank you for using Plastic Beta v0.11 by 1lann")
  1355.     else
  1356.             runPlastic()
  1357.             closeAnimation()
  1358.             term.clear()
  1359.             term.setCursorPos(1,1)
  1360.             print("Thank you for using Plastic Beta v0.11 by 1lann")
  1361.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement