Advertisement
Guest User

Wireless Chat (OpenComputers) - FIXED

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