moskovroma

chat.lua

Mar 4th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. --Библиотеки
  2. local com = require("component")
  3. local computer = require('computer')
  4. local event = require('event')
  5. local sides = require('sides')
  6. local unicode = require('unicode')
  7. local term = require('term')
  8. local shell = require("shell")
  9. local fs = require('filesystem')
  10.  
  11. --Компоненты
  12. local gpu = com.gpu
  13. local color = gpu.setForeground -- Установка заднего фона
  14. local rep = string.rep
  15. local cb = com.chat_box
  16. local color = gpu.setForeground
  17.  
  18. --Загрузка
  19. color(0x00ff00)
  20. term.clear()
  21.  
  22. gpu.setResolution(80,25)
  23. print("\nНастройка библиотек...")
  24. --os.sleep(2)
  25. if not fs.exists("/lib/stem.lua") then
  26. shell.execute("wget https://gitlab.com/UnicornFreedom/stem/raw/master/stem.lua /lib/stem.lua")
  27. end
  28. local stem = require('stem')
  29.  
  30. if not fs.exists("/lib/moskovLib.lua") then
  31. shell.execute("wget https://www.dropbox.com/s/hpsx9fa4jpbpxet/moskovLib.lua?dl=1 /lib/moskovLib.lua")
  32. end
  33. local mos = require("moskovLib")
  34.  
  35. print("\nИнициализация...")
  36. --os.sleep(2)
  37. print("Запуск программы...")
  38. --os.sleep(2)
  39. local server = stem.connect('stem.fomalhaut.me')
  40.  
  41. --Конфиг
  42. local width, height = 146, 42
  43. cb.setName("§4WEB§7")
  44.  
  45. --Глобальные переманные
  46. local roles = {{"NpVpanthera","&f[&4Куратор&f]"},{"nikilum", "&f[&1Гл.модератор&f]"},{"moskovroma","&f[&3Ст.модератор&f]"}}
  47. local roles_chat = {{"§4NpVpanthera","§8[§4К§8]"},{"§9nikilum", "§8[§9ГМ§8]"},{"§3moskovroma","§8[§3СТ§8]"}}
  48. local nick = ""
  49. local role = ""
  50. local channel_id = ""
  51. local log_message = {}
  52. local rol = ""
  53.  
  54. --Фронтэнд
  55. function boxGlobalSmall(width,height,text,col)
  56. color(col)
  57. gpu.fill(1,1,1,height,'│')
  58. gpu.fill(width,1,1,height,'│')
  59. gpu.set(1,1,'┌'..string.rep('─',width-2)..'┐')
  60. gpu.set(1,height,'└'..string.rep('─',width-2)..'┘')
  61. gpu.fill(1+(width/4),1,1,height,'│')
  62. mos.centeringLeft(width,1,"&g┬")
  63. mos.centeringLeft(width,height,"&g┴")
  64. --mos.centeringRight(width,height,"&g╢ &7Автор: moskovroma&g ╟")
  65. if text ~= nil then
  66. mos.centeringMid(width+36,1,"&g╢ &7"..text.." &g╟")
  67. end
  68. end
  69.  
  70. function drawConfig(nick)
  71. mos.centeringAny(8,2,width,2,"&gКонфиг")
  72. gpu.set(1,3,'├'..string.rep('─',((width)/4)-1)..'┤')
  73. mos.centeringAny(8,2,width,5,"&gПодключен: &6"..nick)
  74. for i=1,#roles do
  75. if nick == roles[i][1] then rol = roles[i][2] mos.centeringAny(8,2,width,6,"&gРоль: &3"..roles[i][2]) end
  76. end
  77. color(0x00ff00)
  78. gpu.set(1,8,'├'..string.rep('─',((width)/4)-1)..'┤')
  79. mos.printButton(9,10,20,3,0xffa500,0x00ff00,"Смена пользователя") --x,y,w,h,col1,col2,text
  80. mos.printButton(9,15,20,3,0x24b3a7,0x00ff00,"Смена канала")
  81. mos.printButton(9,height-4,20,3,0xff0000,0x00ff00,"Выход")
  82. end
  83.  
  84.  
  85. --Бекэнд
  86. function Connect( ... )
  87. term.clear()
  88. gpu.setResolution(80,25)
  89. mos.printText(0,1,"Подключение к серверу &3\'stem.fomalhaut.me\'")
  90. color(0x00ff00)
  91. print("\nВведите адрес канала: ")
  92. term.setCursor(23,2)
  93. channel_id = io.read()
  94. channel_id, _ = channel_id:gsub("\n", "")
  95. mos.printText(0,3,"Адрес канала: &3"..channel_id)
  96. color(0x00ff00)
  97. while true do
  98. mos.printText(0,5,"Введите ник: ")
  99. term.setCursor(14,5)
  100. nick = io.read()
  101. nick, _ = nick:gsub("\n", "")
  102. for i=1,#roles do
  103. if nick == roles[i][1] then
  104. role = roles_chat[i][2]
  105. return
  106. end
  107. end
  108. color(0xff0000)
  109. print("\nERROR: Ник веден не верно!")
  110. color(0x00ff00)
  111. os.sleep(2)
  112. mos.clearArea(1,4,80,height-4)
  113. end
  114. end
  115.  
  116.  
  117. function drawMessage( message )
  118. mos.clearArea(38,2,108,height - 2)
  119. local max = height - 4
  120. table.insert(log_message,message)
  121. if #log_message <=max then
  122. local i = 2
  123. for num = #log_message, 1, -1 do
  124. mos.printText(38,height-i,rol.." &8"..nick.."&f: &a"..log_message[num])
  125. i = i + 1
  126. end
  127. elseif #log_message > max then
  128. for num = #log_message, #log_message-max, -1 do
  129. mos.printText(38,height-i,rol.." &8"..nick.."&f: &a"..log_message[num])
  130. i = i + 1
  131. end
  132. end
  133. end
  134.  
  135. function Click( x,y,nickT )
  136. if mos.pressButton(x,y,{9,height-4,20,3}) then --Выход
  137. term.clear()
  138. server:unsubscribe(channel_id)
  139. server:disconnect()
  140. os.exit()
  141. end
  142. end
  143.  
  144. --Запуск
  145. Connect()
  146. term.clear()
  147. gpu.setResolution(width, height)
  148. server:subscribe(channel_id)
  149.  
  150. --event.listen('stem_message', function(_, _, channel_id, message) drawMessage(message) end)
  151. while true do
  152. boxGlobalSmall(width,height,"OpenChat",0x00ff00)
  153. drawConfig(nick)
  154. local e, _, x, y, _, nickT = event.pull(10)
  155. if e == "touch" then
  156. Click(x,y,nickT)
  157. elseif e == "stem_message" then
  158. drawMessage(x)
  159. cb.say(role.." §8"..nick.."§f: §a"..x)
  160. elseif e == "chat_message" then
  161. server:send(channel_id, x..": "..y)
  162. end
  163. end
Add Comment
Please, Sign In to add comment