Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local term = require("term")
- local event = require("event")
- local fs = require("filesystem")
- local gpu = component.gpu
- local LOG_PATH = "chat_log.txt"
- -- Изменение разрещения
- os.execute("resolution 80 26")
- print("разрещение установленно")
- -- Конвертирует строку в массив
- function stringToArray(text)
- t = {}
- text:gsub(".",function(c) table.insert(t,c) end)
- return t
- end
- -- Проверяет является ли текст окрашенным
- function isColored(text)
- for pos, i in pairs(stringToArray(text)) do
- if (i ~= "&") then
- if (i ~= " ") then
- return false
- end
- else
- return true
- end
- end
- return true
- end
- -- Проверяет в глобальном ли чате написано сообщение
- function isGlobal(text)
- for pos, i in pairs(stringToArray(text)) do
- if (i ~= "!") then
- if (i ~= " ") then
- return false
- end
- else
- return true, pos
- end
- end
- return false
- end
- -- Делит строку на части
- function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- -- Устанавливает цвет шрифта в зависимости от патерна
- function setColor(num)
- if (num == "0") then
- gpu.setForeground(0x000000)
- end
- if (num == "1") then
- gpu.setForeground(0x0000ff)
- end
- if (num == "2") then
- gpu.setForeground(0x009000)
- end
- if (num == "3") then
- gpu.setForeground(0x009980)
- end
- if (num == "4") then
- gpu.setForeground(0x6b0000)
- end
- if (num == "5") then
- gpu.setForeground(0x60006b)
- end
- if (num == "6") then
- gpu.setForeground(0xff9900)
- end
- if (num == "7") then
- gpu.setForeground(0x5c5c5c)
- end
- if (num == "8") then
- gpu.setForeground(0x404040)
- end
- if (num == "9") then
- gpu.setForeground(0x0000ff)
- end
- if (num == "a") then
- gpu.setForeground(0x39ff03)
- end
- if (num == "b") then
- gpu.setForeground(0x00fffb)
- end
- if (num == "c") then
- gpu.setForeground(0xff0000)
- end
- if (num == "d") then
- gpu.setForeground(0xff00e6)
- end
- if (num == "e") then
- gpu.setForeground(0xffff00)
- end
- if (num == "f") then
- gpu.setForeground(0xFFFFFF)
- end
- end
- -- Выводит сообщение
- function writeMessage(text)
- local t = split(text, "&")
- for pos, i in pairs(t) do
- if (pos == 1 and not isColored(text)) then
- io.write(i)
- else
- setColor(string.sub(i, 1, 1))
- io.write(string.sub(i, 2))
- end
- end
- end
- -- Выводит остальную часть сообщения
- function message(nick, msg, isGlobal, pos)
- local type = ""
- if (isGlobal) then msg = string.sub(msg, pos + 1) type = "G" else type = "L" end
- local file = fs.open(LOG_PATH, "a")
- file:write("[" .. type .. "] " .. nick .. ": " .. msg .. "\n")
- file:close()
- gpu.setForeground(0x00FFFF)
- if (type == "G") then
- gpu.setForeground(0xFFD700)
- else
- gpu.setForeground(0xE0FFFF)
- end
- io.write("[" .. type .. "] ")
- gpu.setForeground(0x8cFF00)
- io.write(nick)
- gpu.setForeground(0xFFFFFF)
- io.write(": ")
- writeMessage(msg, l)
- io.write("\n")
- end
- print("Ожидание первого сообщения...")
- local _, add, nick, msg = event.pull("chat_message")
- term.clear()
- local type, pos = isGlobal(msg)
- message(nick, msg, type, pos)
- while true do
- local _, add, nick, msg = event.pull("chat_message")
- local type, pos = isGlobal(msg)
- message(nick, msg, type, pos)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement