Advertisement
HarleyCraft

CLIENT TKATM

Mar 31st, 2015
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.52 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2.  
  3. os.loadAPI("gui")
  4.  
  5. local function readLines(sPath)
  6.     local file = fs.open(sPath, "r")
  7.     if file then
  8.         local tLines = {}
  9.         local sLine = file.readLine()
  10.         while sLine do
  11.             table.insert(tLines, sLine)
  12.             sLine = file.readLine()
  13.         end
  14.         file.close()
  15.         return tLines
  16.   end
  17.   return nil
  18. end
  19.  
  20. function lerConfig(name, line)
  21.     local tLines = readLines(name)
  22.     if not tLines then
  23.         return nil
  24.     end
  25.     return tLines[line]
  26. end
  27.  
  28. -- Configs
  29. textStart = gui.textStart
  30. server = 359  --id do servidor
  31. modemSide = lerConfig("config.txt", 1)
  32. diskSide = lerConfig("config.txt", 2)
  33. sorterSide = lerConfig("config.txt", 3)
  34. chestSide = tonumber(lerConfig("config.txt", 4)) --lado do bau acessivel pelo player
  35. inputSide = tonumber(lerConfig("config.txt", 5)) --lado de entrada de itens DO sistema
  36. outputSide = tonumber(lerConfig("config.txt", 6)) -- lado de saida de itens PARA o sistema
  37. versao = "1.0_beta_R4"
  38. nconta = ""
  39. npin = ""
  40. -- Fim das Configs
  41.  
  42. -- Funçoes da GUI
  43. local desenharMenu, desenharBotoes, desenharBotao, desenharErro, center = gui.drawHeader, gui.drawButtons, gui.drawButton, gui.drawError, gui.center
  44. local waitForMouse, waitForButton, waitForChar, waitForKey, waitForEnter = gui.waitForMouse, gui.waitForButton, gui.waitForChar, gui.waitForKey, gui.waitForEnter
  45. local waitForDisk = gui.waitForDisk
  46.  
  47. -- Funções
  48.  
  49. local function limitRead(len, replaceChar)
  50.   len = len or 10
  51.   local input=""
  52.   local key = 0
  53.   term.setCursorBlink(true)
  54.   repeat
  55.         local e,p1 = os.pullEvent()
  56.         if e=="char" then
  57.           if #input < len then
  58.                 input = input .. p1
  59.                 term.write(replaceChar or p1)
  60.           end
  61.         elseif e=="key" and p1==keys.backspace and #input > 0 then
  62.           input = input:sub(1,#input-1)
  63.           local x,y = term.getCursorPos()
  64.           term.setCursorPos(x-1,y)
  65.           term.write(" ")
  66.           term.setCursorPos(x-1,y)
  67.         end
  68.   until p1==keys.enter
  69.   term.setCursorBlink(false)
  70.   return input
  71. end
  72.  
  73. function ler(name)
  74.     local file = name
  75.     file = fs.open(file,"r")
  76.    
  77.     if file then
  78.         file = file.readAll()
  79.         return file
  80.     else
  81.         print("Erro de leitura")
  82.     end
  83. end
  84.  
  85. function cartaoValido()
  86.     if fs.exists("/disk/numero.txt") then
  87.         return true
  88.     else
  89.         return false
  90.     end
  91. end
  92.  
  93. function netstat()
  94.     rednet.send(server, "ping")
  95.     recebi = false
  96.     now = os.time()
  97.    
  98.     while os.time() < now + 0.05 or recebi == true do
  99.     id, message = rednet.receive()
  100.         if message == "pong" then
  101.             recebi = true
  102.             return true
  103.         end
  104.     end
  105.     return false
  106. end
  107.  
  108. function getQuantidade(side, item, obj)
  109.     data = obj.list(side)
  110.     local c = 0;
  111.    
  112.     for i, j in pairs(data) do
  113.         if i == item then
  114.             c = j
  115.         end
  116.     end
  117.     return c
  118. end
  119.  
  120. function valor()
  121.     rednet.send(server, "valor")
  122.     recebi = false
  123.    
  124.     while recebi == false do
  125.     id, message = rednet.receive()
  126.         if id == server then
  127.             recebi = true
  128.             return message
  129.         end
  130.     end
  131. end
  132.  
  133. function otherAmount()
  134.     local amount = ""
  135.     term.clear()    desenharMenu()
  136.     term.setCursorPos(center(textStart, "Valor : B$   ") + 12, textStart)
  137.     return limitRead(20)
  138. end
  139.  
  140. function getUUID(id, meta)
  141.     uuid = id + meta * 32768
  142.     return uuid
  143. end
  144.  
  145. function deposit(amount, from, to)
  146.   sorter = peripheral.wrap(sorterSide)
  147.   sorter.extract(from, getUUID(27527, 0), to, amount)
  148. end
  149.  
  150. function valorM()
  151.     term.clear()    desenharMenu()  desenharBotoes()
  152.    
  153.     center(textStart, "O valor atual da moeda e " ..valor())
  154.    
  155.     desenharBotao(8, "OK")
  156.    
  157.     while true do
  158.         local button = waitForButton()
  159.         if     button == 8 then     return      end
  160.     end
  161. end
  162.  
  163. function mostrarSaldo()
  164.     term.clear()    desenharMenu()  desenharBotoes()
  165.    
  166.     rednet.send(server, "saldo_"..nconta.."_"..npin)
  167.     id, message = rednet.receive()
  168.     recebi = false
  169.    
  170.     while recebi == false do
  171.         if(id == server) then
  172.             recebi = true
  173.             center(textStart, "O saldo da sua conta e " ..message)
  174.         end
  175.     end
  176.    
  177.     desenharBotao(8, "OK")
  178.    
  179.     while true do
  180.         local button = waitForButton()
  181.         if     button == 8 then     return      end
  182.     end
  183. end
  184.  
  185. function transferir()
  186.     term.clear()    desenharMenu()  desenharBotoes()
  187.  
  188.     desenharBotao(1, "B$1")
  189.     desenharBotao(2, "B$2")
  190.     desenharBotao(3, "B$5")
  191.     desenharBotao(5, "B$10")
  192.     desenharBotao(6, "B$20")
  193.     desenharBotao(7, "B$50")
  194.  
  195.     desenharBotao(4, "Cancelar")
  196.     desenharBotao(8, "Outro Valor")
  197.  
  198.     local valorS = nil
  199.     while true do
  200.         local button = waitForButton()
  201.         if     button == 1 then     valorS = "1"            break
  202.         elseif button == 2 then     valorS = "2"            break
  203.         elseif button == 3 then     valorS = "5"            break
  204.         elseif button == 5 then     valorS = "10"           break
  205.         elseif button == 6 then     valorS = "20"           break
  206.         elseif button == 7 then     valorS = "50"           break
  207.         elseif button == 4 then     return
  208.         elseif button == 8 then     valorS = otherAmount()  break
  209.         end
  210.     end
  211.    
  212.     local valor = tonumber(valorS)
  213.     term.clear()    desenharMenu()  desenharBotoes()
  214.     term.setCursorPos(center(textStart, "Valor : B$   ") + 12, textStart)
  215.     term.write(valorS)
  216.     term.setCursorPos(center(textStart + 1, "Conta :        ") + 10, textStart + 1)
  217.     local to = limitRead(20)
  218.    
  219.     desenharBotao(4, "Cancelar")
  220.     desenharBotao(8, "Confirmar")
  221.    
  222.     while true do
  223.         local button = waitForButton()
  224.         if     button == 4 then     return
  225.         elseif button == 8 then     break
  226.         end
  227.     end
  228.    
  229.     if valor == nil or to == nil then
  230.         errou("Valor(es) incompativel(eis)")
  231.     end
  232.  
  233.     term.clear()    desenharMenu()  desenharBotoes()
  234.    
  235.     rednet.send(server, "transferir_"..nconta.."_"..npin.."_"..valor.."_"..to)
  236.     id, message = rednet.receive()
  237.     recebi = false
  238.    
  239.     while recebi == false do
  240.         if(id == server) then
  241.             recebi = true
  242.             if message == "Transferencia Aprovada" then
  243.                 center(textStart, "Transferencia Completa")
  244.                 os.sleep(2)
  245.                 if peripheral.isPresent("left") then
  246.                     imp = peripheral.wrap("left")
  247.                     imp.newPage()
  248.                     imp.setPageTitle("ATM-Transferencia")
  249.                     imp.write("  --- ATM ---")
  250.                     imp.setCursorPos(1,2)
  251.                     imp.write("Conta: "..nconta)
  252.                     imp.setCursorPos(1,3)
  253.                     imp.write("Valor: "..valor)
  254.                     imp.setCursorPos(1,4)
  255.                     imp.write("Para: "..to)
  256.                     imp.endPage()
  257.                 end
  258.             else
  259.                 errou("Houve um erro na Transferencia")
  260.             end
  261.         end
  262.     end
  263.     desenharBotao(8, "OK")
  264.    
  265.     while true do
  266.         local button = waitForButton()
  267.         if     button == 8 then     return      end
  268.     end
  269. end
  270.  
  271. function deposito()
  272.     term.clear()    desenharMenu()  desenharBotoes()
  273.    
  274.     desenharBotao(1, "B$1")
  275.     desenharBotao(2, "B$2")
  276.     desenharBotao(3, "B$5")
  277.     desenharBotao(5, "B$10")
  278.     desenharBotao(6, "B$20")
  279.     desenharBotao(7, "B$50")
  280.  
  281.     desenharBotao(4, "Cancelar")
  282.     desenharBotao(8, "Outro Valor")
  283.    
  284.     local valorS = nil
  285.     while true do
  286.         local button = waitForButton()
  287.         if     button == 1 then     valorS = "1"            break
  288.         elseif button == 2 then     valorS = "2"            break
  289.         elseif button == 3 then     valorS = "5"            break
  290.         elseif button == 5 then     valorS = "10"           break
  291.         elseif button == 6 then     valorS = "20"           break
  292.         elseif button == 7 then     valorS = "50"           break
  293.         elseif button == 4 then     return
  294.         elseif button == 8 then     valorS = otherAmount()  break
  295.         end
  296.     end
  297.    
  298.     sorter = peripheral.wrap(sorterSide)
  299.     local valor = tonumber(valorS)
  300.     term.clear()    desenharMenu()  desenharBotoes()
  301.    
  302.     term.setCursorPos(center(textStart, "Valor : B$   ") + 12, textStart)
  303.     term.write(valorS)
  304.     desenharBotao(4, "Cancelar")
  305.     desenharBotao(8, "Confirmar")
  306.    
  307.     while true do
  308.         local button = waitForButton()
  309.         if      button == 4 then    return
  310.         elseif  button == 8 then    break
  311.         end
  312.     end
  313.  
  314.     if valor == nil then
  315.         errou("Valor incompativel")
  316.     end
  317.  
  318.     term.clear()    desenharMenu()  desenharBotoes()
  319.    
  320.     if getQuantidade(5, 27527, sorter) < valor then
  321.         desenharErro("Voce colocou apenas "..getQuantidade(5, 27527, sorter))
  322.         term.setCursorPos(20, textStart + 1)
  323.         term.write(" e tentou depositar "..valor)
  324.         os.sleep(2)
  325.         disk.eject(diskSide)
  326.         nconta = ""
  327.         npin = ""
  328.         init()
  329.     end
  330.  
  331.     if valor > 64 then
  332.         errou("Voce so pode depositar 64 de uma vez")
  333.     end
  334.  
  335.     deposit(valor, chestSide, outputSide)
  336.     rednet.send(server, "atsaldo_"..nconta.."_"..npin.."_1_"..valor)
  337.    
  338.     center(textStart, "Deposito concluido")
  339.    
  340.     if peripheral.isPresent("left") then
  341.         imp = peripheral.wrap("left")
  342.         imp.newPage()
  343.         imp.setPageTitle("ATM-Deposito")
  344.         imp.write("  --- ATM ---")
  345.         imp.setCursorPos(1,2)
  346.         imp.write("Conta: "..nconta)
  347.         imp.setCursorPos(1,3)
  348.         imp.write("Valor: "..valor)
  349.         imp.endPage()
  350.     end
  351.  
  352.     desenharBotao(8, "OK")
  353.    
  354.     while true do
  355.         local button = waitForButton()
  356.         if     button == 8 then     return      end
  357.     end
  358. end
  359.  
  360. function saque()
  361.     term.clear()    desenharMenu()  desenharBotoes()
  362.    
  363.     desenharBotao(1, "B$1")
  364.     desenharBotao(2, "B$2")
  365.     desenharBotao(3, "B$5")
  366.     desenharBotao(5, "B$10")
  367.     desenharBotao(6, "B$20")
  368.     desenharBotao(7, "B$50")
  369.  
  370.     desenharBotao(4, "Cancelar")
  371.     desenharBotao(8, "Outro Valor")
  372.  
  373.     local valorS = nil
  374.     while true do
  375.         local button = waitForButton()
  376.         if     button == 1 then     valorS = "1"            break
  377.         elseif button == 2 then     valorS = "2"            break
  378.         elseif button == 3 then     valorS = "5"            break
  379.         elseif button == 5 then     valorS = "10"           break
  380.         elseif button == 6 then     valorS = "20"           break
  381.         elseif button == 7 then     valorS = "50"           break
  382.         elseif button == 4 then     return
  383.         elseif button == 8 then     valorS = otherAmount()  break
  384.         end
  385.     end
  386.    
  387.     local valor = tonumber(valorS)
  388.     term.clear()    desenharMenu()  desenharBotoes()
  389.     term.setCursorPos(center(textStart, "Valor : B$   ") + 12, textStart)
  390.     term.write(valorS)
  391.    
  392.     desenharBotao(4, "Cancelar")
  393.     desenharBotao(8, "Confirmar")
  394.    
  395.     while true do
  396.         local button = waitForButton()
  397.         if     button == 4 then     return
  398.         elseif button == 8 then     break
  399.         end
  400.     end
  401.  
  402.     if valor == nil then
  403.         errou("Valor incompativel")
  404.     end
  405.  
  406.     if valor > 64 then
  407.         errou("Voce so pode sacar 64 de uma vez")
  408.     end
  409.    
  410.     rednet.send(server, "saldo_"..nconta.."_"..pin)
  411.     id, message = rednet.receive()
  412.     recebi = false
  413.    
  414.     while recebi == false do
  415.         if(id == server) then
  416.             recebi = true
  417.             saldo = tonumber(message) or "erro"
  418.         end
  419.     end
  420.    
  421.     term.clear()    desenharMenu()  desenharBotoes()
  422.    
  423.     if valor > saldo then
  424.         errou("Voce nao tem dinheiro suficiente")
  425.     end
  426.    
  427.     rednet.send(server, "atsaldo_"..nconta.."_"..npin.."_0_"..valor)
  428.     deposit(valor, inputSide, chestSide)
  429.    
  430.     center(textStart, "Saque concluido, retire o seu dinheiro")
  431.    
  432.     if peripheral.isPresent("left") then
  433.         imp = peripheral.wrap("left")
  434.         imp.newPage()
  435.         imp.setPageTitle("ATM-Saque")
  436.         imp.write("  --- ATM ---")
  437.         imp.setCursorPos(1,2)
  438.         imp.write("Conta: "..nconta)
  439.         imp.setCursorPos(1,3)
  440.         imp.write("Valor: "..valor)
  441.         imp.endPage()
  442.     end
  443.     desenharBotao(8, "OK")
  444.    
  445.     while true do
  446.         local button = waitForButton()
  447.         if     button == 8 then     return      end
  448.     end
  449. end
  450.  
  451. function begin()
  452.     term.clear()
  453.     term.setBackgroundColor(colors.green)
  454.     term.setTextColor(colors.red)
  455.     center(10, "TKATM versao "..versao)
  456.     term.setBackgroundColor(colors.blue)
  457.     term.setTextColor(colors.white)
  458.     center(12, "Para criar uma conta fale com o Esquilo_Azul")
  459.     os.sleep(3)
  460.     term.setBackgroundColor(colors.black)
  461.     term.setTextColor(colors.white)
  462.     term.clear()
  463.     rednet.open(modemSide)
  464.     center(5, "Verificando Conexao com o servidor, aguarde...")
  465.  
  466.     if not netstat() == true then
  467.         center(5, "Servidor nao disponivel, tente mais tarde!")
  468.         os.sleep(5)
  469.         os.shutdown()
  470.     end
  471.    
  472.     init()
  473. end
  474.  
  475. function errou(msg)
  476.     term.clear()    desenharMenu()  desenharBotoes()
  477.  
  478.     desenharErro(msg)
  479.     disk.eject(diskSide)
  480.     nconta = ""
  481.     npin = ""
  482.     os.sleep(2)
  483.     init()
  484. end
  485.  
  486. function init()
  487.     term.clear()    desenharMenu()
  488.     rednet.open(modemSide)
  489.    
  490.     center(textStart, "Insira seu cartao ou aperte ENTER")
  491.     center(textStart + 1, "para usar o ATM sem cartao")
  492.     parallel.waitForAny(waitForEnter, waitForDisk)
  493.    
  494.     term.clear()    desenharMenu()
  495.    
  496.     term.setCursorPos(center(textStart, "No da Conta:           ") + 13, textStart)
  497.    
  498.     if cartaoValido() == true then
  499.         conta = ler("/disk/numero.txt")
  500.         term.write(conta)
  501.     else
  502.         conta = limitRead(20)
  503.     end
  504.    
  505.     term.setCursorPos(center(textStart + 1, "PIN :    ") + 6, textStart + 1)
  506.     pin = limitRead(20, "*")
  507.    
  508.     if conta == "StopCard99785" and pin == "90778547093114" and cartaoValido() == true then
  509.         term.clear()
  510.         term.setCursorPos(1,1)
  511.         print("Cartao de Operador Reconhecido!")
  512.         disk.eject(diskSide)
  513.         error()
  514.     end
  515.    
  516.     rednet.send(server, "auth_"..conta.."_"..pin)
  517.     recebi = false
  518.     while recebi == false do
  519.         id, message = rednet.receive()
  520.         if id == server then
  521.             if message == "autorizado" then
  522.                 term.clear()
  523.                 nconta = conta
  524.                 npin = pin
  525.                 recebi = true
  526.             end
  527.             if message == "negado" then
  528.                 term.clear()
  529.                 recebi = true
  530.                 desenharErro("Conta e PIN incompativeis")
  531.                 disk.eject(diskSide)
  532.                 nconta = ""
  533.                 npin = ""
  534.                 os.sleep(2)
  535.                 init()
  536.             end
  537.         end
  538.     end
  539.    
  540.     bank()
  541.    
  542.     term.clear()    desenharMenu()
  543.    
  544.     disk.eject(diskSide)
  545.     nconta = ""
  546.     npin = ""
  547.     center(textStart, "Obrigado por usar o TKBank")
  548.     center(textStart + 1, "Pegue seu cartao")
  549.  
  550.     sleep(2)
  551.     init()
  552. end
  553.  
  554. function bank()
  555.     while true do
  556.         term.clear()    desenharMenu()  desenharBotoes()
  557.  
  558.         desenharBotao(1, "Valor da Moeda")
  559.         desenharBotao(4, "Retornar Cartao")
  560.         desenharBotao(5, "Saldo")
  561.         desenharBotao(6, "Saque")
  562.         desenharBotao(7, "Deposito")
  563.         desenharBotao(8, "Transferencia")
  564.  
  565.         local button = waitForButton()
  566.         if      button == 1         then        valorM()
  567.         elseif  button == 4         then        return
  568.         elseif  button == 5         then        mostrarSaldo()
  569.         elseif  button == 6         then        saque()
  570.         elseif  button == 7         then        deposito()
  571.         elseif  button == 8         then        transferir()
  572.         end
  573.     end
  574. end
  575.  
  576. while true do
  577.     begin()
  578. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement