Advertisement
Guest User

SW-chat.lua

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