View difference between Paste ID: QHELAPj7 and LHYvxrWm
SHOW: | | - or go back to the newest paste.
1
--pastebin run QHELAPj7
2
--[[
3
4
Создано командой SOCC company
5
Nightmare_Night
6
7
P.S. Программа написана на скорую руку
8
Просьба тапками не кидаться
9
10
P.S.S. Функция получения времени хост-машины была взята из API ECS
11
12
]]--
13
14
local component = require("component")
15
local term = require("term")
16
local event = require("event")
17
local fs = require("filesystem")
18
19
local gpu = component.gpu
20
21
local LOG_PATH = "chat_log.txt"
22
23
-- Конвертирует строку в массив
24
function stringToArray(text)
25
  t = {}
26
  text:gsub(".",function(c) table.insert(t,c) end)
27
  return t
28
end
29
30
--Получить текущее реальное время компьютера, хостящего сервер майна
31
function getHostTime(timezone)
32
	timezone = timezone or 2
33
    local file = io.open("/HostTime.tmp", "w")
34
    file:write("")
35
    file:close()
36
    local timeCorrection = timezone * 3600
37
    local lastModified = tonumber(string.sub(fs.lastModified("/HostTime.tmp"), 1, -4)) + timeCorrection
38
    fs.remove("/HostTime.tmp")
39
    local year, month, day, hour, minute, second = os.date("%Y", lastModified), os.date("%m", lastModified), os.date("%d", lastModified), os.date("%H", lastModified), os.date("%M", lastModified), os.date("%S", lastModified)
40
    return tonumber(day), tonumber(month), tonumber(year), tonumber(hour), tonumber(minute), tonumber(second)
41
end
42
43
-- Получет настоящее время, стоящее на Хост-машине
44
function real_time()
45
  local time = {getHostTime(3)}
46
  local text = string.format("%02d:%02d:%02d", time[4], time[5], time[6])
47
  return text
48
end
49
50
-- Проверяет является ли текст окрашенным
51
function isColored(text)
52
  for pos, i in pairs(stringToArray(text)) do
53
    if (i ~= "&") then
54
      if (i ~= " ") then
55
        return false
56
      end
57
    else
58
      return true
59
    end
60
  end
61
62
  return true
63
end
64
65
-- Проверяет в глобальном ли чате написано сообщение
66
function isGlobal(text)
67
  for pos, i in pairs(stringToArray(text)) do
68
    if (i ~= "!") then
69
      if (i ~= " ") then
70
        return false
71
      end
72
    else
73
      return true, pos
74
    end
75
  end
76
  return false
77
end
78
79
-- Делит строку на части
80
function split(str, pat)
81
   local t = {}
82
   local fpat = "(.-)" .. pat
83
   local last_end = 1
84
   local s, e, cap = str:find(fpat, 1)
85
   while s do
86
      if s ~= 1 or cap ~= "" then
87
      table.insert(t,cap)
88
      end
89
      last_end = e+1
90
      s, e, cap = str:find(fpat, last_end)
91
   end
92
   if last_end <= #str then
93
      cap = str:sub(last_end)
94
      table.insert(t, cap)
95
   end
96
   return t
97
end
98
99
-- Устанавливает цвет шрифта в зависимости от патерна
100
function setColor(num)
101
  if (num == "0") then
102
    gpu.setForeground(0x333333)
103
  end
104
105
  if (num == "1") then
106
    gpu.setForeground(0x000099)
107
  end
108
109
  if (num == "2") then
110
    gpu.setForeground(0x006600)
111
  end
112
113
  if (num == "3") then
114
    gpu.setForeground(0x006666)
115
  end
116
117
  if (num == "4") then
118
    gpu.setForeground(0x660000)
119
  end
120
121
  if (num == "5") then
122
    gpu.setForeground(0x660066)
123
  end
124
125
  if (num == "6") then
126
    gpu.setForeground(0xFF8000)
127
  end
128
129
  if (num == "7") then
130
    gpu.setForeground(0xA0A0A0)
131
  end
132
133
  if (num == "8") then
134
    gpu.setForeground(0x404040)
135
  end
136
137
  if (num == "9") then
138
    gpu.setForeground(0x3399FF)
139
  end
140
141
  if (num == "a") then
142
    gpu.setForeground(0x99FF33)
143
  end
144
145
  if (num == "b") then
146
    gpu.setForeground(0x00FFFF)
147
  end
148
149
  if (num == "c") then
150
    gpu.setForeground(0xFF3333)
151
  end
152
153
  if (num == "d") then
154
    gpu.setForeground(0xFF00FF)
155
  end
156
157
  if (num == "e") then
158
    gpu.setForeground(0xFFFF00)
159
  end
160
161
  if (num == "f") then
162
    gpu.setForeground(0xFFFFFF)
163
  end
164
end
165
166
-- Выводит сообщение
167
function writeMessage(text)  
168
    local t = split(text, "&")
169
    for pos, i in pairs(t) do
170
      if (pos == 1 and not isColored(text)) then
171
        io.write(i)
172
      else
173
        setColor(string.sub(i, 1, 1))
174
        io.write(string.sub(i, 2))
175
      end
176
    end
177
end
178
179
-- Выводит остальную часть сообщения
180
function message(nick, msg, isGlobal, pos)
181
  local type = ""
182
  if (isGlobal) then msg = string.sub(msg, pos + 1) type = "G" else type = "L" end
183
184
  local file = fs.open(LOG_PATH, "a")
185
  file:write("[" .. real_time() .. "] [" .. type .. "] " .. nick .. ": " .. msg .. "\n")
186
  file:close()
187
188
  gpu.setForeground(0x00FFFF)
189
  io.write("[" .. real_time() .. "] ")
190
  gpu.setForeground(0xFFFFFF)
191
  if (type == "G") then
192
    gpu.setForeground(0xFF9933)
193
  else
194
    gpu.setForeground(0xFFFFFF)
195
  end
196
  io.write("[" .. type .. "] ")
197
  gpu.setForeground(0x00FF00)
198
  io.write(nick)
199
  gpu.setForeground(0xFFFFFF)
200
  io.write(": ")
201
  writeMessage(msg, l)
202
  io.write("\n")
203
end
204
205
print("Инициализация...")
206
os.sleep(1)
207
print("Ожидание первого сообщения...")
208
209
local _, add, nick, msg = event.pull("chat_message") 
210
term.clear()
211
local type, pos = isGlobal(msg)
212
message(nick, msg, type, pos)
213
214
215
while true do
216
217
  local _, add, nick, msg = event.pull("chat_message") 
218
  local type, pos = isGlobal(msg)
219
  message(nick, msg, type, pos)
220
221
end