Advertisement
Guest User

startup

a guest
Sep 2nd, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 KB | None | 0 0
  1. relay = 76
  2. server = 75
  3. status = 84
  4. atmb = 69
  5. atmno = 1
  6. atmprot = "atm"..atmno
  7. rednet.open("top")
  8. table1 = {}
  9. receiptvar = {}
  10. table2 = {}
  11. table3 = {}
  12. printer = peripheral.wrap("back")
  13. function receive()
  14.   senderId, message, protocol = rednet.receive("bank")
  15. end
  16.  
  17. function deposit()
  18.   deptable = {"deposit"}
  19.   rednet.send(atmb,deptable,atmprot)
  20.   senderId, message, protocol = rednet.receive(atmprot,7)
  21.   term.setCursorPos(15,15)
  22.   if senderId == atmb then
  23.     if message[1] == "confirmed" then
  24.       print(message[2].." added to your account")
  25.       bal = bal + message[2]
  26.       table.insert(receiptvar,1,"deposit "..message[2])
  27.     else
  28.       print(message[1])
  29.     end
  30.   end
  31.   os.sleep(1)
  32. end
  33.  
  34. function finish()
  35.   signout = true
  36.   --receipt
  37.   if receiptvar[1] ~= nil then
  38.     printer.newPage()
  39.     w, h = printer.getPageSize()
  40.     printer.setCursorPos(1,1)
  41.     printer.write("x------<ChockBank>------x")
  42.     printer.setCursorPos(1,h)
  43.     printer.write("x-----------------------x")
  44.     height = h
  45.     while h > 2 do
  46.       printer.setCursorPos(1,h - 1)
  47.       printer.write("|                       |")
  48.       h = h - 1
  49.     end
  50.     receipty = 1
  51.     testvar = {"15","14"}
  52.     while receiptvar[receipty] ~= nil do
  53.       printer.setCursorPos(5,receipty + 2)
  54.       printer.write(receiptvar[receipty])
  55.       receipty = receipty + 1
  56.     end
  57.     file = io.open("receiptno","r")
  58.     receiptno = file:read()
  59.     file:close()
  60.     fs.delete("receiptno")
  61.     file = io.open("receiptno","w")
  62.     contents = tonumber(receiptno) + 1
  63.     file:write(tostring(contents))
  64.     file:close()
  65.     receiptid = atmprot..tonumber(receiptno)
  66.     printer.setCursorPos(3, height - 1)
  67.     printer.write(receiptid.."  "..username)
  68.     printer.setPageTitle("receipt-"..receiptid)
  69.     printer.endPage()
  70.     file = io.open("receipts/"..receiptid,"w")
  71.     table.insert(receiptvar,1,username)
  72.     file:write(textutils.serialize(receiptvar))
  73.     file:close()
  74.     receiptmes = {75,username,"receipt",receiptvar,receiptid}
  75.     rednet.send(76,receiptmes,"bank")
  76.     receiptvar = {}
  77.     term.clear()
  78.     backdrop()
  79.     term.setCursorPos(13,7)
  80.     print("please take your receipt")
  81.   end
  82.   timeout = os.startTimer(5)
  83.   os.sleep(2)
  84.   os.reboot()
  85. end
  86.  
  87. function withdraw()
  88.   term.clear()
  89.   backdrop()
  90.   term.setCursorPos(15,6)
  91.   print("Enter Value:")
  92.   table2 = {}
  93.   term.setCursorPos(27,6)
  94.   amount = tonumber(read())
  95.   term.setCursorPos(27,6)
  96.   if amount == nil then
  97.     print("invalid value")
  98.   end
  99.   if (bal > amount or bal == amount) and amount ~= nil then
  100.     table2 = {"withdraw",amount}
  101.     rednet.send(atmb,table2,atmprot)
  102.     senderId, message, protocol = rednet.receive(atmprot,7)
  103.     if message == nil then
  104.       print("error")
  105.     end
  106.     if senderId == atmb and message[1] == "confirmed" then
  107.       print("Confirmed")
  108.       table3 = {}
  109.       table3 = {server,username,"withdraw",password,amount}
  110.       table.insert(receiptvar,1,"withdraw "..amount)
  111.       rednet.send(relay,table3,"bank")
  112.       bal = bal - amount
  113.     end
  114.     if senderId == atmb and message[1] == "denied" then
  115.       print("Too large, maximum:"..message[2])
  116.     end
  117.   else
  118.     print("Insufficient funds")
  119.   end
  120. end
  121.  
  122. function atmselect()
  123.   term.setCursorPos(1,6)
  124.   print("|       x-----------x         x-----------x")    
  125.   print("|       | Withdraw  |         |  Deposit  |")
  126.   print("|       x-----------x         x-----------x")
  127.   term.setCursorPos(1,10)
  128.   print("|   x-----------------x       x-----------x")
  129.   print("|   | Balance:        |       |  Finish   |")
  130.   print("|   x-----------------x       x-----------x")
  131.   term.setCursorPos(16,11)
  132.   print(bal)
  133. end
  134.  
  135. function atm()
  136.  
  137.   signout = false
  138.   while signout == false do
  139.     term.clear()
  140.     backdrop()
  141.     atmselect()
  142.     event, button, x, y = os.pullEvent("mouse_click")
  143.     if x > 10 and x < 20 then
  144.       if y == 7 then
  145.         withdraw()
  146.       end
  147.     end
  148.     if x > 32 and x < 42 then
  149.       if y == 7 then
  150.         deposit()
  151.       end
  152.       if y == 11 then
  153.         finish()
  154.       end
  155.     end
  156.     os.sleep(1)
  157.   end
  158. end
  159.  
  160. function signin()
  161.   term.clear()
  162.   backdrop()
  163.   term.setCursorPos(17,9)
  164.   print("Enter Username:")
  165.   term.setCursorPos(17,10)
  166.   print("_______________")
  167.   term.setCursorPos(17,10)
  168.   username = read()
  169.   table1 = {server,username,"signin"}
  170.   rednet.send(relay,table1,"bank")
  171.   local senderId, message, protocol = rednet.receive("bank")
  172.   if message[3] == "confirmed" then
  173.     term.setCursorPos(17,9)
  174.     print("Enter Passphrase:")
  175.     term.setCursorPos(17,10)
  176.     print("__________________")
  177.     term.setCursorPos(17,10)
  178.     password = read("*")
  179.     table1 = {server,username,"signin2",password}
  180.     rednet.send(relay,table1,"bank")
  181.     message[3] = nil
  182.     senderId, message, protocol = rednet.receive("bank")
  183.     if message[3] == "confirmed" and username ~= "" then
  184.       bal = tonumber(message[4])
  185.       os.sleep(0.2)
  186.       atm()
  187.     else
  188.       term.setCursorPos(17,10)
  189.       print("Incorrect passphrase")
  190.       os.sleep(1)
  191.     end
  192.   else
  193.     term.setCursorPos(17,10)
  194.     print("Invalid username      ")
  195.     os.sleep(1)
  196.   end
  197. end
  198.  
  199. function backdrop()
  200.   term.setCursorPos(1,1)
  201.   print("x------------<ChockBank Cash Machine>-------------x")
  202.   backdropn = 0
  203.   while backdropn < 16 do
  204.     term.setCursorPos(1,backdropn + 2)
  205.     print("|")
  206.     term.setCursorPos(51,backdropn + 2)
  207.     print("|")
  208.     backdropn = backdropn + 1
  209.   end
  210.   print("x-------------------------------------------------x")
  211. end
  212. local timeout = os.startTimer(5)
  213. while true do
  214.   statvar = {"idle",atmid}
  215.   statmes = {status,statvar}
  216.   term.clear()
  217.   backdrop()
  218.   term.setCursorPos(15,9)
  219.   if printer.getInkLevel() ~= 0 and printer.getPaperLevel() ~= 0 then
  220.     print("Press Any Key To Begin >")
  221.     event = {os.pullEvent()}
  222.     if event[1] == "key" then
  223.       os.sleep(0.2)
  224.       signin()
  225.     elseif event[1] == "timer" and event[2] == timeout then
  226.       timeout = os.startTimer(5)  
  227.       rednet.send(relay,statmes,"bank")
  228.     end
  229.   else
  230.     print("   Out of ink/paper")
  231.     os.sleep(1)
  232.   end
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement