Advertisement
VeLLeSSS

00chat.lua

Nov 2nd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. -- Программа похожа на публичные чаты на локальной сетке, бывшие популярными когда-то давно.
  2.  
  3.  
  4. -- [ БИБЛИОТЕКИ И КОМПОНЕНТЫ ] --
  5. local com = require('component')
  6. local term = require('term')
  7. local event = require('event')
  8. local net = com.modem
  9. local gpu = com.gpu
  10. local w, h = gpu.getResolution()
  11. local n = 2
  12. local buffer = {'sample'}
  13. -- [ /////////////////////// ] --
  14.  
  15. function scroll(text) -- Тут у нас буфер для более-менее человеческого сдвига сообщений вверх
  16.  
  17.     table.remove(buffer, 1)
  18.     table.insert(buffer, text)
  19.     for i in 1, #t do
  20.         term.setCursor(2, i+1)
  21.         term.write(nick..' : '..buffer[i])
  22.     end
  23.  
  24. end
  25.  
  26. for i = 1, h-4 do -- Генерируем пустой буфер с количеством ячеек == высоте основного окна
  27.     buffer[i] = ' '
  28. end
  29.  
  30. net.open(33)
  31.  
  32. print("Добро пожаловать в The 00's Chat!")
  33. print('Введите ваш никнейм: ')--, term.read) -- На входе логиним пользователя
  34. nick = term.read()
  35.  
  36. term.clear()
  37.  
  38. -- [ ИНТЕРФЕЙС ] --
  39. gpu.fill(1, 1, w, h, '@')
  40. gpu.fill(2, 2, w-2, h-2, ' ') -- Получаем рамку
  41. gpu.fill(1, h-2, w, 1, '@') -- Отделяем окно ввода от основного окна
  42.  
  43. term.setCursor(2, h-1) -- Ставим курсор в начало окна ввода
  44. term.setCursorBlink()
  45. -- [ ///////// ] --
  46.  
  47. net.broadcast(33, 'Admin: '..nick..' присоединился к беседе.') -- Объявляем другим пользователям
  48.  
  49. while true do
  50.  
  51.     local name, _, _, _, message_in = event.pull(0.5, 'modem_message') -- Приём сообщений
  52.  
  53.     if name == 'modem_message' then
  54.  
  55.         scroll(message_in)
  56.  
  57.     end
  58.  
  59.     local name, _, char = event.pull(0.5, 'key_down')
  60.  
  61.     if name == 'key_down' and char == 'i' then -- Отправка
  62.  
  63.         term.setCursor(2, h-1)
  64.         message_out = term.read()
  65.         net.broadcast(33, nick..' : '..message_out)
  66.         scroll(message_out)
  67.  
  68.     end
  69.  
  70.     if name == 'key_down' and char == 'q' then -- Выход
  71.  
  72.         net.broadcast(33, 'Admin: '..nick..' покинул беседу.')
  73.         break
  74.  
  75.     end
  76.  
  77. end
  78.  
  79. net.close()
  80. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement