forked from MoonlightOwl/Totoro-Bank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_supervisor.lua
335 lines (319 loc) · 10.1 KB
/
bank_supervisor.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
-- Bank Supervisor (1.4)
-- computercraft.ru (c) Totoro
local term = require('term')
local event = require('event')
local fs = require('filesystem')
local serial = require('serialization')
local unicode = require('unicode')
local com = require('component')
local gpu = com.gpu
local modem = com.modem
-- константы
local PORT = 27
local comp = {}
local COMPLISTFILE = "supervisor.dat"
local WIDTH, HEIGHT = gpu.getResolution()
local old = {}
old.fore = gpu.getForeground()
old.back = gpu.getBackground()
local REQUEST = "root@admin:~$ "
-- функции
-- вывод в консоль
local function out(message, dark)
if dark then gpu.setForeground( 0x008800) end
term.write(message)
if dark then gpu.setForeground( 0x00ff00) end
end
local function errout(message)
out("[ERROR] ", true)
out(message)
end
local function netout(message)
out("[NET] ", true)
out(message)
end
-- разбиваем строку по словам
local function split(line)
local data = {}
for word in line:gmatch("%S+") do table.insert(data, word) end
return data
end
-- загружаем список
local function loadCompList(filename)
if fs.exists(filename) then
local file = io.open(filename, "r")
local data = file:read("*a")
file:close()
comp = serial.unserialize(data)
return true
else
print("Computers list file not found. New list will be created.")
comp = {}
return false
end
end
-- сохраняем список
local function saveCompList(filename)
local file = io.open(filename, "w")
local data = serial.serialize(comp)
file:write(data)
file:close()
return true
end
-- добавляем адрес в список
local function addTerminal(address, id)
if comp[id] == nil then comp[id] = {} end
if comp[id].terminal ~= address then
comp[id].terminal = address
saveCompList(COMPLISTFILE)
end
end
local function addTeller(address, id)
if comp[id] == nil then comp[id] = {} end
if comp[id].teller ~= address then
comp[id].teller = address
saveCompList(COMPLISTFILE)
end
end
local function addHard(address, id)
if comp[id] == nil then comp[id] = {} end
if comp[id].hard ~= address then
comp[id].hard = address
saveCompList(COMPLISTFILE)
end
end
-- посылаем команду на один компьютер
local function send(id, message, toterminal, toteller)
if comp[id] ~= nil then
if toterminal and comp[id].terminal ~= nil then
modem.send(comp[id].terminal, PORT, message)
end
if toteller and comp[id].teller ~= nil then
modem.send(comp[id].teller, PORT, message)
end
return true
else
return false
end
end
-- рассылаем команду всем компьютерам в списке
local function sendAll(message, toterminal, toteller)
for a,b in pairs(comp) do
send(a, message, toterminal, toteller)
end
return true
end
-- справка
local function help()
out("[Totoro Bank] Supervisor (v1.3)\n")
out("computercraft.ru (c) All right reserved\n")
out("---\n", true)
out("Available commands:\n")
out(" clear -- clear screen\n")
out(" exit -- leave the program\n")
out(" shutdown [PARAMETER] [IDs ...] -- reboot computers\n")
out(" -all -- all computers\n")
out(" -id -- given id's\n")
out(" block [PARAMETER] [IDs ...] -- block terminals\n")
out(" -all -- all terminals\n")
out(" -id -- given id's\n")
out(" unblock [PARAMETER] [IDs ...] -- unblock terminals\n")
out(" -all -- all terminals\n")
out(" -id -- given id's\n")
out(" pricesupd [PARAMETER] [IDs ...] -- force update prices\n")
out(" -all -- all robots\n")
out(" -id -- given id's\n")
out(" doors [PARAMETER] [IDs ...] -- manage cabin doors\n")
out(" -open -- \n")
out(" -close -- action\n")
out(" -all -- all doors\n")
out(" -id -- on given id's\n")
out(" hardlist -- display all hard drive addresses\n")
out(" drop database -- remove all addresses\n")
out(" help -- this text\n")
out("---\n", true)
out("Created by Totoro, AlexCC\n")
end
local function hardlist()
for a,b in pairs(comp) do
out("#"..a.." Hard: ")
if b.hard ~= nil then
out(b.hard.."\n")
else
out(" ---- \n")
end
end
end
-- стандартная команда
local function standart(command, data, message, toterminal, toteller)
if data[1] == command then
-- все компьютеры
if data[2] == nil or data[2] == '-all' then
out("Processing...\n")
sendAll(command, toterminal, toteller)
out("All computers have been "..message.."\n")
-- перечисленные компьютеры
elseif data[2] == '-id' then
local n = 3
while true do
local id = tonumber(data[n])
if id ~= nil then
if send(id, command, toterminal, toteller) then
out("#"..data[n].." "..message.."\n")
else
errout("Not found, ID: "..data[n].."\n")
end
n = n + 1
else
if data[n] ~= nil then
errout("Wrong ID: "..data[n].."\n")
end
break
end
end
out("Done\n")
else
errout("Wrong parameter\n")
end
return true
end
return false
end
-- выполнение команд админа
local function execute(command)
local data = split(command)
if data[1] == 'help' then
help()
elseif data[1] == 'clear' then
term.clear()
elseif data[1] == 'exit' then
return false
elseif data[1] == 'hardlist' then
hardlist()
elseif data[1] == 'drop' and data[2] == 'database' then
comp = {}
saveCompList(COMPLISTFILE)
elseif data[1] == 'doors' then
if data[2] == '-open' then
data[2] = "opendoor"
table.remove(data, 1)
standart("opendoor", data, "opened", true, false)
elseif data[2] == '-close' then
data[2] = "closedoor"
table.remove(data, 1)
standart("closedoor", data, "closed", true, false)
end
elseif data[1] == 'alexcc' then
out("Hi, AlexCC!\n")
elseif data[1] == 'totoro' then
out("Hi, Totoro!\n")
else
local ok = false
ok = ok or standart("shutdown", data, "restarted", true, true)
ok = ok or standart("block", data, "blocked", true, false)
ok = ok or standart("unblock", data, "unblocked", true, false)
ok = ok or standart("pricesupd", data, "updated", false, true)
if not ok then errout("Unknown command") end
end
return true
end
-- инициализация
-- чистим экран
gpu.setForeground( 0x00FF00)
gpu.setBackground( 0x000000)
term.clear()
-- шапка
--out("[Bank Admin Tool] Supervisor v1.2\n")
--out(string.rep("-", WIDTH).."\n", true)
-- загружаем список адресов
loadCompList(COMPLISTFILE)
-- включаем мигание курсора
term.setCursorBlink(true)
-- открываем порт
modem.open(PORT)
-- приглашение
out("\n"..REQUEST, true)
local command = ""
local history = {pos = 1}
-- главный цикл - ловим эвенты, слушаем эфир
while true do
local name, add, sender, code, _, message, data, data2 = event.pull()
if name == 'modem_message' then
-- если получено оповещение от компьютера
-- заносим его адрес в список
local id = tonumber(data)
if id ~= nil then
if message == 'terminal' then
addTerminal(sender, id)
term.clearLine()
netout("New terminal: #"..id.." : "..sender.."\n")
out(REQUEST, true)
term.write(command)
elseif message == 'teller' then
addTeller(sender, id)
addHard(data2, id)
term.clearLine()
netout("New teller: #"..id.." : "..sender.."\n")
out(REQUEST, true)
term.write(command)
end
end
elseif name == 'key_down' then
-- проверка на спецсимволы
if sender > 30 then
local letter = unicode.char(sender)
command = command..letter
term.write(letter)
else
-- enter
if code == 28 then
-- исполняем
print()
if not execute(command) then break end
out("\n"..REQUEST, true)
-- записываем для истории
table.insert(history, command)
if #history > 40 then table.remove(history, 1) end
history.pos = #history+1
-- новая
command = ""
-- backspace
elseif code == 14 then
if unicode.len(command) > 0 then
local x, y = term.getCursor()
gpu.set(x-1, y, ' ')
term.setCursor(x-1, y)
command = unicode.sub(command, 1, -2)
end
-- up
elseif code == 200 or code == 208 then
if #history > 0 then
if code == 200 then
if history.pos > 1 then history.pos = history.pos - 1 end
else
if history.pos < #history+1 then history.pos = history.pos + 1 end
end
local x, y = term.getCursor()
gpu.set(#REQUEST+1, y, string.rep(' ', unicode.len(command)))
term.setCursor(#REQUEST+1, y)
if history[history.pos] ~= nil then
command = history[history.pos]
else
command = ""
end
term.write(command)
end
end
end
end
end
-- сохраняем список адресов
saveCompList(COMPLISTFILE)
-- выключаем мигание курсора
term.setCursorBlink(false)
-- закрываем порт
modem.close(PORT)
-- чистим экран
gpu.setForeground(old.fore)
gpu.setBackground(old.back)
term.clear()