Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find("modem")
- if not modem then error("No modem found. Please attach a wireless modem.") end
- modem.open(1) -- Use channel 1 for all communications
- local function encrypt(text)
- local result = ""
- for i = 1, #text do
- result = result .. string.char((string.byte(text, i) + 5) % 256)
- end
- return result
- end
- local function decrypt(text)
- local result = ""
- for i = 1, #text do
- result = result .. string.char((string.byte(text, i) - 5) % 256)
- end
- return result
- end
- local function saveUsername(username)
- local file = fs.open(".userdata", "w")
- file.write(encrypt(username))
- file.close()
- end
- local function loadUsername()
- if fs.exists(".userdata") then
- local file = fs.open(".userdata", "r")
- local encryptedUsername = file.readAll()
- file.close()
- return decrypt(encryptedUsername)
- end
- return nil
- end
- local function sendRequest(request)
- modem.transmit(1, 1, request)
- local timer = os.startTimer(5)
- while true do
- local event, param1, _, _, message = os.pullEvent()
- if event == "modem_message" and type(message) == "table" then
- return message
- elseif event == "timer" and param1 == timer then
- return nil
- end
- end
- end
- local function createAccount()
- print("Enter new student ID:")
- local newId = read()
- print("Enter password:")
- local password = read("*")
- local response = sendRequest({type = "create_account", studentId = newId, password = password})
- if response and response.success then
- print(response.message)
- saveUsername(newId)
- return newId
- else
- print(response and response.message or "Failed to create account")
- return nil
- end
- end
- local function login(savedUsername)
- local username = savedUsername
- if not username then
- print("Enter your student ID:")
- username = read()
- else
- print("Logged in as: " .. username)
- end
- print("Enter your password:")
- local password = read("*")
- local response = sendRequest({type = "login", studentId = username, password = password})
- if response and response.success then
- print(response.message)
- saveUsername(username)
- return username, password
- else
- print(response and response.message or "Login failed")
- return nil, nil
- end
- end
- local function showNews()
- local response = sendRequest({type = "get_news"})
- if response and response.type == "news" then
- print("\nNews:")
- print(response.content)
- else
- print("Failed to retrieve news")
- end
- end
- local function showDoors(studentId, password)
- local response = sendRequest({type = "get_doors", studentId = studentId, password = password})
- if response and response.type == "doors_list" then
- print("\nDoors you have access to:")
- if response.doors ~= "" then
- local doors = {}
- for door in response.doors:gmatch("[^\r\n]+") do
- table.insert(doors, door)
- end
- for i, door in ipairs(doors) do
- print(i .. ". " .. door)
- end
- else
- print("No doors available")
- end
- else
- print("Failed to retrieve door list")
- end
- end
- local function openDoor(studentId, password)
- local response = sendRequest({type = "get_doors", studentId = studentId, password = password})
- if response and response.type == "doors_list" then
- if response.doors ~= "" then
- print("\nDoors you have access to:")
- local doors = {}
- for door in response.doors:gmatch("[^\r\n]+") do
- table.insert(doors, door)
- end
- for i, door in ipairs(doors) do
- print(i .. ". " .. door)
- end
- print("Enter the number of the door to open:")
- local choice = tonumber(read())
- if choice and doors[choice] then
- local doorId = doors[choice]
- local openResponse = sendRequest({type = "door_access", studentId = studentId, password = password, doorId = doorId, action = "open"})
- if openResponse and openResponse.type == "access_response" then
- if openResponse.granted then
- print("Door " .. doorId .. " command sent successfully.")
- else
- print("Failed to open door " .. doorId .. ": " .. (openResponse.message or "Unknown error"))
- end
- else
- print("Failed to receive a valid response for door " .. doorId)
- end
- else
- print("Invalid choice")
- end
- else
- print("No doors available")
- end
- else
- print("Failed to retrieve door list")
- end
- end
- local function checkBalance(studentId, password)
- local response = sendRequest({type = "get_balance", studentId = studentId, password = password})
- if response and response.type == "balance_response" then
- print("\nYour balance: " .. response.balance .. " diamonds")
- else
- print("Failed to retrieve balance")
- end
- end
- local function transferMoney(studentId, password)
- print("Enter recipient's student ID:")
- local recipientId = read()
- -- Verify recipient exists
- local response = sendRequest({type = "verify_student", recipientId = recipientId})
- if not response or not response.exists then
- print("Recipient does not exist.")
- return
- end
- print("Enter amount to transfer:")
- local amount = tonumber(read())
- if not amount or amount <= 0 then
- print("Invalid amount.")
- return
- end
- -- Verify sufficient funds
- local balanceResponse = sendRequest({type = "get_balance", studentId = studentId, password = password})
- if not balanceResponse or not balanceResponse.balance then
- print("Failed to retrieve balance.")
- return
- end
- local balance = tonumber(balanceResponse.balance)
- if balance < amount then
- print("Insufficient funds.")
- return
- end
- -- Send transfer request
- local transferResponse = sendRequest({
- type = "transfer_money",
- studentId = studentId,
- password = password,
- recipientId = recipientId,
- amount = amount
- })
- if transferResponse and transferResponse.success then
- print("Transfer successful. New balance: " .. transferResponse.newBalance)
- else
- print("Transfer failed: " .. (transferResponse and transferResponse.message or "Unknown error"))
- end
- end
- -- Main program
- while true do
- local studentId = loadUsername()
- local password
- if not studentId then
- print("\n1. Create Account")
- print("2. Login")
- print("3. Exit")
- local choice = read()
- if choice == "1" then
- studentId = createAccount()
- elseif choice == "2" then
- studentId, password = login()
- elseif choice == "3" then
- break
- end
- else
- studentId, password = login(studentId)
- end
- if studentId and password then
- while true do
- print("\n1. Show News")
- print("2. Show Accessible Doors")
- print("3. Open a Door")
- print("4. Check Balance")
- print("5. Transfer Money")
- print("6. Logout")
- local choice = read()
- if choice == "1" then
- showNews()
- elseif choice == "2" then
- showDoors(studentId, password)
- elseif choice == "3" then
- openDoor(studentId, password)
- elseif choice == "4" then
- checkBalance(studentId, password)
- elseif choice == "5" then
- transferMoney(studentId, password)
- elseif choice == "6" then
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment