RemiCraft

Bank Server Dev

Jul 1st, 2022 (edited)
461
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None
  1. --Variables--
  2. local remoteUserAccount
  3. local remoteUserPIN
  4. local userAccount
  5. local userPIN
  6. local userBalance
  7. local remoteOtherAccount
  8. local otherPIN
  9. local otherBalance
  10. local remoteAmount
  11.  
  12. local senderId
  13. local message
  14. local protocol
  15.  
  16. --Setup Networking--
  17. rednet.open("back")
  18.  
  19. --Basic Functions--
  20. local function Clear()
  21.     term.clear()
  22.     term.setCursorPos(1,1)
  23. end
  24.  
  25. --Bank Functions--
  26. local function SendMoney()
  27.     --Gets Variables From Remote Terminal--
  28.     senderId, message, protocol = rednet.receive("bankSend")
  29.     remoteUserAccount = message
  30.     rednet.send(senderId, "confirmed", "confirmation")
  31.     senderId, message, protocol = rednet.receive("bankSend")
  32.     remoteOtherAccount = message
  33.     rednet.send(senderId, "confirmed", "confirmation")
  34.     senderId, message, protocol = rednet.receive("bankSend")
  35.     remoteAmount = message
  36.     rednet.send(senderId, "confirmed", "confirmation")
  37.     senderId, message, protocol = rednet.receive("bankSend")
  38.     remoteUserPIN = message
  39.  
  40.     --Checks If User Accounts Exist And Have Enough Money--
  41.     if fs.exists("accounts/" .. remoteUserAccount) then
  42.         local UserFile = fs.open("accounts/" .. remoteUserAccount, "r")
  43.         UserFile.readLine()
  44.         userPIN = UserFile.readLine()
  45.         if remoteUserPIN ~= userPIN then
  46.             rednet.send(senderId, "Incorrect PIN", "bankStatus")
  47.             os.reboot()
  48.         end
  49.         userBalance = UserFile.readLine()
  50.         UserFile.close()
  51.         if fs.exists("accounts/" .. remoteOtherAccount) then
  52.             local OtherFile = fs.open("accounts/" .. remoteOtherAccount, "r")
  53.             OtherFile.readLine()
  54.             otherPIN = OtherFile.readLine()
  55.             otherBalance = OtherFile.readLine()
  56.             OtherFile.close()
  57.             if tonumber(remoteAmount) <= tonumber(userBalance) then
  58.                 local UserFile = fs.open("accounts/" .. remoteUserAccount, "w")
  59.                 UserFile.writeLine(remoteUserAccount)
  60.                 UserFile.writeLine(remoteUserPIN)
  61.                 userBalance = tostring(tonumber(userBalance) - tonumber(remoteAmount))
  62.                 UserFile.writeLine(userBalance)
  63.                 UserFile.close()
  64.                 local OtherFile = fs.open("accounts/" .. remoteOtherAccount, "w")
  65.                 OtherFile.writeLine(remoteOtherAccount)
  66.                 OtherFile.writeLine(otherPIN)
  67.                 otherBalance = tostring(tonumber(otherBalance) + tonumber(remoteAmount))
  68.                 OtherFile.writeLine(otherBalance)
  69.                 OtherFile.close()
  70.                 local LogFile = fs.open("log", "a")
  71.                 LogFile.writeLine("Sending " .. remoteAmount .. " from " .. remoteUserAccount .. " to " .. remoteOtherAccount)
  72.  
  73.                 rednet.send(senderId, "Transfer Complete", "bankStatus")
  74.                 os.reboot()
  75.             else
  76.                 rednet.send(senderId, "Insuffcient Funds", "bankStatus")
  77.                 os.reboot()
  78.             end
  79.         else
  80.             rednet.send(senderId, "Account Not Found", "bankStatus")
  81.             os.reboot()
  82.         end
  83.     else
  84.         rednet.send(senderId, "Account Not Found", "bankStatus")
  85.         os.reboot()
  86.     end
  87. end
  88.  
  89. local function CheckBalance()
  90.     --Gets Variables From Remote Terminal--
  91.     senderId, message, protocol = rednet.receive("balanceCheck")
  92.     remoteUserAccount = message
  93.     rednet.send(senderId, "confirmed", "confirmation")
  94.     senderId, message, protocol = rednet.receive("balanceCheck")
  95.     remoteUserPIN = message
  96.  
  97. --Validates PIN And Sends Balance--
  98.     if fs.exists("accounts/" .. remoteUserAccount) then
  99.         local UserFile = fs.open("accounts/" .. remoteUserAccount, "r")
  100.         UserFile.readLine()
  101.         userPIN = UserFile.readLine()
  102.         if remoteUserPIN ~= userPIN then
  103.             rednet.send(senderId, "Incorrect PIN", "bankStatus")
  104.             os.reboot()
  105.         end
  106.         userBalance = UserFile.readLine()
  107.         rednet.send(senderId, "confirmed", "bankStatus")
  108.         rednet.send(senderId, userBalance, "bankCheck")
  109.         os.reboot()
  110.     else
  111.         rednet.send(senderId, "Account Not Found", "bankStatus")
  112.         os.reboot()
  113.     end
  114. end
  115.  
  116. --Main Code--
  117. --Imports New User Accounts--
  118. if disk.isPresent("bottom") then
  119.     local rawDiskPath = textutils.serialize(fs.find("/disk/accounts/*"))
  120.     local accountTransferDestination = string.sub(rawDiskPath, 11, string.find(rawDiskPath, ",") - 2)
  121.     local accountTransferDisk = string.sub(rawDiskPath, 6, string.find(rawDiskPath, ",") - 2)
  122.     fs.move(accountTransferDisk, accountTransferDestination)
  123.     disk.eject("bottom")
  124. end
  125. senderId, message, protocol = rednet.receive("bankFunction")
  126. if message == "SendMoney" then
  127.     rednet.send(senderId, "confirmed", "confirmation")
  128.     SendMoney()
  129. elseif message == "CheckBalance" then
  130.     rednet.send(senderId, "confirmed", "confirmation")
  131.     CheckBalance()
  132. else
  133.     rednet.send(senderId, "confirmed", "confirmation")
  134.     os.reboot()
  135. end
RAW Paste Data Copied