Advertisement
Guest User

atm

a guest
Feb 3rd, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. local channels = {
  2.   atm = 1,
  3.   db = 2,
  4. }
  5.  
  6. os.loadAPI("/common")
  7. os.loadAPI("/sha256")
  8.  
  9. local modem = peripheral.find("modem")
  10. modem.open(channels.atm)
  11.  
  12. local function sendCmd(...)
  13.   modem.transmit(channels.db, channels.atm, {...})
  14.   local resp = common.waitForMsg(channels.db, 2)
  15.   if resp then
  16.     if resp[1] then
  17.       return resp
  18.     else
  19.       printError(resp[2])
  20.     end
  21.   else
  22.     printError("Timed out")
  23.   end
  24.   return resp
  25. end
  26.  
  27. while true do
  28.   term.clear()
  29.   term.setCursorPos(1,1)
  30.   print("1. Register\n2. Login\n3. Exit")
  31.   write("> ")
  32.   local mode = tonumber(read())
  33.  
  34.   term.clear()
  35.   term.setCursorPos(1,1)
  36.  
  37.   if mode == 1 then
  38.     print("Register an account")
  39.     write("Username: ")
  40.     local username = read()
  41.     write("Password: ")
  42.     local password = read("*")
  43.     local salt = sendCmd("register", username)
  44.     if salt and salt[1] then
  45.       local hash = sha256.pbkdf2(password, salt[2], 64):toHex()
  46.       sendCmd(true, hash)
  47.       print("Account created!")
  48.     end
  49.    
  50.     sleep(2)
  51.  
  52.   elseif mode == 2 then
  53.     print("Log-in into your account")
  54.     write("Username: ")
  55.     local username = read()
  56.     write("Password: ")
  57.     local password = read("*")
  58.     local salt = sendCmd("auth", username)
  59.     if salt and salt[1] then
  60.       local hash = sha256.pbkdf2(password, salt[2], 64):toHex()
  61.       local resp = sendCmd(true, hash)
  62.       if resp and resp[1] then
  63.         while true do
  64.           term.clear()
  65.           term.setCursorPos(1,1)
  66.           print("Logged in as "..username)
  67.           print("1. Check Balance\n2. Transfer money\n3. Deposit\n4. Withdraw\n5. Exit")
  68.           write("> ")
  69.           local mode = tonumber(read())
  70.           if mode == 1 then
  71.             local bal = sendCmd("balance", username, hash)
  72.             if bal and bal[1] then
  73.               print("Your current balance is "..bal[2])
  74.             end
  75.           elseif mode == 2 then
  76.             write("Recipient: ")
  77.             local recip = read()
  78.             write("Amount: ")
  79.             local amt = read()
  80.             local newbal = sendCmd("transfer", username, hash, recip, amt)
  81.             if newbal and newbal[1] then
  82.               print(amt.." has been sent to "..recip)
  83.               print("Your balance is now "..newbal[2])
  84.             end
  85.         elseif mode == 3 then print("place deposit into slot 1\n then press enter")
  86.          ent = os.pullEvent("key")
  87.          if ent== 28 then
  88.            turtle.getItemDetail("minecraft:coal")                  
  89.            amt = turtle.getItemCount(1)
  90.               sendCmd("add", username, hash, amt)
  91.          end
  92.          turtle.dropDown()
  93.          
  94.               elseif mode == 4 then print("Amount:")
  95.            amt=read()
  96.            sendCmd("balance", username, hash)
  97.            if bal >= amt then
  98.               sendCmd("withdraw", username, hash, amt)
  99.            elseif bal < amt then
  100.               print("insufficient funds")
  101.            end
  102.    
  103.         elseif mode == 5 then print("Logged off") break
  104.           end
  105.           print("Press any key to continue")
  106.           os.pullEvent("key")
  107.         end
  108.       end
  109.     end
  110.     sleep(2)
  111.   elseif mode == 3 then break
  112.   end
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement