NielsUtrecht

c3

Sep 10th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. local baseUrl = "http://localhost:8080/C3/C3Servlet"
  2. local monitor = hydraApi.getMonitor()
  3.  
  4. if(not os.loadAPI("hydraApi")) then
  5.     error("Could not load hydraApi")
  6. end
  7.  
  8. local function getMessages()
  9.     local handle = http.get(baseUrl)
  10.     if(handle == nil) then
  11.         error("Could not get data from " .. baseUrl)
  12.     end
  13.     local data = handle.readAll()
  14.     handle.close()
  15.    
  16.     return textutils.unserialize(data)
  17. end
  18.  
  19. local function writeMessages(monitor, chatList)
  20.     local line = 1
  21.     for k, msg in pairs(chatList["list"]) do
  22.         monitor.setCursorPos(1, line)
  23.         monitor.write(msg["from"])
  24.         monitor.setCursorPos(8, line)
  25.         monitor.write(msg["msg"])      
  26.     end
  27. end
  28.  
  29.  
  30.  
  31. local function command(com)
  32.     print("'"..com.."'")
  33.     if(com == ":quit") then
  34.         error()
  35.     end
  36. end
  37.  
  38. local function clearInput()
  39.     local sizeX, sizeY = term.getSize()
  40.     s = ""
  41.     term.setCursorPos(1,sizeY)
  42.     term.clearLine()
  43.     term.write(">")
  44. end
  45.  
  46. term.clear()
  47. term.setCursorBlink(true)
  48. clearInput()
  49.  
  50. local chatList = getMessages()
  51. monitor.setCursorPos(1, 1)
  52. monitor.write("Messages: " .. chatList["totalMessages"])
  53. writeMessages(monitor, chatList)
  54.  
  55. local s = ""
  56.  
  57. os.startTimer(2)
  58.  
  59. while true do
  60.     event, param1, param2, param3 = os.pullEvent()
  61.  
  62.     if(event == "char") then
  63.         s = s .. param1
  64.         term.write(param1)
  65.     elseif(event == "key") then
  66.         if(param1 == keys.enter) then
  67.             clearInput()
  68.             command(s)
  69.         end
  70.     elseif(event == "timer") then
  71.         os.startTimer(2)
  72.     else
  73.         print(event .. ":" .. param1 .. "," .. param2 .. "," ..param3)
  74.     end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment