Advertisement
Ganeesya

senser

Apr 8th, 2015
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.44 KB | None | 0 0
  1. function monisClr(monis)
  2.     for i,e in pairs(monis) do
  3.         e.clear()
  4.         e.setCursorPos(1, 1)
  5.     end
  6. end
  7.  
  8. function monisWrite(monis,str,clr)
  9.     if monis == nil then return end
  10.     for i,e in pairs(monis) do
  11.         if e.isColor() then e.setTextColor(clr) end
  12.         e.write(str)
  13.         if e.isColor() then e.setTextColor(colors.white) end
  14.         local x,y = e.getCursorPos()
  15.         y = y + 1
  16.         e.setCursorPos(1, y)
  17.     end
  18. end
  19.  
  20. function loadLog()
  21.     local r = {}
  22.     if not fs.exists("log") then return r end
  23.  
  24.     local f = fs.open("log","r")
  25.     local name = f.readLine()
  26.     while name do
  27.         local time = datetime.new()
  28.         time:setTime(f.readLine())
  29.         r[#r+1] = {name,time}
  30.         name = f.readLine()
  31.     end
  32.     f.close()
  33.     return r
  34. end
  35.  
  36. -- function addLog(name,time)
  37. --  local f = fs.open("log","a")
  38. --  f.writeLine(name)
  39. --  f.writeLine(time)
  40. --  f.close()
  41. -- end
  42.  
  43. function addLog(addname)
  44.     local newTime = datetime.new()
  45.     newTime:copy(AnchorTime)
  46.     newTime:addTime(os.clock()-AnchorClock)
  47.     --write(addname.." : "..newTime:writeUTC())
  48.     log[#log+1] = {addname,newTime}
  49.  
  50.     local f = fs.open("log","a")
  51.     f.writeLine(addname)
  52.     f.writeLine(newTime.unix)
  53.     f.close()
  54. end
  55.  
  56. datetime = {}
  57.  
  58. datetime.new = function()
  59.     local this = {}
  60.     this.unix = 0
  61.     this.timezone=9
  62.  
  63.     this.getUTC = function(self)
  64.         local serverRev = http.get("http://www.timeapi.org/utc/now\?format=\\s")
  65.         if not serverRev or not serverRev.readAll then return end
  66.         local serverData = serverRev.readAll()
  67.         --write("test "..serverData)
  68.         if string.match(serverData,"%d+") then
  69.             self.unix = tonumber(serverData)
  70.         end
  71.     end
  72.  
  73.     this.setTime = function(self,newtime)
  74.         if type(newtime) ~= "number" then
  75.             newtime = tonumber(newtime)
  76.         end
  77.         self.unix = newtime
  78.     end
  79.  
  80.     this.addTime = function(self,add)
  81.         self.unix = self.unix + add
  82.     end
  83.  
  84.     this.copy = function(self,c)
  85.         self.unix = c.unix
  86.     end
  87.  
  88.     this.setTimeFromFormat = function(self,year,month,day,hour,min,sec)
  89.         local fyear = math.floor(( year - 1970 ) / 4)
  90.         local oyear = year - 1970 - fyear * 4
  91.  
  92.         local months = {31,28,31,30,31,30,31,31,30,31,30,31}
  93.  
  94.         local addMday = 0
  95.         for i = 1, month - 1 do
  96.             addMday = addMday + months[i]
  97.         end
  98.         self.unix = fyear * 31557600 * 4 + oyear * 365*24*60*60 + (addMday + day - 1)*24*60*60 + hour * 60*60 + min * 60 + sec
  99.     end
  100.  
  101.     this.sendYMDHMS = function(self,offset)
  102.         if not offset then offset = 0 end
  103.         local target_time = self.unix + self.timezone * 60*60 + offset
  104.         -- /4y
  105.         local fyear = math.floor( target_time / 31557600 / 4 )
  106.         local mod = target_time - fyear * 31557600 * 4
  107.         -- /1y
  108.         local year = math.floor( mod / (365*24*60*60) )
  109.         local mod = mod - year * 365*24*60*60
  110.  
  111.         --write("\nyear "..1970+fyear*4+year.." f"..fyear.." y"..year)
  112.         -- /manth
  113.         local days = math.floor( mod / (24*60*60) )
  114.         mod = mod - days * 24*60*60
  115.  
  116.         --write("\nday "..days)
  117.             --          1  2  3  4  5  6  7  8  9  10 11 12
  118.         local months = {31,28,31,30,31,30,31,31,30,31,30,31}
  119.         local month = 1
  120.         for i, v in ipairs(months) do
  121.             if days + 1 < v then
  122.                 break
  123.             else
  124.                 days = days - v
  125.                 month = month + 1
  126.             end
  127.         end
  128.         -- /day
  129.         -- /hour
  130.         local hour = math.floor( mod / (60*60) )
  131.         mod = mod - hour * 60*60
  132.         -- /min
  133.         local min = math.floor( mod / (60) )
  134.         local s = mod - min * 60
  135.         -- s
  136.         return string.format("%4d-%02d-%02d %d:%02d:%02d",1970+fyear*4+year,month,days+1,hour,min,s)
  137.     end
  138.  
  139.     this.sendYYYYMMDD = function(self,offset)
  140.         if not offset then offset = 0 end
  141.         local target_time = self.unix + self.timezone * 60*60 + offset
  142.         -- /4y
  143.         local fyear = math.floor( target_time / 31557600 / 4 )
  144.         local mod = target_time - fyear * 31557600 * 4
  145.         -- /1y
  146.         local year = math.floor( mod / (365*24*60*60) )
  147.         local mod = mod - year * 365*24*60*60
  148.  
  149.         --write("\nyear "..1970+fyear*4+year.." f"..fyear.." y"..year)
  150.         -- /manth
  151.         local days = math.floor( mod / (24*60*60) )
  152.         mod = mod - days * 24*60*60
  153.  
  154.         --write("\nday "..days)
  155.             --          1  2  3  4  5  6  7  8  9  10 11 12
  156.         local months = {31,28,31,30,31,30,31,31,30,31,30,31}
  157.         local month = 1
  158.         for i, v in ipairs(months) do
  159.             if days + 1 < v then
  160.                 break
  161.             else
  162.                 days = days - v
  163.                 month = month + 1
  164.             end
  165.         end
  166.         return string.format("%4d-%02d-%02d",1970+fyear*4+year,month,days+1)
  167.     end
  168.  
  169.     this.writeYYYYMMDD = function(self)
  170.         write(self:sendYYYYMMDD())
  171.     end
  172.  
  173.     this.sendHHMMSS = function(self,offset)
  174.         if not offset then offset = 0 end
  175.         local target_time = self.unix + self.timezone * 60*60 + offset
  176.         -- /4y
  177.         local fyear = math.floor( target_time / 31557600 / 4 )
  178.         local mod = target_time - fyear * 31557600 * 4
  179.         -- /1y
  180.         local year = math.floor( mod / (365*24*60*60) )
  181.         local mod = mod - year * 365*24*60*60
  182.  
  183.         --write("\nyear "..1970+fyear*4+year.." f"..fyear.." y"..year)
  184.         -- /manth
  185.         local days = math.floor( mod / (24*60*60) )
  186.         mod = mod - days * 24*60*60
  187.  
  188.         -- /day
  189.         -- /hour
  190.         local hour = math.floor( mod / (60*60) )
  191.         mod = mod - hour * 60*60
  192.         -- /min
  193.         local min = math.floor( mod / (60) )
  194.         local s = mod - min * 60
  195.         -- s
  196.         return string.format("%d:%02d:%02d",hour,min,s)
  197.     end
  198.  
  199.     this.writeClrHHMMSS = function(self)
  200.         if not offset then offset = 0 end
  201.         local target_time = self.unix + self.timezone * 60*60 + offset
  202.         -- /4y
  203.         local fyear = math.floor( target_time / 31557600 / 4 )
  204.         local mod = target_time - fyear * 31557600 * 4
  205.         -- /1y
  206.         local year = math.floor( mod / (365*24*60*60) )
  207.         local mod = mod - year * 365*24*60*60
  208.  
  209.         --write("\nyear "..1970+fyear*4+year.." f"..fyear.." y"..year)
  210.         -- /manth
  211.         local days = math.floor( mod / (24*60*60) )
  212.         mod = mod - days * 24*60*60
  213.  
  214.         -- /day
  215.         -- /hour
  216.         local hour = math.floor( mod / (60*60) )
  217.         mod = mod - hour * 60*60
  218.         -- /min
  219.         local min = math.floor( mod / (60) )
  220.         local s = mod - min * 60
  221.         -- s
  222.  
  223.         local hourClr = {colors.blue,colors.blue,colors.blue,colors.blue,colors.blue,colors.blue,colors.blue,--7
  224.                          colors.yellow,colors.yellow,colors.yellow,colors.yellow,colors.yellow,--12
  225.                          colors.orange,colors.orange,colors.orange,colors.orange,colors.orange,colors.orange,
  226.                          colors.lightBlue,colors.lightBlue,colors.lightBlue,colors.lightBlue,colors.lightBlue,colors.lightBlue}
  227.  
  228.         for i, v in ipairs(hourClr) do
  229.             --write(i.." "..v.."+")
  230.             if hour < i then
  231.                 term.setTextColor(v)
  232.                 write(string.format("%d:%02d:%02d",hour,min,s))
  233.                 term.setTextColor(colors.white)
  234.                 return
  235.             end
  236.         end
  237.         term.setTextColor(hourClr[24])
  238.         write(string.format("%d:%02d:%02d",hour,min,s))
  239.         term.setTextColor(colors.white)
  240.     end
  241.  
  242.  
  243.     this.getBetweenTime = function(self,target,offset)
  244.         if not offset then offset = 0 end      
  245.         local rObject = {}
  246.         rObject.brankTime = self.unix - target.unix + offset
  247.  
  248.         rObject.sendAbout = function(self)
  249.             if math.abs(self.brankTime) < 60 then
  250.                 return math.floor(self.brankTime).."s"
  251.             end
  252.             if math.abs(self.brankTime) < 60*60 then
  253.                 return math.floor(self.brankTime/60).."m"
  254.             end
  255.             if math.abs(self.brankTime) < 60*60*24 then
  256.                 return math.floor(self.brankTime/60/60).."h"
  257.             end
  258.             if math.abs(self.brankTime) < 60*60*24*7 then
  259.                 return math.floor(self.brankTime/60/60/24).."day"
  260.             end
  261.             if math.abs(self.brankTime) < 60*60*24*31 then
  262.                 return math.floor(self.brankTime/60/60/24/7).."week"
  263.             end
  264.             if math.abs(self.brankTime) < 60*60*24*365 then
  265.                 return math.floor(self.brankTime/60/60/24/31).."month"
  266.             end
  267.             return math.floor(self.brankTime/60/60/24/365).."year"
  268.         end
  269.  
  270.  
  271.         rObject.writeClrAbout = function(self)
  272.             if math.abs(self.brankTime) < 60 then
  273.                 term.setTextColor(colors.lime)
  274.                 write(  math.floor(self.brankTime).."s")
  275.                 term.setTextColor(colors.white)
  276.                 return
  277.             end
  278.             if math.abs(self.brankTime) < 60*60 then
  279.                 term.setTextColor(colors.green)
  280.                 write(  math.floor(self.brankTime/60).."m")
  281.                 term.setTextColor(colors.white)
  282.                 return
  283.             end
  284.             if math.abs(self.brankTime) < 60*60*24 then
  285.                 term.setTextColor(colors.pink)
  286.                 write(  math.floor(self.brankTime/60/60).."h")
  287.                 term.setTextColor(colors.white)
  288.                 return
  289.             end
  290.             if math.abs(self.brankTime) < 60*60*24*7 then
  291.                 term.setTextColor(colors.magenta)
  292.                 write(  math.floor(self.brankTime/60/60/24).."day")
  293.                 term.setTextColor(colors.white)
  294.                 return
  295.             end
  296.             if math.abs(self.brankTime) < 60*60*24*31 then
  297.                 term.setTextColor(colors.orange)
  298.                 write(  math.floor(self.brankTime/60/60/24/7).."week")
  299.                 term.setTextColor(colors.white)
  300.                 return
  301.             end
  302.             if math.abs(self.brankTime) < 60*60*24*365 then
  303.                 term.setTextColor(colours.red)
  304.                 write(  math.floor(self.brankTime/60/60/24/31).."month")
  305.                 term.setTextColor(colors.white)
  306.                 return
  307.             else           
  308.                 term.setTextColor(colors.gray)
  309.                 write( math.floor(self.brankTime/60/60/24/365).."year" )
  310.                 term.setTextColor(colors.white)
  311.                 return
  312.             end
  313.         end
  314.  
  315.         rObject.sendDHMS = function(self)
  316.             local sec = self.brankTime % 60
  317.             local min = (self.brankTime - sec)/60 % 60
  318.             local hour = (self.brankTime -sec - min * 60)/60/60 % 24
  319.             local day = (self.brankTime -sec - min * 60 - hour * 60*60)/60/60/24
  320.             local r = ""
  321.             if day > 0 then
  322.                 r = r .. string.format("%dday,",day)
  323.             end
  324.  
  325.             if hour > 0 or day > 0 then
  326.                 r = r .. string.format("%dh,",hour)
  327.             end
  328.  
  329.             if min > 0 or hour > 0 or day > 0 then
  330.                 r = r .. string.format("%dm,",min)
  331.             end
  332.             return r .. string.format("%ds",sec)
  333.         end
  334.  
  335.         return rObject
  336.     end
  337.  
  338.     return this
  339. end
  340.  
  341. local monis = {}
  342. local sensor = nil
  343. local redstoneSide = ""
  344.  
  345. for i, v in pairs(peripheral.getNames()) do
  346.     if peripheral.getType(v) == "openperipheral_sensor" or peripheral.getType(v) == "turtlesensorencironment" then
  347.         sensor = peripheral.wrap(v)
  348.     end
  349.     if peripheral.getType(v) == "monitor" then
  350.         monis[#monis + 1] = peripheral.wrap(v)
  351.     end
  352.     if peripheral.getType(v) == "computer" then
  353.         redstoneSide = v
  354.     end
  355. end
  356.  
  357. if not sensor then
  358.     write("need sensor")
  359.     shell.exit()
  360. end
  361.  
  362. local visiter = {}
  363. log = loadLog()
  364. local redraw = os.clock()
  365. local cursol = 0
  366. local timer = os.clock()
  367. os.startTimer(1)
  368. local owner = "Ganeesya"
  369. local ClockTypeIsLag = true
  370.  
  371. AnchorTime = datetime.new()
  372. AnchorTime:getUTC()
  373. AnchorClock = os.clock()
  374.  
  375. while true do
  376.     if redraw < os.clock() then
  377.         term.clear()
  378.         term.setCursorPos(1, 1)
  379.         write("Visiters now:"..AnchorTime:sendYMDHMS(os.clock()-AnchorClock).."/"..cursol.."\n")
  380.         for i = #log - cursol, math.max(#log - cursol-16,1), -1 do
  381.             write( string.format("%03d %15s",i,log[i][1]))
  382.             local nowx,nowy = term.getCursorPos()
  383.             term.setCursorPos(20,nowy)
  384.             if ClockTypeIsLag then
  385.                 --write( string.format(": %s\n",AnchorTime:getBetweenTime(log[i][2],os.clock()-AnchorClock):writeAbout()))
  386.                 write(": ")
  387.                 AnchorTime:getBetweenTime(log[i][2],os.clock()-AnchorClock):writeClrAbout()
  388.                 write("\n")
  389.             else
  390.                 --write( string.format(": %s\n",log[i][2]:writeUTC()))
  391.                 write(": ")
  392.                 log[i][2]:writeYYYYMMDD()
  393.                 write(" ")
  394.                 log[i][2]:writeClrHHMMSS()
  395.                 write("\n")
  396.             end
  397.         end
  398.         redraw = os.clock() + 1
  399.     end
  400.  
  401.     local eve = {os.pullEvent()}
  402.  
  403.     if eve[1] == "key" and eve[2] == 200 then -- "up"
  404.         cursol = cursol - 1
  405.         redraw = os.clock() - 1
  406.     end
  407.     if eve[1] == "key" and eve[2] == 208 then -- "down"
  408.         cursol = cursol + 1
  409.         redraw = os.clock() - 1
  410.     end
  411.  
  412.     if eve[1] == "key" and eve[2] == 203 then -- "right"
  413.         ClockTypeIsLag = not ClockTypeIsLag
  414.         redraw = os.clock() - 1
  415.     end
  416.     if eve[1] == "key" and eve[2] == 205 then -- "left"
  417.         ClockTypeIsLag = not ClockTypeIsLag
  418.         redraw = os.clock() - 1
  419.     end
  420.  
  421.     if cursol < 0 then
  422.         cursol = 0
  423.     end
  424.     if cursol > #log then
  425.         cursol = #log
  426.     end
  427.  
  428.     if eve[1] == "timer" then
  429.         if timer < os.clock() then
  430.             local pls = sensor.getPlayers()
  431.             local newVis = {}
  432.             local newComer = false
  433.             for k, v in pairs(pls) do
  434.                 if v.name ~= owner then
  435.                     newVis[v.name] = 1
  436.                     if not visiter[v.name] then
  437.                         addLog(v.name)
  438.                         newComer = true
  439.                     end
  440.                 end
  441.             end
  442.             if newComer == true then
  443.                 monisClr(monis)
  444.                 for k, v in pairs(newVis) do
  445.                     monisWrite(monis,"Hi! "..k.." san!",colors.white)
  446.                 end
  447.                 monisWrite(monis,"this is Ganeesya's home",colors.white)
  448.  
  449.                 if redstoneSide ~= "" then  
  450.                     redstone.setOutput(redstoneSide,not redstone.getOutput(redstoneSide))
  451.                 end
  452.             end
  453.             visiter = newVis
  454.             timer = os.clock() + 1
  455.         end
  456.         -- if alarmT < os.clock() then
  457.         --  if #famima >= alarmS then
  458.         --      alarmT = os.clock() + famima[alarmS].w * 1.5
  459.         --      playSound(notes,famima[alarmS])
  460.         --      alarmS = alarmS + 1
  461.         --  end
  462.         -- end
  463.         os.startTimer(0.5)
  464.     end
  465. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement