Advertisement
Alakazard12

Listen

Mar 9th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. local AMI
  2. local Bridge
  3. for i,v in pairs(rs.getSides()) do
  4.     if peripheral.getType(v) == "adventure map interface" then
  5.         AMI = peripheral.wrap(v)
  6.     elseif peripheral.getType(v) == "glassesbridge" then
  7.         Bridge = peripheral.wrap(v)
  8.     end
  9. end
  10.  
  11. if not AMI then
  12.     error("No AMI attached", 0)
  13. end
  14. if not Bridge then
  15.     error("No bridge attaches", 0)
  16. end
  17.  
  18. local Log = {}
  19. if fs.exists("log") then
  20.     local File = fs.open("log", "r")
  21.     Log = textutils.unserialize(File.readAll())
  22.     File.close()
  23. end
  24.  
  25. local function Save()
  26.     local File = fs.open("log", "w")
  27.     File.write(textutils.serialize(Log))
  28.     File.close()
  29. end
  30.  
  31. Bridge.clear()
  32. -- local Box = Bridge.addBox(10, 10, 200, 110, 0x000000, 0.5)
  33. local Texts = {}
  34. for i = 1, 10 do
  35.     local Tex = Bridge.addText(10, i * 10, "", 0xFFFFFF)
  36.     table.insert(Texts, Tex)
  37. end
  38.  
  39. local Made = 0
  40.  
  41. local Month, Day, Year, Hour, Minute
  42.  
  43. local function TimeT()
  44.     local NowH = http.get("http://www.timeapi.org/utc/4+hours+before+now?" .. textutils.urlEncode([[\m/\d/\Y/\H/\M]]))
  45.     local Now = NowH.readAll()
  46.     NowH.close()
  47.     for i in string.gmatch(Now, "[^/]+") do
  48.         i = tonumber(i)
  49.         if Month then
  50.             if Day then
  51.                 if Year then
  52.                     if Hour then
  53.                         Minute = i
  54.                     else
  55.                         Hour = i
  56.                     end
  57.                 else
  58.                     Year = i
  59.                 end
  60.             else
  61.                 Day = i
  62.             end
  63.         else
  64.             Month = i
  65.         end
  66.     end
  67.    
  68.     while true do
  69.         sleep(60)
  70.         Minute = Minute + 1
  71.         if Minute == 60 then
  72.             Hour = Hour + 1
  73.             Minute = 0
  74.             if Hour == 25 then
  75.                 Day = Day + 1
  76.                 Hour = 1
  77.             end
  78.         end
  79.     end
  80. end
  81.  
  82. local function PTime()
  83.     return Month .. "/" .. Day .. "/" .. Year .. " " .. Hour .. ":" .. Minute
  84. end
  85.  
  86. local function NewMess(Txt)
  87.     Made = Made + 1
  88.     if Made < 11 then
  89.         Texts[Made].setText(Txt)
  90.     else
  91.         for i = 1, 9 do
  92.             Texts[i].setText(Texts[i + 1].getText())
  93.         end
  94.         Texts[10].setText(Txt)
  95.     end
  96. end
  97.  
  98. local function ListenT()
  99.     repeat sleep(1) until Month
  100.     while true do
  101.         local Event, P1, P2, P3, P4, P5 = os.pullEvent()
  102.         if Event == "player_login" then
  103.             table.insert(Log, {"Login", P1, PTime()})
  104.             Save()
  105.             print("[" .. PTime() .. "] " .. P1 .. " joined.")
  106.             NewMess("[" .. PTime() .. "] " .. P1 .. " joined.")
  107.         elseif Event == "player_logout" then
  108.             table.insert(Log, {"Logout", P1, PTime()})
  109.             Save()
  110.             print("[" .. PTime() .. "] " .. P1 .. " left.")
  111.             NewMess("[" .. PTime() .. "] " .. P1 .. " left.")
  112.         elseif Event == "chat_message" then
  113.             table.insert(Log, {"Message", P1, P2, PTime()})
  114.             Save()
  115.             print("[" .. PTime() .. "] " .. P1 .. ": " .. P2)
  116.             NewMess("[" .. PTime() .. "] " .. P1 .. ": " .. P2)
  117.  
  118.         end
  119.     end
  120. end
  121.  
  122.  
  123. -- [[ Threading ]]
  124.  
  125. local Threads = {{coroutine.create(TimeT)}, {coroutine.create(ListenT)}}
  126. os.queueEvent("Init")
  127.  
  128. while true do
  129.     local Obj = {os.pullEventRaw()}
  130.  
  131.     if Obj[1] == "terminate" then
  132.         break
  133.     end
  134.  
  135.     for On = 1, #Threads do
  136.         local TTab = Threads[On]
  137.         if TTab then
  138.             if TTab[2] and Obj[1] == TTab[2] then
  139.                 local Stat, Err = coroutine.resume(TTab[1], unpack(Obj))
  140.                 if Stat == false then
  141.                     printError(Err)
  142.                     os.queueEvent("terminate")
  143.                     break
  144.                 end
  145.                 TTab[2] = Err
  146.             elseif not TTab[2] then
  147.                 local Stat, Err = coroutine.resume(TTab[1], unpack(Obj))
  148.                 if Stat == false then
  149.                     printError(Err)
  150.                     os.queueEvent("terminate")
  151.                     break
  152.                 end
  153.                 TTab[2] = Err
  154.             end
  155.             if coroutine.status(TTab[1]) == "dead" then
  156.                 table.remove(Threads, On)
  157.                 On = On - 1
  158.             end
  159.         end
  160.     end
  161.     if #Threads == 0 then
  162.         break
  163.     end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement