Advertisement
Guest User

SW-chat

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