Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ opencomputers чат by serafim pastebin.com/GBrch6sX
- для проекта computercraft.ru update 05.11.20
- Чат между двумя компьютерами через модем или связанную плату
- Можно одновременно писать и принимать сообщения
- https://i.imgur.com/Lb3wCV6.png
- требования:
- связанная плата или модем
- ]]--
- local port = 7
- 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
- gpu.fill(1, 1, w, h, " ")
- gpu.set(w / 2 - 3, 1, "Чат")
- gpu.set(1, 5, "Enter -- Отправить сообщение")
- gpu.set(1, 6, "Alt -- Смена темы")
- gpu.set(1, 7, "Ctrl -- Выход")
- gpu.set(1, h, "> ")
- if com.isAvailable("tunnel") then
- modem = com.tunnel
- tunnel = true
- gpu.set(1, 10, "связь через связанную плату")
- elseif com.isAvailable("modem") then
- modem = com.modem
- modem.open(port)
- tunnel = false
- gpu.set(1, 10, "связь через модем, порт "..port)
- else
- os.execute("cls")
- print("нет связанной платы или модема")
- 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.set(1, h, "> "..text_buffer)
- if #scren_buffer > h then
- table.remove(scren_buffer)
- end
- end
- local function receive(_,_,_,_,_,message)
- table.insert(scren_buffer,1,"< "..message)
- refresh()
- computer.beep(300, 0.1)
- end
- local function keydown()
- local e,_,char,key = event.pull(1,"key_down")
- if e == "key_down" then
- if key == 28 or key == 156 then--Enter
- if unicode.len(text_buffer) > 0 then
- table.insert(scren_buffer,1,"> "..text_buffer)
- if tunnel then
- modem.send(text_buffer)
- else
- modem.broadcast(port,text_buffer)
- end
- text_buffer = ""
- refresh()
- return
- end
- elseif key == 29 or key == 157 then--Ctrl
- event.ignore("modem_message", receive)
- gpu.setResolution(w, h)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- os.execute("cls")
- os.exit()
- elseif key == 56 or key == 184 then--Alt
- theme = theme + 1
- if theme == 5 then
- theme = 1
- end
- if theme == 1 then
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- elseif theme == 2 then
- gpu.setForeground(0x000000)
- gpu.setBackground(0xFFFFFF)
- elseif theme == 3 then
- gpu.setForeground(0x008000)
- gpu.setBackground(0x000000)
- elseif theme == 4 then
- gpu.setForeground(0x000000)
- gpu.setBackground(0x008000)
- end
- refresh()
- 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, " ")
- end
- pos = unicode.len(text_buffer)+3
- blink = not blink
- if blink then
- gpu.set(pos,h,"█")
- else
- gpu.set(pos,h," ")
- end
- end
- event.listen("modem_message", receive)
- while true do
- keydown()
- end
Add Comment
Please, Sign In to add comment