MtnMCG

student.lua

Sep 2nd, 2024 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. if not modem then error("No modem found. Please attach a wireless modem.") end
  3.  
  4. modem.open(1) -- Use channel 1 for all communications
  5.  
  6. local function encrypt(text)
  7. local result = ""
  8. for i = 1, #text do
  9. result = result .. string.char((string.byte(text, i) + 5) % 256)
  10. end
  11. return result
  12. end
  13.  
  14. local function decrypt(text)
  15. local result = ""
  16. for i = 1, #text do
  17. result = result .. string.char((string.byte(text, i) - 5) % 256)
  18. end
  19. return result
  20. end
  21.  
  22. local function saveUsername(username)
  23. local file = fs.open(".userdata", "w")
  24. file.write(encrypt(username))
  25. file.close()
  26. end
  27.  
  28. local function loadUsername()
  29. if fs.exists(".userdata") then
  30. local file = fs.open(".userdata", "r")
  31. local encryptedUsername = file.readAll()
  32. file.close()
  33. return decrypt(encryptedUsername)
  34. end
  35. return nil
  36. end
  37.  
  38. local function sendRequest(request)
  39. modem.transmit(1, 1, request)
  40. local timer = os.startTimer(5)
  41. while true do
  42. local event, param1, _, _, message = os.pullEvent()
  43. if event == "modem_message" and type(message) == "table" then
  44. return message
  45. elseif event == "timer" and param1 == timer then
  46. return nil
  47. end
  48. end
  49. end
  50.  
  51. local function createAccount()
  52. print("Enter new student ID:")
  53. local newId = read()
  54. print("Enter password:")
  55. local password = read("*")
  56. local response = sendRequest({type = "create_account", studentId = newId, password = password})
  57. if response and response.success then
  58. print(response.message)
  59. saveUsername(newId)
  60. return newId
  61. else
  62. print(response and response.message or "Failed to create account")
  63. return nil
  64. end
  65. end
  66.  
  67. local function login(savedUsername)
  68. local username = savedUsername
  69. if not username then
  70. print("Enter your student ID:")
  71. username = read()
  72. else
  73. print("Logged in as: " .. username)
  74. end
  75. print("Enter your password:")
  76. local password = read("*")
  77. local response = sendRequest({type = "login", studentId = username, password = password})
  78. if response and response.success then
  79. print(response.message)
  80. saveUsername(username)
  81. return username, password
  82. else
  83. print(response and response.message or "Login failed")
  84. return nil, nil
  85. end
  86. end
  87.  
  88. local function showNews()
  89. local response = sendRequest({type = "get_news"})
  90. if response and response.type == "news" then
  91. print("\nNews:")
  92. print(response.content)
  93. else
  94. print("Failed to retrieve news")
  95. end
  96. end
  97.  
  98. local function showDoors(studentId, password)
  99. local response = sendRequest({type = "get_doors", studentId = studentId, password = password})
  100. if response and response.type == "doors_list" then
  101. print("\nDoors you have access to:")
  102. if response.doors ~= "" then
  103. local doors = {}
  104. for door in response.doors:gmatch("[^\r\n]+") do
  105. table.insert(doors, door)
  106. end
  107. for i, door in ipairs(doors) do
  108. print(i .. ". " .. door)
  109. end
  110. else
  111. print("No doors available")
  112. end
  113. else
  114. print("Failed to retrieve door list")
  115. end
  116. end
  117.  
  118. local function openDoor(studentId, password)
  119. local response = sendRequest({type = "get_doors", studentId = studentId, password = password})
  120. if response and response.type == "doors_list" then
  121. if response.doors ~= "" then
  122. print("\nDoors you have access to:")
  123. local doors = {}
  124. for door in response.doors:gmatch("[^\r\n]+") do
  125. table.insert(doors, door)
  126. end
  127. for i, door in ipairs(doors) do
  128. print(i .. ". " .. door)
  129. end
  130. print("Enter the number of the door to open:")
  131. local choice = tonumber(read())
  132. if choice and doors[choice] then
  133. local doorId = doors[choice]
  134. local openResponse = sendRequest({type = "door_access", studentId = studentId, password = password, doorId = doorId, action = "open"})
  135. if openResponse and openResponse.type == "access_response" then
  136. if openResponse.granted then
  137. print("Door " .. doorId .. " command sent successfully.")
  138. else
  139. print("Failed to open door " .. doorId .. ": " .. (openResponse.message or "Unknown error"))
  140. end
  141. else
  142. print("Failed to receive a valid response for door " .. doorId)
  143. end
  144. else
  145. print("Invalid choice")
  146. end
  147. else
  148. print("No doors available")
  149. end
  150. else
  151. print("Failed to retrieve door list")
  152. end
  153. end
  154.  
  155. local function checkBalance(studentId, password)
  156. local response = sendRequest({type = "get_balance", studentId = studentId, password = password})
  157. if response and response.type == "balance_response" then
  158. print("\nYour balance: " .. response.balance .. " diamonds")
  159. else
  160. print("Failed to retrieve balance")
  161. end
  162. end
  163.  
  164. local function transferMoney(studentId, password)
  165. print("Enter recipient's student ID:")
  166. local recipientId = read()
  167.  
  168. -- Verify recipient exists
  169. local response = sendRequest({type = "verify_student", recipientId = recipientId})
  170. if not response or not response.exists then
  171. print("Recipient does not exist.")
  172. return
  173. end
  174.  
  175. print("Enter amount to transfer:")
  176. local amount = tonumber(read())
  177. if not amount or amount <= 0 then
  178. print("Invalid amount.")
  179. return
  180. end
  181.  
  182. -- Verify sufficient funds
  183. local balanceResponse = sendRequest({type = "get_balance", studentId = studentId, password = password})
  184. if not balanceResponse or not balanceResponse.balance then
  185. print("Failed to retrieve balance.")
  186. return
  187. end
  188.  
  189. local balance = tonumber(balanceResponse.balance)
  190. if balance < amount then
  191. print("Insufficient funds.")
  192. return
  193. end
  194.  
  195. -- Send transfer request
  196. local transferResponse = sendRequest({
  197. type = "transfer_money",
  198. studentId = studentId,
  199. password = password,
  200. recipientId = recipientId,
  201. amount = amount
  202. })
  203.  
  204. if transferResponse and transferResponse.success then
  205. print("Transfer successful. New balance: " .. transferResponse.newBalance)
  206. else
  207. print("Transfer failed: " .. (transferResponse and transferResponse.message or "Unknown error"))
  208. end
  209. end
  210.  
  211. -- Main program
  212. while true do
  213. local studentId = loadUsername()
  214. local password
  215. if not studentId then
  216. print("\n1. Create Account")
  217. print("2. Login")
  218. print("3. Exit")
  219. local choice = read()
  220. if choice == "1" then
  221. studentId = createAccount()
  222. elseif choice == "2" then
  223. studentId, password = login()
  224. elseif choice == "3" then
  225. break
  226. end
  227. else
  228. studentId, password = login(studentId)
  229. end
  230.  
  231. if studentId and password then
  232. while true do
  233. print("\n1. Show News")
  234. print("2. Show Accessible Doors")
  235. print("3. Open a Door")
  236. print("4. Check Balance")
  237. print("5. Transfer Money")
  238. print("6. Logout")
  239. local choice = read()
  240. if choice == "1" then
  241. showNews()
  242. elseif choice == "2" then
  243. showDoors(studentId, password)
  244. elseif choice == "3" then
  245. openDoor(studentId, password)
  246. elseif choice == "4" then
  247. checkBalance(studentId, password)
  248. elseif choice == "5" then
  249. transferMoney(studentId, password)
  250. elseif choice == "6" then
  251. break
  252. end
  253. end
  254. end
  255. end
Advertisement
Add Comment
Please, Sign In to add comment