Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to get the disk ID from the bottom drive
- local function getDiskID()
- -- Wrap the disk drive at the bottom of the computer
- local drive = peripheral.wrap("bottom")
- if drive and peripheral.getType("bottom") == "drive" then
- -- Check if there is a disk in the drive
- if drive.isDiskPresent() then
- -- Get the disk ID
- local diskID = drive.getDiskID()
- return diskID
- else
- print("No disk present in the drive.")
- end
- else
- print("No drive found at the bottom.")
- end
- return nil
- end
- -- Function to list all cards in /disk/Cards
- local function listCards()
- local cardDir = "/disk/Cards/"
- local files = fs.list(cardDir)
- if #files > 0 then
- print("Cards in /disk/Cards:")
- for _, file in ipairs(files) do
- print("- " .. file)
- end
- else
- print("No cards found in /disk/Cards.")
- end
- end
- -- Function to link a card
- local function linkCard()
- while true do
- print("\nLink Card:")
- print("1. Insert Card to Link")
- print("2. Enter Card ID")
- print("0. Back to Main Menu")
- local choice = tonumber(read())
- if choice == 1 then
- print("Insert a disk to link...")
- local diskID = getDiskID()
- if diskID then
- local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
- local file = fs.open(cardFileName, "w")
- file.close()
- print("Card linked successfully.")
- end
- elseif choice == 2 then
- print("Enter the card ID:")
- local cardID = read()
- local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
- local file = fs.open(cardFileName, "w")
- file.close()
- print("Card linked successfully.")
- elseif choice == 0 then
- break -- Back to main menu
- else
- print("Invalid choice. Please enter 1, 2, or 0.")
- end
- end
- end
- -- Function to unlink a card
- local function unlinkCard()
- while true do
- print("\nUnlink Card:")
- print("1. Insert Card to Unlink")
- print("2. Enter Card ID")
- print("0. Back to Main Menu")
- local choice = tonumber(read())
- if choice == 1 then
- print("Insert a disk to unlink...")
- local diskID = getDiskID()
- if diskID then
- local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
- if fs.exists(cardFileName) then
- fs.delete(cardFileName)
- print("Card unlinked successfully.")
- else
- print("No matching card found to unlink.")
- end
- end
- elseif choice == 2 then
- print("Enter the card ID:")
- local cardID = read()
- local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
- if fs.exists(cardFileName) then
- fs.delete(cardFileName)
- print("Card unlinked successfully.")
- else
- print("No matching card found to unlink.")
- end
- elseif choice == 0 then
- break -- Back to main menu
- else
- print("Invalid choice. Please enter 1, 2, or 0.")
- end
- end
- end
- -- Main program loop
- while true do
- print("\nCard System")
- print("1. List Cards")
- print("2. Link Card")
- print("3. Unlink Card")
- print("0. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- listCards()
- elseif choice == 2 then
- linkCard()
- elseif choice == 3 then
- unlinkCard()
- elseif choice == 0 then
- print("Exiting...")
- break
- else
- print("Invalid choice. Please enter a number from the menu.")
- end
- end
Add Comment
Please, Sign In to add comment