Advertisement
leonardi

VisitorDB

Oct 16th, 2013
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.99 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor")
  2.  
  3. -- Wrap the sensor
  4.  
  5. for a,b in pairs(rs.getSides()) do
  6.   if peripheral.getType(b) == 'sensor' then
  7.    prox = sensor.wrap(b)
  8.    break
  9.   end
  10. end
  11.  
  12. -- Create database if it doesn't exist
  13.  
  14. if fs.exists("database") == false then
  15.         term.clear()
  16.         term.setCursorPos(11,4)
  17.         print("Enter your In-game name here")
  18.         term.setCursorPos(20,6)
  19.         owner = read()
  20.         ini = fs.open("database", "a")
  21.         ini.writeLine(owner)
  22.         ini.close()
  23.  end
  24.  
  25. term.clear()
  26. term.setCursorPos(13,4)
  27. print("Now logging to database \n open the file 'database' to see who visited")
  28.  
  29. -- Loop through the database and store data in table
  30.  
  31. function readTable()
  32.         h = fs.open("database", "r")
  33.         l = {}
  34.         count = 0
  35.         while true do
  36.                 local line = h.readLine()
  37.                         if line == nil then
  38.                                 break
  39.                         end
  40.         count = count + 1
  41.         table.insert(l, line)
  42.         end
  43. end
  44.  
  45. -- Run the function one time to get everything into the table
  46. readTable()
  47.  
  48. while true do
  49. owner = (l[1])
  50. example = http.get("http://www.timeapi.org/gmt/in+2+hours")
  51. everything = tostring(example.readAll())
  52. example.close()
  53. date = string.sub(everything, 1,10)
  54. time = string.sub(everything, 12,19)
  55.  
  56. local targets = prox.getTargets()
  57.         for name, basicDetails in pairs(targets) do
  58.          lastLine = string.find(l[count], name)
  59.                 if lastLine == nil and name ~= owner then
  60.                         p = fs.open("database", "a")
  61.                         p.writeLine(name .. " " .. time .. " " .. date)
  62.                         p.close()
  63.                         table.insert(l, name .. " " .. time .. " " .. date)
  64.                         count = count + 1
  65.                         break
  66.                 end
  67.         end
  68. -- Change this if you want to check for names more than once every 10 seconds. (Time API might not like it)
  69. sleep(10)
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement