Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server startup.lua
- local modem = peripheral.find("modem")
- if not modem then error("No modem found. Please attach a wireless modem.") end
- local disk = peripheral.find("drive")
- if not disk then error("No disk drive found. Please attach a disk drive.") end
- modem.open(1) -- Use channel 1 for all communications
- local diskMount = disk.getMountPath()
- if not diskMount then error("No disk inserted. Please insert a disk.") end
- local function setup()
- if not fs.exists(diskMount .. "/students") then fs.makeDir(diskMount .. "/students") end
- if not fs.exists(diskMount .. "/news.txt") then
- local file = fs.open(diskMount .. "/news.txt", "w")
- file.write("Welcome to the college!")
- file.close()
- end
- if not fs.exists(diskMount .. "/student.lua") then
- error("student.lua not found on the disk. Please create this file.")
- end
- print("Server setup complete. Files stored on disk.")
- end
- local function createAccount(studentId, password)
- local studentDir = diskMount .. "/students/" .. studentId
- if fs.exists(studentDir) then
- return false, "Account already exists"
- end
- fs.makeDir(studentDir)
- local passFile = fs.open(studentDir .. "/password.txt", "w")
- passFile.write(password)
- passFile.close()
- local doorsFile = fs.open(studentDir .. "/doors.txt", "w")
- doorsFile.write("") -- Initially, no door access
- doorsFile.close()
- local balanceFile = fs.open(studentDir .. "/balance.txt", "w")
- balanceFile.write("0") -- Initial balance of 0
- balanceFile.close()
- return true, "Account created successfully"
- end
- local function verifyLogin(studentId, password)
- local studentDir = diskMount .. "/students/" .. studentId
- if not fs.exists(studentDir) then return false, "Account not found" end
- local passFile = fs.open(studentDir .. "/password.txt", "r")
- local storedPass = passFile.readAll():gsub("%s+$", "") -- Trim trailing whitespace
- passFile.close()
- return storedPass == password, storedPass == password and "Login successful" or "Incorrect password"
- end
- local function getDoorAccess(studentId)
- local doorsFile = fs.open(diskMount .. "/students/" .. studentId .. "/doors.txt", "r")
- local doors = doorsFile.readAll()
- doorsFile.close()
- return doors
- end
- local function handleDoorAccess(studentId, doorId, action)
- local doors = getDoorAccess(studentId)
- local hasAccess = string.find(doors, doorId) ~= nil
- if hasAccess then
- modem.transmit(1, 1, {
- type = "door_command",
- doorId = doorId,
- action = action
- })
- print("Sending command to " .. action .. " door " .. doorId .. " for student " .. studentId)
- return true, "Command sent to door"
- else
- print("Access denied for student " .. studentId .. " to door " .. doorId)
- return false, "Access denied"
- end
- end
- local function handleBalance(studentId, action, amount)
- local balanceFile = fs.open(diskMount .. "/students/" .. studentId .. "/balance.txt", "r")
- local balance = tonumber(balanceFile.readAll()) or 0
- balanceFile.close()
- print("Processing balance update for " .. studentId)
- print("Current balance: " .. balance)
- print("Action: " .. action .. ", Amount: " .. amount)
- if action == "deposit" then
- balance = balance + amount
- elseif action == "withdraw" then
- if balance >= amount then
- balance = balance - amount
- else
- print("Insufficient funds. Transaction failed.")
- return false, "Insufficient funds"
- end
- end
- balanceFile = fs.open(diskMount .. "/students/" .. studentId .. "/balance.txt", "w")
- balanceFile.write(tostring(balance))
- balanceFile.close()
- print("New balance: " .. balance)
- print("Transaction successful")
- return true, balance
- end
- local function handleRequests()
- print("Server is running. Waiting for requests...")
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if type(message) == "table" then
- if message.type == "request_update" then
- local file = fs.open(diskMount .. "/student.lua", "r")
- local content = file.readAll()
- file.close()
- modem.transmit(replyChannel, 1, {type = "update", content = content})
- print("Sent student.lua update to a device")
- elseif message.type == "create_account" then
- if message.studentId and message.password then
- local success, msg = createAccount(message.studentId, message.password)
- modem.transmit(replyChannel, 1, {type = "account_response", success = success, message = msg})
- else
- modem.transmit(replyChannel, 1, {type = "error", message = "Invalid account creation request"})
- end
- elseif message.type == "login" then
- if message.studentId and message.password then
- local success, msg = verifyLogin(message.studentId, message.password)
- modem.transmit(replyChannel, 1, {type = "login_response", success = success, message = msg})
- else
- modem.transmit(replyChannel, 1, {type = "error", message = "Invalid login request"})
- end
- elseif message.type == "get_doors" then
- if message.studentId and message.password then
- local success, msg = verifyLogin(message.studentId, message.password)
- if success then
- local doors = getDoorAccess(message.studentId)
- modem.transmit(replyChannel, 1, {type = "doors_list", doors = doors})
- else
- modem.transmit(replyChannel, 1, {type = "error", message = msg})
- end
- else
- modem.transmit(replyChannel, 1, {type = "error", message = "Invalid door access request"})
- end
- elseif message.type == "get_news" then
- local newsFile = fs.open(diskMount .. "/news.txt", "r")
- local news = newsFile.readAll()
- newsFile.close()
- modem.transmit(replyChannel, 1, {type = "news", content = news})
- elseif message.type == "door_access" then
- if message.studentId and message.password and message.doorId and message.action then
- local success, msg = verifyLogin(message.studentId, message.password)
- if success then
- local granted, result = handleDoorAccess(message.studentId, message.doorId, message.action)
- modem.transmit(replyChannel, 1, {type = "access_response", granted = granted, message = result})
- else
- modem.transmit(replyChannel, 1, {type = "error", message = msg})
- end
- else
- modem.transmit(replyChannel, 1, {type = "error", message = "Invalid door access request"})
- end
- elseif message.type == "get_balance" then
- if message.studentId and message.password then
- local success, msg = verifyLogin(message.studentId, message.password)
- if success then
- local balanceFile = fs.open(diskMount .. "/students/" .. message.studentId .. "/balance.txt", "r")
- local balance = balanceFile.readAll()
- balanceFile.close()
- modem.transmit(replyChannel, 1, {type = "balance_response", balance = balance})
- else
- modem.transmit(replyChannel, 1, {type = "error", message = msg})
- end
- else
- modem.transmit(replyChannel, 1, {type = "error", message = "Invalid balance request"})
- end
- elseif message.type == "update_balance" then
- if message.studentId and message.password and message.action and message.amount then
- local success, msg = verifyLogin(message.studentId, message.password)
- if success then
- local success, result = handleBalance(message.studentId, message.action, message.amount)
- modem.transmit(replyChannel, 1, {type = "balance_update_response", success = success, result = result})
- else
- modem.transmit(replyChannel, 1, {type = "error", message = msg})
- end
- else
- modem.transmit(replyChannel, 1, {type = "error", message = "Invalid balance update request"})
- end
- elseif message.type == "verify_student" then
- if message.recipientId then
- local recipientDir = diskMount .. "/students/" .. message.recipientId
- local exists = fs.exists(recipientDir)
- modem.transmit(replyChannel, 1, {exists = exists})
- else
- modem.transmit(replyChannel, 1, {type = "error", message = "Invalid student verification request"})
- end
- elseif message.type == "transfer_money" then
- if message.studentId and message.password and message.recipientId and message.amount then
- local success, msg = verifyLogin(message.studentId, message.password)
- if success then
- local senderBalanceFile = fs.open(diskMount .. "/students/" .. message.studentId .. "/balance.txt", "r")
- local senderBalance = tonumber(senderBalanceFile.readAll()) or 0
- senderBalanceFile.close()
- local recipientBalanceFile = fs.open(diskMount .. "/students/" .. message.recipientId .. "/balance.txt", "r")
- local recipientBalance = tonumber(recipientBalanceFile.readAll()) or 0
- recipientBalanceFile.close()
- local expectedSenderBalance = senderBalance - message.amount
- local expectedRecipientBalance = recipientBalance + message.amount
- if expectedSenderBalance >= 0 then
- local withdrawSuccess, _ = handleBalance(message.studentId, "withdraw", message.amount)
- if withdrawSuccess then
- local depositSuccess, _ = handleBalance(message.recipientId, "deposit", message.amount)
- if depositSuccess then
- -- Verify balances
- senderBalanceFile = fs.open(diskMount .. "/students/" .. message.studentId .. "/balance.txt", "r")
- local newSenderBalance = tonumber(senderBalanceFile.readAll()) or 0
- senderBalanceFile.close()
- recipientBalanceFile = fs.open(diskMount .. "/students/" .. message.recipientId .. "/balance.txt", "r")
- local newRecipientBalance = tonumber(recipientBalanceFile.readAll()) or 0
- recipientBalanceFile.close()
- if newSenderBalance == expectedSenderBalance and newRecipientBalance == expectedRecipientBalance then
- modem.transmit(replyChannel, 1, {success = true, newBalance = newSenderBalance})
- else
- -- Revert transaction
- handleBalance(message.studentId, "deposit", message.amount)
- handleBalance(message.recipientId, "withdraw", message.amount)
- modem.transmit(replyChannel, 1, {success = false, message = "Transaction verification failed"})
- end
- else
- handleBalance(message.studentId, "deposit", message.amount)
- modem.transmit(replyChannel, 1, {success = false, message = "Failed to deposit to recipient"})
- end
- else
- modem.transmit(replyChannel, 1, {success = false, message = "Failed to withdraw from sender"})
- end
- else
- modem.transmit(replyChannel, 1, {success = false, message = "Insufficient funds"})
- end
- else
- modem.transmit(replyChannel, 1, {success = false, message = msg})
- end
- else
- modem.transmit(replyChannel, 1, {success = false, message = "Invalid transfer request"})
- end
- end
- end
- end
- end
- setup()
- handleRequests()
Advertisement
Add Comment
Please, Sign In to add comment