Deftry

Wireless Chat (OpenComputers)

Apr 14th, 2021 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.09 KB | None | 0 0
  1. local com = require("component")
  2. local event = require("event")
  3. local unicode = require("unicode")
  4. local computer = require("computer")
  5. local gpu = com.gpu
  6. local w, h = gpu.getViewport()
  7. local scren_buffer = {}
  8. local text_buffer = ""
  9. local theme = 1
  10. local modem,tunnel
  11. local nick,port
  12. local component = require("component")
  13. local caesar = require("caesar")
  14. local ping = 0
  15. local strings = {}
  16. local string_ch=1
  17. local working=true
  18. local note = require("note")
  19.  
  20.  
  21. local function connect()
  22.   os.execute("cls")
  23.   print()
  24.   gpu.setBackground(0x222222)
  25.   gpu.fill(1,1,w,h," ")
  26.   io.write("Ник:")
  27.   nick = io.read()
  28.   io.write("Код чата:")
  29.   port = io.read()
  30.  
  31. end
  32.  
  33. connect()
  34. gpu.setBackground(0x222222)
  35. gpu.fill(1, 1, w, h, " ")
  36. gpu.set(w / 2 - 3, 1, "Чат")
  37. gpu.set(1, 5, "Enter -- Отправить сообщение")
  38. gpu.set(1, 7, "Ctrl  -- Выход")
  39. gpu.set(1, h, "> ")
  40.  
  41. if com.isAvailable("tunnel") then
  42.   modem = com.tunnel
  43.   tunnel = true
  44.   gpu.set(1, h-1, "Cвязь через связанную плату")
  45. elseif com.isAvailable("modem") then
  46.   modem = com.modem
  47.   modem.open(tonumber(port))
  48.   tunnel = false
  49.   gpu.set(1, h-1, "Cвязь через модем")
  50. else
  51.   os.execute("cls")
  52.   print("нет связанной платы или модема")
  53.   note.play("A3")
  54.   note.play("A3")
  55.   note.play("A3")
  56.   os.exit()
  57. end
  58.  
  59. local function refresh()
  60.   gpu.fill(1, 1, w, h, " ")
  61.   for i, v in ipairs(scren_buffer) do
  62.     gpu.set(1, h-1-i, v)
  63.   end
  64.   gpu.setBackground(0xFF0000)
  65.   gpu.set(1,1,"X")
  66.   gpu.setBackground(0x222222)
  67.   gpu.set(1, h, "> "..text_buffer)
  68.   if #scren_buffer > h then
  69.     table.remove(scren_buffer)
  70.   end
  71.   gpu.set(1,h-1, "Задержка:"..ping.."s")
  72. end
  73.  
  74. local function receive(_,_,_,_,d,msg)
  75.   local old = computer.uptime()
  76.   ping = tonumber(tostring(d / 60):sub(0,3))
  77.   local target = old + ping
  78.   if ping>0 then
  79.   repeat
  80.   until old < target
  81.   end
  82.   local message = caesar.convert(msg, "Enc", port)
  83.  
  84.   table.insert(scren_buffer,1,"< "..message)
  85.   refresh()
  86.   note.play("D4")
  87.   note.play("B4")
  88. end
  89.  
  90. local function keydown()
  91.   local e,_,char,key = event.pull(1)
  92.   if e == "key_down" then
  93.     if key == 200 then
  94.       string_ch=string_ch+1
  95.       if string_ch > #strings then
  96.         string_ch=string_ch-1
  97.         return
  98.       else
  99.         text_buffer=strings[string_ch]
  100.       end
  101.     elseif key == 208 then
  102.       string_ch=string_ch-1
  103.       if string_ch<1 then
  104.         string_ch=1
  105.       else
  106.         text_buffer=strings[string_ch]
  107.       end
  108.     elseif key == 28 or key == 156 then--Enter
  109.       if unicode.len(text_buffer) > 0 then
  110.         if text_buffer:sub(0,1)=="/" then
  111.           if text_buffer=="/help" then
  112.             table.insert(scren_buffer,1,"Помощь:")
  113.             table.insert(scren_buffer,1,"/help - это сообщение")
  114.             table.insert(scren_buffer,1,"/reconnect - открывает диалог подключения")
  115.             table.insert(scren_buffer,1,"")
  116.           elseif text_buffer=="/reconnect" then
  117.             connect()
  118.           else
  119.             table.insert(scren_buffer,1,"Неверная команда")
  120.           end
  121.         else
  122.           table.insert(scren_buffer,1,">"..nick..": "..text_buffer)
  123.           local buffer = caesar.convert(nick..": "..text_buffer, "Dec", port)
  124.           if tunnel then
  125.             modem.send(buffer)
  126.           else
  127.             modem.broadcast(tonumber(port),buffer)
  128.           end
  129.         end
  130.         table.insert(strings,1,text_buffer)
  131.         string_ch=0
  132.         text_buffer = ""
  133.         refresh()
  134.         return
  135.       end
  136.     elseif key == 29 or key == 157 then--Ctrl
  137.       event.ignore("modem_message", receive)
  138.       gpu.setResolution(w, h)
  139.       event.ignore("touch",mouse)
  140.       gpu.setBackground(0x000000)
  141.       gpu.setForeground(0xFFFFFF)
  142.       note.play("E4")
  143.       note.play("E2")
  144.       os.execute("cls")
  145.       os.exit()
  146.     elseif key == 56 or key == 184 then--Alt
  147.       --игнорируем
  148.     elseif key == 14 then--Backspace
  149.       text_buffer = unicode.sub(text_buffer,0,unicode.len(text_buffer)-1)
  150.     elseif key == 211 then--Del
  151.       text_buffer = ""
  152.     elseif key == 42 or key == 54 then--Shift
  153.       --игнорировать нажатие
  154.     elseif unicode.len(text_buffer) <= w-3 then
  155.       text_buffer = text_buffer..unicode.char(char)
  156.     end
  157.     blink = false
  158.     gpu.set(1, h, "> "..text_buffer)
  159.     gpu.fill(unicode.len(text_buffer)+3, h, w, 1, " ")
  160.   else
  161.     return
  162.   end
  163.   pos = unicode.len(text_buffer)+3
  164.   blink = not blink
  165.   if blink then
  166.     gpu.set(pos,h,"█")
  167.   else
  168.     gpu.set(pos,h," ")
  169.   end
  170. end
  171. local function mouse(_,_,x,y)
  172.   if x == 1 then
  173.     if y == 1 then
  174.       event.ignore("modem_message",receive)
  175.       event.ignore("touch",mouse)
  176.       gpu.setBackground(0x000000)
  177.       gpu.setForeground(0xFFFFFF)
  178.       note.play("E4")
  179.       note.play("E2")
  180.       os.execute("cls")
  181.       working=false
  182.       os.exit()
  183.     end
  184.   end
  185. end
  186.  
  187. event.listen("modem_message", receive)
  188. event.listen("touch", mouse)
  189. while working do
  190.   keydown()
  191. end
Add Comment
Please, Sign In to add comment