informator

ANB

Nov 3rd, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 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© 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 <= #str then
  80. cap = str:sub(last_end)
  81. table.insert(t, cap)
  82. end
  83. return t
  84. end
  85.  
  86. -- Устанавливает цвет шрифта в зависимости от патерна
  87. function setColor(num)
  88. if (num == "0") then
  89. gpu.setForeground(0x333333)
  90. end
  91.  
  92. if (num == "1") then
  93. gpu.setForeground(0x000099)
  94. end
  95.  
  96. if (num == "2") then
  97. gpu.setForeground(0x006600)
  98. end
  99.  
  100. if (num == "3") then
  101. gpu.setForeground(0x006666)
  102. end
  103.  
  104. if (num == "4") then
  105. gpu.setForeground(0x660000)
  106. end
  107.  
  108. if (num == "5") then
  109. gpu.setForeground(0x660066)
  110. end
  111.  
  112. if (num == "6") then
  113. gpu.setForeground(0xFF8000)
  114. end
  115.  
  116. if (num == "7") then
  117. gpu.setForeground(0xA0A0A0)
  118. end
  119.  
  120. if (num == "8") then
  121. gpu.setForeground(0x404040)
  122. end
  123.  
  124. if (num == "9") then
  125. gpu.setForeground(0x3399FF)
  126. end
  127.  
  128. if (num == "a") then
  129. gpu.setForeground(0x99FF33)
  130. end
  131.  
  132. if (num == "b") then
  133. gpu.setForeground(0x00FFFF)
  134. end
  135.  
  136. if (num == "c") then
  137. gpu.setForeground(0xFF3333)
  138. end
  139.  
  140. if (num == "d") then
  141. gpu.setForeground(0xFF00FF)
  142. end
  143.  
  144. if (num == "e") then
  145. gpu.setForeground(0xFFFF00)
  146. end
  147.  
  148. if (num == "f") then
  149. gpu.setForeground(0xFFFFFF)
  150. end
  151. end
  152.  
  153. -- Выводит сообщение
  154. function writeMessage(text)
  155. local t = split(text, "&")
  156. for pos, i in pairs(t) do
  157. if (pos == 1 and not isColored(text)) then
  158. io.write(i)
  159. else
  160. setColor(string.sub(i, 1, 1))
  161. io.write(string.sub(i, 2))
  162. end
  163. end
  164. end
  165.  
  166. -- Выводит остальную часть сообщения
  167. function message(nick, msg, isGlobal, pos)
  168. local type = ""
  169. if (isGlobal) then msg = string.sub(msg, pos + 1) type = "G" else type = "L" end
  170.  
  171. local file = fs.open(LOG_PATH, "a")
  172. file:write("[" .. real_time() .. "] [" .. type .. "] " .. nick .. ": " .. msg .. "\n")
  173. file:close()
  174.  
  175. gpu.setForeground(0x00FFFF)
  176. io.write("[" .. real_time() .. "] ")
  177. gpu.setForeground(0xFFFFFF)
  178. if (type == "G") then
  179. gpu.setForeground(0xFF9933)
  180. else
  181. gpu.setForeground(0xFFFFFF)
  182. end
  183. io.write("[" .. type .. "] ")
  184. gpu.setForeground(0x00FF00)
  185. io.write(nick)
  186. gpu.setForeground(0xFFFFFF)
  187. io.write(": ")
  188. writeMessage(msg, l)
  189. io.write("\n")
  190. end
  191.  
  192. print("Инициализация...")
  193. os.sleep(1)
  194. print("Ожидание первого сообщения...")
  195.  
  196. local _, add, nick, msg = event.pull("chat_message")
  197. term.clear()
  198. local type, pos = isGlobal(msg)
  199. message(nick, msg, type, pos)
  200.  
  201.  
  202. while true do
  203.  
  204. local _, add, nick, msg = event.pull("chat_message")
  205. local type, pos = isGlobal(msg)
  206. message(nick, msg, type, pos)
  207.  
  208. end
Add Comment
Please, Sign In to add comment