serafim7

чат [OpenComputers]

Nov 1st, 2020 (edited)
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. --[[ opencomputers чат by serafim    pastebin.com/GBrch6sX
  2.      для проекта   computercraft.ru        update 05.11.20
  3.  
  4. Чат между двумя компьютерами через модем или связанную плату
  5.  
  6. Можно одновременно писать и принимать сообщения
  7.  
  8. https://i.imgur.com/Lb3wCV6.png
  9.  
  10. требования:
  11. связанная плата или модем
  12. ]]--
  13.  
  14. local port = 7
  15.  
  16. local com = require("component")
  17. local event = require("event")
  18. local unicode = require("unicode")
  19. local computer = require("computer")
  20. local gpu = com.gpu
  21. local w, h = gpu.getViewport()
  22. local scren_buffer = {}
  23. local text_buffer = ""
  24. local theme = 1
  25. local modem,tunnel
  26.  
  27. gpu.fill(1, 1, w, h, " ")
  28. gpu.set(w / 2 - 3, 1, "Чат")
  29. gpu.set(1, 5, "Enter -- Отправить сообщение")
  30. gpu.set(1, 6, "Alt   -- Смена темы")
  31. gpu.set(1, 7, "Ctrl  -- Выход")
  32. gpu.set(1, h, "> ")
  33.  
  34. if com.isAvailable("tunnel") then
  35.   modem = com.tunnel
  36.   tunnel = true
  37.   gpu.set(1, 10, "связь через связанную плату")
  38. elseif com.isAvailable("modem") then
  39.   modem = com.modem
  40.   modem.open(port)
  41.   tunnel = false
  42.   gpu.set(1, 10, "связь через модем, порт "..port)
  43. else
  44.   os.execute("cls")
  45.   print("нет связанной платы или модема")
  46.   os.exit()
  47. end
  48.  
  49. local function refresh()
  50.   gpu.fill(1, 1, w, h, " ")
  51.   for i, v in ipairs(scren_buffer) do
  52.     gpu.set(1, h-1-i, v)
  53.   end
  54.   gpu.set(1, h, "> "..text_buffer)
  55.   if #scren_buffer > h then
  56.     table.remove(scren_buffer)
  57.   end
  58. end
  59.  
  60. local function receive(_,_,_,_,_,message)
  61.   table.insert(scren_buffer,1,"< "..message)
  62.   refresh()
  63.   computer.beep(300, 0.1)
  64. end
  65.  
  66. local function keydown()
  67.   local e,_,char,key = event.pull(1,"key_down")
  68.   if e == "key_down" then
  69.     if key == 28 or key == 156 then--Enter
  70.       if unicode.len(text_buffer) > 0 then
  71.         table.insert(scren_buffer,1,"> "..text_buffer)
  72.         if tunnel then
  73.           modem.send(text_buffer)
  74.         else
  75.           modem.broadcast(port,text_buffer)
  76.         end
  77.         text_buffer = ""
  78.         refresh()
  79.         return
  80.       end
  81.     elseif key == 29 or key == 157 then--Ctrl
  82.       event.ignore("modem_message", receive)
  83.       gpu.setResolution(w, h)
  84.       gpu.setBackground(0x000000)
  85.       gpu.setForeground(0xFFFFFF)
  86.       os.execute("cls")
  87.       os.exit()
  88.     elseif key == 56 or key == 184 then--Alt
  89.       theme = theme + 1
  90.       if theme == 5 then
  91.         theme = 1
  92.       end
  93.       if theme == 1 then
  94.         gpu.setBackground(0x000000)
  95.         gpu.setForeground(0xFFFFFF)
  96.       elseif theme == 2 then
  97.         gpu.setForeground(0x000000)
  98.         gpu.setBackground(0xFFFFFF)
  99.       elseif theme == 3 then
  100.         gpu.setForeground(0x008000)
  101.         gpu.setBackground(0x000000)
  102.       elseif theme == 4 then
  103.         gpu.setForeground(0x000000)
  104.         gpu.setBackground(0x008000)
  105.       end
  106.       refresh()
  107.     elseif key == 14 then--Backspace
  108.       text_buffer = unicode.sub(text_buffer,0,unicode.len(text_buffer)-1)
  109.     elseif key == 211 then--Del
  110.       text_buffer = ""
  111.     elseif key == 42 or key == 54 then--Shift
  112.       --игнорировать нажатие
  113.     elseif unicode.len(text_buffer) <= w-3 then
  114.       text_buffer = text_buffer..unicode.char(char)
  115.     end
  116.     blink = false
  117.     gpu.set(1, h, "> "..text_buffer)
  118.     gpu.fill(unicode.len(text_buffer)+3, h, w, 1, " ")
  119.   end
  120.   pos = unicode.len(text_buffer)+3
  121.   blink = not blink
  122.   if blink then
  123.     gpu.set(pos,h,"█")
  124.   else
  125.     gpu.set(pos,h," ")
  126.   end
  127. end
  128.  
  129. event.listen("modem_message", receive)
  130. while true do
  131.   keydown()
  132. end
Add Comment
Please, Sign In to add comment