Guest User

Untitled

a guest
Nov 9th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. local thread = require("thread")
  2. local computer = require("computer")
  3. local component = require("component")
  4. local event = require("event")
  5. local serialization = require("serialization")
  6. local text = require("text")
  7. local term = require("term")
  8. local unicode = require("unicode")
  9. local gpu = component.gpu
  10.  
  11. local modem, serverAddress, primaryPort
  12. local name, myMessage
  13. local A, B
  14. local sHandler, rHandler
  15.  
  16. local function Registration()
  17. local nickname, password, repetition, _, _, address, _, _, message
  18. term.clear()
  19. term.write("Имя пользователя: ") nickname = text.trim(term.read())
  20. term.write("Пароль: ") password = text.trim(term.read(nil, true, nil, "*"))
  21. term.write("Повторите пароль: ") repetition = text.trim(term.read(nil, true, nil, "*"))
  22. if password ~= repetition then
  23. term.write("\nЗначения полей 'Пароль' и 'Повтор пароля' должны совпадать")
  24. os.sleep(0.5)
  25. else
  26. local user = {[1] = nickname, [2] = password}
  27. local packet = serialization.serialize(user)
  28. modem.open(255)
  29. modem.send(serverAddress, 255, packet)
  30. while address ~= serverAddress do
  31. _, _, address, _, _, message = event.pull("modem_message")
  32. end
  33. modem.close(255)
  34. if type(message) == "number" then
  35. term.clear()
  36. print("Регистрация завершена")
  37. else print(message) end
  38. end
  39. event.pull("key_up")
  40. term.clear()
  41. end
  42.  
  43. local function Authentication()
  44. local nickname, password, _, _, address, _, _, message
  45. term.write("Имя пользователя: ") nickname = text.trim(term.read())
  46. term.write("Пароль: ") password = text.trim(term.read(nil, true, nil, "*"))
  47. local user = {[1] = nickname, [2] = password}
  48. local packet = serialization.serialize(user)
  49. modem.open(256)
  50. modem.send(serverAddress, 256, packet)
  51. while address ~= serverAddress do
  52. _, _, address, _, _, message = event.pull("modem_message")
  53. end
  54. modem.close(256)
  55. if type(message) == "number" then
  56. name = nickname
  57. return message
  58. else
  59. print(message)
  60. os.sleep(0.5)
  61. event.pull("key_up")
  62. term.clear()
  63. return 0
  64. end
  65. end
  66.  
  67. local function Choice()
  68. while true do
  69. print("1. Войти в чат\n2. Регистрация\n3. Выход\n")
  70. local _, _, _, choice = event.pull("key_up")
  71. term.clear()
  72. if choice == 2 then
  73. local authResult = Authentication()
  74. if authResult ~= 0 then return authResult end
  75. end
  76. if choice == 3 then Registration() end
  77. if choice == 4 then break end
  78. end
  79. return 0
  80. end
  81.  
  82. local function Receiver()
  83. local x, y
  84. local _, _, address, port, _, message, mesHeight
  85. local chatWidth = math.floor(A * 0.75)
  86. local onlineLeftBorder = math.floor(A * 0.8) - 1
  87. local onlineWidth = math.floor(A * 0.2) - 1
  88. local onlineCenter = math.floor(A * 0.9) - 1
  89. local online = {}
  90. while true do
  91. _, _, address, port, _, message = event.pull("modem_message")
  92. if address == serverAddress then
  93. address = nil
  94. if port == 253 then
  95. if message == 'P' then modem.send(serverAddress, 253, name)
  96. else online = serialization.unserialize(message) end
  97. else
  98. if message == 'R' or message == 'C' then
  99. term.setCursor(1, B - 3)
  100. if message == 'R' then print(" [Server] Restarting...")
  101. else print(" [Server] Shutting down...") end
  102. thread.kill(sHandler)
  103. term.setCursorBlink(false)
  104. os.sleep(3)
  105. term.clear()
  106. break
  107. else
  108. x, y = term.getCursor()
  109. local unsent = ''
  110. for i=2, x, 1 do unsent = unsent .. gpu.get(i, y) end
  111. gpu.fill(1, B - 2, A, 3, " ") -- clear input textbox
  112. -- move chat up
  113. mesHeight = math.floor(unicode.len(message) / chatWidth) + 1
  114. for i=1, mesHeight, 1 do
  115. term.setCursor(A, B)
  116. term.write(' ', true)
  117. end
  118. term.setCursor(1, B - 3 - mesHeight)
  119. -- print message
  120. message = text.trim(message)
  121. while true do
  122. if unicode.len(message) < chatWidth then print(' ' .. message) break end
  123. print(' ' .. unicode.sub(message, 1, chatWidth))
  124. message = unicode.sub(message, chatWidth + 1)
  125. end
  126. -- online list
  127. gpu.setForeground(0xffffff)
  128. gpu.setBackground(0x008000)
  129. gpu.fill(onlineLeftBorder, 1, onlineWidth, 3, " ")
  130. term.setCursor(onlineCenter - 3, 2) term.write("ONLINE")
  131. gpu.setForeground(0x000000)
  132. gpu.setBackground(0xffffff)
  133. gpu.fill(onlineLeftBorder, 4, onlineWidth, B - 7, " ")
  134. for i=1,#online,1 do
  135. term.setCursor(onlineLeftBorder, i+4)
  136. term.write(' ' .. online[i])
  137. end
  138. gpu.setForeground(0xffffff)
  139. gpu.setBackground(0x000000)
  140. -- input field
  141. gpu.fill(1, B, A, 1, "—")
  142. gpu.fill(1, B - 2, A, 1, "—")
  143. -- setting cursor to start position
  144. term.setCursor(2, B - 1) print(unsent)
  145. term.setCursor(x, B - 1)
  146. if message ~= myMessage then computer.beep(1000, 0.1) end
  147. end
  148. end
  149. end
  150. end
  151. end
  152.  
  153. local function Sender()
  154. local result
  155. local history = {}
  156. while true do
  157. term.setCursor(2, B - 1)
  158. myMessage = term.read(history, false)
  159. result = text.trim(myMessage)
  160. result = text.detab(result, 1)
  161. if result == "exit" then
  162. term.clear()
  163. thread.kill(rHandler)
  164. break
  165. end
  166. if unicode.len(result) > 0 and unicode.len(result) < 256 then
  167. myMessage = name .. ": " .. myMessage
  168. modem.send(serverAddress, primaryPort, myMessage)
  169. end
  170. term.clearLine()
  171. if #history > 5 then table.remove(history, 1) end
  172. end
  173. end
  174.  
  175. local function CheckModem()
  176. if component.isAvailable("modem") == false then return 0 end
  177. modem = component.modem
  178. modem.setStrength(5000)
  179. return 1
  180. end
  181.  
  182. local function CheckConnection()
  183. if CheckModem() == 0 then
  184. print("Не найдена плата беспроводной сети") return 0 end
  185. local serverOn = false
  186. term.clear()
  187. term.write("Соединение")
  188. modem.open(254)
  189. for try = 1, 3 do
  190. modem.broadcast(254, 1)
  191. local _, _, address, _, _, _ = event.pull(3, "modem_message")
  192. if address ~= nil then
  193. serverAddress = address
  194. serverOn = true
  195. break
  196. end
  197. term.write(".")
  198. end
  199. modem.close(254)
  200. term.clear()
  201. if serverOn == true then return 1 end
  202. print("Сервер недоступен")
  203. return 0
  204. end
  205.  
  206. if CheckConnection() == 1 then
  207. local choice = Choice()
  208. if choice > 0 then
  209. primaryPort = choice
  210. modem.open(primaryPort)
  211. modem.open(253)
  212. A, B = gpu.getResolution()
  213. term.clear()
  214. term.setCursor(1, B - 1)
  215. thread.init()
  216. rHandler = thread.create(Receiver)
  217. sHandler = thread.create(Sender)
  218. thread.waitForAll()
  219. modem.close()
  220. elseif choice == -1 then
  221. print("Вы забанены на сервере") end
  222. else
  223. os.sleep(0.5)
  224. event.pull("key_up")
  225. term.clear()
  226. end
Add Comment
Please, Sign In to add comment