eniallator

Scanner Server Program

Jun 14th, 2016
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 0 0
  1. scanner = peripheral.wrap("left")
  2. rednet.open("right")
  3. timeInterval = 1
  4. logOutputFile = "playerLog"
  5. newLogTitle = "New logging session started."
  6. permittedIDs = {}
  7. -- To add a new id, just put the id into the table above as a value
  8.  
  9. time = {m = -timeInterval, h = 0, d = 0}
  10.  
  11. function main()
  12.   while true do
  13.  
  14.     players = scanner.getPlayers()
  15.     time.m = time.m + timeInterval
  16.  
  17.     if time.m >= 60 then
  18.       time.m = time.m - 60
  19.       time.h = time.h + 1
  20.       if players[1] == nil then logger("Nobody is here.", false) end
  21.     end
  22.  
  23.     if time.h >= 24 then time.h = time.h - 24 time.d = time.d + 1 end
  24.  
  25.     function logger(input, title)
  26.  
  27.       logFile = fs.open(logOutputFile,"a")
  28.       log = input
  29.  
  30.       if title == false then
  31.         log = "[" .. tostring(time.m) .. "/" .. tostring(time.h) .. "/" .. tostring(time.d) .. "] " .. input
  32.       end
  33.  
  34.       print(log)
  35.       logFile.writeLine(log)
  36.       logFile.close()
  37.     end
  38.  
  39.     if time.m == 0 and time.h == 0 and time.d == 0 then logger("\n" .. newLogTitle, true) end
  40.  
  41.     for i=1,#players do
  42.       for key,value in pairs(players[i]) do
  43.         if key == "name" and value ~= nil then
  44.  
  45.           logger(value, false)
  46.  
  47.         end
  48.       end
  49.     end
  50.     sleep(timeInterval * 60)
  51.   end
  52. end
  53.  
  54. function respond()
  55.   while true do
  56.  
  57.     local currID, msg = rednet.receive()
  58.  
  59.     for i=1,#permittedIDs do
  60.       if permittedIDs[i] == currID then
  61.         if msg == "sendScan" then
  62.  
  63.           local logFile = fs.open(logOutputFile,"r")
  64.           logFileContents = logFile.readAll()
  65.           logFile.close()
  66.  
  67.           rednet.send(currID,logFileContents)
  68.           break
  69.         end
  70.       end
  71.     end
  72.   end
  73. end
  74.  
  75. parallel.waitForAll(main,respond)
Advertisement
Add Comment
Please, Sign In to add comment