Advertisement
Guest User

patentWriter.lua

a guest
Oct 28th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. local patentFile = "disk/patents.pat"
  2. local adminPassword = "PVSfVF"    
  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. patTable = load(patentFile)
  25. numPats = #patTable
  26. cpn = 1
  27. patentWriting = false
  28.  
  29. local function checkCurNumber()
  30.     if cpn > numPats then cpn = numPats end
  31.     if cpn < 1 then cpn = 1 end
  32. end
  33.  
  34. local function writePatent()
  35.     patentWriting = true
  36.     newPatNum = numPats + 1
  37.     term.clear()
  38.     term.setCursorPos(1,1)
  39.     term.write("Owner: ")
  40.     patentOwner = read()
  41.     term.setCursorPos(1,2)
  42.     term.write("Name: ")
  43.     patentName = read()
  44.     term.setCursorPos(1,3)
  45.     term.write("Description: ")
  46.     patentDesc = read()
  47.    
  48.     patTable[newPatNum] = {owner = patentOwner,name = patentName,desc = patentDesc}
  49.     patentWriting = false
  50.     save(patTable,patentFile)
  51.     numPats = #patTable
  52.     term.clear()
  53. end
  54.  
  55. local function deletePatent(tbl,num)
  56.     patentWriting = true
  57.     term.clear()
  58.     term.setCursorPos(1,1)
  59.     term.write("Are you sure you want to delete this patent? (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("Patent 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 VIEW ONLY.")
  73.         end
  74.     sleep(1)
  75.     numPats = #patTable
  76.     save(patTable,patentFile)
  77.     patentWriting = 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 writePatent() end
  88.         if key == keys.semiColon then deletePatent(patTable,cpn) end
  89.     end
  90. end
  91.  
  92. local function main()
  93.     while true do
  94.         if patentWriting == false then
  95.             term.setCursorPos(1,1)
  96.             term.clearLine()
  97.             term.setTextColour(colours.white)
  98.             centerText(tostring(cpn) .. "/" .. tostring(numPats),1)
  99.             checkCurNumber()
  100.             if patTable[cpn] ~= nil then
  101.                 term.setTextColour(colours.red)
  102.                 centerText("Owner: "..patTable[cpn]["owner"],2)
  103.                 term.setTextColour(colours.yellow)
  104.                 centerText("Name: "..patTable[cpn]["name"],3)
  105.                 term.setTextColour(colours.white)
  106.                 term.setCursorPos(1,4)
  107.                 print(patTable[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)
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement