mrWhiskasss

локальный чат в очках от чарли рог

Oct 31st, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local event = require("event")
  4. local term = require("term")
  5.  
  6. local bridge = component.openperipheral_bridge
  7. local gpu = component.gpu
  8.  
  9. local chatLog_path = "chat_log.txt"
  10.  
  11. function splByToken(str, token)
  12. local t = {}
  13. local tempStr = ""
  14. local i = 1
  15.  
  16. while i <= str:len() do
  17. if (string.sub(str, i, i) == "&") then
  18. table.insert(t, tempStr)
  19. tempStr = ""
  20. end
  21.  
  22. tempStr = tempStr .. string.sub(str, i, i)
  23. i = i + 1
  24.  
  25. end
  26.  
  27. if (tempStr ~= "") then
  28. table.insert(t, tempStr)
  29. end
  30.  
  31. return t
  32. end
  33.  
  34. function writeMessage(text)
  35. if (string.sub(text, 1, 1) == "!") then
  36. text = string.sub(text, 2)
  37. end
  38. local t = splByToken(text, "&")
  39.  
  40. for k, v in pairs(t) do
  41. if (not isColored(v)) then
  42. gpu.setForeground(0xFFFFFF)
  43. io.write(v)
  44. else
  45. setColor(string.sub(v, 2, 2))
  46. io.write(string.sub(v, 3))
  47. end
  48. end
  49. end
  50.  
  51. function pcMsg(nick, msg) -- вывод строки в компе
  52. local type = ""
  53. if (not isGlobal(msg)) then
  54. if (msg ~= ".........") then
  55. type = "L"
  56. local file = fs.open(chatLog_path, "a")
  57. file:write("[" .. type .. "] " .. nick .. ": " .. msg .. "\n")
  58. file:close()
  59.  
  60. gpu.setForeground(0x00FFFF)
  61. io.write(" ")
  62. gpu.setForeground(0xFFFFFF)
  63. if (type == "G") then
  64. gpu.setForeground(0xFF9933)
  65. else
  66. gpu.setForeground(0xFFFFFF)
  67. end
  68. io.write("[" .. type .. "] ")
  69. gpu.setForeground(0x00FF00)
  70. io.write(nick)
  71. gpu.setForeground(0xFFFFFF)
  72. io.write(": ")
  73. writeMessage(msg)
  74. io.write("\n")
  75. end
  76. end
  77. end
  78.  
  79. function stringToArray(text)
  80. t = {}
  81. text:gsub(".", function()
  82. table.insert(t, c)
  83. end)
  84. return t
  85. end
  86.  
  87. function countOfItems(arr)
  88. Count = 0
  89. for k, v in ipairs(arr) do
  90. Count = Count + 1
  91. end
  92. return Count
  93. end
  94.  
  95. function isColored(text)
  96. if (string.sub(text, 1, 1) == "&") then
  97. return true
  98. else
  99. return false
  100. end
  101. end
  102.  
  103. -- Проверяет в глобальном ли чате написано сообщение
  104. function isGlobal(msg)
  105.  
  106. if (string.sub(msg, 1, 1) == "!") then
  107. return true
  108. else
  109. return false
  110. end
  111. end
  112.  
  113. -- возвращает оформленную строку с сообщением вида [23:12] [G] player: Hello
  114. function message(nick, msg)
  115. if (not isGlobal(msg)) then
  116. if (msg ~= ".........") then
  117. local type = "L"
  118. local file = fs.open(chatLog_path, "a")
  119.  
  120. file:write("[" .. type .. "] " .. nick .. ": " .. msg .. "\n")
  121.  
  122. return "[" .. type .. "] " .. nick .. ": " .. msg
  123. end
  124. end
  125. end
  126.  
  127. local nowY = 20
  128. local constX = 0
  129. local nowMsg
  130.  
  131. bridge.addText(constX, 100, "Initialize", 0x334000)
  132. term.clear()
  133. bridge.clear()
  134.  
  135. local messages = {}
  136.  
  137. while true do
  138.  
  139. local _, add, nick, msg = event.pull("chat_message")
  140. local type = isGlobal(msg)
  141. local color = 0xFFFF00
  142.  
  143. local cof = countOfItems(messages)
  144.  
  145. table.insert(messages, 1, message(nick, msg, type))
  146.  
  147. if (cof > 16) then
  148. table.remove(messages, 16)
  149. end
  150.  
  151. for k, v in ipairs(messages) do
  152.  
  153. bridge.addText(constX, 9 * (20 - k) - 50, v, color)
  154.  
  155. if (k >= 13) then
  156. break
  157. end
  158. end
  159.  
  160. bridge.sync()
  161. --pcMsg(nick, msg, type)
  162. bridge.clear()
  163.  
  164. nowY = nowY + 20
  165. end
  166.  
Add Comment
Please, Sign In to add comment