April_The_Sergal

stateManager

Nov 25th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.95 KB | Gaming | 0 0
  1. local stateFile = "stayOnlineStates.json"
  2.  
  3. local function loadNodeStates(nodes)
  4.     if not fs.exists(stateFile) then return end
  5.     local file = fs.open(stateFile, "r")
  6.     local data = textutils.unserializeJSON(file.readAll())
  7.     file.close()
  8.  
  9.     if type(data) == "table" then
  10.         for hostName, state in pairs(data) do
  11.             if nodes[hostName] then
  12.                 nodes[hostName].stayOnline = state.stayOnline
  13.                 nodes[hostName].status = state.status
  14.             end
  15.         end
  16.     end
  17. end
  18.  
  19. local function saveNodeStates(nodes)
  20.     local data = {}
  21.     for hostName, node in pairs(nodes) do
  22.         data[hostName] = {
  23.             stayOnline = node.stayOnline,
  24.             status = node.status,
  25.         }
  26.     end
  27.  
  28.     local file = fs.open(stateFile, "w")
  29.     file.write(textutils.serializeJSON(data))
  30.     file.close()
  31. end
  32.  
  33. return {
  34.     loadNodeStates = loadNodeStates,
  35.     saveNodeStates = saveNodeStates,
  36. }
  37.  
Add Comment
Please, Sign In to add comment