Revector

chat.lua

May 26th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3. Создано командой SOCC company
  4. Nightmare_Night
  5.  
  6. P.S. Программа написана на скорую руку
  7. Просьба тапками не кидаться
  8.  
  9. P.S.S. Функция получения времени хост-машины была взята из API ECS
  10.  
  11. ]]--
  12.  
  13. local component = require("component")
  14. local term = require("term")
  15. local event = require("event")
  16. local fs = require("filesystem")
  17.  
  18. local gpu = component.gpu
  19.  
  20. local LOG_PATH = "chat_log.txt"
  21.  
  22. -- Конвертирует строку в массив
  23. function stringToArray(text)
  24. t = {}
  25. text:gsub(".",function(c) table.insert(t,c) end)
  26. return t
  27. end
  28.  
  29. --Получить текущее реальное время компьютера, хостящего сервер майна
  30. function getHostTime(timezone)
  31. timezone = timezone or 2
  32. local file = io.open("/HostTime.tmp", "w")
  33. file:write("")
  34. file:close()
  35. local timeCorrection = timezone * 3600
  36. local lastModified = tonumber(string.sub(fs.lastModified("/HostTime.tmp"), 1, -4)) + timeCorrection
  37. fs.remove("/HostTime.tmp")
  38. local year, month, day, hour, minute, second = os.date("%Y", lastModified), os.date("%m", lastModified), os.date("%d", lastModified), os.date("%H", lastModified), os.date("%M", lastModified), os.date("%S", lastModified)
  39. return tonumber(day), tonumber(month), tonumber(year), tonumber(hour), tonumber(minute), tonumber(second)
  40. end
  41.  
  42. -- Получет настоящее время, стоящее на Хост-машине
  43. function real_time()
  44. local time = {getHostTime(3)}
  45. local text = string.format("%02d:%02d:%02d", time[4], time[5], time[6])
  46. return text
  47. end
  48.  
  49. -- Проверяет является ли текст окрашенным
  50. function isColored(text)
  51. for pos, i in pairs(stringToArray(text)) do
  52. if (i ~= "&") then
  53. if (i ~= " ") then
  54. return false
  55. end
  56. else
  57. return true
  58. end
  59. end
  60.  
  61. return true
  62. end
  63.  
  64. -- Проверяет в глобальном ли чате написано сообщение
  65. function isGlobal(text)
  66. for pos, i in pairs(stringToArray(text)) do
  67. if (i ~= "!") then
  68. if (i ~= " ") then
  69. return false
  70. end
  71. else
  72. return true, pos
  73. end
  74. end
  75. return false
  76. end
  77.  
  78. -- Делит строку на части
  79. function split(str, pat)
  80. local t = {}
  81. local fpat = "(.-)" .. pat
  82. local last_end = 1
  83. local s, e, cap = str:find(fpat, 1)
  84. while s do
  85. if s ~= 1 or cap ~= "" then
  86. table.insert(t,cap)
  87. end
  88. last_end = e+1
  89. s, e, cap = str:find(fpat, last_end)
  90. end
  91. if last_end <= #str then
  92. cap = str:sub(last_end)
  93. table.insert(t, cap)
  94. end
  95. return t
  96. end
  97.  
  98. -- Устанавливает цвет шрифта в зависимости от патерна
  99. function setColor(num)
  100. if (num == "0") then
  101. gpu.setForeground(0x333333)
  102. end
  103.  
  104. if (num == "1") then
  105. gpu.setForeground(0x000099)
  106. end
  107.  
  108. if (num == "2") then
  109. gpu.setForeground(0x006600)
  110. end
  111.  
  112. if (num == "3") then
  113. gpu.setForeground(0x006666)
  114. end
  115.  
  116. if (num == "4") then
  117. gpu.setForeground(0x660000)
  118. end
  119.  
  120. if (num == "5") then
  121. gpu.setForeground(0x660066)
  122. end
  123.  
  124. if (num == "6") then
  125. gpu.setForeground(0xFF8000)
  126. end
  127.  
  128. if (num == "7") then
  129. gpu.setForeground(0xA0A0A0)
  130. end
  131.  
  132. if (num == "8") then
  133. gpu.setForeground(0x404040)
  134. end
  135.  
  136. if (num == "9") then
  137. gpu.setForeground(0x3399FF)
  138. end
  139.  
  140. if (num == "a") then
  141. gpu.setForeground(0x99FF33)
  142. end
  143.  
  144. if (num == "b") then
  145. gpu.setForeground(0x00FFFF)
  146. end
  147.  
  148. if (num == "c") then
  149. gpu.setForeground(0xFF3333)
  150. end
  151.  
  152. if (num == "d") then
  153. gpu.setForeground(0xFF00FF)
  154. end
  155.  
  156. if (num == "e") then
  157. gpu.setForeground(0xFFFF00)
  158. end
  159.  
  160. if (num == "f") then
  161. gpu.setForeground(0xFFFFFF)
  162. end
  163. end
  164.  
  165. -- Выводит сообщение
  166. function writeMessage(text)
  167. local t = split(text, "&")
  168. for pos, i in pairs(t) do
  169. if (pos == 1 and not isColored(text)) then
  170. io.write(i)
  171. else
  172. setColor(string.sub(i, 1, 1))
  173. io.write(string.sub(i, 2))
  174. end
  175. end
  176. end
  177.  
  178. -- Выводит остальную часть сообщения
  179. function message(nick, msg, isGlobal, pos)
  180. local type = ""
  181. if (isGlobal) then msg = string.sub(msg, pos + 1) type = "G" else type = "L" end
  182.  
  183. local file = fs.open(LOG_PATH, "a")
  184. file:write("[" .. real_time() .. "] [" .. type .. "] " .. nick .. ": " .. msg .. "\n")
  185. file:close()
  186.  
  187. gpu.setForeground(0x00FFFF)
  188. io.write("[" .. real_time() .. "] ")
  189. gpu.setForeground(0xFFFFFF)
  190. if (type == "G") then
  191. gpu.setForeground(0xFF9933)
  192. else
  193. gpu.setForeground(0xFFFFFF)
  194. end
  195. io.write("[" .. type .. "] ")
  196. gpu.setForeground(0x00FF00)
  197. io.write(nick)
  198. gpu.setForeground(0xFFFFFF)
  199. io.write(": ")
  200. writeMessage(msg, l)
  201. io.write("\n")
  202. end
  203.  
  204. print("Инициализация...")
  205. os.sleep(1)
  206. print("Ожидание первого сообщения...")
  207.  
  208. local _, add, nick, msg = event.pull("chat_message")
  209. term.clear()
  210. local type, pos = isGlobal(msg)
  211. message(nick, msg, type, pos)
  212.  
  213.  
  214. while true do
  215.  
  216. local _, add, nick, msg = event.pull("chat_message")
  217. local type, pos = isGlobal(msg)
  218. message(nick, msg, type, pos)
  219.  
  220. end
Add Comment
Please, Sign In to add comment