MtnMCG

sethmc student.lua

Dec 15th, 2025 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.73 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, expectedType)
  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. if expectedType then
  45. if message.type == expectedType then return message end
  46. if message.type == "error" then return message end
  47. else
  48. if message.type then return message end
  49. end
  50. elseif event == "timer" and param1 == timer then
  51. return nil
  52. end
  53. end
  54. end
  55.  
  56. local function createAccount()
  57. print("Enter new student ID:")
  58. local newId = read()
  59. print("Enter password:")
  60. local password = read("*")
  61. local response = sendRequest({type = "create_account", studentId = newId, password = password}, "account_response")
  62. if response and response.success then
  63. print(response.message)
  64. saveUsername(newId)
  65. return newId
  66. else
  67. print(response and response.message or "Failed to create account")
  68. return nil
  69. end
  70. end
  71.  
  72. local function login(savedUsername)
  73. local username = savedUsername
  74. if not username then
  75. print("Enter your student ID:")
  76. username = read()
  77. else
  78. print("Logged in as: " .. username)
  79. end
  80. print("Enter your password:")
  81. local password = read("*")
  82. local response = sendRequest({type = "login", studentId = username, password = password}, "login_response")
  83. if response and response.success then
  84. print(response.message)
  85. saveUsername(username)
  86. return username, password
  87. else
  88. print(response and response.message or "Login failed")
  89. return nil, nil
  90. end
  91. end
  92.  
  93. local function showNews()
  94. local response = sendRequest({type = "get_news"})
  95. if response and response.type == "news" then
  96. print("\nNews:")
  97. print(response.content)
  98. else
  99. print("Failed to retrieve news")
  100. end
  101. end
  102.  
  103.  
  104.  
  105. local function checkBalance(studentId, password)
  106. local response = sendRequest({type = "get_balance", studentId = studentId, password = password})
  107. if response and response.type == "balance_response" then
  108. print("\nYour balance: " .. response.balance .. " Eddies")
  109. else
  110. print("Failed to retrieve balance")
  111. end
  112. end
  113.  
  114. local function transferMoney(studentId, password)
  115. print("Enter recipient's student ID:")
  116. local recipientId = read()
  117.  
  118. -- Verify recipient exists
  119. local response = sendRequest({type = "verify_student", recipientId = recipientId})
  120. if not response or not response.exists then
  121. print("Recipient does not exist.")
  122. return
  123. end
  124.  
  125. print("Enter amount to transfer:")
  126. local amount = tonumber(read())
  127. if not amount or amount <= 0 then
  128. print("Invalid amount.")
  129. return
  130. end
  131.  
  132. -- Verify sufficient funds
  133. local balanceResponse = sendRequest({type = "get_balance", studentId = studentId, password = password})
  134. if not balanceResponse or not balanceResponse.balance then
  135. print("Failed to retrieve balance.")
  136. return
  137. end
  138.  
  139. local balance = tonumber(balanceResponse.balance)
  140. if balance < amount then
  141. print("Insufficient funds.")
  142. return
  143. end
  144.  
  145. -- Send transfer request
  146. local transferResponse = sendRequest({
  147. type = "transfer_money",
  148. studentId = studentId,
  149. password = password,
  150. recipientId = recipientId,
  151. amount = amount
  152. })
  153.  
  154. if transferResponse and transferResponse.success then
  155. print("Transfer successful. New balance: " .. transferResponse.newBalance)
  156. else
  157. print("Transfer failed: " .. (transferResponse and transferResponse.message or "Unknown error"))
  158. end
  159. end
  160.  
  161. local function composeEmail(studentId, password, recipientId, subject, body)
  162. if not recipientId then
  163. print("Enter Recipient ID:")
  164. recipientId = read()
  165. end
  166. if not subject then
  167. print("Enter Subject:")
  168. subject = read()
  169. end
  170. if not body then
  171. print("Enter Body:")
  172. body = read()
  173. end
  174.  
  175. local response = sendRequest({
  176. type = "send_email",
  177. studentId = studentId,
  178. password = password,
  179. recipientId = recipientId,
  180. subject = subject,
  181. body = body
  182. })
  183.  
  184. if response and response.success then
  185. print("Email sent successfully!")
  186. else
  187. print("Failed to send email: " .. (response and response.message or "Unknown error"))
  188. end
  189. end
  190.  
  191. local function deleteEmail(studentId, password, index)
  192. local response = sendRequest({
  193. type = "delete_email",
  194. studentId = studentId,
  195. password = password,
  196. emailIndex = index
  197. })
  198. if response and response.success then
  199. print("Email deleted.")
  200. return true
  201. else
  202. print("Failed to delete email: " .. (response and response.message or "Unknown error"))
  203. return false
  204. end
  205. end
  206.  
  207. local function readEmail(studentId, password, email, index)
  208. print("\n--- Email ---")
  209. print("From: " .. email.sender)
  210. print("Date: " .. os.date("%Y-%m-%d %H:%M", email.timestamp / 1000)) -- approximate conversion if needed, or just display raw
  211. print("Subject: " .. email.subject)
  212. print("----------------")
  213. print(email.body)
  214. print("----------------")
  215.  
  216. while true do
  217. print("\n1. Reply")
  218. print("2. Forward")
  219. print("3. Delete")
  220. print("4. Back to Inbox")
  221. local choice = read()
  222.  
  223. if choice == "1" then
  224. composeEmail(studentId, password, email.sender, "Re: " .. email.subject)
  225. elseif choice == "2" then
  226. print("Enter recipient to forward to:")
  227. local fwdRecipient = read()
  228. composeEmail(studentId, password, fwdRecipient, "Fwd: " .. email.subject, email.body)
  229. elseif choice == "3" then
  230. if deleteEmail(studentId, password, index) then
  231. return -- Go back to inbox refresh
  232. end
  233. elseif choice == "4" then
  234. return
  235. end
  236. end
  237. end
  238.  
  239. local function showInbox(studentId, password)
  240. while true do
  241. local response = sendRequest({type = "get_inbox", studentId = studentId, password = password})
  242. if response and response.type == "inbox_response" then
  243. local emails = response.emails
  244. if #emails == 0 then
  245. print("\nInbox is empty")
  246. print("1. Compose")
  247. print("2. Back")
  248. local choice = read()
  249. if choice == "1" then composeEmail(studentId, password)
  250. elseif choice == "2" then break end
  251. else
  252. print("\n--- Inbox ---")
  253. for i, email in ipairs(emails) do
  254. print(string.format("%d. [%s] %s", i, email.sender, email.subject))
  255. end
  256. print("\n1. Read Email")
  257. print("2. Compose")
  258. print("3. Back")
  259. local choice = read()
  260. if choice == "1" then
  261. print("Enter email number:")
  262. local idx = tonumber(read())
  263. if idx and emails[idx] then
  264. readEmail(studentId, password, emails[idx], idx)
  265. else
  266. print("Invalid email number")
  267. end
  268. elseif choice == "2" then
  269. composeEmail(studentId, password)
  270. elseif choice == "3" then
  271. break
  272. end
  273. end
  274. else
  275. print("Failed to retrieve inbox")
  276. break
  277. end
  278. end
  279. end
  280.  
  281. local function showPhonebook()
  282. local response = sendRequest({type = "get_phonebook"}, "phonebook_response")
  283. if not response or not response.students then
  284. print("Failed to retrieve phonebook")
  285. return
  286. end
  287.  
  288. local students = response.students
  289. if #students == 0 then
  290. print("\nPhonebook is empty")
  291. print("Press any key to continue...")
  292. read()
  293. return
  294. end
  295.  
  296. local _, screenHeight = term.getSize()
  297. local itemsPerPage = screenHeight - 6 -- Leave room for header and footer
  298. if itemsPerPage < 5 then itemsPerPage = 5 end
  299.  
  300. local currentPage = 1
  301. local totalPages = math.ceil(#students / itemsPerPage)
  302.  
  303. while true do
  304. term.clear()
  305. term.setCursorPos(1, 1)
  306. print("=== Phonebook ===")
  307. print("Page " .. currentPage .. " of " .. totalPages .. " (" .. #students .. " students)")
  308. print("")
  309.  
  310. local startIdx = (currentPage - 1) * itemsPerPage + 1
  311. local endIdx = math.min(startIdx + itemsPerPage - 1, #students)
  312.  
  313. for i = startIdx, endIdx do
  314. print(" " .. students[i])
  315. end
  316.  
  317. print("")
  318. local navOptions = ""
  319. if currentPage > 1 then navOptions = navOptions .. "[P]rev " end
  320. if currentPage < totalPages then navOptions = navOptions .. "[N]ext " end
  321. navOptions = navOptions .. "[B]ack"
  322. print(navOptions)
  323.  
  324. local choice = read():lower()
  325. if choice == "p" and currentPage > 1 then
  326. currentPage = currentPage - 1
  327. elseif choice == "n" and currentPage < totalPages then
  328. currentPage = currentPage + 1
  329. elseif choice == "b" then
  330. break
  331. end
  332. end
  333. end
  334.  
  335. local function showEconomy()
  336. local response = sendRequest({type = "get_economy"}, "economy_response")
  337. if not response then
  338. print("Failed to retrieve economy data")
  339. return
  340. end
  341.  
  342. term.clear()
  343. term.setCursorPos(1, 1)
  344. print("=== Economy Stats ===")
  345. print("")
  346. print("GDP (Total Eddies): " .. (response.gdp or 0))
  347. print("")
  348. print("--- Top 5 Richest ---")
  349.  
  350. if response.top5 and #response.top5 > 0 then
  351. for i, entry in ipairs(response.top5) do
  352. print(i .. ". " .. entry.name .. " - " .. entry.balance .. " Eddies")
  353. end
  354. else
  355. print(" No data available")
  356. end
  357.  
  358. print("")
  359. print("Press any key to continue...")
  360. read()
  361. end
  362.  
  363. -- Main program
  364. -- GPS Background Task
  365. local function gpsSender()
  366. local studentId = loadUsername() -- Initial load
  367. while true do
  368. -- Update ID in case of login change
  369. studentId = loadUsername()
  370.  
  371. if studentId then
  372. local x, y, z = gps.locate(5) -- 5 second timeout
  373. if x and y and z then
  374. modem.transmit(1, 1, {
  375. type = "track_location",
  376. studentId = studentId,
  377. x = math.floor(x),
  378. y = math.floor(y),
  379. z = math.floor(z)
  380. })
  381. end
  382. end
  383.  
  384. sleep(30) -- Send every 30 seconds
  385. end
  386. end
  387.  
  388. -- Main Application wrapper
  389. local function mainApp()
  390. while true do
  391. local studentId = loadUsername()
  392. local password
  393. if not studentId then
  394. print("\n1. Create Account")
  395. print("2. Login")
  396. print("3. Exit")
  397. local choice = read()
  398. if choice == "1" then
  399. studentId = createAccount()
  400. elseif choice == "2" then
  401. studentId, password = login()
  402. elseif choice == "3" then
  403. break
  404. end
  405. else
  406. studentId, password = login(studentId)
  407. end
  408.  
  409. if studentId and password then
  410. while true do
  411. print("\n1. Show News")
  412. print("2. Check Balance")
  413. print("3. Transfer Money")
  414. print("4. Email")
  415. print("5. Phonebook")
  416. print("6. Economy")
  417. print("7. Logout")
  418. local choice = read()
  419. if choice == "1" then
  420. showNews()
  421. elseif choice == "2" then
  422. checkBalance(studentId, password)
  423. elseif choice == "3" then
  424. transferMoney(studentId, password)
  425. elseif choice == "4" then
  426. showInbox(studentId, password)
  427. elseif choice == "5" then
  428. showPhonebook()
  429. elseif choice == "6" then
  430. showEconomy()
  431. elseif choice == "7" then
  432. break
  433. end
  434. end
  435. end
  436. end
  437. end
  438.  
  439. -- Run both in parallel
  440. parallel.waitForAny(mainApp, gpsSender)
Advertisement
Add Comment
Please, Sign In to add comment