Advertisement
natie3

eventLogger

Oct 22nd, 2014
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.70 KB | None | 0 0
  1. function write(screen,x,y,text)
  2.   screen.setCursorPos(x,y)
  3.   screen.write(text)
  4. end
  5.  
  6. function clearEventLog()
  7.   eventLog.setBackgroundColor(colors.black)
  8.   eventLog.clear()
  9.   for y = 1, 20 do
  10.     for x = 1, 63 do
  11.       if y == 1 then
  12.         eventLog.setBackgroundColor(colors.blue)
  13.       elseif y%2 == 0 then
  14.         eventLog.setBackgroundColor(colors.gray)
  15.       else
  16.         eventLog.setBackgroundColor(colors.black)
  17.       end
  18.       eventLog.setCursorPos(x,y)
  19.       eventLog.setTextColor(colors.white)
  20.       if x == 9 or x == 30 then
  21.         eventLog.write("|")
  22.       else
  23.         eventLog.write(" ")
  24.       end
  25.     end
  26.   end
  27.   eventLog.setBackgroundColor(colors.blue)
  28.   eventLog.setTextColor(colors.black)
  29.   write(eventLog,3,1,"Time")
  30.   write(eventLog,16,1,"Object")
  31.   write(eventLog,46,1,"Event")
  32. end  
  33.  
  34. function testData()
  35.   object1 = { time = "21:23", category = "turtle", object = "turtle:mining", event = "turtle ran out of fuel" }
  36.   object2 = { time = "22:11", category = "energy", object = "energyLevel", event = "energylevel reached below 20%" }
  37.   object3 = { time = "22:34", category = "item", object = "redstone", event = "now less than 10.000 redstone" }
  38.   object4 = { time = "22:56", category = "command", object = "command", event = "redstone farm turned off" }
  39.   object5 = { time = "23:03", category = "screen", object = "screen241", event = "natie3 is using screen241" }
  40.   object6 = { time = "23:23", category = "turtle", object = "turtle:felling", event = "turtle ran out of fuel" }
  41.   object7 = { time = "23:37", category = "energy", object = "energyLevel", event = "energylevel reached below 20%" }
  42.   object8 = { time = "23:46", category = "item", object = "diamond", event = "now less than 1.000 diamond" }
  43.   object9 = { time = "23:59", category = "command", object = "command", event = "all doors are now locked" }
  44.   object10 = { time = "00:12", category = "screen", object = "screen145", event = "c00lgamer is using screen145" }
  45.   table.insert(events,object1)  
  46.   table.insert(events,object2)
  47.   table.insert(events,object3)
  48.   table.insert(events,object4)
  49.   table.insert(events,object5)
  50.   table.insert(events,object6)
  51.   table.insert(events,object7)
  52.   table.insert(events,object8)
  53.   table.insert(events,object9)
  54.   table.insert(events,object10)
  55. end
  56.  
  57. function displayEvents(category)
  58.   clearEventLog()
  59.   current = category
  60.   eventLog.setTextColor(colors.white)
  61.   count = 0
  62.   index = #events
  63.   while count < 19 and index > 0 do
  64.     if category == nil or events[index].category ==  category then
  65.       count = count + 1
  66.       if count%2 == 1 then
  67.         eventLog.setBackgroundColor(colors.gray)
  68.       else
  69.         eventLog.setBackgroundColor(colors.black)
  70.       end
  71.       write(eventLog,2,count+1,events[index].time)
  72.       write(eventLog,11,count+1,events[index].object)
  73.       write(eventLog,32,count+1,events[index].event)
  74.     end
  75.     index = index - 1
  76.   end
  77. end
  78.  
  79. -- initialize main
  80. m = peripheral.wrap("back")
  81. modem = peripheral.wrap("left")
  82. m.setBackgroundColor(colors.green)
  83. m.setTextColor(colors.blue)
  84. m.setTextScale(1)
  85. m.clear()
  86. write(m,31,2,"Event logger")
  87. events = { }
  88. current = nil
  89. modem.open(5000)
  90.  
  91. -- initialize menu
  92. menu = window.create(m,1,4,9,27)
  93. menu.setBackgroundColor(colors.white)
  94. menu.setTextColor(colors.black)
  95. menu.clear()
  96. menu.setBackgroundColor(colors.gray)
  97. write(menu,2,2,"  All  ")
  98. write(menu,2,4,"Energy ")
  99. write(menu,2,6," Items ")
  100. write(menu,2,8,"Turtle ")
  101. write(menu,2,10,"Command")
  102. write(menu,2,12,"Screen ")
  103.  
  104. -- initialize eventLog
  105. eventLog = window.create(m,10,4,63,27)
  106. eventLog.setTextColor(colors.white)
  107. clearEventLog()
  108.  
  109. -- initialize time
  110. http.request("http://www.timeapi.org/utc/in+one+hours")
  111. hour = 0
  112. minute = 0
  113. sec = 0
  114. count = 0
  115.  
  116. displayEvents()
  117.  
  118. while true do
  119.   event = { os.pullEvent() }
  120.   if event[1] == "http_success" then
  121.     count = 0
  122.     timeHandle = event[3]
  123.     time = timeHandle.readLine()
  124.     hour = tonumber(time:sub(12, 13))
  125.     minute = tonumber(time:sub(15, 16))
  126.     sec = tonumber(time:sub(18, 19))
  127.     os.startTimer(60-sec)
  128.     m.setTextColor(colors.white)
  129.     m.setBackgroundColor(colors.green)
  130.     write(m,65,1,string.format("%02d:%02d",hour,minute))
  131.     event[3]:close()
  132.   elseif event[1] == "http_failure" then
  133.     http.request("http://www.timeapi.org/utc/in+one+hours")
  134.   elseif event[1] == "timer" then
  135.     count = count + 1
  136.     if count > 14 then
  137.       http.request("http://www.timeapi.org/utc/in+one+hours")
  138.     else
  139.       os.startTimer(60)
  140.     end
  141.     if minute == 59 then
  142.       if hour == 23 then
  143.         hour = 0
  144.         minute = 0
  145.       else
  146.         minute = 0
  147.         hour = hour + 1
  148.       end
  149.     else
  150.       minute = minute + 1
  151.     end
  152.     m.setBackgroundColor(colors.green)
  153.     write(m,65,1,string.format("%02d:%02d",hour,minute))
  154.   elseif event[1] == "monitor_touch" then
  155.     x = tonumber(event[3])
  156.     y = tonumber(event[4])
  157.     if x < 10 and (y == 5 or y == 7 or y == 9 or y == 11 or y == 13 or y == 15) then
  158.       if y == 5 then
  159.         displayEvents()
  160.       elseif y == 7 then
  161.         displayEvents("energy")
  162.       elseif y == 9 then
  163.         displayEvents("item")
  164.       elseif y == 11 then
  165.         displayEvents("turtle")
  166.       elseif y == 13 then
  167.         displayEvents("command")
  168.       elseif y == 15 then
  169.         displayEvents("screen")
  170.       end
  171.     end
  172.   elseif event[1] == "modem_message" then
  173.     object = textutils.unserialize(event[5])
  174.     if object.type == "event" then
  175.       event = object.event
  176.       if event.category ~= nil and event.object ~= nil and event.event ~= nil then
  177.         event["time"] = string.format("%02d:%02d",hour,minute)
  178.         table.insert(events,event)
  179.         displayEvents(current)
  180.       end
  181.     end
  182.   end
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement