ThommyCraft

client

Mar 3rd, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.38 KB | None | 0 0
  1. --[[
  2.     CC-Bank
  3.     Copyright © 2012  Yingtong Li
  4.  
  5.     CC-Bank is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     CC-Bank is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with CC-Bank.  If not, see <http://www.gnu.org/licenses/>.
  17. --]]
  18.  
  19. os.loadAPI("crypt")
  20. os.loadAPI("gui")
  21.  
  22. local textStart = gui.textStart
  23. local modemSide = "back"
  24. local diskSide = "right"
  25. local serverId = 1
  26. local withdrawId = 2
  27. local version = "CC-Bank-0.2.0-Compatible"
  28.  
  29. -- Import GUI functions
  30. local drawHeader, drawButtons, drawButton, drawError, center = gui.drawHeader, gui.drawButtons, gui.drawButton, gui.drawError, gui.center
  31. local waitForMouse, waitForButton, waitForChar, waitForKey, waitForEnter = gui.waitForMouse, gui.waitForButton, gui.waitForChar, gui.waitForKey, gui.waitForEnter
  32. local waitForDisk = gui.waitForDisk
  33.  
  34. function otherAmount()
  35.     local amount = ""
  36.     term.clear()    drawHeader()
  37.     term.setCursorPos(center(textStart, "Amount : MC$   ") + 12, textStart)
  38.     return read()
  39. end
  40.  
  41. function balance(acc, pin)
  42.     term.clear()    drawHeader()    drawButtons()
  43.  
  44.     drawButton(4, "Cancel")
  45.     drawButton(5, "MC Dollars")
  46.     drawButton(6, "Gold Ingots")
  47.  
  48.     local mult = nil
  49.     while true do
  50.         local button = waitForButton()
  51.         if     button == 4 then     return
  52.         elseif button == 5 then     mult = 1        break
  53.         elseif button == 6 then     mult = 1/100    break
  54.         end
  55.     end
  56.  
  57.     local senderId, response = nil, nil
  58.     rednet.open(modemSide)
  59.  
  60.     rednet.send(serverId, version .. textutils.serialize( { "BALANCE", acc, crypt.hashPassword(pin) } ) )
  61.  
  62.     while true do
  63.         senderId, response = rednet.receive(5)
  64.         if senderId == serverId then    break   end
  65.     end
  66.     rednet.close(modemSide)
  67.  
  68.     term.clear()    drawHeader()    drawButtons()
  69.     drawButton(8, "OK")
  70.  
  71.     if response ~= "-" then
  72.         local displayBalance = math.floor(tonumber(response) * mult * 100) / 100
  73.         center(textStart, "Balance: " .. displayBalance)
  74.     else
  75.         drawError("Transaction Error")
  76.     end
  77.  
  78.     while true do
  79.         local button = waitForButton()
  80.         if     button == 8 then     return      end
  81.     end
  82. end
  83.  
  84. function transfer(acc, pin)
  85.     term.clear()    drawHeader()    drawButtons()
  86.  
  87.     drawButton(1, "MC$100")
  88.     drawButton(2, "MC$200")
  89.     drawButton(3, "MC$500")
  90.     drawButton(5, "MC$1000")
  91.     drawButton(6, "MC$2000")
  92.     drawButton(7, "MC$5000")
  93.  
  94.     drawButton(4, "Cancel")
  95.     drawButton(8, "Other Amount")
  96.  
  97.     local amountS = nil
  98.     while true do
  99.         local button = waitForButton()
  100.         if     button == 1 then     amountS = "100"         break
  101.         elseif button == 2 then     amountS = "200"         break
  102.         elseif button == 3 then     amountS = "500"         break
  103.         elseif button == 5 then     amountS = "1000"        break
  104.         elseif button == 6 then     amountS = "2000"        break
  105.         elseif button == 7 then     amountS = "5000"        break
  106.         elseif button == 4 then     return
  107.         elseif button == 8 then     amountS = otherAmount() break
  108.         end
  109.     end
  110.  
  111.     term.clear()    drawHeader()    drawButtons()
  112.  
  113.     local amount = tonumber(amountS)
  114.     if amount ~= nil and (amount % 1) == 0 then
  115.         term.clear()    drawHeader()
  116.         term.setCursorPos(center(textStart, "Amount : MC$   ") + 12, textStart)
  117.         term.write(amountS)
  118.         term.setCursorPos(center(textStart + 1, "Account :        ") + 10, textStart + 1)
  119.         local account = read()
  120.  
  121.         drawButtons()
  122.         drawButton(4, "Cancel")
  123.         drawButton(8, "Confirm")
  124.  
  125.         while true do
  126.             local button = waitForButton()
  127.             if     button == 4 then     return
  128.             elseif button == 8 then     break
  129.             end
  130.         end
  131.  
  132.         local senderId, response = nil, nil
  133.         rednet.open(modemSide)
  134.  
  135.         rednet.send(serverId, version .. textutils.serialize( { "TRANSFER", acc, crypt.hashPassword(pin), account, amountS } ) )
  136.  
  137.         while true do
  138.             senderId, response = rednet.receive(5)
  139.             if senderId == serverId then    break   end
  140.         end
  141.         rednet.close(modemSide)
  142.  
  143.         term.clear()    drawHeader()    drawButtons()
  144.  
  145.         if response ~= "-" then
  146.             center(textStart, "Transfer Successful")
  147.         else
  148.             drawError("Transaction Error")
  149.         end
  150.  
  151.     else
  152.         term.clear()    drawHeader()    drawButtons()
  153.         drawError("Invalid Amount")
  154.  
  155.         --[[
  156.         while true do
  157.             local button = waitForButton()
  158.             if     button == 8 then     return      end
  159.         end
  160.         --]]
  161.     end
  162.  
  163.     drawButton(8, "OK")
  164.     while true do
  165.         local button = waitForButton()
  166.         if     button == 8 then     return      end
  167.     end
  168. end
  169.  
  170. function withdraw(acc, pin)
  171.     term.clear()    drawHeader()    drawButtons()
  172.  
  173.     drawButton(1, "MC$100")
  174.     drawButton(2, "MC$200")
  175.     drawButton(3, "MC$500")
  176.     drawButton(5, "MC$1000")
  177.     drawButton(6, "MC$2000")
  178.     drawButton(7, "MC$5000")
  179.  
  180.     drawButton(4, "Cancel")
  181.     drawButton(8, "Other Amount")
  182.  
  183.     local amountS = nil
  184.     while true do
  185.         local button = waitForButton()
  186.         if     button == 1 then     amountS = "100"         break
  187.         elseif button == 2 then     amountS = "200"         break
  188.         elseif button == 3 then     amountS = "500"         break
  189.         elseif button == 5 then     amountS = "1000"        break
  190.         elseif button == 6 then     amountS = "2000"        break
  191.         elseif button == 7 then     amountS = "5000"        break
  192.         elseif button == 4 then     return
  193.         elseif button == 8 then     amountS = otherAmount() break
  194.         end
  195.     end
  196.  
  197.     term.clear()    drawHeader()    drawButtons()
  198.  
  199.     local amount = tonumber(amountS)
  200.     if amount ~= nil and (amount % 100) == 0 then
  201.         term.clear()    drawHeader()
  202.         term.setCursorPos(center(textStart, "Amount : MC$   ") + 12, textStart)
  203.         term.write(amountS)
  204.         drawButtons()
  205.         drawButton(4, "Cancel")
  206.         drawButton(8, "Confirm")
  207.  
  208.         while true do
  209.             local button = waitForButton()
  210.             if     button == 4 then     return
  211.             elseif button == 8 then     break
  212.             end
  213.         end
  214.  
  215.         local senderId, response = nil, nil
  216.         rednet.open(modemSide)
  217.  
  218.         rednet.send(serverId, version .. textutils.serialize( { "WITHDRAW", acc, crypt.hashPassword(pin), amountS } ) )
  219.  
  220.         while true do
  221.             senderId, response = rednet.receive(5)
  222.             if senderId == serverId then    break   end
  223.         end
  224.         rednet.close(modemSide)
  225.  
  226.         term.clear()    drawHeader()
  227.  
  228.         if response ~= "-" then
  229.             center(textStart, "Withdrawing " .. (amount / 100) .. " gold ingots")
  230.             rednet.open(modemSide)
  231.             rednet.send(withdrawId, version .. textutils.serialize( { "WITHDRAW", (amount/100) .. "" } ) )
  232.            
  233.             while true do
  234.                 senderId, response = rednet.receive(5)
  235.                 if senderId == withdrawId then  break   end
  236.             end
  237.             rednet.close(modemSide)
  238.            
  239.             term.clear()    drawHeader()
  240.             if response ~= "-" then
  241.                 center(textStart, "Please take your cash")
  242.                 sleep(2)
  243.                 return
  244.             else
  245.                 drawButtons()
  246.                 drawError("Withdrawal Error")
  247.             end
  248.         else
  249.             drawError("Transaction Error")
  250.         end
  251.  
  252.     else
  253.         term.clear()    drawHeader()    drawButtons()
  254.         drawError("Invalid Amount")
  255.  
  256.         --[[
  257.         while true do
  258.             local button = waitForButton()
  259.             if     button == 8 then     return      end
  260.         end
  261.         --]]
  262.     end
  263.  
  264.     drawButton(8, "OK")
  265.     while true do
  266.         local button = waitForButton()
  267.         if     button == 8 then     return      end
  268.     end
  269. end
  270.  
  271. function bank(acc, pin)
  272.     while true do
  273.         term.clear()    drawHeader()    drawButtons()
  274.  
  275.         drawButton(4, "Return Card")
  276.         drawButton(5, "Balance Check")
  277.         drawButton(6, "Withdrawal")
  278.         drawButton(7, "Deposit")
  279.         drawButton(8, "Transfer")
  280.  
  281.         local button = waitForButton()
  282.         if     button == 4 then     return
  283.         elseif button == 5 then     balance(acc, pin)
  284.         elseif button == 6 then     withdraw(acc, pin)
  285.         --elseif button == 7 then       deposit(acc, pin)
  286.         elseif button == 8 then     transfer(acc, pin)
  287.         end
  288.     end
  289. end
  290.  
  291. function begin()
  292.     local acc, pin
  293.  
  294.     term.clear()    drawHeader()
  295.  
  296.     center(textStart, "Insert keycard or press ENTER")
  297.     center(textStart + 1, "to transact without a keycard")
  298.     parallel.waitForAny(waitForEnter, waitForDisk)
  299.  
  300.     term.clear()    drawHeader()
  301.  
  302.     term.setCursorPos(center(textStart, "Account No :           ") + 13, textStart)
  303.  
  304.     if disk.hasData(diskSide) then
  305.         acc = disk.getLabel(diskSide)
  306.         term.write(acc)
  307.     else
  308.         acc = read()
  309.     end
  310.  
  311.     term.setCursorPos(center(textStart + 1, "PIN :    ") + 6, textStart + 1)
  312.     pin = read("*")
  313.  
  314.     bank(acc, pin)
  315.  
  316.     term.clear()    drawHeader()
  317.    
  318.     disk.eject(diskSide)
  319.     center(textStart, "Thankyou for banking with CBM")
  320.     center(textStart + 1, "Please take your keycard")
  321.  
  322.     sleep(2)
  323. end
  324.  
  325. while true do
  326.     begin()
  327. end
Add Comment
Please, Sign In to add comment