Advertisement
Maxstripe

glasschat3client

Nov 6th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. -- GlassChat 3 Client
  2. -- By Tiiger87 and Alexandrov01
  3.  
  4. --[[
  5. Variables explained:
  6. clientV = version
  7. clientID = PC ID
  8. clientM = modem
  9. clientB = bridge
  10. clientN = name
  11. clientS = chatroom
  12. ]]--
  13.  
  14. clientV = "3.0.0 BETA"
  15. clientID = os.getComputerID()
  16.  
  17. scroll = {}
  18. scrollEntries = 0
  19.  
  20. startx = 20
  21. x = startx
  22. starty = 9
  23. y = starty
  24. z = 9 -- Space between lines
  25. maxlines = 10 -- Maximum amount of messages allowed in chat before scrolling
  26. startscroll = 99 -- start scroll at .. ?
  27. clientM = nil
  28. clientB = nil
  29.  
  30. -- Checks if computer is an advanced computer
  31. if term.isColor() == false then
  32. print("ERROR: Not an advanced computer! Please check your computer setup.")
  33. error()
  34. end
  35.  
  36. -- Gets terminal bridge and wired modem side
  37. sides = {"top", "bottom", "left", "right", "front", "back"}
  38. for key1, value1 in pairs(sides) do
  39. if peripheral.getType(value1)=="modem" then
  40. clientM = peripheral.wrap(value1)
  41. rednet.open(value1)
  42. elseif peripheral.getType(value1)=="openperipheral_glassesbridge" or peripheral.getType(value1)=="openperipheral_bridge" then
  43. clientB = peripheral.wrap(value1)
  44. clientB_side = value1
  45. end
  46. end
  47.  
  48. -- Checks if modem and bridge are present.
  49. if clientM == nil or clientB == nil then
  50. term.setTextColor(colors.red)
  51. print("ERROR: Missing modem or bridge! Please check your computer setup.")
  52. term.setTextColor(colors.white)
  53. error()
  54. end
  55. clientB.clear()
  56.  
  57. -- Get username from file
  58. if fs.exists("data/username") then
  59. usern = fs.open("data/username", "r")
  60. clientN = usern.readAll()
  61. usern.close()
  62. if clientN == "" then
  63. term.setTextColor(colors.red)
  64. print("ERROR: Empty username in data/username!")
  65. term.setTextColor(colors.white)
  66. error()
  67. end
  68. else
  69. term.setTextColor(colors.red)
  70. print("ERROR: Missing username file in data/username!")
  71. term.setTextColor(colors.white)
  72. error()
  73. end
  74.  
  75. -- Get server cluster from file
  76. if fs.exists("data/chatroom") then
  77. servern = fs.open("data/chatroom", "r")
  78. clientS = tonumber ( servern.readAll() )
  79. servern.close()
  80. if clientS == "" then
  81. term.setTextColor(colors.red)
  82. print("ERROR: Empty server ID in data/chatroom!")
  83. term.setTextColor(colors.white)
  84. error()
  85. end
  86. else
  87. term.setTextColor(colors.red)
  88. print("ERROR: Missing server ID file in data/chatroom!")
  89. term.setTextColor(colors.white)
  90. error()
  91. end
  92.  
  93. -- Print basic info to screen
  94. term.clear()
  95. term.setCursorPos(1,1)
  96. term.setTextColor(colors.yellow)
  97. print("GlassChat 3 Client "..clientV)
  98. term.setTextColor(colors.white)
  99. print("ID: "..clientID)
  100. print("Server: "..clientS)
  101. print("Name: "..clientN)
  102. print("----------------------")
  103. print("U: Update this client.")
  104. print("R: Restart this client.")
  105. print("K: Kill this client.")
  106. print("----------------------")
  107.  
  108. -- Catches chat messages and decides what to do
  109. function sendChat()
  110. while true do
  111. -- For old OpenPeripheral versions: e, msg_raw = os.pullEvent("chat_command") --$$ message from chat
  112. e, side, player, uuid, msg_raw = os.pullEvent("glasses_chat_command") -- $$ message from chat
  113. msg_low = string.lower( msg_raw )
  114.  
  115. if msg_low == "help" then
  116. autoscroll("0xDAA520", "GlassChat Commands")
  117. autoscroll("0xDAA520", "All commands start with `$$gc `. Commands:")
  118. autoscroll("0xDAA520", "version Get the version of your client.")
  119. autoscroll("0xDAA520", "reboot Reboot your client.")
  120. autoscroll("0xDAA520", "stop Stop your client.")
  121. autoscroll("0xDAA520", "update Update your client.")
  122. autoscroll("0xDAA520", "nick <name> Change your name.")
  123. autoscroll("0xDAA520", "info List chatroom information.")
  124. autoscroll("0xDAA520", "users List connected users.")
  125. autoscroll("0xDAA520", "hud Add a box to the chat.")
  126.  
  127. elseif string.match(msg_low, "^gc") then
  128.  
  129. if string.match(msg_low, 'reboot$') then
  130. autoscroll("0xDAA520", "Rebooting your client...")
  131. rednet.send(clientS, "!gc leaving")
  132. sleep(1)
  133. shell.run("reboot")
  134. elseif string.match(msg_low, 'stop$') then
  135. autoscroll("0xDAA520", "Stopping your client...")
  136. rednet.send(clientS, "!gc leaving")
  137. error("Exiting glasschat.")
  138. elseif string.match(msg_low, 'update$') then
  139. autoscroll("0xDAA520", "Updating your client...")
  140. rednet.send(clientS, "!gc updating")
  141. shell.run("update")
  142. elseif string.match(msg_low, '^gc nick') then
  143. clientN = string.sub(msg_raw, 9)
  144. rednet.send(clientS, "!gc newusername "..clientN)
  145. elseif string.match(msg_low, '^gc hud') then
  146. autoscroll("0xDAA520", "Applying box..")
  147. clientB.addBox(15, 4, 240, 120, 0x000000, 0.2)
  148. clientB.sync()
  149. else
  150. autoscroll("0xDAA520", "Invalid command! Do $$help")
  151. end
  152.  
  153. else
  154. rednet.send(clientS, msg_raw) --Relays message to server
  155. end
  156. end
  157. end
  158.  
  159. -- Recieves rednet messages and decides what to do
  160. function receiveChat()
  161. while true do
  162. senderID, message = rednet.receive() --Recieves rednet message's origin computer ID and the message itself
  163. print(senderID.." - "..message)
  164.  
  165. if string.match(message, "^!gc") then
  166.  
  167. if string.match(message, 'update$') then
  168. autoscroll("0xFFFF00", "GlassChat Server - Updating your client...")
  169. rednet.send(clientS, "!gc updating")
  170. shell.run("update")
  171. elseif string.match(message, 'reboot$') then
  172. autoscroll("0xFFFF00", "GlassChat Server - Rebooting your client...")
  173. rednet.send(clientS, "!gc leaving")
  174. sleep(1)
  175. shell.run("reboot")
  176. else
  177. end
  178.  
  179. elseif string.match(message, "^!sysmsg") then
  180. autoscroll("0xFFFF00", string.sub(message, 9)) --Passes the system message onto autoscroll()
  181. else
  182. autoscroll("0xFFFFFF", message) --Passes the user message onto autoscroll()
  183. end
  184. end
  185. end
  186.  
  187. -- Decides when to scroll the chat and when to make a new line
  188. function autoscroll(color, msg)
  189. table.insert(scroll, color..msg)
  190. if scrollEntries >= maxlines then
  191. -- remove first message from table
  192. --table.remove(scroll, 1)
  193.  
  194. -- replace lines
  195. for key1, value1 in pairs(scroll) do
  196. -- we didn't set the last message yet.
  197. if #scroll == key1 then
  198. print("setting last message")
  199. if _G[key1] == nil then
  200. _G[key1] = clientB.addText(x, y, msg, tonumber(color) )
  201. else
  202. _G[key1].setText(msg)
  203. _G[key1].setColor(tonumber(color))
  204. end
  205. -- finish up
  206. table.remove(scroll, 1)
  207. clientB.sync()
  208. return
  209. end
  210. if key1 == 0 then
  211. -- skip this line.
  212. else
  213. _G[key1-1].setText( string.sub(value1, 9) )
  214. _G[key1-1].setColor( tonumber ( string.sub(value1, 1, 8) ) )
  215. end
  216. end
  217. else
  218. newLine(color, msg)
  219. end
  220. end
  221.  
  222. --Adds a new line on the screen
  223. function newLine(color, msg)
  224. _G[scrollEntries] = clientB.addText(x, y, msg, tonumber(color) )
  225. clientB.sync()
  226. scrollEntries = scrollEntries + 1
  227. y = y + z
  228. end
  229.  
  230. --Changes the title
  231. function changeTitle(title)
  232. title.setText("GlassChat ".. clientV .." - "..title)
  233. end
  234.  
  235. -- Clears the HUD and replaces everything
  236. function refreshHUD()
  237. clientB.clear()
  238. y = starty
  239. x = startx
  240. scrollEntries = 0
  241. sleep(0.1)
  242. title = clientB.addText(x, y, "GlassChat ".. clientV .." - Do $$(msg) to chat!", 0xFFFF00)
  243. clientB.sync()
  244. y = y + z
  245. end
  246.  
  247. -- On keypress does a local command
  248. function localCommands()
  249. while true do
  250. local evt, c = os.pullEvent("char") -- wait for a key press
  251. c = string.lower(c) -- convert to lower case, to register both r & R.
  252. if c == "r" then
  253. print("Client - R pressed. Restarting this client.")
  254. rednet.send(clientS, "!gc leaving")
  255. sleep(1)
  256. os.reboot()
  257. elseif c == "u" then
  258. print("Client - U pressed. Updating this client.")
  259. rednet.send(clientS, "!gc updating")
  260. sleep(1)
  261. shell.run("update")
  262. elseif c == "k" then
  263. print("Client - K pressed. Killing this client.")
  264. rednet.send(clientS, "!gc leaving")
  265. sleep(1)
  266. error()
  267. end
  268. sleep(0)
  269. end
  270. end
  271.  
  272. --Send name to server
  273. rednet.send(clientS, "!gc username "..clientN)
  274.  
  275. -- Refresh HUD
  276. refreshHUD()
  277.  
  278. -- Wait for any function
  279. parallel.waitForAny(sendChat, receiveChat, localCommands)
  280.  
  281.  
  282.  
  283. -- End of GlassChat Client file...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement