MtnMCG

MtnLabs Server

Sep 2nd, 2024 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.27 KB | None | 0 0
  1. -- Server startup.lua
  2.  
  3. local modem = peripheral.find("modem")
  4. if not modem then error("No modem found. Please attach a wireless modem.") end
  5.  
  6. local disk = peripheral.find("drive")
  7. if not disk then error("No disk drive found. Please attach a disk drive.") end
  8.  
  9. modem.open(1) -- Use channel 1 for all communications
  10.  
  11. local diskMount = disk.getMountPath()
  12. if not diskMount then error("No disk inserted. Please insert a disk.") end
  13.  
  14. local function setup()
  15. if not fs.exists(diskMount .. "/students") then fs.makeDir(diskMount .. "/students") end
  16. if not fs.exists(diskMount .. "/news.txt") then
  17. local file = fs.open(diskMount .. "/news.txt", "w")
  18. file.write("Welcome to the college!")
  19. file.close()
  20. end
  21. if not fs.exists(diskMount .. "/student.lua") then
  22. error("student.lua not found on the disk. Please create this file.")
  23. end
  24. print("Server setup complete. Files stored on disk.")
  25. end
  26.  
  27. local function createAccount(studentId, password)
  28. local studentDir = diskMount .. "/students/" .. studentId
  29. if fs.exists(studentDir) then
  30. return false, "Account already exists"
  31. end
  32. fs.makeDir(studentDir)
  33. local passFile = fs.open(studentDir .. "/password.txt", "w")
  34. passFile.write(password)
  35. passFile.close()
  36. local doorsFile = fs.open(studentDir .. "/doors.txt", "w")
  37. doorsFile.write("") -- Initially, no door access
  38. doorsFile.close()
  39. local balanceFile = fs.open(studentDir .. "/balance.txt", "w")
  40. balanceFile.write("0") -- Initial balance of 0
  41. balanceFile.close()
  42. return true, "Account created successfully"
  43. end
  44.  
  45. local function verifyLogin(studentId, password)
  46. local studentDir = diskMount .. "/students/" .. studentId
  47. if not fs.exists(studentDir) then return false, "Account not found" end
  48. local passFile = fs.open(studentDir .. "/password.txt", "r")
  49. local storedPass = passFile.readAll():gsub("%s+$", "") -- Trim trailing whitespace
  50. passFile.close()
  51. return storedPass == password, storedPass == password and "Login successful" or "Incorrect password"
  52. end
  53.  
  54. local function getDoorAccess(studentId)
  55. local doorsFile = fs.open(diskMount .. "/students/" .. studentId .. "/doors.txt", "r")
  56. local doors = doorsFile.readAll()
  57. doorsFile.close()
  58. return doors
  59. end
  60.  
  61. local function handleDoorAccess(studentId, doorId, action)
  62. local doors = getDoorAccess(studentId)
  63. local hasAccess = string.find(doors, doorId) ~= nil
  64. if hasAccess then
  65. modem.transmit(1, 1, {
  66. type = "door_command",
  67. doorId = doorId,
  68. action = action
  69. })
  70. print("Sending command to " .. action .. " door " .. doorId .. " for student " .. studentId)
  71. return true, "Command sent to door"
  72. else
  73. print("Access denied for student " .. studentId .. " to door " .. doorId)
  74. return false, "Access denied"
  75. end
  76. end
  77.  
  78. local function handleBalance(studentId, action, amount)
  79. local balanceFile = fs.open(diskMount .. "/students/" .. studentId .. "/balance.txt", "r")
  80. local balance = tonumber(balanceFile.readAll()) or 0
  81. balanceFile.close()
  82. print("Processing balance update for " .. studentId)
  83. print("Current balance: " .. balance)
  84. print("Action: " .. action .. ", Amount: " .. amount)
  85. if action == "deposit" then
  86. balance = balance + amount
  87. elseif action == "withdraw" then
  88. if balance >= amount then
  89. balance = balance - amount
  90. else
  91. print("Insufficient funds. Transaction failed.")
  92. return false, "Insufficient funds"
  93. end
  94. end
  95. balanceFile = fs.open(diskMount .. "/students/" .. studentId .. "/balance.txt", "w")
  96. balanceFile.write(tostring(balance))
  97. balanceFile.close()
  98. print("New balance: " .. balance)
  99. print("Transaction successful")
  100. return true, balance
  101. end
  102.  
  103. local function handleRequests()
  104. print("Server is running. Waiting for requests...")
  105. while true do
  106. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  107. if type(message) == "table" then
  108. if message.type == "request_update" then
  109. local file = fs.open(diskMount .. "/student.lua", "r")
  110. local content = file.readAll()
  111. file.close()
  112. modem.transmit(replyChannel, 1, {type = "update", content = content})
  113. print("Sent student.lua update to a device")
  114. elseif message.type == "create_account" then
  115. if message.studentId and message.password then
  116. local success, msg = createAccount(message.studentId, message.password)
  117. modem.transmit(replyChannel, 1, {type = "account_response", success = success, message = msg})
  118. else
  119. modem.transmit(replyChannel, 1, {type = "error", message = "Invalid account creation request"})
  120. end
  121. elseif message.type == "login" then
  122. if message.studentId and message.password then
  123. local success, msg = verifyLogin(message.studentId, message.password)
  124. modem.transmit(replyChannel, 1, {type = "login_response", success = success, message = msg})
  125. else
  126. modem.transmit(replyChannel, 1, {type = "error", message = "Invalid login request"})
  127. end
  128. elseif message.type == "get_doors" then
  129. if message.studentId and message.password then
  130. local success, msg = verifyLogin(message.studentId, message.password)
  131. if success then
  132. local doors = getDoorAccess(message.studentId)
  133. modem.transmit(replyChannel, 1, {type = "doors_list", doors = doors})
  134. else
  135. modem.transmit(replyChannel, 1, {type = "error", message = msg})
  136. end
  137. else
  138. modem.transmit(replyChannel, 1, {type = "error", message = "Invalid door access request"})
  139. end
  140. elseif message.type == "get_news" then
  141. local newsFile = fs.open(diskMount .. "/news.txt", "r")
  142. local news = newsFile.readAll()
  143. newsFile.close()
  144. modem.transmit(replyChannel, 1, {type = "news", content = news})
  145. elseif message.type == "door_access" then
  146. if message.studentId and message.password and message.doorId and message.action then
  147. local success, msg = verifyLogin(message.studentId, message.password)
  148. if success then
  149. local granted, result = handleDoorAccess(message.studentId, message.doorId, message.action)
  150. modem.transmit(replyChannel, 1, {type = "access_response", granted = granted, message = result})
  151. else
  152. modem.transmit(replyChannel, 1, {type = "error", message = msg})
  153. end
  154. else
  155. modem.transmit(replyChannel, 1, {type = "error", message = "Invalid door access request"})
  156. end
  157. elseif message.type == "get_balance" then
  158. if message.studentId and message.password then
  159. local success, msg = verifyLogin(message.studentId, message.password)
  160. if success then
  161. local balanceFile = fs.open(diskMount .. "/students/" .. message.studentId .. "/balance.txt", "r")
  162. local balance = balanceFile.readAll()
  163. balanceFile.close()
  164. modem.transmit(replyChannel, 1, {type = "balance_response", balance = balance})
  165. else
  166. modem.transmit(replyChannel, 1, {type = "error", message = msg})
  167. end
  168. else
  169. modem.transmit(replyChannel, 1, {type = "error", message = "Invalid balance request"})
  170. end
  171. elseif message.type == "update_balance" then
  172. if message.studentId and message.password and message.action and message.amount then
  173. local success, msg = verifyLogin(message.studentId, message.password)
  174. if success then
  175. local success, result = handleBalance(message.studentId, message.action, message.amount)
  176. modem.transmit(replyChannel, 1, {type = "balance_update_response", success = success, result = result})
  177. else
  178. modem.transmit(replyChannel, 1, {type = "error", message = msg})
  179. end
  180. else
  181. modem.transmit(replyChannel, 1, {type = "error", message = "Invalid balance update request"})
  182. end
  183. elseif message.type == "verify_student" then
  184. if message.recipientId then
  185. local recipientDir = diskMount .. "/students/" .. message.recipientId
  186. local exists = fs.exists(recipientDir)
  187. modem.transmit(replyChannel, 1, {exists = exists})
  188. else
  189. modem.transmit(replyChannel, 1, {type = "error", message = "Invalid student verification request"})
  190. end
  191. elseif message.type == "transfer_money" then
  192. if message.studentId and message.password and message.recipientId and message.amount then
  193. local success, msg = verifyLogin(message.studentId, message.password)
  194. if success then
  195. local senderBalanceFile = fs.open(diskMount .. "/students/" .. message.studentId .. "/balance.txt", "r")
  196. local senderBalance = tonumber(senderBalanceFile.readAll()) or 0
  197. senderBalanceFile.close()
  198.  
  199. local recipientBalanceFile = fs.open(diskMount .. "/students/" .. message.recipientId .. "/balance.txt", "r")
  200. local recipientBalance = tonumber(recipientBalanceFile.readAll()) or 0
  201. recipientBalanceFile.close()
  202.  
  203. local expectedSenderBalance = senderBalance - message.amount
  204. local expectedRecipientBalance = recipientBalance + message.amount
  205.  
  206. if expectedSenderBalance >= 0 then
  207. local withdrawSuccess, _ = handleBalance(message.studentId, "withdraw", message.amount)
  208. if withdrawSuccess then
  209. local depositSuccess, _ = handleBalance(message.recipientId, "deposit", message.amount)
  210. if depositSuccess then
  211. -- Verify balances
  212. senderBalanceFile = fs.open(diskMount .. "/students/" .. message.studentId .. "/balance.txt", "r")
  213. local newSenderBalance = tonumber(senderBalanceFile.readAll()) or 0
  214. senderBalanceFile.close()
  215.  
  216. recipientBalanceFile = fs.open(diskMount .. "/students/" .. message.recipientId .. "/balance.txt", "r")
  217. local newRecipientBalance = tonumber(recipientBalanceFile.readAll()) or 0
  218. recipientBalanceFile.close()
  219.  
  220. if newSenderBalance == expectedSenderBalance and newRecipientBalance == expectedRecipientBalance then
  221. modem.transmit(replyChannel, 1, {success = true, newBalance = newSenderBalance})
  222. else
  223. -- Revert transaction
  224. handleBalance(message.studentId, "deposit", message.amount)
  225. handleBalance(message.recipientId, "withdraw", message.amount)
  226. modem.transmit(replyChannel, 1, {success = false, message = "Transaction verification failed"})
  227. end
  228. else
  229. handleBalance(message.studentId, "deposit", message.amount)
  230. modem.transmit(replyChannel, 1, {success = false, message = "Failed to deposit to recipient"})
  231. end
  232. else
  233. modem.transmit(replyChannel, 1, {success = false, message = "Failed to withdraw from sender"})
  234. end
  235. else
  236. modem.transmit(replyChannel, 1, {success = false, message = "Insufficient funds"})
  237. end
  238. else
  239. modem.transmit(replyChannel, 1, {success = false, message = msg})
  240. end
  241. else
  242. modem.transmit(replyChannel, 1, {success = false, message = "Invalid transfer request"})
  243. end
  244. end
  245. end
  246. end
  247. end
  248.  
  249. setup()
  250. handleRequests()
Advertisement
Add Comment
Please, Sign In to add comment