ShadowPlay1

chat

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×