Advertisement
TraerAlone

Untitled

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