Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- facility = {
- facility_id = 0,
- facility_name = "",
- facility_password = "",
- facility_registered = false
- }
- url = "http://localhost:3000/"
- allChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- function facility:new(name)
- setmetatable({}, facility)
- self.facility_id = os.getComputerID()
- self.facility_name = name
- self.facility_password = ""
- self.facility_registered = false
- return self
- end
- function facility:register(name)
- if self.facility_registered == true then
- print("I'm already registered")
- return false
- else
- -- register facility on server
- -- generate a random password
- local new_password = ""
- for i = 1, 16 do
- r = math.random(#allChars)
- new_password = new_password .. string.sub(allChars, r, r)
- end
- self.facility_password = new_password
- -- set facility if not nil or not set
- facility_data = {facility_name = name}
- local facility_exists =
- http.post(
- url .. "facility_exists",
- textutils.serializeJSON(facility_data),
- {
- ["Content-Type"] = "application/json",
- ["content-encoding"] = "identity"
- },
- true
- )
- if facility_exists ~= nil then
- if facility_exists.getResponseCode() == 200 then
- if string.find(facility_exists.readAll(), "false") == nil then
- return false
- end
- else
- print("No 200 response code")
- return false
- end
- else
- print("Server error")
- return false
- end
- local server_data = {
- facility_id = self.facility_id,
- facility_name = self.facility_name,
- facility_password = self.facility_password
- }
- print(self.this_id)
- local register =
- http.post(
- url .. "register_facility",
- textutils.serializeJSON(server_data),
- {
- ["Content-Type"] = "application/json",
- ["content-encoding"] = "identity"
- },
- true
- )
- if register ~= nil then
- if register.getResponseCode() == 200 then
- self.facility_registered = true
- self.facility_id = os.getComputerID()
- -- Save data
- local h = fs.open("api/facility", "w")
- h.writeLine(self.facility_id)
- h.writeLine(self.facility_name)
- h.writeLine(self.facility_password)
- h.writeLine(self.facility_registered)
- h.close()
- return true
- else
- print("E: post 'register_facility' response code was not 200")
- return false
- end
- else
- print("register returned nil")
- return false
- end
- end
- end
- function facility:is_registered()
- if fs.exists("api/facility") then
- local h = fs.open("api/facility", "r")
- some_facility_id = tonumber(h.readLine())
- some_facility_name = h.readLine()
- some_facility_password = h.readLine()
- some_facility_registered = h.readLine() == true
- h.close()
- self.facility_id = some_facility_id
- self.facility_name = some_facility_name
- self.facility_password = some_facility_password
- self.facility_registered = some_facility_registered
- local server_data = {
- this_id = self.facility_id,
- this_name = self.facility_name,
- password = self.facility_password
- }
- local found =
- http.post(
- url .. self.type .. "/find_facility",
- textutils.serializeJSON(server_data),
- {
- ["Content-Type"] = "application/json",
- ["content-encoding"] = "identity"
- },
- true
- )
- if found ~= nil then
- if found.getResponseCode() == 200 then
- if found.readAll() == true then
- return true
- else
- return false
- end
- else
- print("E: post 'find_facility' response code was not 200")
- return false
- end
- else
- print("found returned nil")
- return false
- end
- else
- return false
- end
- end
- function facility:join(name)
- facility_data = {facility_name = name}
- local facility_exists =
- http.post(
- url .. "facility_exists",
- textutils.serializeJSON(facility_data),
- {
- ["Content-Type"] = "application/json",
- ["content-encoding"] = "identity"
- },
- true
- )
- if facility_exists ~= nil then
- if facility_exists.getResponseCode() == 200 then
- if string.find(facility_exists.readAll(), "false") == nil then
- setmetatable({}, facility)
- self.facility_id = os.getComputerID()
- self.facility_name = name
- self.facility_password = ""
- self.facility_registered = true
- return self
- else
- return false
- end
- else
- print("No 200 response code")
- return false
- end
- else
- print("Server error")
- return false
- end
- end
Add Comment
Please, Sign In to add comment