Krobys

[Computer] Admins Local Chat

Mar 24th, 2023 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.36 KB | Gaming | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local event = require("event")
  4. local bridge = component.openperipheral_bridge
  5. local text = require("text")
  6. local gpu = component.gpu
  7. local charWidthLib = require("charsCheckerLib")
  8. local chatLog_path = "chat_log.txt"
  9. local defaultSlotValue = 18
  10. local constraints = {
  11.     messages_local_chat_width = defaultSlotValue * 15, --ширина локального чата 15 стандартных клеток
  12.     messages_local_chat_height = defaultSlotValue * 7,--высота локального чата 7 стандартных клеток
  13. }
  14.  
  15. local adminsList = {
  16.     {name = "4epB9Ik", color = "§6", type = "ADMIN"},
  17.     {name = "DevilPuppy", color = "§6", type = "ADMIN"},
  18.     {name = "CharleyRog", color = "§6", type = "ADMIN"},
  19.     {name = "Noise71", color = "§6", type = "ADMIN"},
  20.     {name = "_FATALITY_", color = "§6", type = "BUILDER"},
  21.     {name = "Murzoid", color = "§6", type = "BUILDER"},
  22.     {name = "LilPodupiPUPI", color = "§6", type = "ADMIN"},
  23.     {name = "KyKyPy3a", color = "§6", type = "ADMIN"},
  24.     {name = "max32", color = "§6", type = "TRAINEE"}
  25. }
  26.  
  27. function splByToken(str, token)
  28.     local t = {}
  29.     local tempStr = ""
  30.     local i = 1
  31.  
  32.     while i <= str:len() do
  33.         if (string.sub(str, i, i) == "&") then
  34.             table.insert(t, tempStr)
  35.             tempStr = ""
  36.         end
  37.  
  38.         tempStr = tempStr .. string.sub(str, i, i)
  39.         i = i + 1
  40.  
  41.     end
  42.  
  43.     if (tempStr ~= "") then
  44.         table.insert(t, tempStr)
  45.     end
  46.  
  47.     return t
  48. end
  49.  
  50. function writeMessage(text)
  51.     if (string.sub(text, 1, 1) == "!") then
  52.         text = string.sub(text, 2)
  53.     end
  54.     local t = splByToken(text, "&")
  55.  
  56.     for k, v in pairs(t) do
  57.         if (not isColored(v)) then
  58.             gpu.setForeground(0xFFFFFF)
  59.             io.write(v)
  60.         else
  61.             setColor(string.sub(v, 2, 2))
  62.             io.write(string.sub(v, 3))
  63.         end
  64.     end
  65. end
  66.  
  67. function pcMsg(nick, msg) -- вывод строки в компе
  68.     local type = ""
  69.     if (not isGlobal(msg)) then
  70.         if (msg ~= ".........") then
  71.             type = "L"
  72.             local file = fs.open(chatLog_path, "a")
  73.             file:write("[" .. type .. "] " .. nick .. ": " .. msg .. "\n")
  74.             file:close()
  75.  
  76.             gpu.setForeground(0x00FFFF)
  77.             io.write(" ")
  78.             gpu.setForeground(0xFFFFFF)
  79.             if (type == "G") then
  80.                 gpu.setForeground(0xFF9933)
  81.             else
  82.                 gpu.setForeground(0xFFFFFF)
  83.             end
  84.             io.write("[" .. type .. "] ")
  85.             gpu.setForeground(0x00FF00)
  86.             io.write(nick)
  87.             gpu.setForeground(0xFFFFFF)
  88.             io.write(": ")
  89.             writeMessage(msg)
  90.             io.write("\n")
  91.         end
  92.     end
  93. end
  94.  
  95. function stringToArray(text)
  96.     t = {}
  97.     text:gsub(".", function()
  98.         table.insert(t, c)
  99.     end)
  100.     return t
  101. end
  102.  
  103. function countOfItems(arr)
  104.     Count = 0
  105.     for k, v in ipairs(arr) do
  106.         Count = Count + 1
  107.     end
  108.     return Count
  109. end
  110.  
  111. function isColored(text)
  112.     if (string.sub(text, 1, 1) == "&") then
  113.         return true
  114.     else
  115.         return false
  116.     end
  117. end
  118.  
  119. -- Проверяет в глобальном ли чате написано сообщение
  120. function isGlobal(msg)
  121.  
  122.     if (string.sub(msg, 1, 1) == "!") then
  123.         return true
  124.     else
  125.         return false
  126.     end
  127. end
  128.  
  129. -- возвращает оформленную строку с сообщением вида [23:12] [G] player: Hello
  130. function message(nick, msg)
  131.     if (not isGlobal(msg)) then
  132.         if (msg ~= ".........") then
  133.             local type = "§fL"
  134.             local file = fs.open(chatLog_path, "a")
  135.  
  136.             file:write("[" .. type .. "] " ..nick .. ": " .. msg .. "\n")
  137.             for index = 1, #adminsList do
  138.                 if adminsList[index].name == nick then
  139.                     return "§8[" .."&5"..adminsList[index].type .. "§8] " .. adminsList[index].color.. nick .. "§f: " .. msg
  140.                 end
  141.             end
  142.             return "§8[" .. type .. "§8] " .. "§7".. nick .. "§f: " .. msg
  143.         end
  144.     end
  145. end
  146. --§
  147. local messages = {}
  148.  
  149. local function monitorMessages(_, add, nick, msg)
  150.     --for index = 1, #adminsList do
  151.     --    if nick == adminsList[index].name then
  152.     --        return
  153.     --    end
  154.     --end
  155.     local type = isGlobal(msg)
  156.     local color = 0x52ff00
  157.  
  158.     local cof = countOfItems(messages)
  159.  
  160.     table.insert(messages, 1, message(nick, msg, type))
  161.  
  162.     if (cof > 16) then
  163.         table.remove(messages, 16)
  164.     end
  165. end
  166.  
  167. -- Регистрируем обработчик событий
  168. event.listen("chat_message", monitorMessages)
  169.  
  170. local function addText(surface, x, y, text, color)
  171.     surface.addText(x, y, text, color)
  172. end
  173.  
  174. local function drawSlot(surface, x, y, slotWidth, slotHeight, textToDrawInSlot, textColorToDrawInSlot, bgColor, percentsBgColor, alpha)--percents 0.52
  175.     local borderColor = 0xFFFFFF
  176.     local borderWidth = 1
  177.  
  178.     surface.addLine({ x, y }, { x, y + slotHeight }, borderColor)
  179.     surface.addLine({ x + slotWidth, y }, { x + slotWidth, y + slotHeight }, borderColor)
  180.     surface.addLine({ x, y }, { x + slotWidth, y }, borderColor)
  181.     surface.addLine({ x, y + slotHeight }, { x + slotWidth, y + slotHeight}, borderColor)
  182.     if percentsBgColor then
  183.         local boxWidthPercents = (slotWidth - 2) * percentsBgColor
  184.         surface.addBox(x + borderWidth, y + borderWidth, slotWidth - 2, slotHeight - 2, 0xCCCCCC, 0.5)
  185.         surface.addBox(x + borderWidth, y + borderWidth, boxWidthPercents, slotHeight - 2, bgColor, 0.8)
  186.     else
  187.         surface.addBox(x + borderWidth, y + borderWidth, slotWidth - 2, slotHeight - 2, bgColor or 0xCCCCCC, alpha or 0.5)
  188.     end
  189.  
  190.     if textToDrawInSlot and textColorToDrawInSlot then
  191.         local marginTop = (slotHeight - 8) / 2
  192.         addText(surface, x + 3, y + marginTop, textToDrawInSlot, textColorToDrawInSlot)
  193.     end
  194. end
  195.  
  196. local function splitForLines(textToSplit, chunkSizeWidthPixels)
  197.     local words = {}
  198.     for word in textToSplit:gmatch("%S+") do
  199.         table.insert(words, word)
  200.     end
  201.  
  202.     local lines = {}
  203.     local currentLine = ""
  204.     for _, word in ipairs(words) do
  205.         local wordWidth = charWidthLib.checkWidth(word)
  206.         local linePlusWordWidth = charWidthLib.checkWidth(currentLine .. " " .. word)
  207.         if linePlusWordWidth <= chunkSizeWidthPixels then
  208.             if currentLine == "" then
  209.                 currentLine = word
  210.             else
  211.                 currentLine = currentLine .. " " .. word
  212.             end
  213.         else
  214.             table.insert(lines, currentLine)
  215.             currentLine = word
  216.         end
  217.     end
  218.     if currentLine ~= "" then
  219.         table.insert(lines, currentLine)
  220.     end
  221.  
  222.     return lines
  223. end
  224.  
  225. local function stringContains(str, substr)
  226.     return string.find(str, substr) ~= nil
  227. end
  228.  
  229. local function drawMessages(surface, userName, messages)
  230.     surface.clear()
  231.     local messageColor = 0x000000
  232.     local x = 5
  233.     local y = 5
  234.     local borderPadding = 2
  235.     local symbolHeight = 9
  236.     local messageGap = 0.3
  237.     local maxLines = (constraints.messages_local_chat_height - (borderPadding * 2)) / (symbolHeight  + messageGap)
  238.     drawSlot(surface, x, y, constraints.messages_local_chat_width, constraints.messages_local_chat_height, nil, nil, 0x000000, nill, 0.3)
  239.     local messageX = x + borderPadding
  240.     local currentY = y + borderPadding + constraints.messages_local_chat_height - messageGap
  241.     local currentLine = 1
  242.     local lastY = currentY
  243.     for i = 1, #messages do -- итерируем по всем сообщениям
  244.         lastY = currentY
  245.         local message = tostring(messages[i]):gsub("&", "§")
  246.         if (not stringContains(message, userName .. "§f: ")) then
  247.             local lines = splitForLines(message, constraints.messages_local_chat_width - borderPadding * 2)
  248.  
  249.             currentY = currentY - (#lines * (symbolHeight + messageGap))
  250.             if currentY < y then
  251.                 currentY = y + messageGap
  252.             end
  253.             for j = 1, #lines do
  254.                 local trimmedText = text.trim(lines[j])
  255.                 if #trimmedText > 0 then
  256.                     local line = "§f"..trimmedText
  257.                     if currentLine < maxLines then -- если строка влезает в контейнер
  258.                         surface.addText(messageX, currentY, line, messageColor) -- выводим ее на экран
  259.                         currentY = currentY + symbolHeight + messageGap -- сдвигаемся на следующую строку
  260.                         currentLine = currentLine + 1
  261.                     else
  262.                         break
  263.                     end
  264.                 end
  265.             end
  266.             if currentLine > maxLines then
  267.                 break
  268.             end
  269.             currentY = currentY - (#lines * (symbolHeight + messageGap))
  270.             if currentY < y then
  271.                 currentY = y + messageGap
  272.             end
  273.         end
  274.     end
  275. end
  276.  
  277. while true do
  278.     for key, value in pairs(bridge.getUsers()) do
  279.         local user = value
  280.         local surface = bridge.getSurfaceByUUID(user.uuid)
  281.         drawMessages(surface, user.name, messages)
  282.     end
  283.     bridge.sync()
  284.     os.sleep(0.5)
  285. end
Advertisement
Add Comment
Please, Sign In to add comment