Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local patentFile = "disk/patents.pat"
- local adminPassword = "PVSfVF"
- local function save(tbl,name)
- local file = fs.open(name,"w")
- file.write(textutils.serialise(tbl))
- file.close()
- end
- local function load(name)
- local file = fs.open(name,"r")
- local data = file.readAll()
- file.close()
- return textutils.unserialise(data)
- end
- local function centerText(text,y)
- local x,_ = term.getSize()
- local x = math.floor(x/2) - math.floor(string.len(text)/2)
- term.setCursorPos(x,y)
- term.write(text)
- end
- patTable = load(patentFile)
- numPats = #patTable
- cpn = 1
- patentWriting = false
- local function checkCurNumber()
- if cpn > numPats then cpn = numPats end
- if cpn < 1 then cpn = 1 end
- end
- local function writePatent()
- patentWriting = true
- newPatNum = numPats + 1
- term.clear()
- term.setCursorPos(1,1)
- term.write("Owner: ")
- patentOwner = read()
- term.setCursorPos(1,2)
- term.write("Name: ")
- patentName = read()
- term.setCursorPos(1,3)
- term.write("Description: ")
- patentDesc = read()
- patTable[newPatNum] = {owner = patentOwner,name = patentName,desc = patentDesc}
- patentWriting = false
- save(patTable,patentFile)
- numPats = #patTable
- term.clear()
- end
- local function deletePatent(tbl,num)
- patentWriting = true
- term.clear()
- term.setCursorPos(1,1)
- term.write("Are you sure you want to delete this patent? (y/n)")
- term.setCursorPos(1,2)
- acceptance = read()
- if acceptance == "y" then
- term.setCursorPos(1,3)
- term.write("Please enter the admin password: ")
- local password = read("*")
- if adminPassword == password then
- table.remove(tbl,num)
- term.setCursorPos(1,5)
- print("Patent deleted. A copy is stored on the working disk, and a read only copy is stored on the backup disk.")
- else
- term.setCursorPos(1,5)
- print("PASSWORD INCORRECT, RETURNING TO VIEW ONLY.")
- end
- sleep(1)
- numPats = #patTable
- save(patTable,patentFile)
- patentWriting = false
- term.clear()
- end
- end
- local function handleKeys()
- while true do
- _,key = os.pullEvent("key")
- if key == keys.q then cpn = cpn - 1 term.clear() end
- if key == keys.e then cpn = cpn + 1 term.clear() end
- if key == keys.p then writePatent() end
- if key == keys.semiColon then deletePatent(patTable,cpn) end
- end
- end
- local function main()
- while true do
- if patentWriting == false then
- term.setCursorPos(1,1)
- term.clearLine()
- term.setTextColour(colours.white)
- centerText(tostring(cpn) .. "/" .. tostring(numPats),1)
- checkCurNumber()
- if patTable[cpn] ~= nil then
- term.setTextColour(colours.red)
- centerText("Owner: "..patTable[cpn]["owner"],2)
- term.setTextColour(colours.yellow)
- centerText("Name: "..patTable[cpn]["name"],3)
- term.setTextColour(colours.white)
- term.setCursorPos(1,4)
- print(patTable[cpn]["desc"])
- else
- term.clear()
- end
- end
- sleep(0.1)
- end
- end
- term.clear()
- parallel.waitForAny(handleKeys,main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement