Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local fs = require("filesystem")
- local event = require("event")
- local bridge = component.openperipheral_bridge
- local text = require("text")
- local gpu = component.gpu
- local charWidthLib = require("charsCheckerLib")
- local chatLog_path = "chat_log.txt"
- local defaultSlotValue = 18
- local constraints = {
- messages_local_chat_width = defaultSlotValue * 15, --ширина локального чата 15 стандартных клеток
- messages_local_chat_height = defaultSlotValue * 7,--высота локального чата 7 стандартных клеток
- }
- local adminsList = {
- {name = "4epB9Ik", color = "§6", type = "ADMIN"},
- {name = "DevilPuppy", color = "§6", type = "ADMIN"},
- {name = "CharleyRog", color = "§6", type = "ADMIN"},
- {name = "Noise71", color = "§6", type = "ADMIN"},
- {name = "_FATALITY_", color = "§6", type = "BUILDER"},
- {name = "Murzoid", color = "§6", type = "BUILDER"},
- {name = "LilPodupiPUPI", color = "§6", type = "ADMIN"},
- {name = "KyKyPy3a", color = "§6", type = "ADMIN"},
- {name = "max32", color = "§6", type = "TRAINEE"}
- }
- function splByToken(str, token)
- local t = {}
- local tempStr = ""
- local i = 1
- while i <= str:len() do
- if (string.sub(str, i, i) == "&") then
- table.insert(t, tempStr)
- tempStr = ""
- end
- tempStr = tempStr .. string.sub(str, i, i)
- i = i + 1
- end
- if (tempStr ~= "") then
- table.insert(t, tempStr)
- end
- return t
- end
- function writeMessage(text)
- if (string.sub(text, 1, 1) == "!") then
- text = string.sub(text, 2)
- end
- local t = splByToken(text, "&")
- for k, v in pairs(t) do
- if (not isColored(v)) then
- gpu.setForeground(0xFFFFFF)
- io.write(v)
- else
- setColor(string.sub(v, 2, 2))
- io.write(string.sub(v, 3))
- end
- end
- end
- function pcMsg(nick, msg) -- вывод строки в компе
- local type = ""
- if (not isGlobal(msg)) then
- if (msg ~= ".........") then
- type = "L"
- local file = fs.open(chatLog_path, "a")
- file:write("[" .. type .. "] " .. nick .. ": " .. msg .. "\n")
- file:close()
- gpu.setForeground(0x00FFFF)
- io.write(" ")
- gpu.setForeground(0xFFFFFF)
- if (type == "G") then
- gpu.setForeground(0xFF9933)
- else
- gpu.setForeground(0xFFFFFF)
- end
- io.write("[" .. type .. "] ")
- gpu.setForeground(0x00FF00)
- io.write(nick)
- gpu.setForeground(0xFFFFFF)
- io.write(": ")
- writeMessage(msg)
- io.write("\n")
- end
- end
- end
- function stringToArray(text)
- t = {}
- text:gsub(".", function()
- table.insert(t, c)
- end)
- return t
- end
- function countOfItems(arr)
- Count = 0
- for k, v in ipairs(arr) do
- Count = Count + 1
- end
- return Count
- end
- function isColored(text)
- if (string.sub(text, 1, 1) == "&") then
- return true
- else
- return false
- end
- end
- -- Проверяет в глобальном ли чате написано сообщение
- function isGlobal(msg)
- if (string.sub(msg, 1, 1) == "!") then
- return true
- else
- return false
- end
- end
- -- возвращает оформленную строку с сообщением вида [23:12] [G] player: Hello
- function message(nick, msg)
- if (not isGlobal(msg)) then
- if (msg ~= ".........") then
- local type = "§fL"
- local file = fs.open(chatLog_path, "a")
- file:write("[" .. type .. "] " ..nick .. ": " .. msg .. "\n")
- for index = 1, #adminsList do
- if adminsList[index].name == nick then
- return "§8[" .."&5"..adminsList[index].type .. "§8] " .. adminsList[index].color.. nick .. "§f: " .. msg
- end
- end
- return "§8[" .. type .. "§8] " .. "§7".. nick .. "§f: " .. msg
- end
- end
- end
- --§
- local messages = {}
- local function monitorMessages(_, add, nick, msg)
- --for index = 1, #adminsList do
- -- if nick == adminsList[index].name then
- -- return
- -- end
- --end
- local type = isGlobal(msg)
- local color = 0x52ff00
- local cof = countOfItems(messages)
- table.insert(messages, 1, message(nick, msg, type))
- if (cof > 16) then
- table.remove(messages, 16)
- end
- end
- -- Регистрируем обработчик событий
- event.listen("chat_message", monitorMessages)
- local function addText(surface, x, y, text, color)
- surface.addText(x, y, text, color)
- end
- local function drawSlot(surface, x, y, slotWidth, slotHeight, textToDrawInSlot, textColorToDrawInSlot, bgColor, percentsBgColor, alpha)--percents 0.52
- local borderColor = 0xFFFFFF
- local borderWidth = 1
- surface.addLine({ x, y }, { x, y + slotHeight }, borderColor)
- surface.addLine({ x + slotWidth, y }, { x + slotWidth, y + slotHeight }, borderColor)
- surface.addLine({ x, y }, { x + slotWidth, y }, borderColor)
- surface.addLine({ x, y + slotHeight }, { x + slotWidth, y + slotHeight}, borderColor)
- if percentsBgColor then
- local boxWidthPercents = (slotWidth - 2) * percentsBgColor
- surface.addBox(x + borderWidth, y + borderWidth, slotWidth - 2, slotHeight - 2, 0xCCCCCC, 0.5)
- surface.addBox(x + borderWidth, y + borderWidth, boxWidthPercents, slotHeight - 2, bgColor, 0.8)
- else
- surface.addBox(x + borderWidth, y + borderWidth, slotWidth - 2, slotHeight - 2, bgColor or 0xCCCCCC, alpha or 0.5)
- end
- if textToDrawInSlot and textColorToDrawInSlot then
- local marginTop = (slotHeight - 8) / 2
- addText(surface, x + 3, y + marginTop, textToDrawInSlot, textColorToDrawInSlot)
- end
- end
- local function splitForLines(textToSplit, chunkSizeWidthPixels)
- local words = {}
- for word in textToSplit:gmatch("%S+") do
- table.insert(words, word)
- end
- local lines = {}
- local currentLine = ""
- for _, word in ipairs(words) do
- local wordWidth = charWidthLib.checkWidth(word)
- local linePlusWordWidth = charWidthLib.checkWidth(currentLine .. " " .. word)
- if linePlusWordWidth <= chunkSizeWidthPixels then
- if currentLine == "" then
- currentLine = word
- else
- currentLine = currentLine .. " " .. word
- end
- else
- table.insert(lines, currentLine)
- currentLine = word
- end
- end
- if currentLine ~= "" then
- table.insert(lines, currentLine)
- end
- return lines
- end
- local function stringContains(str, substr)
- return string.find(str, substr) ~= nil
- end
- local function drawMessages(surface, userName, messages)
- surface.clear()
- local messageColor = 0x000000
- local x = 5
- local y = 5
- local borderPadding = 2
- local symbolHeight = 9
- local messageGap = 0.3
- local maxLines = (constraints.messages_local_chat_height - (borderPadding * 2)) / (symbolHeight + messageGap)
- drawSlot(surface, x, y, constraints.messages_local_chat_width, constraints.messages_local_chat_height, nil, nil, 0x000000, nill, 0.3)
- local messageX = x + borderPadding
- local currentY = y + borderPadding + constraints.messages_local_chat_height - messageGap
- local currentLine = 1
- local lastY = currentY
- for i = 1, #messages do -- итерируем по всем сообщениям
- lastY = currentY
- local message = tostring(messages[i]):gsub("&", "§")
- if (not stringContains(message, userName .. "§f: ")) then
- local lines = splitForLines(message, constraints.messages_local_chat_width - borderPadding * 2)
- currentY = currentY - (#lines * (symbolHeight + messageGap))
- if currentY < y then
- currentY = y + messageGap
- end
- for j = 1, #lines do
- local trimmedText = text.trim(lines[j])
- if #trimmedText > 0 then
- local line = "§f"..trimmedText
- if currentLine < maxLines then -- если строка влезает в контейнер
- surface.addText(messageX, currentY, line, messageColor) -- выводим ее на экран
- currentY = currentY + symbolHeight + messageGap -- сдвигаемся на следующую строку
- currentLine = currentLine + 1
- else
- break
- end
- end
- end
- if currentLine > maxLines then
- break
- end
- currentY = currentY - (#lines * (symbolHeight + messageGap))
- if currentY < y then
- currentY = y + messageGap
- end
- end
- end
- end
- while true do
- for key, value in pairs(bridge.getUsers()) do
- local user = value
- local surface = bridge.getSurfaceByUUID(user.uuid)
- drawMessages(surface, user.name, messages)
- end
- bridge.sync()
- os.sleep(0.5)
- end
Advertisement
Add Comment
Please, Sign In to add comment