Advertisement
Guest User

a

a guest
Sep 18th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. local chans = {
  2.   atm = 1,
  3.   db = 2,
  4. }
  5.  
  6. local common = dofile("/disk/common")
  7. local blake = dofile("/disk/blake")
  8.  
  9. local modem = peripheral.find("modem")
  10. modem.open(chans.atm)
  11.  
  12. local function sendCmd(...)
  13.   modem.transmit(chans.db, chans.atm, {...})
  14.   local resp = common.waitForMsg(chans.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.  
  28. while true do
  29.   term.clear()
  30.   term.setCursorPos(1,1)
  31.   print("1. Register\n2. Login\n3. Exit")
  32.   write("> ")
  33.   local mode = tonumber(read())
  34.  
  35.   term.clear()
  36.   term.setCursorPos(1,1)
  37.  
  38.   if mode == 1 then
  39.     print("Register an account")
  40.     write("Username: ")
  41.     local username = read()
  42.     write("Password: ")
  43.     local password = read("*")
  44.     local salt = sendCmd("register", username)
  45.     if salt and salt[1] then
  46.       local hash = blake.digest(password, salt[2])
  47.       sendCmd(true, hash)
  48.       print("Account created!")
  49.     end
  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 = blake.digest(password, salt[2])
  61.       local resp = sendCmd(true, hash)
  62.       if resp and resp[1] then
  63.         while true do
  64.           term.clear() term.setCursorPos(1,1)
  65.           print("Logged in as "..username)
  66.           print("1. Check Balance\n2. Transfer money\n3. Exit")
  67.           write("> ")
  68.           local mode = tonumber(read())
  69.           if mode == 1 then
  70.             local bal = sendCmd("balance", username, hash)
  71.             if bal and bal[1] then
  72.               print("Your current balance is "..bal[2])
  73.             end
  74.           elseif mode == 2 then
  75.             write("Recipient: ")
  76.             local recip = read()
  77.             write("Amount: ")
  78.             local amt = read()
  79.             local newbal = sendCmd("transfer", username, hash, recip, amt)
  80.             if newbal and newbal[1] then
  81.               print(amt.." has been sent to "..recip)
  82.               print("Your balance is now "..newbal[2])
  83.             end
  84.           elseif mode == 3 then print("Logged off") break
  85.           end
  86.           print("Press any key to continue")
  87.           os.pullEvent("key")
  88.         end
  89.       end
  90.     end
  91.     sleep(2)
  92.   elseif mode == 3 then break
  93.   end
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement