Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function monisClr(monis)
- for i,e in pairs(monis) do
- e.clear()
- e.setCursorPos(1, 1)
- end
- end
- function monisWrite(monis,str,clr)
- if monis == nil then return end
- for i,e in pairs(monis) do
- if e.isColor() then e.setTextColor(clr) end
- e.write(str)
- if e.isColor() then e.setTextColor(colors.white) end
- local x,y = e.getCursorPos()
- y = y + 1
- e.setCursorPos(1, y)
- end
- end
- function loadLog()
- local r = {}
- if not fs.exists("log") then return r end
- local f = fs.open("log","r")
- local name = f.readLine()
- while name do
- local time = datetime.new()
- time:setTime(f.readLine())
- r[#r+1] = {name,time}
- name = f.readLine()
- end
- f.close()
- return r
- end
- -- function addLog(name,time)
- -- local f = fs.open("log","a")
- -- f.writeLine(name)
- -- f.writeLine(time)
- -- f.close()
- -- end
- function addLog(addname)
- local newTime = datetime.new()
- newTime:copy(AnchorTime)
- newTime:addTime(os.clock()-AnchorClock)
- --write(addname.." : "..newTime:writeUTC())
- log[#log+1] = {addname,newTime}
- local f = fs.open("log","a")
- f.writeLine(addname)
- f.writeLine(newTime.unix)
- f.close()
- end
- datetime = {}
- datetime.new = function()
- local this = {}
- this.unix = 0
- this.timezone=9
- this.getUTC = function(self)
- local serverRev = http.get("http://www.timeapi.org/utc/now\?format=\\s")
- if not serverRev or not serverRev.readAll then return end
- local serverData = serverRev.readAll()
- --write("test "..serverData)
- if string.match(serverData,"%d+") then
- self.unix = tonumber(serverData)
- end
- end
- this.setTime = function(self,newtime)
- if type(newtime) ~= "number" then
- newtime = tonumber(newtime)
- end
- self.unix = newtime
- end
- this.addTime = function(self,add)
- self.unix = self.unix + add
- end
- this.copy = function(self,c)
- self.unix = c.unix
- end
- this.setTimeFromFormat = function(self,year,month,day,hour,min,sec)
- local fyear = math.floor(( year - 1970 ) / 4)
- local oyear = year - 1970 - fyear * 4
- local months = {31,28,31,30,31,30,31,31,30,31,30,31}
- local addMday = 0
- for i = 1, month - 1 do
- addMday = addMday + months[i]
- end
- self.unix = fyear * 31557600 * 4 + oyear * 365*24*60*60 + (addMday + day - 1)*24*60*60 + hour * 60*60 + min * 60 + sec
- end
- this.sendYMDHMS = function(self,offset)
- if not offset then offset = 0 end
- local target_time = self.unix + self.timezone * 60*60 + offset
- -- /4y
- local fyear = math.floor( target_time / 31557600 / 4 )
- local mod = target_time - fyear * 31557600 * 4
- -- /1y
- local year = math.floor( mod / (365*24*60*60) )
- local mod = mod - year * 365*24*60*60
- --write("\nyear "..1970+fyear*4+year.." f"..fyear.." y"..year)
- -- /manth
- local days = math.floor( mod / (24*60*60) )
- mod = mod - days * 24*60*60
- --write("\nday "..days)
- -- 1 2 3 4 5 6 7 8 9 10 11 12
- local months = {31,28,31,30,31,30,31,31,30,31,30,31}
- local month = 1
- for i, v in ipairs(months) do
- if days + 1 < v then
- break
- else
- days = days - v
- month = month + 1
- end
- end
- -- /day
- -- /hour
- local hour = math.floor( mod / (60*60) )
- mod = mod - hour * 60*60
- -- /min
- local min = math.floor( mod / (60) )
- local s = mod - min * 60
- -- s
- return string.format("%4d-%02d-%02d %d:%02d:%02d",1970+fyear*4+year,month,days+1,hour,min,s)
- end
- this.sendYYYYMMDD = function(self,offset)
- if not offset then offset = 0 end
- local target_time = self.unix + self.timezone * 60*60 + offset
- -- /4y
- local fyear = math.floor( target_time / 31557600 / 4 )
- local mod = target_time - fyear * 31557600 * 4
- -- /1y
- local year = math.floor( mod / (365*24*60*60) )
- local mod = mod - year * 365*24*60*60
- --write("\nyear "..1970+fyear*4+year.." f"..fyear.." y"..year)
- -- /manth
- local days = math.floor( mod / (24*60*60) )
- mod = mod - days * 24*60*60
- --write("\nday "..days)
- -- 1 2 3 4 5 6 7 8 9 10 11 12
- local months = {31,28,31,30,31,30,31,31,30,31,30,31}
- local month = 1
- for i, v in ipairs(months) do
- if days + 1 < v then
- break
- else
- days = days - v
- month = month + 1
- end
- end
- return string.format("%4d-%02d-%02d",1970+fyear*4+year,month,days+1)
- end
- this.writeYYYYMMDD = function(self)
- write(self:sendYYYYMMDD())
- end
- this.sendHHMMSS = function(self,offset)
- if not offset then offset = 0 end
- local target_time = self.unix + self.timezone * 60*60 + offset
- -- /4y
- local fyear = math.floor( target_time / 31557600 / 4 )
- local mod = target_time - fyear * 31557600 * 4
- -- /1y
- local year = math.floor( mod / (365*24*60*60) )
- local mod = mod - year * 365*24*60*60
- --write("\nyear "..1970+fyear*4+year.." f"..fyear.." y"..year)
- -- /manth
- local days = math.floor( mod / (24*60*60) )
- mod = mod - days * 24*60*60
- -- /day
- -- /hour
- local hour = math.floor( mod / (60*60) )
- mod = mod - hour * 60*60
- -- /min
- local min = math.floor( mod / (60) )
- local s = mod - min * 60
- -- s
- return string.format("%d:%02d:%02d",hour,min,s)
- end
- this.writeClrHHMMSS = function(self)
- if not offset then offset = 0 end
- local target_time = self.unix + self.timezone * 60*60 + offset
- -- /4y
- local fyear = math.floor( target_time / 31557600 / 4 )
- local mod = target_time - fyear * 31557600 * 4
- -- /1y
- local year = math.floor( mod / (365*24*60*60) )
- local mod = mod - year * 365*24*60*60
- --write("\nyear "..1970+fyear*4+year.." f"..fyear.." y"..year)
- -- /manth
- local days = math.floor( mod / (24*60*60) )
- mod = mod - days * 24*60*60
- -- /day
- -- /hour
- local hour = math.floor( mod / (60*60) )
- mod = mod - hour * 60*60
- -- /min
- local min = math.floor( mod / (60) )
- local s = mod - min * 60
- -- s
- local hourClr = {colors.blue,colors.blue,colors.blue,colors.blue,colors.blue,colors.blue,colors.blue,--7
- colors.yellow,colors.yellow,colors.yellow,colors.yellow,colors.yellow,--12
- colors.orange,colors.orange,colors.orange,colors.orange,colors.orange,colors.orange,
- colors.lightBlue,colors.lightBlue,colors.lightBlue,colors.lightBlue,colors.lightBlue,colors.lightBlue}
- for i, v in ipairs(hourClr) do
- --write(i.." "..v.."+")
- if hour < i then
- term.setTextColor(v)
- write(string.format("%d:%02d:%02d",hour,min,s))
- term.setTextColor(colors.white)
- return
- end
- end
- term.setTextColor(hourClr[24])
- write(string.format("%d:%02d:%02d",hour,min,s))
- term.setTextColor(colors.white)
- end
- this.getBetweenTime = function(self,target,offset)
- if not offset then offset = 0 end
- local rObject = {}
- rObject.brankTime = self.unix - target.unix + offset
- rObject.sendAbout = function(self)
- if math.abs(self.brankTime) < 60 then
- return math.floor(self.brankTime).."s"
- end
- if math.abs(self.brankTime) < 60*60 then
- return math.floor(self.brankTime/60).."m"
- end
- if math.abs(self.brankTime) < 60*60*24 then
- return math.floor(self.brankTime/60/60).."h"
- end
- if math.abs(self.brankTime) < 60*60*24*7 then
- return math.floor(self.brankTime/60/60/24).."day"
- end
- if math.abs(self.brankTime) < 60*60*24*31 then
- return math.floor(self.brankTime/60/60/24/7).."week"
- end
- if math.abs(self.brankTime) < 60*60*24*365 then
- return math.floor(self.brankTime/60/60/24/31).."month"
- end
- return math.floor(self.brankTime/60/60/24/365).."year"
- end
- rObject.writeClrAbout = function(self)
- if math.abs(self.brankTime) < 60 then
- term.setTextColor(colors.lime)
- write( math.floor(self.brankTime).."s")
- term.setTextColor(colors.white)
- return
- end
- if math.abs(self.brankTime) < 60*60 then
- term.setTextColor(colors.green)
- write( math.floor(self.brankTime/60).."m")
- term.setTextColor(colors.white)
- return
- end
- if math.abs(self.brankTime) < 60*60*24 then
- term.setTextColor(colors.pink)
- write( math.floor(self.brankTime/60/60).."h")
- term.setTextColor(colors.white)
- return
- end
- if math.abs(self.brankTime) < 60*60*24*7 then
- term.setTextColor(colors.magenta)
- write( math.floor(self.brankTime/60/60/24).."day")
- term.setTextColor(colors.white)
- return
- end
- if math.abs(self.brankTime) < 60*60*24*31 then
- term.setTextColor(colors.orange)
- write( math.floor(self.brankTime/60/60/24/7).."week")
- term.setTextColor(colors.white)
- return
- end
- if math.abs(self.brankTime) < 60*60*24*365 then
- term.setTextColor(colours.red)
- write( math.floor(self.brankTime/60/60/24/31).."month")
- term.setTextColor(colors.white)
- return
- else
- term.setTextColor(colors.gray)
- write( math.floor(self.brankTime/60/60/24/365).."year" )
- term.setTextColor(colors.white)
- return
- end
- end
- rObject.sendDHMS = function(self)
- local sec = self.brankTime % 60
- local min = (self.brankTime - sec)/60 % 60
- local hour = (self.brankTime -sec - min * 60)/60/60 % 24
- local day = (self.brankTime -sec - min * 60 - hour * 60*60)/60/60/24
- local r = ""
- if day > 0 then
- r = r .. string.format("%dday,",day)
- end
- if hour > 0 or day > 0 then
- r = r .. string.format("%dh,",hour)
- end
- if min > 0 or hour > 0 or day > 0 then
- r = r .. string.format("%dm,",min)
- end
- return r .. string.format("%ds",sec)
- end
- return rObject
- end
- return this
- end
- local monis = {}
- local sensor = nil
- local redstoneSide = ""
- for i, v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "openperipheral_sensor" or peripheral.getType(v) == "turtlesensorencironment" then
- sensor = peripheral.wrap(v)
- end
- if peripheral.getType(v) == "monitor" then
- monis[#monis + 1] = peripheral.wrap(v)
- end
- if peripheral.getType(v) == "computer" then
- redstoneSide = v
- end
- end
- if not sensor then
- write("need sensor")
- shell.exit()
- end
- local visiter = {}
- log = loadLog()
- local redraw = os.clock()
- local cursol = 0
- local timer = os.clock()
- os.startTimer(1)
- local owner = "Ganeesya"
- local ClockTypeIsLag = true
- AnchorTime = datetime.new()
- AnchorTime:getUTC()
- AnchorClock = os.clock()
- while true do
- if redraw < os.clock() then
- term.clear()
- term.setCursorPos(1, 1)
- write("Visiters now:"..AnchorTime:sendYMDHMS(os.clock()-AnchorClock).."/"..cursol.."\n")
- for i = #log - cursol, math.max(#log - cursol-16,1), -1 do
- write( string.format("%03d %15s",i,log[i][1]))
- local nowx,nowy = term.getCursorPos()
- term.setCursorPos(20,nowy)
- if ClockTypeIsLag then
- --write( string.format(": %s\n",AnchorTime:getBetweenTime(log[i][2],os.clock()-AnchorClock):writeAbout()))
- write(": ")
- AnchorTime:getBetweenTime(log[i][2],os.clock()-AnchorClock):writeClrAbout()
- write("\n")
- else
- --write( string.format(": %s\n",log[i][2]:writeUTC()))
- write(": ")
- log[i][2]:writeYYYYMMDD()
- write(" ")
- log[i][2]:writeClrHHMMSS()
- write("\n")
- end
- end
- redraw = os.clock() + 1
- end
- local eve = {os.pullEvent()}
- if eve[1] == "key" and eve[2] == 200 then -- "up"
- cursol = cursol - 1
- redraw = os.clock() - 1
- end
- if eve[1] == "key" and eve[2] == 208 then -- "down"
- cursol = cursol + 1
- redraw = os.clock() - 1
- end
- if eve[1] == "key" and eve[2] == 203 then -- "right"
- ClockTypeIsLag = not ClockTypeIsLag
- redraw = os.clock() - 1
- end
- if eve[1] == "key" and eve[2] == 205 then -- "left"
- ClockTypeIsLag = not ClockTypeIsLag
- redraw = os.clock() - 1
- end
- if cursol < 0 then
- cursol = 0
- end
- if cursol > #log then
- cursol = #log
- end
- if eve[1] == "timer" then
- if timer < os.clock() then
- local pls = sensor.getPlayers()
- local newVis = {}
- local newComer = false
- for k, v in pairs(pls) do
- if v.name ~= owner then
- newVis[v.name] = 1
- if not visiter[v.name] then
- addLog(v.name)
- newComer = true
- end
- end
- end
- if newComer == true then
- monisClr(monis)
- for k, v in pairs(newVis) do
- monisWrite(monis,"Hi! "..k.." san!",colors.white)
- end
- monisWrite(monis,"this is Ganeesya's home",colors.white)
- if redstoneSide ~= "" then
- redstone.setOutput(redstoneSide,not redstone.getOutput(redstoneSide))
- end
- end
- visiter = newVis
- timer = os.clock() + 1
- end
- -- if alarmT < os.clock() then
- -- if #famima >= alarmS then
- -- alarmT = os.clock() + famima[alarmS].w * 1.5
- -- playSound(notes,famima[alarmS])
- -- alarmS = alarmS + 1
- -- end
- -- end
- os.startTimer(0.5)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement