Advertisement
Doug4347

ecoSystem ATM

Aug 8th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.39 KB | None | 0 0
  1. serverID = 0
  2.  
  3. creditItem = "CreditBill50"
  4.  
  5. creditPerItem = 50
  6. creditPerKEU = 10
  7. pMan = "powaMan"
  8.  
  9. depositExtractor = 18
  10. bankDepositer = 19
  11. bankExtractor = 20
  12.  
  13. withdrawalExtractor = 21
  14.  
  15. depositBox = peripheral.wrap("chest_1")
  16. bankBox = peripheral.wrap("chest_3")
  17.  
  18. depositSize = tonumber(depositBox.getInventorySize())
  19. bankSize = tonumber(bankBox.getInventorySize())
  20.  
  21. peripherals = peripheral.getNames()
  22. periph = true
  23. index = 1
  24. while periph do
  25.     periph = peripherals[index]
  26.     if periph then
  27.         periphType = peripheral.getType(periph)
  28.         if periphType == "modem" then
  29.             rednet.open(periph)
  30.         end
  31.     end
  32.     index = index + 1
  33. end
  34.  
  35. local info = { ... }
  36.  
  37. while true do
  38.     shell.run("clear")
  39.     print("Please enter one of the following options:")
  40.     print("Withdraw")
  41.     print("Deposit")
  42.     print("View Balance")
  43.     print("Transfer")
  44.     print("Register")
  45.     print("Buy")
  46.     option = read()
  47.     info[1] = option
  48.     if option == "Register" then
  49.         shell.run("clear")
  50.         print("Please enter a username:")
  51.         info[2] = read()
  52.         if info[2] then
  53.             print("Please enter a password:")
  54.             info[3] = read('*')
  55.             if info[3] then
  56.                 rednet.send(serverID, info, "reg")
  57.                 id, servInfo, servProtocol = rednet.receive()
  58.                 if servProtocol == "failure" then
  59.                     print("Failed to register new account. Reason:")
  60.                     print(servInfo[1])
  61.                     os.sleep(5)
  62.                 else
  63.                     print("Account successfully registered.")
  64.                     os.sleep(5)
  65.                 end
  66.             else
  67.                 print("Please try again and enter a vaild password.")
  68.                 os.sleep(5)
  69.             end
  70.         else
  71.             print("Please try again and enter a vaild username.")
  72.             os.sleep(5)
  73.         end
  74.     elseif (option == "Withdraw") or (option == "Deposit") or (option == "View Balance") or (option == "Transfer") or (option == "Buy") then
  75.         shell.run("clear")
  76.         print("Please enter your username:")
  77.         info[2] = read()
  78.         print("Please enter your password:")
  79.         info[3] = read('*')
  80.         rednet.send(serverID, info, "login")
  81.         id, servInfo, servProtocol = rednet.receive()
  82.         if servProtocol == "success" then
  83.             loggedIn = true
  84.         else
  85.             print("Failed to log in. Reason:")
  86.             print(servInfo[1])
  87.             sleep(5)
  88.         end
  89.     else
  90.         print("Please enter a vaild option.")
  91.         sleep(5)
  92.     end
  93.     if loggedIn then
  94.         loggedIn = false
  95.         if option == "Withdraw" then
  96.             info[1] = "View Balance"
  97.             rednet.send(serverID, info, "balCheck")
  98.             id, servInfo, servProtocol = rednet.receive()
  99.             Bal = servInfo[1]
  100.             info[1] = "Withdraw"
  101.             print("Please enter the amount you wish to withdraw (must be multiples of ".. creditPerItem.. ":")
  102.             withdrawalAmount = read()
  103.             if withdrawalAmount > Bal then
  104.                 print("Sorry, but you do not have that amount in your bank account. Your current balance is: ".. Bal)
  105.             else
  106.                 newBal = Bal - withdrawalAmount
  107.                 info[4] = newBal
  108.                 rednet.send(serverID, info, "withdrawal")
  109.                 info2 = withdrawalAmount/creditPerItem
  110.                 rednet.send(withdrawalExtractor, info2, "withdrawal")
  111.                 print("Your withdrawl was successful, thank you for banking with us.")
  112.                 sleep(10)
  113.             end
  114.         elseif option == "Deposit" then
  115.             info[1] = "View Balance"
  116.             rednet.send(serverID, info, "balCheck")
  117.             id, servInfo, servProtocol = rednet.receive()
  118.             oldBal = servInfo[1]
  119.             info[1] = "Deposit"
  120.             shell.run("clear")
  121.             print("Please place the items in the chest to the right.")
  122.             while not itemsInDepositBox do
  123.                 totalQty = 0
  124.                 loop = 0
  125.                 while loop < depositSize do
  126.                     loop = loop + 1
  127.                     num = depositBox.getStackInSlot(loop)
  128.                     if num ~= nil then
  129.                         if num.name == creditItem then
  130.                             totalQty = totalQty + num.qty
  131.                         end
  132.                     end
  133.                     if totalQty > 0 then
  134.                         itemsInDepositBox = true
  135.                     end
  136.                     os.sleep(0.1)
  137.                 end
  138.                 os.sleep(0.1)
  139.             end
  140.             rednet.send(depositExtractor, "true")
  141.             shell.run("clear")
  142.             print("Transfering items, please wait...")
  143.             while itemsInDepositBox do
  144.                 loop = 0
  145.                 totalQty = 0
  146.                 while loop < depositSize do
  147.                     loop = loop + 1
  148.                     num = depositBox.getStackInSlot(loop)
  149.                     if num ~= nil then
  150.                         if num.name == creditItem then
  151.                             totalQty = totalQty + num.qty
  152.                         end
  153.                     end
  154.                     if totalQty == 0 then
  155.                         loop = 0
  156.                         while loop < depositSize do
  157.                             loop = loop + 1
  158.                             num = depositBox.getStackInSlot(loop)
  159.                             if num ~= nil then
  160.                                 if num.name == creditItem then
  161.                                     totalQty = totalQty + num.qty
  162.                                 end
  163.                             end
  164.                         end
  165.                         if totalQty == 0 then
  166.                             itemsInDepositBox = false
  167.                         end
  168.                     end
  169.                     os.sleep(0.1)
  170.                 end
  171.                 os.sleep(0.1)
  172.             end
  173.             rednet.send(depositExtractor, "false")
  174.             os.sleep(10)
  175.             totalQty = 0
  176.             loop = 0
  177.             while loop < bankSize do
  178.                 loop = loop + 1
  179.                 num = bankBox.getStackInSlot(loop)
  180.                 if num ~= nil then
  181.                     if num.name == creditItem then
  182.                         totalQty = totalQty + num.qty
  183.                     end
  184.                 end
  185.             end
  186.             shell.run("clear")
  187.             print("Doing the math...")
  188.             addedCredit = creditPerItem*totalQty
  189.             newBal = oldBal + addedCredit
  190.             shell.run("clear")
  191.             print("ecoATM has counted ".. totalQty.. " Items, and your account will be credited with ".. addedCredit.. " credits, is this correct? (y/n)")
  192.             invalidInput = true
  193.             while invalidInput do
  194.                 ATMInput = read()
  195.                 if ATMInput == "y" or ATMInput == "n" then
  196.                     invalidInput = false
  197.                 else
  198.                     print("Invalid input, please enter either \"n\" (No) or \"y\" (Yes) to continue.")
  199.                 end
  200.             end
  201.             if ATMInput == "y" then
  202.                 info[4] = addedCredit
  203.                 info[5] = newBal
  204.                 rednet.send(serverID, info, "deposit")
  205.                 id, servInfo, servProtocol = rednet.receive()
  206.                 print("Your final balance is: $".. newBal)
  207.                 print("Thank you for banking with us.")
  208.                 rednet.send(bankDepositer, "true")
  209.                 itemsInBankBox = true
  210.                 while itemsInBankBox do
  211.                     loop = 0
  212.                     totalQty = 0
  213.                     while loop < bankSize do
  214.                         loop = loop + 1
  215.                         num = bankBox.getStackInSlot(loop)
  216.                         if num ~= nil then
  217.                             if num.name == creditItem then
  218.                                 totalQty = totalQty + num.qty
  219.                             end
  220.                         end
  221.                         if totalQty == 0 then
  222.                             loop = 0
  223.                             while loop < bankSize do
  224.                                 loop = loop + 1
  225.                                 num = bankBox.getStackInSlot(loop)
  226.                                 if num ~= nil then
  227.                                     if num.name == creditItem then
  228.                                         totalQty = totalQty + num.qty
  229.                                     end
  230.                                 end
  231.                             end
  232.                             if totalQty == 0 then
  233.                                 itemsInBankBox = false
  234.                             end
  235.                         end
  236.                         os.sleep(0.1)
  237.                     end
  238.                     os.sleep(0.1)
  239.                 end
  240.                 rednet.send(bankDepositer, "false")
  241.                 os.sleep(5)
  242.             else
  243.                 print("Transaction canceled, refunding now.")
  244.                 rednet.send(bankExtractor, "true")
  245.                 itemsInBankBox = true
  246.                 while itemsInBankBox do
  247.                     loop = 0
  248.                     totalQty = 0
  249.                     while loop < bankSize do
  250.                         loop = loop + 1
  251.                         num = bankBox.getStackInSlot(loop)
  252.                         if num ~= nil then
  253.                             if num.name == creditItem then
  254.                                 totalQty = totalQty + num.qty
  255.                             end
  256.                         end
  257.                         if totalQty == 0 then
  258.                             loop = 0
  259.                             while loop < bankSize do
  260.                                 loop = loop + 1
  261.                                 num = bankBox.getStackInSlot(loop)
  262.                                 if num ~= nil then
  263.                                     if num.name == creditItem then
  264.                                         totalQty = totalQty + num.qty
  265.                                     end
  266.                                 end
  267.                             end
  268.                             if totalQty == 0 then
  269.                                 itemsInBankBox = false
  270.                             end
  271.                         end
  272.                         os.sleep(0.1)
  273.                     end
  274.                     os.sleep(0.1)
  275.                 end
  276.                 os.sleep(5)
  277.                 rednet.send(bankExtractor, "false")
  278.                 print("Successfully refunded. We hope to see you again soon!")
  279.                 os.sleep(5)
  280.             end
  281.            
  282.         elseif option == "View Balance" then
  283.             shell.run("clear")
  284.             rednet.send(serverID, info, "balCheck")
  285.             id, servInfo, servProtocol = rednet.receive()
  286.             print("User:".. info[2])
  287.             print("Bal :".. servInfo[1])
  288.             os.sleep(10)
  289.         elseif option == "Transfer" then
  290.             shell.run("clear")
  291.             print("Please enter the name of the account you would like to send money to:")
  292.             sendAcc = read()
  293.             print("Please enter the amount of money you would like to send:")
  294.             sendAmount = read()
  295.             info[4] = sendAcc
  296.             info[5] = sendAmount
  297.             rednet.send(serverID, info, "transfer")
  298.             id, servInfo, servProtocol = rednet.receive()
  299.             if servProtocol == "failure" then
  300.                 print("Failed to send funds. Reason:")
  301.                 print(servInfo[1])
  302.             else
  303.                 print("Successfully transfered all funds.")
  304.             end
  305.             os.sleep(10)
  306.         elseif option == "Buy" then
  307.             print("What would you like to buy? Please enter:")
  308.             print("Electric Units (EU)")
  309.             print("Water")
  310.             buyOption = read()
  311.             info[1] = buyOption
  312.             info[1] = "View Balance"
  313.             rednet.send(serverID, info, "balCheck")
  314.             id, servInfo, servProtocol = rednet.receive()
  315.             Bal = tonumber(servInfo[1])
  316.             info[1] = buyOption
  317.             if buyOption == "Electric Units (EU)" then
  318.                 shell.run("clear")
  319.                 print("Please enter the account ID you wish to purchace power from.")
  320.                 accID = tonumber(read())
  321.                 if not accID then
  322.                     print("Invalid Input. Please try again.")
  323.                     os.sleep(5)
  324.                 else
  325.                     print("How many kEU would you like to buy? (1 = 1000 units) (".. creditPerKEU.. " credits/kEU) (Bal: ".. Bal.." credits")
  326.                     kEU = read()
  327.                     if not kEU then
  328.                         print("Invalid Input. Please try again.")
  329.                         os.sleep(5)
  330.                     else
  331.                         cost = tonumber(kEU * creditPerKEU)
  332.                         info[4] = pMan
  333.                         info[5] = cost
  334.                         info[6] = kEU
  335.                         info[7] = accID
  336.                         shell.run("clear")
  337.                         print("This will cost you ".. cost.. " credits. Would you like to continue? (y/n)")
  338.                         invalidInput = true
  339.                         while invalidInput do
  340.                             ATMInput = read()
  341.                             if ATMInput == "y" or ATMInput == "n" then
  342.                                 invalidInput = false
  343.                             else
  344.                                 print("Invalid input, please enter either \"n\" (No) or \"y\" (Yes) to continue.")
  345.                             end
  346.                         end
  347.                         if ATMInput == "y" then
  348.                             if cost > Bal then
  349.                                 print("You have insufficient funds to pay for that much power. Please try again with a smaller amount, or deposit some credits.")
  350.                                 os.sleep(5)
  351.                             else
  352.                                 rednet.send(serverID, info, "buy")
  353.                                 id, servInfo, servProtocol = rednet.receive()
  354.                             end
  355.                         else
  356.                             print("Transaction canceled.")
  357.                             os.sleep(5)
  358.                         end
  359.                     end
  360.                 end
  361.             elseif buyOption == "Water" then
  362.                 shell.run("clear")
  363.                 print("You've successfully logged in, but I haven't done this part of the code yet. Sorry!")
  364.                 os.sleep(5)
  365.             else
  366.                 print("Invalid option. Please try again.")
  367.                 os.sleep(5)
  368.             end
  369.             -- rednet.send(serverID, info, "buy")
  370.             -- id, servInfo, servProtocol = rednet.receive()
  371.         end
  372.     end
  373. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement