RobotBubble

IMClient

Oct 23rd, 2017
45
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local w, h = term.getSize()
  2. local input = ""
  3. local name = ""
  4. local s_rednet = "back"
  5.  
  6. while true do
  7. term.clear()
  8. term.setCursorPos(1, 1)
  9. write( "Connect To Server ID: "..input.."_" )
  10. local event, p1 = os.pullEvent()
  11. if event == "char" then
  12. input = input..p1
  13. elseif event == "key" then
  14. if p1 == 14 then
  15. input = string.sub( input, 1, string.len( input ) - 1 )
  16. elseif p1 == 28 then
  17. serverID = tonumber( input )
  18. break
  19. end
  20. end
  21. end
  22.  
  23. while true do
  24. term.clear()
  25. term.setCursorPos(1, 1)
  26. write( "NickName: "..name.."_" )
  27. local event, p1 = os.pullEvent()
  28. if event == "char" then
  29. name = name..p1
  30. elseif event == "key" then
  31. if p1 == 14 then
  32. name = string.sub( name, 1, string.len( name ) - 1 )
  33. elseif p1 == 28 then
  34. name = tostring( name )
  35. break
  36. end
  37. end
  38. end
  39.  
  40. rednet.open( s_rednet )
  41. rednet.send( serverID, "#CONNECT#"..name )
  42. local cInput = ""
  43. local sBars = ""
  44. local tChatHistory = {}
  45. local cBlink = ""
  46. local cBlinkT = 0
  47.  
  48. for i=1,w - 1 do
  49. sBars = sBars.."-"
  50. end
  51.  
  52. function draw()
  53. while true do
  54. sleep( 0 )
  55. cBlinkT = cBlinkT + 1
  56. if cBlinkT == 5 then
  57. if cBlink == "" then
  58. cBlink = "_"
  59. else
  60. cBlink = ""
  61. end
  62. cBlinkT = 0
  63. end
  64. term.clear()
  65. term.setCursorPos(1, 1)
  66. local calch = h - 4
  67. for i=table.getn( tChatHistory ) - calch,table.getn( tChatHistory ) do
  68. if tChatHistory[i] ~= nil then
  69. write( tChatHistory[i] )
  70. end
  71. end
  72. term.setCursorPos( 1, h - 1 )
  73. write( "> "..cInput..cBlink )
  74. term.setCursorPos( 1, h - 2 )
  75. write( sBars )
  76. end
  77. end
  78.  
  79. function key()
  80. while true do
  81. local event, p1 = os.pullEvent()
  82. if event == "key" then
  83. if p1 == 41 then
  84. rednet.send( serverID, "#QUIT___#"..name )
  85. break
  86. elseif p1 == 14 then
  87. cInput = string.sub( cInput, 1, string.len( cInput ) - 1 )
  88. elseif p1 == 28 then
  89. rednet.send( serverID, "ch_"..cInput )
  90. cInput = ""
  91. end
  92. elseif event == "char" then
  93. cInput = cInput..p1
  94. elseif event == "terminate" then
  95. rednet.send( serverID, "#QUIT___#"..name )
  96. break
  97. end
  98. end
  99. end
  100.  
  101. function receive()
  102. while true do
  103. local event, ID, MSG, DIS = os.pullEvent( "rednet_message" )
  104. if ID == serverID then
  105. tChatHistory[table.getn( tChatHistory ) + 1] = MSG
  106. end
  107. end
  108. end
  109.  
  110. parallel.waitForAny( draw, key, receive )
RAW Paste Data Copied