Advertisement
Guest User

chat for computercraft

a guest
Nov 28th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. function LR()
  4. local n=1
  5. while true do
  6. local x, y=term.getCursorPos()
  7. term.clearLine()
  8. if n==1 then write(">LEFT< RIGHT") else write (" LEFT >RIGHT<") end
  9. term.setCursorPos(x, y)
  10. a, b=os.pullEventRaw()
  11. while a~="key" do a, b=os.pullEventRaw() end
  12. if b==203 and n==2 then n=1 end
  13. if b==205 and n==1 then n=2 end
  14. if b==28 then print("") break end
  15. end
  16. if n==1 then return true end
  17. if n==2 then return false end
  18. return false
  19. end
  20. local chatTable = { [1] = "Welcome to DSC Chat! (/help for commands)" }
  21. local chatLine = 1
  22. local message = ""
  23. local user = "???"
  24. local compID = os.computerID()
  25. local toID = ""
  26. local msgMessage = ""
  27. local msgSent = ""
  28.  
  29. function drawLine()
  30. term.setCursorPos( 1, 17 )
  31. term.clearLine()
  32. for n=1,49 do
  33. io.write("=")
  34. end
  35. term.setCursorPos(1,18)
  36. term.clearLine()
  37. write(username)
  38. write(": ")
  39. write(message)
  40. end
  41. function addChat(str)
  42. if chatLine < 16 then
  43. table.insert(chatTable, str)
  44. chatLine = chatLine + 1
  45. else
  46. for n=1,15 do
  47. chatTable[n] = chatTable[n+1]
  48. end
  49. chatTable[16] = str
  50. end
  51. end
  52. function quit()
  53. os.exit()
  54. end
  55. function showChat()
  56. term.setCursorPos( 1, 1 )
  57. for n=1,chatLine do
  58. term.clearLine()
  59. print(chatTable[n])
  60. end
  61. end
  62. print("Is your modem on the Left or the Right?")
  63. term.setCursorPos(1,18)
  64. if LR() == true then
  65. rednet.open("left")
  66. else
  67. rednet.open("right")
  68. end
  69. term.clear()
  70. repeat
  71. term.setCursorPos(1,1)
  72. print("Enter a username for yourself.")
  73. term.setCursorPos(1,18)
  74. user = read()
  75. if string.len(user) > 9 then
  76. term.setCursorPos(1,2)
  77. print("Name too long! 9 character maximum.")
  78. term.setCursorPos(1,18)
  79. elseif string.len(user) > 0 then
  80. write("Your username is ")
  81. write(user)
  82. print("")
  83. print("Entering Chat...")
  84. username = user
  85. end
  86. until string.len(user) ~=0
  87. sleep(2.0)
  88. term.clear()
  89. drawLine()
  90. rednet.broadcast("")
  91. rednet.broadcast(username .. " enters the room!")
  92. repeat
  93.  
  94. term.setCursorPos(1,1)
  95. write(chatTable[chatLine] or "")
  96. drawLine()
  97. showChat()
  98. term.setCursorPos((3 + string.len(username) + string.len(message)),18)
  99. term.setCursorBlink( true )
  100.  
  101. event, param = os.pullEventRaw()
  102. if event == "key" then
  103. if param == 28 then
  104. if string.sub(message,1,1) == "/" then
  105. if string.sub(message,1,6) == "/name " then
  106. if string.len(message) > 6 then
  107. if string.len(message) < 16 then
  108. user = username
  109. username = string.sub(message,7,string.len(message))
  110. message = "User '" .. user .. "' is now called '" .. username .. "'!"
  111. rednet.broadcast("")
  112. rednet.broadcast(message)
  113. addChat(message)
  114. message = ""
  115. drawLine()
  116. else
  117. addChat("<SYS> Name too long! Maximum 9 characters.")
  118. message = ""
  119. end
  120. end
  121. elseif string.sub(message,1,4) == "/me " then
  122. local meMessage = string.sub(message,4,string.len(message))
  123. message = "*" .. username .. meMessage
  124. rednet.broadcast("")
  125. rednet.broadcast(message)
  126. addChat(message)
  127. message = ""
  128. drawLine()
  129. elseif string.sub(message,1,5) == "/list" then
  130. rednet.broadcast("")
  131. rednet.broadcast(message)
  132. message = ""
  133. elseif string.sub(message,1,5) == "/exit" then
  134. addChat("<SYS> Goodbye!")
  135. rednet.broadcast("")
  136. rednet.broadcast("User '" .. username .. "' exits the room.")
  137. sleep(1.0)
  138. term.clear()
  139. term.setCursorPos(1,1)
  140. break
  141. elseif string.sub(message,1,5) == "/quit" then
  142. addChat("<SYS> Goodbye!")
  143. rednet.broadcast("")
  144. rednet.broadcast("User '" .. username .. "' exits the room.")
  145. sleep(1.0)
  146. term.clear()
  147. term.setCursorPos(1,1)
  148. break
  149. elseif string.sub(message,1,5) == "/help" then
  150. addChat("/help - lists programs")
  151. addChat("/name <name> - changes username")
  152. addChat("/list - lists connected users, and console IDs")
  153. addChat("/me - used as emote")
  154. addChat("/clear - clears the screen")
  155. addChat("/msg <ID> <message> - sends a private message")
  156. addChat("/exit - exits the program")
  157. message = ""
  158. elseif string.sub(message,1,5) == "/msg " then
  159. if string.len(message) > 5 then
  160. if string.sub(message,7,7) == " " then
  161. toID = string.sub(message,6,6)
  162. msgMessage = string.sub(message,8,string.len(message))
  163. addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  164. message = username .. " whispers to you: " .. msgMessage
  165. toID = tonumber(toID)
  166. rednet.send(toID,"")
  167. rednet.send(toID,message)
  168. message = ""
  169. elseif string.sub(message, 8,8) == " " then
  170. toID = string.sub(message,6,7)
  171. msgMessage = string.sub(message,9,string.len(message))
  172. addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  173. message = username .. " whispers to you: " .. msgMessage
  174. toID = tonumber(toID)
  175. rednet.send(toID,"")
  176. rednet.send(toID,message)
  177. message = ""
  178. elseif string.sub(message, 9,9) == " " then
  179. toID = string.sub(message,6,8)
  180. msgMessage = string.sub(message,10,string.len(message))
  181. addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  182. message = username .. " whispers to you: " .. msgMessage
  183. toID = tonumber(toID)
  184. rednet.send(toID,"")
  185. rednet.send(toID,message)
  186. message = ""
  187. elseif string.sub(message,10,10) == " " then
  188. toID = string.sub(message,6,9)
  189. msgMessage = string.sub(message,11,string.len(message))
  190. addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  191. message = username .. " whispers to you: " .. msgMessage
  192. toID = tonumber(toID)
  193. rednet.send(toID,"")
  194. rednet.send(toID,message)
  195. message = ""
  196. end
  197. end
  198. elseif string.sub(message,1,3) == "/m " then
  199. if string.len(message) > 3 then
  200. if string.sub(message,5,5) == " " then
  201. toID = string.sub(message,4,4)
  202. msgMessage = string.sub(message,6,string.len(message))
  203. addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  204. message = username .. " whispers to you: " .. msgMessage
  205. toID = tonumber(toID)
  206. rednet.send(toID,"")
  207. rednet.send(toID,message)
  208. message = ""
  209. elseif string.sub(message, 6,6) == " " then
  210. toID = string.sub(message,4,5)
  211. msgMessage = string.sub(message,7,string.len(message))
  212. addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  213. message = username .. " whispers to you: " .. msgMessage
  214. toID = tonumber(toID)
  215. rednet.send(toID,"")
  216. rednet.send(toID,message)
  217. message = ""
  218. elseif string.sub(message, 7,7) == " " then
  219. toID = string.sub(message,4,6)
  220. msgMessage = string.sub(message,8,string.len(message))
  221. addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  222. message = username .. " whispers to you: " .. msgMessage
  223. toID = tonumber(toID)
  224. rednet.send(toID,"")
  225. rednet.send(toID,message)
  226. message = ""
  227. elseif string.sub(message,8,8) == " " then
  228. toID = string.sub(message,4,7)
  229. msgMessage = string.sub(message,9,string.len(message))
  230. addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  231. message = username .. " whispers to you: " .. msgMessage
  232. toID = tonumber(toID)
  233. rednet.send(toID,"")
  234. rednet.send(toID,message)
  235. message = ""
  236. end
  237. end
  238. elseif string.sub(message,1,6) == "/clear" then
  239. term.clear()
  240. for n=1,16 do
  241. addChat(" ")
  242. end
  243. chatTable = {}
  244. chatLine = 1
  245. message = ""
  246. else
  247. message = ""
  248. end
  249. else
  250. if string.len(message) > 0 then
  251. username = tostring(username)
  252. message = tostring(message)
  253. message = "<" .. username .. "> " .. message
  254. rednet.broadcast("")
  255. rednet.broadcast(message)
  256. addChat(message)
  257. message = ""
  258. drawLine()
  259. end
  260. end
  261. elseif param == 14 then
  262. message = string.sub(message,1,string.len(message)-1)
  263. drawLine()
  264. end
  265. end
  266.  
  267. if event == "char" then
  268. if string.len(message) < 40 then
  269. message = message .. param
  270. end
  271. drawLine()
  272. end
  273.  
  274. if event == "rednet_message" then
  275. id, incoming = rednet.receive(0.5)
  276.  
  277. if incoming == "/list" then
  278. rednet.send(id,"")
  279. rednet.send(id,"User '" .. username .. "' is connected at terminal #" .. compID)
  280. else
  281. addChat(incoming)
  282. end
  283. end
  284.  
  285. until message == "override"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement