Larvix

data

Jan 11th, 2024 (edited)
1,148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.56 KB | None | 0 0
  1. -- Data API v0.0
  2. -- A handy list of variables which can be returned as table/string or saved to file
  3. -- .get() returns all data toggled to true or takes a string parameter for a single type of data
  4. -- data types are listed below under data.settings.save, using "settings" as a type returns the current settings
  5. -- .toggle() lets you change which settings are saved to file and which are gathered by empty .info()
  6.  
  7. -- Declare variable for data storage
  8. local data = {["cache"] = {}, ["settings"] = {}}
  9.  
  10. -- Default pathing for data storage
  11. data.settings.path = {
  12. ["root"] = "//data/",
  13. }
  14. data.settings.path.settings = data.settings.path.root .. "settings.txt"
  15. data.settings.path.data = data.settings.path.root .. "system.txt"
  16.  
  17. -- Default settings to determine which data is saved to file
  18. data.settings.save = {
  19. ["basic"] = true,
  20. ["peripheral"] = true,
  21. ["inventory"] = true,
  22. ["fuel"] = true,
  23. ["position"] = true,
  24. ["bearing"] = true,
  25. }
  26.  
  27. -- Default settings to determine external feature usage
  28. data.settings.use = {
  29. ["gps"] = true,
  30. ["settingsFile"] = false,
  31. ["dataFile"] = false,
  32. }
  33.  
  34. -- Function to load data from file if exists
  35. local load = function()
  36.     if fs.exists(data.settings.path.settings) then
  37.         local file = fs.open(data.settings.path.settings,"r")
  38.         data.settings = textutils.unserialise(file.readAll())
  39.         file.close()
  40.     end
  41. end
  42.  
  43. -- Function to save data to file
  44. local save = function()
  45.     if data.settings.use.settingsFile then
  46.         local file = fs.open(data.settings.path.settings,"w")
  47.         file.write(textutils.serialise(data.settings))
  48.         file.close()
  49.     else
  50.         if fs.exists(data.settings.path.settings) then
  51.             fs.delete(data.settings.path.settings)
  52.         end
  53.     end
  54.     if data.settings.use.dataFile then
  55.         local hasData = false
  56.         for k,v in pairs(data.cache) do hasData = true end
  57.         if hasData then
  58.             local file = fs.open(data.settings.path.data,"w")
  59.             file.write(textutils.serialise(data.cache))
  60.             file.close()
  61.         elseif fs.exists(data.settings.path.data) then
  62.             fs.delete(data.settings.path.data)
  63.         end
  64.     else
  65.         if fs.exists(data.settings.path.data) then
  66.             fs.delete(data.settings.path.data)
  67.         end
  68.     end
  69.     if fs.exists(data.settings.path.root) and #fs.list(data.settings.path.root) == 0 then
  70.         fs.delete(data.settings.path.root)
  71.     end
  72. end
  73.  
  74. -- Function to get basic system data
  75. data.basic = function()
  76.     data.cache.basic = {}
  77.  -- Check device ID
  78.     data.cache.basic.id = os.getComputerID()
  79.  -- Check device label if device has one
  80.     if os.getComputerLabel() then data.cache.basic.name = os.getComputerLabel() end
  81.  -- Check if advanced/basic
  82.     if term.isColour() then data.cache.basic.grade = "advanced"
  83.     else data.cache.basic.grade = "basic" end
  84.  -- Check device type
  85.     if peripheral.getType("back") == "neuralInterface" then data.cache.basic.type = "neural"
  86.     elseif turtle then data.cache.basic.type = "turtle"
  87.     elseif pocket then data.cache.basic.type = "pocket"
  88.     else data.cache.basic.type = "computer" end
  89.  
  90.  -- Return data, save if set to
  91.     local output = data.cache.basic
  92.     if data.settings.save.basic then
  93.         save()
  94.     else
  95.         data.cache.basic = nil
  96.     end
  97.     return output
  98. end
  99.  
  100. -- Function to get system peripherals
  101. data.peripheral = function()
  102.     data.cache.peripheral = {}
  103.  -- Check peripheral
  104.     side = peripheral.getNames()
  105.     for i = 1,#side do
  106.         data.cache.peripheral[side[i]] = peripheral.getType(side[i])
  107.     end
  108.  -- Check neural interface modules
  109.     if peripheral.getType("back") == "neuralInterface" then
  110.         if peripheral.wrap("back").listModules() then
  111.             data.cache.peripheral.modules = peripheral.wrap("back").listModules()
  112.         end
  113.     end
  114.  
  115.  -- Return data, save if set to
  116.     local output = data.cache.peripheral
  117.     if data.settings.save.peripheral then
  118.         save()
  119.     else
  120.         data.cache.peripheral = nil
  121.     end
  122.     return output
  123. end
  124.  
  125. -- Function to get turtle inventory or neural interface equipment
  126. data.inventory = function()
  127.     data.cache.inventory = {}
  128.  -- Check turtle inventory
  129.     if turtle then
  130.         for i = 1,16 do
  131.             if turtle.getItemDetail(i) then
  132.                 data.cache.inventory[i] = {turtle.getItemDetail(i).count, turtle.getItemDetail(i).name}
  133.             end
  134.         end
  135.  -- Check neural interface equipment
  136.     elseif peripheral.getType("back") == "neuralInterface" then
  137.             if peripheral.wrap("back").hasModule("plethora:introspection") then
  138.                 for itemSlot,itemData in pairs(peripheral.wrap("back").getEquipment().list()) do
  139.                     data.cache.inventory[itemSlot] = itemData.name
  140.                 end
  141.             end
  142.     else
  143.         data.cache.inventory = nil
  144.     end
  145.  
  146.  -- Return data, save if set to
  147.     local output = data.cache.inventory
  148.     if data.settings.save.inventory then
  149.         save()
  150.     else
  151.         data.cache.inventory = nil
  152.     end
  153.     return output
  154. end
  155.  
  156. -- Function to get turtle fuel count
  157. data.fuel = function()
  158.     if turtle then
  159.         data.cache.fuel = turtle.getFuelLevel()
  160.     else
  161.         data.cache.fuel = nil
  162.     end
  163.  
  164.  -- Return data, save if set to
  165.     local output = data.cache.fuel
  166.     if data.settings.save.fuel then
  167.         save()
  168.     else
  169.         data.cache.fuel = nil
  170.     end
  171.     return output
  172. end
  173.  
  174. -- Function to get system co-ordinates
  175. data.position = function()
  176.     if data.settings.use.gps and gps.locate() then
  177.      -- Check position with GPS
  178.         data.cache.position = {}
  179.         data.cache.position["x"],data.cache.position["y"],data.cache.position["z"] = gps.locate()
  180.         if not data.cache.position["x"] then data.cache.position = "lost" end
  181.     else
  182.         data.cache.position = nil
  183.     end
  184.  
  185.  -- Return data, save if set to
  186.     local output = data.cache.position
  187.     if data.settings.save.position then
  188.         save()
  189.     else
  190.         data.cache.position = nil
  191.     end
  192.     return output
  193. end
  194.  
  195. -- Function to find which way turtle is facing or get neural interface yaw
  196. data.bearing = function()
  197.     if data.settings.use.gps and gps.locate() then
  198.         data.cache.bearing = nil
  199.      -- Check where turtle is facing
  200.         if turtle and turtle.getFuelLevel() > 1 then
  201.             local oldX,oldY,oldZ = gps.locate()
  202.             local turns = 0
  203.             for attempt = 1,4 do
  204.                 if not data.cache.bearing and turtle.forward() then
  205.                     local newX,newY,newZ = gps.locate()
  206.                     if oldX and newX then
  207.                         newX = newX - oldX
  208.                         newZ = newZ - oldZ
  209.                         if newX ~= 0 then
  210.                             if newX > 0 then
  211.                                 data.cache.bearing = "east"
  212.                             else
  213.                                 data.cache.bearing = "west"
  214.                             end
  215.                         elseif newZ ~= 0 then
  216.                             if newZ > 0 then
  217.                                 data.cache.bearing = "south"
  218.                             else
  219.                                 data.cache.bearing = "north"
  220.                             end
  221.                         end
  222.                     end
  223.                     if not turtle.back() then
  224.                         data.position()
  225.                     end
  226.                 end
  227.                 if not data.cache.bearing then
  228.                     turtle.turnLeft()
  229.                     turns = turns + 1
  230.                 else
  231.                     if turns == 3 then
  232.                         turtle.turnLeft()
  233.                         if data.cache.bearing == "north" then
  234.                             data.cache.bearing = "west"
  235.                         elseif data.cache.bearing == "east" then
  236.                             data.cache.bearing = "north"
  237.                         elseif data.cache.bearing == "south" then
  238.                             data.cache.bearing = "east"
  239.                         elseif data.cache.bearing == "west" then
  240.                             data.cache.bearing = "south"
  241.                         end
  242.                     else
  243.                         for t = 1,turns do
  244.                             turtle.turnRight()
  245.                             if data.cache.bearing == "north" then
  246.                                 data.cache.bearing = "east"
  247.                             elseif data.cache.bearing == "east" then
  248.                                 data.cache.bearing = "south"
  249.                             elseif data.cache.bearing == "south" then
  250.                                 data.cache.bearing = "west"
  251.                             elseif data.cache.bearing == "west" then
  252.                                 data.cache.bearing = "north"
  253.                             end
  254.                         end
  255.                     end
  256.                     break
  257.                 end
  258.             end
  259.      -- Check neural interface yaw
  260.         elseif peripheral.getType("back") == "neuralInterface" then
  261.             if peripheral.wrap("back").hasModule("plethora:sensor") then
  262.                 if peripheral.wrap("back").hasModule("plethora:introspection") then
  263.                     data.cache.bearing = peripheral.wrap("back").getMetaOwner().yaw
  264.                 end
  265.             end
  266.         end
  267.     end
  268.  
  269.  -- Return data, save if set to
  270.     local output = data.cache.bearing
  271.     if data.settings.save.bearing then
  272.         save()
  273.     else
  274.         data.cache.position = nil
  275.     end
  276.     return output
  277. end
  278.  
  279. -- Function to get all system data in one
  280. local getData = function(type)
  281.     if not type then
  282.         if data.settings.save.basic then
  283.             data.basic()
  284.         end
  285.         if data.settings.save.peripheral then
  286.             data.peripheral()
  287.         end
  288.         if data.settings.save.inventory then
  289.             data.inventory()
  290.         end
  291.         if data.settings.save.fuel then
  292.             data.fuel()
  293.         end
  294.         if data.settings.save.position then
  295.             data.position()
  296.         end
  297.         if data.settings.save.bearing then
  298.             data.bearing()
  299.         end
  300.         return data.cache
  301.     elseif type == "settings" then
  302.         return data.settings
  303.     elseif type == "basic" or type == "basics" then
  304.         return data.basic()
  305.     elseif type == "peripheral" or type == "peripherals" or type == "periph" then
  306.         return data.peripheral()
  307.     elseif type == "inventory" or type == "inv" then
  308.         return data.inventory()
  309.     elseif type == "fuel" then
  310.         return data.fuel()
  311.     elseif type == "position" or type == "pos" then
  312.         return data.position()
  313.     elseif type == "bearing" or type == "facing" then
  314.         return data.bearing()
  315.     else
  316.         return "Usage: info([type])"
  317.     end
  318. end
  319.  
  320. -- Function to switch settings between true and false
  321. local toggleSettings = function(setting,state)
  322.     local output = nil
  323.     if state ~= true and state ~= false then
  324.         local state = nil
  325.     end
  326.     if setting == "all" then
  327.         if state ~= nil then
  328.             for k,v in pairs(data.settings.save) do
  329.                 data.settings.save[k] = state
  330.             end
  331.             output = "All "..tostring(state)
  332.         else
  333.             output = "New state required"
  334.         end
  335.     elseif data.settings.use[setting] ~= nil then
  336.         if state ~= nil then
  337.             data.settings.use[setting] = state
  338.         else
  339.             data.settings.use[setting] = not data.settings.use[setting]
  340.         end
  341.         output = data.settings.use[setting]
  342.     elseif data.settings.save[setting] ~= nil then
  343.         if state ~= nil then
  344.             data.settings.save[setting] = state
  345.         else
  346.             data.settings.save[setting] = not data.settings.save[setting]
  347.         end
  348.         output = data.settings.save[setting]
  349.     else
  350.         output = "Usage: toggle(<setting/all>[, true/false])"
  351.     end
  352.     getData()
  353.     return output
  354. end
  355.  
  356. -- Load system if exists or create if not
  357. if not fs.exists(data.settings.path.settings) then
  358.     getData()
  359. else
  360.     load()
  361.     getData()
  362. end
  363.  
  364. -- Allow function access
  365. return {
  366. get = getData,
  367. toggle = toggleSettings,
  368. }
Advertisement
Add Comment
Please, Sign In to add comment