Advertisement
mrWhiskasss

чат

Sep 28th, 2022
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local event = require("event")
  4. local fs = require("filesystem")
  5.  
  6. local gpu = component.gpu
  7.  
  8. local LOG_PATH = "chat_log.txt"
  9.  
  10. -- Конвертирует строку в массив
  11. function stringToArray(text)
  12. t = {}
  13. text:gsub(".",function(c) table.insert(t,c) end)
  14. return t
  15. end
  16. -- Проверяет является ли текст окрашенным
  17. function isColored(text)
  18. for pos, i in pairs(stringToArray(text)) do
  19. if (i ~= "&") then
  20. if (i ~= " ") then
  21. return false
  22. end
  23. else
  24. return true
  25. end
  26. end
  27.  
  28. return true
  29. end
  30.  
  31. -- Проверяет в глобальном ли чате написано сообщение
  32. function isGlobal(text)
  33. for pos, i in pairs(stringToArray(text)) do
  34. if (i ~= "!") then
  35. if (i ~= " ") then
  36. return false
  37. end
  38. else
  39. return true, pos
  40. end
  41. end
  42. return false
  43. end
  44.  
  45. -- Делит строку на части
  46. function split(str, pat)
  47. local t = {}
  48. local fpat = "(.-)" .. pat
  49. local last_end = 1
  50. local s, e, cap = str:find(fpat, 1)
  51. while s do
  52. if s ~= 1 or cap ~= "" then
  53. table.insert(t,cap)
  54. end
  55. last_end = e+1
  56. s, e, cap = str:find(fpat, last_end)
  57. end
  58. if last_end <= #str then
  59. cap = str:sub(last_end)
  60. table.insert(t, cap)
  61. end
  62. return t
  63. end
  64.  
  65. -- Устанавливает цвет шрифта в зависимости от патерна
  66. function setColor(num)
  67. if (num == "0") then
  68. gpu.setForeground(0x000000)
  69. end
  70.  
  71. if (num == "1") then
  72. gpu.setForeground(0x0000ff)
  73. end
  74.  
  75. if (num == "2") then
  76. gpu.setForeground(0x009000)
  77. end
  78.  
  79. if (num == "3") then
  80. gpu.setForeground(0x009980)
  81. end
  82.  
  83. if (num == "4") then
  84. gpu.setForeground(0x6b0000)
  85. end
  86.  
  87. if (num == "5") then
  88. gpu.setForeground(0x60006b)
  89. end
  90.  
  91. if (num == "6") then
  92. gpu.setForeground(0xff9900)
  93. end
  94.  
  95. if (num == "7") then
  96. gpu.setForeground(0x5c5c5c)
  97. end
  98.  
  99. if (num == "8") then
  100. gpu.setForeground(0x404040)
  101. end
  102.  
  103. if (num == "9") then
  104. gpu.setForeground(0x0000ff)
  105. end
  106.  
  107. if (num == "a") then
  108. gpu.setForeground(0x39ff03)
  109. end
  110.  
  111. if (num == "b") then
  112. gpu.setForeground(0x00fffb)
  113. end
  114.  
  115. if (num == "c") then
  116. gpu.setForeground(0xff0000)
  117. end
  118.  
  119. if (num == "d") then
  120. gpu.setForeground(0xff00e6)
  121. end
  122.  
  123. if (num == "e") then
  124. gpu.setForeground(0xbfff00)
  125. end
  126.  
  127. if (num == "f") then
  128. gpu.setForeground(0xFFFFFF)
  129. end
  130. end
  131.  
  132. -- Выводит сообщение
  133. function writeMessage(text)
  134. local t = split(text, "&")
  135. for pos, i in pairs(t) do
  136. if (pos == 1 and not isColored(text)) then
  137. io.write(i)
  138. else
  139. setColor(string.sub(i, 1, 1))
  140. io.write(string.sub(i, 2))
  141. end
  142. end
  143. end
  144.  
  145. -- Выводит остальную часть сообщения
  146. function message(nick, msg, isGlobal, pos)
  147. local type = ""
  148. if (isGlobal) then msg = string.sub(msg, pos + 1) type = "G" else type = "L" end
  149.  
  150. local file = fs.open(LOG_PATH, "a")
  151. file:write("[" .. type .. "] " .. nick .. ": " .. msg .. "\n")
  152. file:close()
  153. gpu.setForeground(0x00FFFF)
  154. if (type == "G") then
  155. gpu.setForeground(0xFFD700)
  156. else
  157. gpu.setForeground(0xCCCCCC)
  158. end
  159. io.write("[" .. type .. "] ")
  160. gpu.setForeground(0x6A5ACD)
  161. io.write(nick)
  162. gpu.setForeground(0xFFFFFF)
  163. io.write(": ")
  164. writeMessage(msg, l)
  165. io.write("\n")
  166. end
  167.  
  168. print("Ожидание первого сообщения...")
  169.  
  170. local _, add, nick, msg = event.pull("chat_message")
  171. term.clear()
  172. local type, pos = isGlobal(msg)
  173. message(nick, msg, type, pos)
  174.  
  175.  
  176. while true do
  177.  
  178. local _, add, nick, msg = event.pull("chat_message")
  179. local type, pos = isGlobal(msg)
  180. message(nick, msg, type, pos)
  181.  
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement