Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local owner = {ign="", uuid=""}
- local hasOwner = false
- local compID = os.getComputerID()
- local compLabel = os.getComputerLabel()
- local secret = ""
- local password = ""
- local proxRadius = 0
- local sensor = peripheral.find("playerSensor")
- if sensor == nil then
- error("No player sensor found. Is one attached?")
- end
- function round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- local function setCompLabel()
- term.clear()
- term.setCursorPos(1,1)
- print("This computer is not labeled. Unlabeled computers")
- print("will lose all their information when broken. Set")
- print("a label here to ensure that doesn't happen.")
- print()
- local myLabel = ""
- while myLabel == "" do
- print("Enter a computer label:")
- print()
- myLabel = read()
- end
- compLabel = myLabel
- os.setComputerLabel(myLabel)
- return
- end
- local function getProxRadius()
- local filePR = 0
- if fs.exists("proxradius") then
- local file = fs.open("proxradius", "r")
- filePR = tonumber(file.readAll())
- file.close()
- if filePR < 1 or filePR > 64 then
- fs.delete("proxradius")
- filePR = 0
- end
- end
- return filePR
- end
- local function setProxRadius()
- term.clear()
- term.setCursorPos(1,1)
- print("This system will scan for players within")
- print("a defined radius. The minimum radius is")
- print("1 block, and the maximum is 64 blocks.")
- print()
- local scanRadius = 0
- while scanRadius < 1 or scanRadius > 64 do
- print("Please enter the scanning radius between 1 and 64:")
- print()
- scanRadius = math.floor(tonumber(read()))
- end
- proxRadius = scanRadius
- local file = fs.open("proxradius", "w")
- file.write(proxRadius)
- file.close()
- end
- local function getSecret()
- if fs.exists("secret") then
- local file = fs.open("secret", "r")
- secret = file.readAll()
- file.close()
- else
- term.clear()
- term.setCursorPos(1,1)
- print("In order to help protect this")
- print("computer's data you must enter")
- print("a secret word or passphrase below.")
- print("You do not need to remember this.")
- print()
- print("Enter your secret:")
- secret = read("*")
- local file = fs.open("secret", "w")
- file.write(secret)
- file.close()
- end
- return
- end
- local function getTime()
- local response = http.get("http://www.circlecraft.info/ccapi/cctime.php")
- local contents = response.readAll()
- response.close()
- return contents
- end
- local function getOwner(ign)
- local response = http.get("http://www.circlecraft.info/ccapi/secsys_getowner.php?ign=" .. ign)
- local contents = response.readAll()
- response.close()
- if contents == "Error: invalid request" then
- error("Error: getOwner failed with invalid request")
- os.exit()
- end
- local retData = {ign="", uuid=""}
- local data = textutils.unserialize(contents)
- if data.uuid ~= "" then
- retData = data
- end
- return retData
- end
- local function getComputerOwner()
- local response = http.get("http://www.circlecraft.info/ccapi/secsys_getcompowner.php?id=" .. compID)
- local contents = response.readAll()
- response.close()
- if contents == "Error: invalid request" then
- error("Error: getOwner failed with invalid request")
- os.exit()
- end
- local data = textutils.unserialize(contents)
- if data.ign ~= "secsys_unowned" then
- owner.ign = data.ign
- owner.uuid = data.uuid
- hasOwner = true
- end
- end
- local function proxLog(ign, distance)
- local response = http.post("http://www.circlecraft.info/ccapi/secsys_proxlog.php",
- "compid=" .. compID .. "&" ..
- "ign=" .. textutils.urlEncode(tostring(ign)) .. "&" ..
- "distance=" .. distance .. "&" ..
- "secret=" .. textutils.urlEncode(tostring(secret)))
- return
- end
- local function createOwner(ign)
- term.clear()
- term.setCursorPos(1,1)
- print("It seems that this is your first time")
- print("registering a computer with this system.")
- print()
- local goodPass = false
- local pass1 = ""
- while goodPass == false do
- print("Enter a password for your account: ")
- pass1 = read("*")
- print("Confirm the password for your account: ")
- local pass2 = read("*")
- if pass1 ~= pass2 and pass1 ~= "" then
- print("Error: passwords do not match!!")
- print()
- else
- goodPass = true
- end
- end
- password = pass1
- local response = http.post("http://www.circlecraft.info/ccapi/secsys_createowner.php",
- "ign=" .. tostring(ign) .. "&" ..
- "pass=" .. textutils.urlEncode(tostring(pass1)))
- local data = textutils.unserialize(response.readAll())
- if data.errorNum > 0 then
- error("Error saving new owner account")
- end
- return data
- end
- local function setComputerOwner()
- term.clear()
- term.setCursorPos(1,1)
- print("Select the owner of this computer from")
- print("the list of players below:")
- print()
- local players = sensor.getNearbyPlayers(2)
- for index, player in ipairs(players) do
- print(index .. ". " .. player.player)
- end
- print()
- print("Choose a number from the list above:")
- local chosen = false
- while chosen == false do
- local event, p1, p2, p3, p4 = os.pullEvent("key")
- local keyVal = tonumber(p1) - 1
- if keyVal <= #players then
- os.pullEvent("char")
- owner.ign = players[keyVal].player
- local newOwner = getOwner(owner.ign)
- if newOwner.ign == "" then
- newOwner = createOwner(owner.ign)
- else
- print("Enter the password for your account: ")
- print()
- password = read("*")
- end
- print()
- local response = http.post("http://www.circlecraft.info/ccapi/secsys_setowner.php",
- "id=" .. tostring(compID) .. "&" ..
- "owner=" .. tostring(owner.ign) .. "&" ..
- "pass=" .. textutils.urlEncode(tostring(password)) .. "&" ..
- "secret=" .. textutils.urlEncode(tostring(secret)))
- local contents = response.readAll()
- local data = textutils.unserialize(contents)
- if data.errorNum == 1 then
- print("Invalid request, rebooting...")
- sleep(2)
- os.reboot()
- elseif data.errorNum == 2 or data.errorNum == 4 then
- print("Couldn't verify account, rebooting...")
- sleep(2)
- os.reboot()
- elseif data.errorNum == 3 then
- print("Couldn't save owner data, rebooting...")
- sleep(2)
- os.reboot()
- elseif data.errorNum == 4 then
- print("Computer already registered, rebooting...")
- sleep(2)
- os.reboot()
- elseif data.errorNum == 0 then
- owner.uuid = data.uuid
- print("Found player UUID: " .. data.uuid)
- print("Set owner to: " .. data.ign)
- sleep(2)
- else
- error(contents)
- end
- return
- end
- end
- end
- local function showMain()
- term.clear()
- term.setCursorPos(1,1)
- print("Player Proximity Security System")
- print()
- print("Current Owner: " .. owner.ign)
- print("Current Scan Radius: " .. proxRadius)
- print()
- print("Press 'q' to exit")
- -- print("Press 'o' to change owner")
- print()
- print("Visit this url to view this computer's log:")
- print()
- print("https://www.circlecraft.info/secsys/")
- print()
- print("Select ID# " .. compID .. " from the menu in your browser")
- end
- if compLabel == nil then setCompLabel() end
- getComputerOwner()
- getSecret()
- if hasOwner == false then setComputerOwner() end
- proxRadius = getProxRadius()
- if proxRadius == 0 then setProxRadius() end
- showMain()
- local pollTimer = os.startTimer(0)
- while true do
- local event, p1, p2, p3, p4 = os.pullEvent()
- if event == "key" then
- os.pullEvent("char")
- if p1 == keys.q then
- term.setCursorPos(1,11)
- return
- -- elseif p1 == keys.o then
- -- setOwner()
- -- showMain()
- end
- elseif event == "timer" and p1 == pollTimer then
- local players = sensor.getNearbyPlayers(proxRadius)
- for index, player in ipairs(players) do
- if player.player ~= owner.ign then
- proxLog(player.player, player.distance)
- -- print("Player: " .. player.player .. " Distance: " .. round(player.distance, 1))
- end
- end
- pollTimer = os.startTimer(5)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement