Created
November 13, 2014 20:30
-
-
Save Quader/33fe56e3c22cd2cf0701 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local myid = os.computerID() | |
local doorList = { 11,13 } | |
local passwordForDoor = { "notch","minecraft" } | |
mon=peripheral.wrap("top") | |
print("Access Terminal") | |
os.loadAPI("json") | |
os.loadAPI("peripherals") | |
-- List of all sides | |
local lstSides = {"left","right","up","down","front","back"}; | |
for i, side in pairs(lstSides) do | |
if (peripheral.isPresent(side)) then | |
-- Yup, there is something on this side | |
-- Check the type against the device | |
-- string.lower() just ensures that the passed | |
-- device type is in lower case. | |
-- peripheral.getType() always returns lower case texts | |
if (peripheral.getType(side) == string.lower("modem"))) then | |
-- Yes, this is the device type we need, so return the side | |
print(side); | |
-- Note, that this call to "return" also terminates | |
-- further running of this function | |
end -- if .getType() | |
end -- if .isPresent() | |
end -- for-do | |
-- local deviceSide = peripherals.getDeviceSide("modem") | |
rednet.open("top") | |
print("Computer id for Access Terminal is "..tostring(myid)) | |
function findIndexForId(id) | |
for i,v in ipairs(doorList) do | |
if id == v then | |
return i | |
end | |
end | |
return 0 | |
end | |
function setPasswordForLock(id,password) | |
local i = findIndexForId(id) | |
if i == 0 then | |
return false | |
end | |
passwordForDoor[i] = password | |
print("in setPasswordForLock"..id..password) | |
return true | |
end | |
function checkPasswordForLock(password, player) | |
fileStr = http.get("https://quadercc.cloudant.com/cc/b4c17ee1b81a2799bd30e95ceb2cbb4e") | |
jsonObj = json.decode(fileStr.readAll()) | |
for key, value in pairs(jsonObj) do | |
if key == "player" then | |
local playerObj = value | |
for key,value in pairs(playerObj) do | |
if value["name"] == player and value["password"] == password then | |
return 1 | |
end | |
end | |
return 0 | |
end | |
end | |
if i == 0 then | |
return -1 | |
end | |
if passwordForDoor[i] == password then | |
return 1 | |
else | |
return 0 | |
end | |
end | |
local isValid = 0 | |
while true do | |
local timeString = textutils.formatTime(os.time(),false) | |
local senderId, response = rednet.receive() | |
local responseTable = json.decode(response) | |
isValid = checkPasswordForLock(responseTable["password"], responseTable["name"]) | |
print(tostring(isValid)) | |
if isValid == -1 then | |
print("server "..senderId.." sent us a request but is not in our list") | |
elseif isValid == 1 then | |
rednet.send(senderId, "Valid") | |
else | |
rednet.send(senderId, "Not Valid") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment