Advertisement
MavricMC

Bank Server API

Aug 21st, 2022 (edited)
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. --Must be called bankAPI.lua--
  2.  
  3. rednet.open("top")
  4.  
  5. function balance(acc, atm, pin)
  6.     file = fs.open("saves/".. acc, "r")
  7.     if file ~= nil then
  8.         local fileData = {}
  9.         local line = file.readLine()
  10.         repeat
  11.         table.insert(fileData, line)
  12.         line = file.readLine()
  13.         until line == nil
  14.         file.close()
  15.         if fileData[2] == pin then
  16.             return true, fileData[1]
  17.         else
  18.             return false, "Incorrect pin"
  19.         end
  20.     end
  21.     return false, "Not real id"
  22. end
  23.  
  24. function deposit(acc, amm, atm, pin)
  25.     file = fs.open("saves/".. acc, "r")
  26.     if file ~= nil then
  27.         local fileData = {}
  28.         local line = file.readLine()
  29.         repeat
  30.         table.insert(fileData,line)
  31.         line = file.readLine()
  32.         until line == nil
  33.         file.close()
  34.         if fileData[2] == pin then
  35.             fs.delete("saves/"..acc)
  36.             file = fs.open("saves/".. acc, "a")
  37.             file.writeLine(tonumber(fileData[1]) + amm)
  38.             file.writeLine(fileData[2])
  39.             file.close()
  40.             return true, amm
  41.         else
  42.             return false, "Incorrect pin"
  43.         end
  44.     end
  45.     return false, "Not a real id"
  46. end
  47.  
  48. function withdraw(acc, amm, atm, pin)
  49.     file = fs.open("saves/".. acc, "r")
  50.     if file ~= nil then
  51.         local fileData = {}
  52.         local line = file.readLine()
  53.         repeat
  54.         table.insert(fileData,line)
  55.         line = file.readLine()
  56.         until line == nil
  57.         file.close()
  58.         if (tonumber(fileData[1]) - amm) >= 0 then
  59.             if fileData[2] == pin then
  60.                 fs.delete("saves/".. acc)
  61.                 file = fs.open("saves/".. acc, "a")
  62.                 file.writeLine(tonumber(fileData[1]) - amm)
  63.                 file.writeLine(fileData[2])
  64.                 file.close()
  65.                 return true, amm
  66.             else
  67.                 return false, "Incorrect pin"
  68.             end
  69.         else
  70.             return false, "Balance to low"
  71.         end
  72.     end
  73.     return false, "Not a real id"
  74. end
  75.  
  76. function transfer(acc, acc2, amm, atm, pin)
  77.     local suc, res = withdraw(acc, amm, atm, pin)
  78.     if (suc) then
  79.         file = fs.open("saves/".. acc2, "r")
  80.         if file ~= nil then
  81.             local fileData = {}
  82.             local line = file.readLine()
  83.             repeat
  84.             table.insert(fileData,line)
  85.             line = file.readLine()
  86.             until line == nil
  87.             file.close()
  88.             fs.delete("saves/".. acc2)
  89.             file = fs.open("saves/".. acc2, "a")
  90.             file.writeLine(tonumber(fileData[1]) + amm)
  91.             file.writeLine(fileData[2])
  92.             file.close()
  93.             return true, amm
  94.         else
  95.             local suc2, res2 = deposit(acc2, amm, "")
  96.             if (suc2) then
  97.                 return false, "withdraw refund worked"
  98.             else
  99.                 return false, "withdraw fail refund failed"
  100.             end
  101.         end
  102.     end
  103.     return false, res
  104. end
  105.  
  106. function create(acc, atm, pin)
  107.     file = fs.open("saves/".. acc, "r")
  108.     if file == nil then
  109.         file = fs.open("saves/".. acc, "a")
  110.         file.writeLine(0)
  111.         file.writeLine(pin)
  112.         return true, acc
  113.     else
  114.         file.close()
  115.         return false, "already an account"
  116.     end
  117. end
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement