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 term = require("term")
- local bridge = component.openperipheral_bridge
- local gpu = component.gpu
- local chatLog_path = "chat_log.txt"
- 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 = "L"
- local file = fs.open(chatLog_path, "a")
- file:write("[" .. type .. "] " .. nick .. ": " .. msg .. "\n")
- return "[" .. type .. "] " .. nick .. ": " .. msg
- end
- end
- end
- local nowY = 20
- local constX = 0
- local nowMsg
- bridge.addText(constX, 100, "Initialize", 0x334000)
- term.clear()
- bridge.clear()
- local messages = {}
- while true do
- local _, add, nick, msg = event.pull("chat_message")
- local type = isGlobal(msg)
- local color = 0xFFFF00
- local cof = countOfItems(messages)
- table.insert(messages, 1, message(nick, msg, type))
- if (cof > 16) then
- table.remove(messages, 16)
- end
- for k, v in ipairs(messages) do
- bridge.addText(constX, 9 * (20 - k) - 50, v, color)
- if (k >= 13) then
- break
- end
- end
- bridge.sync()
- --pcMsg(nick, msg, type)
- bridge.clear()
- nowY = nowY + 20
- end
Add Comment
Please, Sign In to add comment