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, expectedType)
- 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
- if expectedType then
- if message.type == expectedType then return message end
- if message.type == "error" then return message end
- else
- if message.type then return message end
- end
- 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}, "account_response")
- 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}, "login_response")
- 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 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 .. " Eddies")
- 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
- local function composeEmail(studentId, password, recipientId, subject, body)
- if not recipientId then
- print("Enter Recipient ID:")
- recipientId = read()
- end
- if not subject then
- print("Enter Subject:")
- subject = read()
- end
- if not body then
- print("Enter Body:")
- body = read()
- end
- local response = sendRequest({
- type = "send_email",
- studentId = studentId,
- password = password,
- recipientId = recipientId,
- subject = subject,
- body = body
- })
- if response and response.success then
- print("Email sent successfully!")
- else
- print("Failed to send email: " .. (response and response.message or "Unknown error"))
- end
- end
- local function deleteEmail(studentId, password, index)
- local response = sendRequest({
- type = "delete_email",
- studentId = studentId,
- password = password,
- emailIndex = index
- })
- if response and response.success then
- print("Email deleted.")
- return true
- else
- print("Failed to delete email: " .. (response and response.message or "Unknown error"))
- return false
- end
- end
- local function readEmail(studentId, password, email, index)
- print("\n--- Email ---")
- print("From: " .. email.sender)
- print("Date: " .. os.date("%Y-%m-%d %H:%M", email.timestamp / 1000)) -- approximate conversion if needed, or just display raw
- print("Subject: " .. email.subject)
- print("----------------")
- print(email.body)
- print("----------------")
- while true do
- print("\n1. Reply")
- print("2. Forward")
- print("3. Delete")
- print("4. Back to Inbox")
- local choice = read()
- if choice == "1" then
- composeEmail(studentId, password, email.sender, "Re: " .. email.subject)
- elseif choice == "2" then
- print("Enter recipient to forward to:")
- local fwdRecipient = read()
- composeEmail(studentId, password, fwdRecipient, "Fwd: " .. email.subject, email.body)
- elseif choice == "3" then
- if deleteEmail(studentId, password, index) then
- return -- Go back to inbox refresh
- end
- elseif choice == "4" then
- return
- end
- end
- end
- local function showInbox(studentId, password)
- while true do
- local response = sendRequest({type = "get_inbox", studentId = studentId, password = password})
- if response and response.type == "inbox_response" then
- local emails = response.emails
- if #emails == 0 then
- print("\nInbox is empty")
- print("1. Compose")
- print("2. Back")
- local choice = read()
- if choice == "1" then composeEmail(studentId, password)
- elseif choice == "2" then break end
- else
- print("\n--- Inbox ---")
- for i, email in ipairs(emails) do
- print(string.format("%d. [%s] %s", i, email.sender, email.subject))
- end
- print("\n1. Read Email")
- print("2. Compose")
- print("3. Back")
- local choice = read()
- if choice == "1" then
- print("Enter email number:")
- local idx = tonumber(read())
- if idx and emails[idx] then
- readEmail(studentId, password, emails[idx], idx)
- else
- print("Invalid email number")
- end
- elseif choice == "2" then
- composeEmail(studentId, password)
- elseif choice == "3" then
- break
- end
- end
- else
- print("Failed to retrieve inbox")
- break
- end
- end
- end
- local function showPhonebook()
- local response = sendRequest({type = "get_phonebook"}, "phonebook_response")
- if not response or not response.students then
- print("Failed to retrieve phonebook")
- return
- end
- local students = response.students
- if #students == 0 then
- print("\nPhonebook is empty")
- print("Press any key to continue...")
- read()
- return
- end
- local _, screenHeight = term.getSize()
- local itemsPerPage = screenHeight - 6 -- Leave room for header and footer
- if itemsPerPage < 5 then itemsPerPage = 5 end
- local currentPage = 1
- local totalPages = math.ceil(#students / itemsPerPage)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("=== Phonebook ===")
- print("Page " .. currentPage .. " of " .. totalPages .. " (" .. #students .. " students)")
- print("")
- local startIdx = (currentPage - 1) * itemsPerPage + 1
- local endIdx = math.min(startIdx + itemsPerPage - 1, #students)
- for i = startIdx, endIdx do
- print(" " .. students[i])
- end
- print("")
- local navOptions = ""
- if currentPage > 1 then navOptions = navOptions .. "[P]rev " end
- if currentPage < totalPages then navOptions = navOptions .. "[N]ext " end
- navOptions = navOptions .. "[B]ack"
- print(navOptions)
- local choice = read():lower()
- if choice == "p" and currentPage > 1 then
- currentPage = currentPage - 1
- elseif choice == "n" and currentPage < totalPages then
- currentPage = currentPage + 1
- elseif choice == "b" then
- break
- end
- end
- end
- local function showEconomy()
- local response = sendRequest({type = "get_economy"}, "economy_response")
- if not response then
- print("Failed to retrieve economy data")
- return
- end
- term.clear()
- term.setCursorPos(1, 1)
- print("=== Economy Stats ===")
- print("")
- print("GDP (Total Eddies): " .. (response.gdp or 0))
- print("")
- print("--- Top 5 Richest ---")
- if response.top5 and #response.top5 > 0 then
- for i, entry in ipairs(response.top5) do
- print(i .. ". " .. entry.name .. " - " .. entry.balance .. " Eddies")
- end
- else
- print(" No data available")
- end
- print("")
- print("Press any key to continue...")
- read()
- end
- -- Main program
- -- GPS Background Task
- local function gpsSender()
- local studentId = loadUsername() -- Initial load
- while true do
- -- Update ID in case of login change
- studentId = loadUsername()
- if studentId then
- local x, y, z = gps.locate(5) -- 5 second timeout
- if x and y and z then
- modem.transmit(1, 1, {
- type = "track_location",
- studentId = studentId,
- x = math.floor(x),
- y = math.floor(y),
- z = math.floor(z)
- })
- end
- end
- sleep(30) -- Send every 30 seconds
- end
- end
- -- Main Application wrapper
- local function mainApp()
- 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. Check Balance")
- print("3. Transfer Money")
- print("4. Email")
- print("5. Phonebook")
- print("6. Economy")
- print("7. Logout")
- local choice = read()
- if choice == "1" then
- showNews()
- elseif choice == "2" then
- checkBalance(studentId, password)
- elseif choice == "3" then
- transferMoney(studentId, password)
- elseif choice == "4" then
- showInbox(studentId, password)
- elseif choice == "5" then
- showPhonebook()
- elseif choice == "6" then
- showEconomy()
- elseif choice == "7" then
- break
- end
- end
- end
- end
- end
- -- Run both in parallel
- parallel.waitForAny(mainApp, gpsSender)
Advertisement
Add Comment
Please, Sign In to add comment