le_Fish

Extra Facility Funcs - v2

Aug 15th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.58 KB | None | 0 0
  1. facility = {
  2.     facility_id = 0,
  3.     facility_name = "",
  4.     facility_password = "",
  5.     facility_registered = false
  6. }
  7.  
  8. url = "http://localhost:3000/"
  9. allChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  10.  
  11. function facility:new(name)
  12.     setmetatable({}, facility)
  13.     self.facility_id = os.getComputerID()
  14.     self.facility_name = name
  15.     self.facility_password = ""
  16.     self.facility_registered = false
  17.     return self
  18. end
  19.  
  20. function facility:register(name)
  21.     if self.facility_registered == true then
  22.         print("I'm already registered")
  23.         return false
  24.     else
  25.         -- register facility on server
  26.         -- generate a random password
  27.         local new_password = ""
  28.         for i = 1, 16 do
  29.             r = math.random(#allChars)
  30.             new_password = new_password .. string.sub(allChars, r, r)
  31.         end
  32.  
  33.         self.facility_password = new_password
  34.  
  35.         -- set facility if not nil or not set
  36.         facility_data = {facility_name = name}
  37.         local facility_exists =
  38.             http.post(
  39.             url .. "facility_exists",
  40.             textutils.serializeJSON(facility_data),
  41.             {
  42.                 ["Content-Type"] = "application/json",
  43.                 ["content-encoding"] = "identity"
  44.             },
  45.             true
  46.         )
  47.         if facility_exists ~= nil then
  48.             if facility_exists.getResponseCode() == 200 then
  49.                 if string.find(facility_exists.readAll(), "false") == nil then
  50.                     return false
  51.                 end
  52.             else
  53.                 print("No 200 response code")
  54.                 return false
  55.             end
  56.         else
  57.             print("Server error")
  58.             return false
  59.         end
  60.  
  61.         local server_data = {
  62.             facility_id = self.facility_id,
  63.             facility_name = self.facility_name,
  64.             facility_password = self.facility_password
  65.         }
  66.         print(self.this_id)
  67.  
  68.         local register =
  69.             http.post(
  70.             url .. "register_facility",
  71.             textutils.serializeJSON(server_data),
  72.             {
  73.                 ["Content-Type"] = "application/json",
  74.                 ["content-encoding"] = "identity"
  75.             },
  76.             true
  77.         )
  78.  
  79.         if register ~= nil then
  80.             if register.getResponseCode() == 200 then
  81.                 self.facility_registered = true
  82.                 self.facility_id = os.getComputerID()
  83.                 -- Save data
  84.                 local h = fs.open("api/facility", "w")
  85.                 h.writeLine(self.facility_id)
  86.                 h.writeLine(self.facility_name)
  87.                 h.writeLine(self.facility_password)
  88.                 h.writeLine(self.facility_registered)
  89.                 h.close()
  90.                 return true
  91.             else
  92.                 print("E: post 'register_facility' response code was not 200")
  93.                 return false
  94.             end
  95.         else
  96.             print("register returned nil")
  97.             return false
  98.         end
  99.     end
  100. end
  101.  
  102. function facility:is_registered()
  103.     if fs.exists("api/facility") then
  104.         local h = fs.open("api/facility", "r")
  105.         some_facility_id = tonumber(h.readLine())
  106.         some_facility_name = h.readLine()
  107.         some_facility_password = h.readLine()
  108.         some_facility_registered = h.readLine() == true
  109.         h.close()
  110.  
  111.         self.facility_id = some_facility_id
  112.         self.facility_name = some_facility_name
  113.         self.facility_password = some_facility_password
  114.         self.facility_registered = some_facility_registered
  115.  
  116.         local server_data = {
  117.             this_id = self.facility_id,
  118.             this_name = self.facility_name,
  119.             password = self.facility_password
  120.         }
  121.  
  122.         local found =
  123.             http.post(
  124.             url .. self.type .. "/find_facility",
  125.             textutils.serializeJSON(server_data),
  126.             {
  127.                 ["Content-Type"] = "application/json",
  128.                 ["content-encoding"] = "identity"
  129.             },
  130.             true
  131.         )
  132.  
  133.         if found ~= nil then
  134.             if found.getResponseCode() == 200 then
  135.                 if found.readAll() == true then
  136.                     return true
  137.                 else
  138.                     return false
  139.                 end
  140.             else
  141.                 print("E: post 'find_facility' response code was not 200")
  142.                 return false
  143.             end
  144.         else
  145.             print("found returned nil")
  146.             return false
  147.         end
  148.     else
  149.         return false
  150.     end
  151. end
  152.  
  153. function facility:join(name)
  154.     facility_data = {facility_name = name}
  155.  
  156.     local facility_exists =
  157.         http.post(
  158.         url .. "facility_exists",
  159.         textutils.serializeJSON(facility_data),
  160.         {
  161.             ["Content-Type"] = "application/json",
  162.             ["content-encoding"] = "identity"
  163.         },
  164.         true
  165.     )
  166.     if facility_exists ~= nil then
  167.         if facility_exists.getResponseCode() == 200 then
  168.             if string.find(facility_exists.readAll(), "false") == nil then
  169.                 setmetatable({}, facility)
  170.                 self.facility_id = os.getComputerID()
  171.                 self.facility_name = name
  172.                 self.facility_password = ""
  173.                 self.facility_registered = true
  174.                 return self
  175.             else
  176.                 return false
  177.             end
  178.         else
  179.             print("No 200 response code")
  180.             return false
  181.         end
  182.     else
  183.         print("Server error")
  184.         return false
  185.     end
  186. end
Add Comment
Please, Sign In to add comment