SkyCrafter0

databaseWriter.lua

Oct 28th, 2020 (edited)
893
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local databaseFile = "disk/patents.skydb"
  2. local adminPassword = ""    
  3.  
  4. local function save(tbl,name)
  5.     local file = fs.open(name,"w")
  6.     file.write(textutils.serialise(tbl))
  7.     file.close()
  8. end
  9.  
  10. local function load(name)
  11.     local file = fs.open(name,"r")
  12.     local data = file.readAll()
  13.     file.close()
  14.     return textutils.unserialise(data)
  15. end
  16.  
  17. local function centerText(text,y)
  18.     local x,_ = term.getSize()
  19.     local x = math.floor(x/2) - math.floor(string.len(text)/2)
  20.     term.setCursorPos(x,y)
  21.     term.write(text)
  22. end
  23.  
  24. database = load(databaseFile)
  25. numData = #database
  26. cpn = 1
  27. dataWriting = false
  28.  
  29. local function checkCurNumber()
  30.     if cpn > numData then cpn = numData end
  31.     if cpn < 1 then cpn = 1 end
  32. end
  33.  
  34. local function writeData()
  35.     patentWriting = true
  36.     newDataNum = numData + 1
  37.     term.clear()
  38.     term.setCursorPos(1,1)
  39.     term.write("Owner: ")
  40.     dOwner = read()
  41.     term.setCursorPos(1,2)
  42.     term.write("Name: ")
  43.     dName = read()
  44.     term.setCursorPos(1,3)
  45.     term.write("Description: ")
  46.     dDesc = read()
  47.    
  48.     database[newDataNum] = {owner = dOwner,name = dName,desc = dDesc}
  49.     patentWriting = false
  50.     save(database,patentFile)
  51.     numData = #database
  52.     term.clear()
  53. end
  54.  
  55. local function deleteData(tbl,num)
  56.     dataWriting = true
  57.     term.clear()
  58.     term.setCursorPos(1,1)
  59.     term.write("Are you sure you want to delete this data? (y/n)")
  60.     term.setCursorPos(1,2)
  61.     acceptance = read()
  62.     if acceptance == "y" then
  63.         term.setCursorPos(1,3)
  64.         term.write("Please enter the admin password: ")
  65.         local password = read("*")
  66.         if adminPassword == password then
  67.             table.remove(tbl,num)
  68.             term.setCursorPos(1,5)
  69.             print("Data deleted. A copy is stored on the working disk, and a read only copy is stored on the backup disk.")
  70.         else
  71.             term.setCursorPos(1,5)
  72.             print("Password incorrect, returning to data view")
  73.         end
  74.     sleep(1)
  75.     numData = #database
  76.     save(database,databaseFile)
  77.     dataWriting = false    
  78.     term.clear()
  79.     end
  80. end
  81.  
  82. local function handleKeys()
  83.     while true do
  84.         _,key = os.pullEvent("key")
  85.         if key == keys.q then cpn = cpn - 1 term.clear() end
  86.         if key == keys.e then cpn = cpn + 1 term.clear() end
  87.         if key == keys.p then writeData() end
  88.         if key == keys.semiColon then deleteData(database,cpn) end
  89.     end
  90. end
  91.  
  92. local function main()
  93.     while true do
  94.         if not patentWriting then
  95.             term.setCursorPos(1,1)
  96.             term.clearLine()
  97.             term.setTextColour(colours.white)
  98.             centerText(tostring(cpn) .. "/" .. tostring(numData),1)
  99.             checkCurNumber()
  100.             if database[cpn] ~= nil then
  101.                 term.setTextColour(colours.red)
  102.                 centerText("Owner: "..database[cpn]["owner"],2)
  103.                 term.setTextColour(colours.yellow)
  104.                 centerText("Name: "..database[cpn]["name"],3)
  105.                 term.setTextColour(colours.white)
  106.                 term.setCursorPos(1,4)
  107.                 print(database[cpn]["desc"])
  108.             else
  109.                 term.clear()
  110.             end
  111.         end
  112.         sleep(0.1)
  113.     end
  114. end
  115. term.clear()
  116. parallel.waitForAny(handleKeys,main)
RAW Paste Data