Advertisement
Clorith

OnlineController

Dec 20th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. os.loadAPI( "DateAndTime" )
  2. wifi = "left"
  3. monitor = peripheral.wrap( "right" )
  4. timeAPI = "http://www.timeapi.org/utc/now"
  5. names = {}
  6.  
  7. monitor.setTextScale( 1.5 )
  8.  
  9. function stringSplit( str )
  10.     local t = {}
  11.  
  12.     local function helper( word )
  13.         table.insert( t, word )
  14.         return ""
  15.     end
  16.  
  17.     if not str:gsub( "%w+", helper ):find"%S" then
  18.         return t
  19.     end
  20. end
  21.  
  22. function redrawPlayers()
  23.     monitor.clear()
  24.     local line = 0
  25.  
  26.     for player, table in pairs( names ) do
  27.         line = line + 2
  28.         local status = ""
  29.  
  30.         if "true" == names[player]["status"] then
  31.             status = "Online"
  32.         else
  33.             status = "Last seen " .. DateAndTime.output( names[player]["seen"], "H:i (d/m)" )
  34.         end
  35.  
  36.         monitor.setCursorPos( 5, line )
  37.         monitor.write( tostring( player ) .. " - " .. status )
  38.     end
  39. end
  40.  
  41. function getTime()
  42.     local currentTime = http.get( timeAPI )
  43.  
  44.     return currentTime.readAll()
  45. end
  46.  
  47. function parseMsg( id, msg )
  48.     local incomingLine = stringSplit( msg )
  49.  
  50.     if "playerstatus" == incomingLine[1] then
  51.         local player = incomingLine[2]
  52.         local status = incomingLine[3]
  53.  
  54.         if nil == names[player] then
  55.             names[player] = {}
  56.             names[player]["online"] = status
  57.             names[player]["seen"] = "Unknown"
  58.         end
  59.  
  60.         if "false" == status and "true" == names[player]["online"] then
  61.             names[player]["seen"] = getTime()
  62.         end
  63.  
  64.         names[player]["online"] = status
  65.     end
  66.  
  67.     redrawPlayers()
  68. end
  69.  
  70.  
  71. rednet.open( wifi )
  72.  
  73. while true do
  74.     id, msg, time = rednet.receive()
  75.  
  76.     parseMsg( id, msg )
  77. end
  78.  
  79. rednet.close( wifi )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement