Advertisement
nikita2007558

чат с большим шрифтом

Jan 15th, 2023 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 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. os.execute("resolution 80 26")
  11. print("разрещение установленно")
  12.  
  13. -- Конвертирует строку в массив
  14. function stringToArray(text)
  15.   t = {}
  16.   text:gsub(".",function(c) table.insert(t,c) end)
  17.   return t
  18. end
  19. -- Проверяет является ли текст окрашенным
  20. function isColored(text)
  21.   for pos, i in pairs(stringToArray(text)) do
  22.     if (i ~= "&") then
  23.       if (i ~= " ") then
  24.         return false
  25.       end
  26.     else
  27.       return true
  28.     end
  29.   end
  30.  
  31.   return true
  32. end
  33.  
  34. -- Проверяет в глобальном ли чате написано сообщение
  35. function isGlobal(text)
  36.   for pos, i in pairs(stringToArray(text)) do
  37.     if (i ~= "!") then
  38.       if (i ~= " ") then
  39.         return false
  40.       end
  41.     else
  42.       return true, pos
  43.     end
  44.   end
  45.   return false
  46. end
  47.  
  48. -- Делит строку на части
  49. function split(str, pat)
  50.    local t = {}
  51.    local fpat = "(.-)" .. pat
  52.    local last_end = 1
  53.    local s, e, cap = str:find(fpat, 1)
  54.    while s do
  55.       if s ~= 1 or cap ~= "" then
  56.       table.insert(t,cap)
  57.       end
  58.       last_end = e+1
  59.       s, e, cap = str:find(fpat, last_end)
  60.    end
  61.    if last_end <= #str then
  62.       cap = str:sub(last_end)
  63.       table.insert(t, cap)
  64.    end
  65.    return t
  66. end
  67.  
  68. -- Устанавливает цвет шрифта в зависимости от патерна
  69. function setColor(num)
  70.       if (num == "0") then
  71.     gpu.setForeground(0x000000)
  72.   end
  73.  
  74.   if (num == "1") then
  75.     gpu.setForeground(0x0000ff)
  76.   end
  77.  
  78.   if (num == "2") then
  79.     gpu.setForeground(0x009000)
  80.   end
  81.  
  82.   if (num == "3") then
  83.     gpu.setForeground(0x009980)
  84.   end
  85.  
  86.   if (num == "4") then
  87.     gpu.setForeground(0x6b0000)
  88.   end
  89.  
  90.   if (num == "5") then
  91.     gpu.setForeground(0x60006b)
  92.   end
  93.  
  94.   if (num == "6") then
  95.     gpu.setForeground(0xff9900)
  96.   end
  97.  
  98.   if (num == "7") then
  99.     gpu.setForeground(0x5c5c5c)
  100.   end
  101.  
  102.   if (num == "8") then
  103.     gpu.setForeground(0x404040)
  104.   end
  105.  
  106.   if (num == "9") then
  107.     gpu.setForeground(0x0000ff)
  108.   end
  109.  
  110.   if (num == "a") then
  111.     gpu.setForeground(0x39ff03)
  112.   end
  113.  
  114.   if (num == "b") then
  115.     gpu.setForeground(0x00fffb)
  116.   end
  117.  
  118.   if (num == "c") then
  119.     gpu.setForeground(0xff0000)
  120.   end
  121.  
  122.   if (num == "d") then
  123.     gpu.setForeground(0xff00e6)
  124.   end
  125.  
  126.   if (num == "e") then
  127.     gpu.setForeground(0xffff00)
  128.   end
  129.  
  130.   if (num == "f") then
  131.     gpu.setForeground(0xFFFFFF)
  132.   end
  133. end
  134.      
  135. -- Выводит сообщение
  136. function writeMessage(text)  
  137.     local t = split(text, "&")
  138.     for pos, i in pairs(t) do
  139.       if (pos == 1 and not isColored(text)) then
  140.         io.write(i)
  141.       else
  142.         setColor(string.sub(i, 1, 1))
  143.         io.write(string.sub(i, 2))
  144.       end
  145.     end
  146. end
  147.  
  148. -- Выводит остальную часть сообщения
  149. function message(nick, msg, isGlobal, pos)
  150.   local type = ""
  151.   if (isGlobal) then msg = string.sub(msg, pos + 1) type = "G" else type = "L" end
  152.  
  153.   local file = fs.open(LOG_PATH, "a")
  154.   file:write("[" .. type .. "] " .. nick .. ": " .. msg .. "\n")
  155.   file:close()
  156.    gpu.setForeground(0x00FFFF)
  157.   if (type == "G") then
  158.     gpu.setForeground(0xFFD700)
  159.   else
  160.     gpu.setForeground(0xE0FFFF)
  161.   end
  162.   io.write("[" .. type .. "] ")
  163.   gpu.setForeground(0x8cFF00)
  164.   io.write(nick)
  165.   gpu.setForeground(0xFFFFFF)
  166.   io.write(": ")
  167.   writeMessage(msg, l)
  168.   io.write("\n")
  169. end
  170.  
  171. print("Ожидание первого сообщения...")
  172.  
  173. local _, add, nick, msg = event.pull("chat_message")
  174. term.clear()
  175. local type, pos = isGlobal(msg)
  176. message(nick, msg, type, pos)
  177.  
  178. while true do
  179.  
  180.   local _, add, nick, msg = event.pull("chat_message")
  181.   local type, pos = isGlobal(msg)
  182.   message(nick, msg, type, pos)
  183.  
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement