I have created a main loop (At the bottom of the code) which should get data from a table then compare it to the whitelist table then if the player detected from the sensor is equal to the whitelist then it will do nothing and continue the loop however if the player is not on the whitelist something else will hapen My main issue is that I do not know how to do this and if I need to have both tables unserialized or serialised to compare the data. I don't even know how to do this. or where to begin.
Spoiler
-- Code Written by Ian
-- Before using you need to manually add and assign a table to the WL file by removing the commented line then re adding it.
-- Bootstrapper
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")
term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")
os.pullEvent("mouse_click")
term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)
local x = 14
while x < 25 do
paintutils.drawPixel(x,7, colors.green)
sleep(0.2)
x = x + 1
end
-- breakInterface Function
function breakInterface()
term.clear()
term.setCursorPos(12,5)
term.setTextColor(colors.white)
write("Defence Operational")
term.setCursorPos(8,7)
write("Code > ")
local code = read()
if code == "Safe" then
term.clear()
term.setCursorPos(10,7)
print("Protection Deactivated")
sleep(1)
os.shutdown()
elseif code == "Vent" then
venting()
elseif code == "List" then
list()
else
term.clear()
term.setCursorPos(10,7)
printError("Command Not Recognised")
sleep(1)
breakInterface()
end
end
-- venting Function
function venting()
term.clear()
term.setCursorPos(12,5)
term.setTextColor(colors.white)
write("Are you sure?")
term.setCursorPos(8,7)
write("[Y/N] > ")
local code = read()
if code == "Y" then
term.clear()
term.setCursorPos(17,5)
textutils.slowPrint("Venting")
for i = 0,4 do
rs.setOutput("back", true)
sleep(0.1)
rs.setOutput("back", false)
sleep(6)
end
term.clear()
term.setCursorPos(1,1)
shell.run("delete ME")
os.shutdown()
elseif code == "N" then
term.clear()
term.setCursorPos(12,5)
textutils.slowPrint("Venting Stopped")
sleep(1)
breakInterface()
else
term.clear()
term.setCursorPos(10,7)
printError("Command Not Recognised")
sleep(1)
venting()
end
end
-- whitelist function
function list()
-- Save
local function save(whitelist,WL)
local file = fs.open("WL","w")
file.write(textutils.serialize(whitelist))
file.close()
end
-- Load
local function pull(WL)
local file = fs.open("WL","r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
-- Variables
whitelist = {}
whitelist = pull(WL) -- A table needs to exist before using this
sensor = peripheral.wrap("left")
term.clear()
term.setCursorPos(2,2)
write("Whitelisted Players [Max 3]")
sleep(1)
for i = 1,3 do
term.setCursorPos(2,i+3)
textutils.slowPrint(whitelist[i])
sleep(0.2)
end
term.setCursorPos(2,8)
write("[Add/Remove] > ")
local code = read()
if code == "Add" then
term.clear()
term.setCursorPos(2,2)
write("Insert Player IGN > ")
local IGN = read()
table.insert(whitelist,IGN)
save(whitelist,WL)
term.clear()
term.setCursorPos(2,2)
write(IGN.." has been whitelisted.")
sleep(2)
breakInterface()
elseif code == "Remove" then
term.clear()
term.setCursorPos(2,2)
write("Whitelisted Players [Max 3]")
sleep(1)
for i = 1,3 do
term.setCursorPos(2,i+3)
textutils.slowPrint(whitelist[i].." "..i)
sleep(0.2)
end
term.setCursorPos(2,8)
write("Insert Player Number > ")
local IGN = read()
table.remove(whitelist,IGN)
save(whitelist,WL)
term.clear()
term.setCursorPos(2,2)
write("Whitelisted Players [Max 3]")
sleep(1)
for i = 1,3 do
term.setCursorPos(2,i+3)
textutils.slowPrint(whitelist[i])
sleep(0.2)
end
sleep(2)
breakInterface()
else
term.clear()
term.setCursorPos(10,7)
printError("Command Not Recognised")
sleep(1)
breakInterface()
end
end
-- mainDefence Function
function mainDefence()
while true do
local detect = textutils.serialize(sensor.getPlayers())
end
end
breakInterface()
Any help will be greatly appreciated.
Thanks
- Ian