SHOW:
|
|
- or go back to the newest paste.
1 | --Library for saving/loading table for all this code. all the settings below are saved in it. | |
2 | local ttf=require("tableToFile") | |
3 | local doorVersion = "7.0" | |
4 | - | local testR = true |
4 | + | testR = true |
5 | - | --0 = doorcontrol block. 1 = redstone. 2 = bundled redstone. 3 = rolldoor |
5 | + | |
6 | - | local doorType = 0 |
6 | + | --0 = doorcontrol block. 1 = redstone. 2 = bundled redstone. Always bundled redstone with this version of the code. |
7 | local doorType = 2 | |
8 | - | --bottom = 0; top = 1; back = 2 front = 3 right = 4 left = 5 |
8 | + | |
9 | - | local redSide = 0 |
9 | + | --bottom = 0; top = 1; back = 2 front = 3 right = 4 left = 5. Should always be 2 for back. |
10 | local redSide = 2 | |
11 | --if doortype =2, set this to the color you want to output in. | |
12 | local redColor = 0 | |
13 | --Delay before the door closes again | |
14 | local delay = 5 | |
15 | - | --0 = level; 1 = armory level; 2 = MTF; 3 = GOI; 4 = Security; 5 = Department |
15 | + | |
16 | --0 = level; 1 = armory level; 2 = MTF; 3 = GOI; 4 = Security | |
17 | local cardRead = 0; | |
18 | - | --If cardRead = 0, then it is the card level. |
18 | + | |
19 | - | --If cardRead = 1, then it is the armory level. |
19 | + | |
20 | - | --If cardRead = 5, then it is the department. 1=SD, 2=ScD, 3=MD, 4=E&T, 5=O5 (any department door) |
20 | + | |
21 | local forceOpen = 1 | |
22 | local bypassLock = 0 | |
23 | - | --toggle=0: it will automatically close after being opened. |
23 | + | |
24 | - | --toggle=1: it will stay open/closed when opened. |
24 | + | local doorAddress = "" |
25 | ||
26 | local toggle = 0 | |
27 | ||
28 | local adminCard = "admincard" | |
29 | - | --Labels for admin security cards, which are cards that make the security system send diagnostic info of the door. |
29 | + | |
30 | ||
31 | local cryptKey = {1, 2, 3, 4, 5} | |
32 | local modemPort = 199 | |
33 | - | local departments = {"SD","ScD","MD","E&T","O5"} |
33 | + | local updatePort = 197 |
34 | local diagPort = 180 | |
35 | - | local updatePort = 198 |
35 | + | |
36 | local serverSend = {"checkuser","checkarmor","checkMtf","checkgoi","checksec","checkdepartment","checkint","checkstaff"} | |
37 | ||
38 | local component = require("component") | |
39 | local gpu = component.gpu | |
40 | local event = require("event") | |
41 | local ser = require("serialization") | |
42 | local term = require("term") | |
43 | local thread = require("thread") | |
44 | local process = require("process") | |
45 | local computer = component.computer | |
46 | ||
47 | local magReader = component.os_magreader | |
48 | ||
49 | local modem = component.modem | |
50 | ||
51 | local settingData = {} | |
52 | ||
53 | local function convert( chars, dist, inv ) | |
54 | return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 ) | |
55 | end | |
56 | ||
57 | local function crypt(str,k,inv) | |
58 | local enc= ""; | |
59 | for i=1,#str do | |
60 | if(#str-k[5] >= i or not inv)then | |
61 | for inc=0,3 do | |
62 | if(i%4 == inc)then | |
63 | enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv); | |
64 | break; | |
65 | end | |
66 | end | |
67 | end | |
68 | end | |
69 | if(not inv)then | |
70 | for i=1,k[5] do | |
71 | enc = enc .. string.char(math.random(32,126)); | |
72 | end | |
73 | end | |
74 | return enc; | |
75 | end | |
76 | ||
77 | ||
78 | function splitString(str, sep) | |
79 | local sep, fields = sep or ":", {} | |
80 | local pattern = string.format("([^%s]+)", sep) | |
81 | str:gsub(pattern, function(c) fields[#fields+1] = c end) | |
82 | return fields | |
83 | end | |
84 | ||
85 | local function update(msg, localAddress, remoteAddress, port, distance, msg, data) | |
86 | if(port == updatePort and testR == true) then | |
87 | data = crypt(data, cryptKey, true) | |
88 | if msg == "update" then | |
89 | term.write("Updating door") | |
90 | local fileReceiveFinal = io.open("ctrl.lua","w") | |
91 | fileReceiveFinal:write(data) | |
92 | fileReceiveFinal:flush() | |
93 | fileReceiveFinal:close() | |
94 | event.ignore("modem_message", update) | |
95 | os.execute("ctrl") | |
96 | os.exit() | |
97 | elseif msg == "forceopen" then | |
98 | - | elseif msg == "forceopen" and forceOpen ~= 0 then |
98 | + | local keyed = nil |
99 | if data == "open" then | |
100 | - | if(doorType == 0)then |
100 | + | for key, valued in pairs(settingData) do |
101 | - | component.os_doorcontroller.open() |
101 | + | if valued.forceOpen ~= 0 then |
102 | - | elseif(doorType == 1)then |
102 | + | if valued.doorType == 0 then |
103 | - | if(component.redstone.getOutput(redSide) == 0) then |
103 | + | component.proxy(valued.doorAddress).open() |
104 | - | component.redstone.setOutput(redSide,15) |
104 | + | elseif valued.doorType == 1 then |
105 | ||
106 | - | component.redstone.setOutput(redSide,0) |
106 | + | elseif valued.doorType == 2 then |
107 | component.redstone.setBundledOutput(redSide, { [valued.redColor] = 255}) | |
108 | - | elseif(doorType == 2)then |
108 | + | elseif valued.doorType == 3 then |
109 | - | if(component.redstone.getBundledOutput(redSide, redColor) == 0) then |
109 | + | component.proxy(valued.doorAddress).open() |
110 | - | component.redstone.setBundledOutput(redSide, { [redColor] = 255 } ) |
110 | + | end |
111 | end | |
112 | - | component.redstone.setBundledOutput(redSide, { [redColor] = 0 } ) |
112 | + | end |
113 | else | |
114 | for key, valued in pairs(settingData) do | |
115 | - | component.os_rolldoorcontroller.open() |
115 | + | if valued.forceOpen ~= 0 then |
116 | if valued.doorType == 0 then | |
117 | component.proxy(valued.doorAddress).close() | |
118 | - | if(doorType == 0)then |
118 | + | elseif valued.doorType == 1 then |
119 | - | component.os_doorcontroller.close() |
119 | + | |
120 | - | elseif(doorType == 1)then |
120 | + | elseif valued.doorType == 2 then |
121 | - | if(component.redstone.getOutput(redSide) == 0) then |
121 | + | component.redstone.setBundledOutput(redSide, { [valued.redColor] = 0}) |
122 | - | component.redstone.setOutput(redSide,15) |
122 | + | elseif valued.doorType == 3 then |
123 | component.proxy(valued.doorAddress).close() | |
124 | - | component.redstone.setOutput(redSide,0) |
124 | + | end |
125 | end | |
126 | - | elseif(doorType == 2)then |
126 | + | end |
127 | - | if(component.redstone.getBundledOutput(redSide, redColor) == 0) then |
127 | + | |
128 | - | component.redstone.setBundledOutput(redSide, { [redColor] = 255 } ) |
128 | + | |
129 | end | |
130 | - | component.redstone.setBundledOutput(redSide, { [redColor] = 0 } ) |
130 | + | |
131 | ||
132 | function openDoor() | |
133 | - | component.os_rolldoorcontroller.close() |
133 | + | local delayH = delay |
134 | local redColorH = redColor | |
135 | local doorAddressH = doorAddress | |
136 | local toggleH = toggle | |
137 | if(toggleH == 0) then | |
138 | if(doorType == 0 or doorType == 3)then | |
139 | component.proxy(doorAddressH).toggle() | |
140 | os.sleep(delayH) | |
141 | - | if(toggle == 0) then |
141 | + | component.proxy(doorAddressH).toggle() |
142 | - | if(doorType == 0)then |
142 | + | elseif(doorType == 1)then |
143 | - | component.os_doorcontroller.toggle() |
143 | + | component.redstone.setOutput(redSide,15) |
144 | - | os.sleep(delay) |
144 | + | os.sleep(delayH) |
145 | - | component.os_doorcontroller.toggle() |
145 | + | component.redstone.setOutput(redSide,0) |
146 | - | elseif(doorType == 1)then |
146 | + | elseif(doorType == 2)then |
147 | - | component.redstone.setOutput(redSide,15) |
147 | + | component.redstone.setBundledOutput(redSide, { [redColorH] = 255 } ) |
148 | - | os.sleep(delay) |
148 | + | os.sleep(delayH) |
149 | - | component.redstone.setOutput(redSide,0) |
149 | + | component.redstone.setBundledOutput(redSide, { [redColorH] = 0 } ) |
150 | - | elseif(doorType == 2)then |
150 | + | else |
151 | - | component.redstone.setBundledOutput(redSide, { [redColor] = 255 } ) |
151 | + | os.sleep(1) |
152 | - | os.sleep(delay) |
152 | + | end |
153 | - | component.redstone.setBundledOutput(redSide, { [redColor] = 0 } ) |
153 | + | |
154 | if(doorType == 0 or doorType == 3)then | |
155 | - | component.os_rolldoorcontroller.toggle() |
155 | + | component.proxy(doorAddressH).toggle() |
156 | - | os.sleep(delay) |
156 | + | elseif(doorType == 1)then |
157 | - | component.os_rolldoorcontroller.toggle() |
157 | + | if(component.redstone.getOutput(redSide) == 0) then |
158 | component.redstone.setOutput(redSide,15) | |
159 | else | |
160 | - | if(doorType == 0)then |
160 | + | component.redstone.setOutput(redSide,0) |
161 | - | component.os_doorcontroller.toggle() |
161 | + | end |
162 | - | elseif(doorType == 1)then |
162 | + | elseif(doorType == 2)then |
163 | - | if(component.redstone.getOutput(redSide) == 0) then |
163 | + | if(component.redstone.getBundledOutput(redSide, redColorH) == 0) then |
164 | - | component.redstone.setOutput(redSide,15) |
164 | + | component.redstone.setBundledOutput(redSide, { [redColorH] = 255 } ) |
165 | else | |
166 | - | component.redstone.setOutput(redSide,0) |
166 | + | component.redstone.setBundledOutput(redSide, { [redColorH] = 0 } ) |
167 | end | |
168 | - | elseif(doorType == 2)then |
168 | + | else |
169 | - | if(component.redstone.getBundledOutput(redSide, redColor) == 0) then |
169 | + | os.sleep(1) |
170 | - | component.redstone.setBundledOutput(redSide, { [redColor] = 255 } ) |
170 | + | end |
171 | end | |
172 | - | component.redstone.setBundledOutput(redSide, { [redColor] = 0 } ) |
172 | + | |
173 | ||
174 | term.clear() | |
175 | - | component.os_rolldoorcontroller.toggle() |
175 | + | |
176 | if fill~=nil then | |
177 | io.close(fill) | |
178 | else | |
179 | settingData["q"] = {} | |
180 | settingData["w"] = {} | |
181 | settingData["q"]["reader"] = "" | |
182 | settingData["q"]["redColor"] = 0 | |
183 | settingData["q"]["delay"] = 5 | |
184 | settingData["q"]["cardRead"] = 0 | |
185 | - | settingData["doorType"] = 0 |
185 | + | settingData["q"]["accessLevel"] = 1 |
186 | - | settingData["redSide"] = 0 |
186 | + | settingData["q"]["doorType"] = 2 |
187 | - | settingData["redColor"] = 0 |
187 | + | settingData["q"]["doorAddress"] = "" |
188 | - | settingData["delay"] = 5 |
188 | + | settingData["q"]["toggle"] = 0 |
189 | - | settingData["cardRead"] = 0 |
189 | + | settingData["q"]["forceOpen"] = 1 |
190 | - | settingData["accessLevel"] = 1 |
190 | + | settingData["q"]["bypassLock"] = 0 |
191 | - | settingData["toggle"] = 1 |
191 | + | settingData["w"]["reader"] = "" |
192 | - | settingData["forceOpen"] = 1 |
192 | + | settingData["w"]["redColor"] = 0 |
193 | - | settingData["bypassLock"] = 0 |
193 | + | settingData["w"]["delay"] = 5 |
194 | settingData["w"]["cardRead"] = 0 | |
195 | settingData["w"]["accessLevel"] = 1 | |
196 | settingData["w"]["doorType"] = 2 | |
197 | settingData["w"]["doorAddress"] = "" | |
198 | - | |
198 | + | settingData["w"]["toggle"] = 0 |
199 | - | doorType = settingData.doorType |
199 | + | settingData["w"]["forceOpen"] = 1 |
200 | - | redSide = settingData.redSide |
200 | + | settingData["w"]["bypassLock"] = 0 |
201 | - | redColor = settingData.redColor |
201 | + | |
202 | - | delay = settingData.delay |
202 | + | |
203 | - | cardRead = settingData.cardRead |
203 | + | |
204 | - | accessLevel = settingData.accessLevel |
204 | + | |
205 | - | toggle = settingData.toggle |
205 | + | |
206 | - | forceOpen = settingData.forceOpen |
206 | + | print("Multi-Door Control terminal") |
207 | - | bypassLock = settingData.bypassLock |
207 | + | |
208 | - | |
208 | + | |
209 | if modem.isOpen(modemPort) == false then | |
210 | - | if (cardRead == 0)then |
210 | + | |
211 | - | print("ACCESS LEVEL " .. tostring(accessLevel) .. " REQUIRED") |
211 | + | |
212 | - | elseif (cardRead == 1)then |
212 | + | |
213 | - | print("ARMORY CLEARANCE LEVEL " .. tostring(accessLevel) .. " REQUIRED") |
213 | + | |
214 | - | elseif (cardRead == 2)then |
214 | + | |
215 | - | print("MTF PASS REQUIRED") |
215 | + | |
216 | - | elseif (cardRead == 3)then |
216 | + | |
217 | - | print("GOI PASS REQUIRED") |
217 | + | |
218 | - | elseif (cardRead == 4)then |
218 | + | |
219 | - | print("SECURITY PASS REQUIRED") |
219 | + | |
220 | - | elseif (cardRead == 5)then |
220 | + | |
221 | - | print("DEPARTMENT " .. departments[accessLevel] .. " ONLY") |
221 | + | |
222 | - | elseif (cardRead == 6)then |
222 | + | |
223 | - | print("INTERCOM PASS REQUIRED") |
223 | + | |
224 | - | elseif (cardRead == 7)then |
224 | + | |
225 | - | print("STAFF ONLY") |
225 | + | |
226 | end | |
227 | ev, address, user, str, uuid, data = event.pull("magData") | |
228 | term.write(str .. "\n") | |
229 | ||
230 | local keyed = nil | |
231 | for key, valuedd in pairs(settingData) do | |
232 | if(valuedd.reader == address) then | |
233 | keyed = key | |
234 | end | |
235 | end | |
236 | local isOk = "incorrect magreader" | |
237 | if(keyed ~= nil)then | |
238 | term.write(settingData[keyed].redColor) | |
239 | redColor = settingData[keyed].redColor | |
240 | delay = settingData[keyed].delay | |
241 | - | end |
241 | + | cardRead = settingData[keyed].cardRead |
242 | accessLevel = settingData[keyed].accessLevel | |
243 | doorType = settingData[keyed].doorType | |
244 | doorAddress = settingData[keyed].doorAddress | |
245 | toggle = settingData[keyed].toggle | |
246 | forceOpen = settingData[keyed].forceOpen | |
247 | - | ev, _, user, str, uuid, data = event.pull("magData") |
247 | + | bypassLock = settingData[keyed].bypassLock |
248 | isOk = "ok" | |
249 | else | |
250 | print("MAG READER IS NOT SET UP! PLEASE FIX") | |
251 | end | |
252 | ||
253 | local data = crypt(str, cryptKey, true) | |
254 | - | local diagData = settingData |
254 | + | |
255 | - | diagData["status"] = "ok" |
255 | + | |
256 | - | diagData["type"] = "single" |
256 | + | |
257 | modem.open(diagPort) | |
258 | - | diagData["key"] = "NAN" |
258 | + | local diagData = settingData[keyed] |
259 | if diagData == nil then | |
260 | - | modem.broadcast(diagPort, "diag", data) |
260 | + | diagData = {} |
261 | end | |
262 | diagData["status"] = isOk | |
263 | diagData["type"] = "multi" | |
264 | diagData["version"] = doorVersion | |
265 | diagData["key"] = keyed | |
266 | local counter = 0 | |
267 | for index in pairs(settingData) do | |
268 | counter = counter + 1 | |
269 | end | |
270 | - | |
270 | + | diagData["entries"] = counter |
271 | data = crypt(ser.serialize(diagData),cryptKey) | |
272 | - | data = crypt(tostring(accessLevel), cryptKey) |
272 | + | modem.broadcast(diagPort, "temp", data) |
273 | else | |
274 | local tmpTable = ser.unserialize(data) | |
275 | term.write(tmpTable["name"] .. ":") | |
276 | if modem.isOpen(modemPort) == false then | |
277 | modem.open(modemPort) | |
278 | end | |
279 | - | (tmpTable["uuid"], cryptKey) |
279 | + | |
280 | - | modem.broadcast(modemPort, serverSend[(cardRead + 1)], data, bypassLock) |
280 | + | |
281 | end | |
282 | - | |
282 | + | |
283 | data = crypt(tostring(accessLevel), cryptKey) | |
284 | modem.broadcast(modemPort, "setlevel", data) | |
285 | data = crypt | |
286 | (tmpTable["uuid"], cryptKey) | |
287 | modem.broadcast(modemPort, serverSend[(cardRead + 1)], data, bypassLock) | |
288 | elseif (cardRead == 2 or cardRead == 3 or cardRead == 4 or cardRead == 6 or cardRead == 7) then | |
289 | data = crypt | |
290 | - | openDoor() |
290 | + | |
291 | modem.broadcast(modemPort, serverSend[(cardRead + 1)], data, bypassLock) | |
292 | end | |
293 | local e, _, from, port, _, msg = event.pull(1, "modem_message") | |
294 | if e then | |
295 | data = crypt(msg, cryptKey, true) | |
296 | -- print(data) | |
297 | if data == "true" then | |
298 | term.write("Access granted\n") | |
299 | computer.beep() | |
300 | thread.create(function(delayH, redColorH, doorAddressH, toggleH, doorTypeH, redSideH) | |
301 | if(toggleH == 0) then | |
302 | if(doorTypeH == 0 or doorTypeH == 3)then | |
303 | component.proxy(doorAddressH).toggle() | |
304 | os.sleep(delayH) | |
305 | component.proxy(doorAddressH).toggle() | |
306 | elseif(doorTypeH == 1)then | |
307 | component.redstone.setOutput(redSideH,15) | |
308 | os.sleep(delayH) | |
309 | component.redstone.setOutput(redSideH,0) | |
310 | elseif(doorTypeH == 2)then | |
311 | component.redstone.setBundledOutput(redSideH, { [redColorH] = 255 } ) | |
312 | os.sleep(delayH) | |
313 | component.redstone.setBundledOutput(redSideH, { [redColorH] = 0 } ) | |
314 | else | |
315 | os.sleep(1) | |
316 | end | |
317 | else | |
318 | if(doorTypeH == 0 or doorTypeH == 3)then | |
319 | component.proxy(doorAddressH).toggle() | |
320 | elseif(doorTypeH == 1)then | |
321 | if(component.redstone.getOutput(redSideH) == 0) then | |
322 | component.redstone.setOutput(redSideH,15) | |
323 | else | |
324 | component.redstone.setOutput(redSideH,0) | |
325 | end | |
326 | elseif(doorTypeH == 2)then | |
327 | if(component.redstone.getBundledOutput(redSideH, redColorH) == 0) then | |
328 | component.redstone.setBundledOutput(redSideH, { [redColorH] = 255 } ) | |
329 | else | |
330 | component.redstone.setBundledOutput(redSideH, { [redColorH] = 0 } ) | |
331 | end | |
332 | else | |
333 | os.sleep(1) | |
334 | end | |
335 | end | |
336 | end, delay, redColor, doorAddress, toggle, doorType, redSide) | |
337 | elseif data == "false" then | |
338 | term.write("Access denied\n") | |
339 | computer.beep() | |
340 | computer.beep() | |
341 | elseif data == "locked" then | |
342 | term.write("Doors have been locked\n") | |
343 | computer.beep() | |
344 | computer.beep() | |
345 | computer.beep() | |
346 | else | |
347 | term.write("Unknown command\n") | |
348 | end | |
349 | else | |
350 | term.write("server timeout\n") | |
351 | end | |
352 | end | |
353 | end | |
354 | end |