Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local com = require("component")
- local event = require("event")
- local unicode = require("unicode")
- local computer = require("computer")
- local gpu = com.gpu
- local w, h = gpu.getViewport()
- local scren_buffer = {}
- local text_buffer = ""
- local theme = 1
- local modem,tunnel
- local nick,port
- local component = require("component")
- local caesar = require("caesar")
- local ping = 0
- local strings = {}
- local string_ch=1
- local working=true
- local note = require("note")
- local function connect()
- os.execute("cls")
- print()
- gpu.setBackground(0x222222)
- gpu.fill(1,1,w,h," ")
- io.write("Ник:")
- nick = io.read()
- io.write("Код чата:")
- port = io.read()
- end
- connect()
- gpu.setBackground(0x222222)
- gpu.fill(1, 1, w, h, " ")
- gpu.set(w / 2 - 3, 1, "Чат")
- gpu.set(1, 5, "Enter -- Отправить сообщение")
- gpu.set(1, 7, "Ctrl -- Выход")
- gpu.set(1, h, "> ")
- if com.isAvailable("tunnel") then
- modem = com.tunnel
- tunnel = true
- gpu.set(1, h-1, "Cвязь через связанную плату")
- elseif com.isAvailable("modem") then
- modem = com.modem
- modem.open(tonumber(port))
- tunnel = false
- gpu.set(1, h-1, "Cвязь через модем")
- else
- os.execute("cls")
- print("нет связанной платы или модема")
- note.play("A3")
- note.play("A3")
- note.play("A3")
- os.exit()
- end
- local function refresh()
- gpu.fill(1, 1, w, h, " ")
- for i, v in ipairs(scren_buffer) do
- gpu.set(1, h-1-i, v)
- end
- gpu.setBackground(0xFF0000)
- gpu.set(1,1,"X")
- gpu.setBackground(0x222222)
- gpu.set(1, h, "> "..text_buffer)
- if #scren_buffer > h then
- table.remove(scren_buffer)
- end
- gpu.set(1,h-1, "Задержка:"..ping.."s")
- end
- local function receive(_,_,_,_,d,msg)
- local old = computer.uptime()
- ping = tonumber(tostring(d / 60):sub(0,3))
- local target = old + ping
- if ping>0 then
- repeat
- until old < target
- end
- local message = caesar.convert(msg, "Enc", port)
- table.insert(scren_buffer,1,"< "..message)
- refresh()
- note.play("D4")
- note.play("B4")
- end
- local function keydown()
- local e,_,char,key = event.pull(1)
- if e == "key_down" then
- if key == 200 then
- string_ch=string_ch+1
- if string_ch > #strings then
- string_ch=string_ch-1
- return
- else
- text_buffer=strings[string_ch]
- end
- elseif key == 208 then
- string_ch=string_ch-1
- if string_ch<1 then
- string_ch=1
- else
- text_buffer=strings[string_ch]
- end
- elseif key == 28 or key == 156 then--Enter
- if unicode.len(text_buffer) > 0 then
- if text_buffer:sub(0,1)=="/" then
- if text_buffer=="/help" then
- table.insert(scren_buffer,1,"Помощь:")
- table.insert(scren_buffer,1,"/help - это сообщение")
- table.insert(scren_buffer,1,"/reconnect - открывает диалог подключения")
- table.insert(scren_buffer,1,"")
- elseif text_buffer=="/reconnect" then
- connect()
- else
- table.insert(scren_buffer,1,"Неверная команда")
- end
- else
- table.insert(scren_buffer,1,">"..nick..": "..text_buffer)
- local buffer = caesar.convert(nick..": "..text_buffer, "Dec", port)
- if tunnel then
- modem.send(buffer)
- else
- modem.broadcast(tonumber(port),buffer)
- end
- end
- table.insert(strings,1,text_buffer)
- string_ch=0
- text_buffer = ""
- refresh()
- return
- end
- elseif key == 29 or key == 157 then--Ctrl
- event.ignore("modem_message", receive)
- gpu.setResolution(w, h)
- event.ignore("touch",mouse)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- note.play("E4")
- note.play("E2")
- os.execute("cls")
- os.exit()
- elseif key == 56 or key == 184 then--Alt
- --игнорируем
- elseif key == 14 then--Backspace
- text_buffer = unicode.sub(text_buffer,0,unicode.len(text_buffer)-1)
- elseif key == 211 then--Del
- text_buffer = ""
- elseif key == 42 or key == 54 then--Shift
- --игнорировать нажатие
- elseif unicode.len(text_buffer) <= w-3 then
- text_buffer = text_buffer..unicode.char(char)
- end
- blink = false
- gpu.set(1, h, "> "..text_buffer)
- gpu.fill(unicode.len(text_buffer)+3, h, w, 1, " ")
- else
- return
- end
- pos = unicode.len(text_buffer)+3
- blink = not blink
- if blink then
- gpu.set(pos,h,"█")
- else
- gpu.set(pos,h," ")
- end
- end
- local function mouse(_,_,x,y)
- if x == 1 then
- if y == 1 then
- event.ignore("modem_message",receive)
- event.ignore("touch",mouse)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- note.play("E4")
- note.play("E2")
- os.execute("cls")
- working=false
- os.exit()
- end
- end
- end
- event.listen("modem_message", receive)
- event.listen("touch", mouse)
- while working do
- keydown()
- end
Add Comment
Please, Sign In to add comment