Guest User

bankServer

a guest
May 5th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.38 KB | None | 0 0
  1. --bankServer V: 0.2.1
  2.  
  3. local id = os.getComputerID()
  4. if fs.exists("server/id") then
  5.   local time = os.time()
  6.   local date = os.day()
  7.   local file = fs.open("backups/serverlog","a")
  8.   file.write(date..","..time.."("..id.."): Server started\n")
  9.   rednet.open("left")
  10.   local file = fs.open("backups/serverlog","a")
  11.   file.write(date..","..time.."("..id.."): RedNet activated\n")
  12.   file.close()
  13.   shell.run("clear")
  14.   print("Opening listening ports")
  15.   os.loadAPI("crypt")
  16.   sleep(2)
  17.   shell.run("clear")
  18.   print("Ports open, System online")
  19.   while true do
  20.     local id,msg = rednet.receive()
  21.     local data = textutils.unserialize(msg)
  22.     local cmd = data[1]
  23.     local acc = data[2]
  24.     local pwd = data[3]
  25.     local amt = data[4]
  26.     local toA = data[5]
  27.     local mayor = mistamadd001
  28.     if cmd == "install" then
  29.       local file = fs.open("backups/serverlog","a")
  30.       file.write(date..","..time.."("..id.."): Attempting client install\n")
  31.       file.close()
  32.       print("("..id.."): Attempting Client Install")
  33.       if fs.exists("clients/")then
  34.         if fs.exists ("clients/"..id) then
  35.           rednet.send(id, " Client already installed")
  36.           local file = fs.open("backups/serverlog","a")
  37.           file.write(date..","..time..":"..id.."): Client already installed\n")
  38.           file.close()
  39.           print("("..id.."): Client already installed")
  40.         else
  41.           local file = fs.open("clients/"..id,"w")
  42.           file.close()
  43.           rednet.send(id, " Client installed")
  44.           local file = fs.open("backups/serverlog","a")
  45.           file.write(date..","..time.."("..id.."): Client installed\n")
  46.           file.close()
  47.           print("("..id.."): Client installed")
  48.         end
  49.       else
  50.         fs.makeDir("clients")
  51.         local file = fs.open("clients/"..id,"w")
  52.         file.close()
  53.         rednet.send(id, " Client installed")
  54.         local file = fs.open("backups/serverlog","a")
  55.         file.write(date..","..time.."("..id.."): Client installed\n")
  56.         file.close()
  57.         print("("..id.."):  Client installed")
  58.       end
  59.     elseif cmd == "create" then
  60.       if fs.exists("clients/"..id) then
  61.         if fs.exists("users/" ..acc)then
  62.           rednet.send(id, "used")
  63.           local file = fs.open("backups/serverlog","a")
  64.           file.write(date..","..time.."("..id.."): Account "..acc.." already created\n")
  65.           file.close()
  66.           print("("..id.."): Account "..acc.." already created")
  67.         else
  68.           local file = fs.open("users/"..acc, "w")
  69.           file.write(textutils.serialize({name = acc, pass = pwd, bal = 1000}))
  70.           file.close()
  71.           rednet.send(id, "created")
  72.           local file = fs.open("backups/serverlog","a")
  73.           file.write(date..","..time.."("..id.."): Account "..acc.." created\n")
  74.           file.close()
  75.           print("("..id.."): Account "..acc.." created")
  76.         end
  77.     elseif cmd == "login" then
  78.       local file = fs.open("backups/serverlog","a")
  79.       file.write(date..","..time.."("..id.."): User: "..acc.." login\n")
  80.       file.close()
  81.       print("("..id.."): User "..acc.." authentication attempted")
  82.       if fs.exists("clients/"..id) then
  83.         if fs.exists("users/"..acc) then
  84.           local file = fs.open("users/"..acc, "r")
  85.           local info = file.readall()
  86.           file.close()
  87.           cData = textutils.unserialize(info)
  88.           local cAcc = cData[name]
  89.           local cPwd = cData[pass]
  90.           local cBal = cData[bal]
  91.           if cPwd == pwd then
  92.             rednet.send(id, textutils.serialize({"confirm", cAcc, cBal}))
  93.             local id,msg = rednet.receive()
  94.             local data = textutils.unserialize(msg)
  95.             if cmd == "withdraw" then
  96.               local file = fs.open("backups/serverlog","a")
  97.               file.write(date..","..time.."("..id.."): "..acc.." Withdraw attempt\n")
  98.               file.close()
  99.               print("("..id.."): User "..acc.." Withdrawal attempted")
  100.               newBal = math.floor(cBal-amt)  
  101.               local file = fs.open("users/"..acc, "w")
  102.               file.write(textutils.serialize({name = cAcc, pass = cPwd, bal = newBal}))
  103.               file.close()
  104.               local file = fs.open("backups/serverlog","a")
  105.               file.write(date..","..time.."("..id.."): "..acc.." Withdrew "..amt"\n")
  106.               file.close()
  107.               rednet.send(id, textutils.serialize({"confirm", acc, newBal}))
  108.             elseif cmd == "deposit" then
  109.               local file = fs.open("backups/serverlog","a")
  110.               file.write(date..","..time.."("..id.."): "..acc.." Deposit attempt\n")
  111.               file.close()
  112.               print("("..id.."): User "..acc.." Deposit attempted")
  113.               newBal = math.floor(cBal+amt)  
  114.               local file = fs.open("users/"..acc, "w")
  115.               file.write(textutils.serialize({name = cAcc, pass = cPwd, bal = newBal}))
  116.               file.close()
  117.               local file = fs.open("backups/serverlog","a")
  118.               file.write(date..","..time.."("..id.."): "..acc.." Deposited "..amt"\n")
  119.               file.close()
  120.               rednet.send(id, textutils.serialize({"confirm", acc, newBal}))
  121.             elseif cmd == "transfer" then
  122.               local file = fs.open("backups/serverlog","a")
  123.               file.write(date..","..time.."("..id.."): "..acc.." transfer attempt\n")
  124.               file.close()
  125.               print("("..id.."): User "..acc.." Transfer attempted")
  126.               newBal = math.floor(cBal-amt)  
  127.               local file = fs.open("users/"..acc, "w")
  128.               file.write(textutils.serialize({name = cAcc, pass = cPwd, bal = newBal}))
  129.               file.close()
  130.               rednet.send(id, textutils.serialize({"confirm", acc, bal = newBal}))
  131.               local file = fs.open("users/"..toA, "r")
  132.               local cData = file.readall()
  133.               file.close()
  134.               newBal = math.floor(cBal+amt)  
  135.               local file = fs.open("users/"..toA, "w")
  136.               file.write(textutils.serialize({name = cAcc, pass = cPwd, bal = newBal}))
  137.               file.close()
  138.               local file = fs.open("backups/serverlog","a")
  139.               file.write(date..","..time.."("..id.."): "..acc.." transferred $"..amt.." to: "..toA"\n")
  140.               file.close()
  141.               print("("..id.."): User "..acc.." transferred $"..amt.." to: "..toA)
  142.             else
  143.               rednet.send(id, textutils.serialize({"declined"}))
  144.               local file = fs.open("backups/serverlog","a")
  145.               file.write(date..","..time.."("..id.."): Unknown command "..cmd.." entered\n")
  146.               file.close()
  147.               print("("..id.."): Unknown command "..cmd.." entered")
  148.             end
  149.           else
  150.             rednet.send(id, textutils.serialize({"declined"}))
  151.             local file = fs.open("backups/serverlog","a")
  152.             file.write(date..","..time.."("..id.."): "..acc.." Password incorrect\n")
  153.             file.close()
  154.             print("("..id.."): User "..acc.." entered an incorrect password")
  155.           end
  156.         else
  157.           rednet.send(id, textutils.serialize({"declined"}))
  158.           local file = fs.open("backups/serverlog","a")
  159.           file.write(date..","..time.."("..id.."): User "..acc.." doesn't exist\n")
  160.           file.close()
  161.           print("("..id.."): User "..acc.." doesn't exist")
  162.         end
  163.       else
  164.         rednet.send(id, textutils.serialize({"declined"}))
  165.         local file = fs.open("backups/serverlog","a")
  166.         file.write(date..","..time.."("..id.."):Unrecognised client\n")
  167.         file.close()
  168.         print("("..id.."): Unrecognised client, access denied")
  169.       end
  170.     elseif cmd == "eCharge" then
  171.       local file = fs.open("backups/serverlog","a")
  172.       file.write(date..","..time.."("..id.."): "..acc.." transfer attempt\n")
  173.       file.close()
  174.       print("("..id.."): User "..acc.." Transfer attempted")
  175.       local file = fs.open("users/"..acc, "r")
  176.       local info = file.readall()
  177.       file.close()
  178.       cData = textutils.unserialize(info)
  179.       local cAcc = cData[name]
  180.       local cPwd = cData[pass]
  181.       local cBal = cData[bal]
  182.       local amt = 0.5
  183.       newBal = math.floor(cBal-amt)  
  184.       local file = fs.open("users/"..acc, "w")
  185.       file.write(textutils.serialize({name = acc, pass = pwd, bal = newBal}))
  186.       file.close()
  187.       rednet.send(id, textutils.serialize({"confirm", acc, bal = newBal}))
  188.       local file = fs.open("users/"..toA, "r")
  189.       local info = file.readall()
  190.       file.close()
  191.       cData = textutils.unserialize(info)
  192.       local cAcc = cData[name]
  193.       local cPwd = cData[pass]
  194.       local cBal = cData[bal]
  195.       newBal = math.floor(cBal+amt)  
  196.       local file = fs.open("users/"..toA, "w")
  197.       file.write(textutils.serialize({name = cAcc, pass = cPwd, bal = newBal}))
  198.       file.close()
  199.       local file = fs.open("backups/serverlog","a")
  200.       file.write(date..","..time.."("..id.."): "..acc.." transferred $"..amt.." to: "..toA"\n")
  201.       file.close()
  202.       print("("..id.."): User "..acc.." transferred $"..amt.." to: "..toA)
  203.     elseif cmd == "check" then
  204.       local file = fs.open("backups/serverlog","a")
  205.       file.write(date..","..time.."("..id.."): "..acc.." house "..amt.." access attempt\n")
  206.       file.close()
  207.       print("("..id.."): User: "..acc.." attempting house "..amt.." access")
  208.       local file = fs.open("users/"..acc, "r")
  209.       local info = file.readall()
  210.       file.close()
  211.       cData = textutils.unserialize(info)
  212.       local cAcc = cData[name]
  213.       local cPwd = cData[pass]
  214.       local cBal = cData[bal]
  215.       if cBal > 1500 or cBal == 1500 then
  216.         newBal = math.floor(cBal-1500)
  217.         local file = fs.open("users/"..acc, "w")
  218.         file.write(textutils.serialize({name = cAcc, pass = cPwd, bal = newBal}))
  219.         file.close()
  220.         local file = fs.open("users/"..mayor, "r")
  221.         local info = file.readall()
  222.         file.close()
  223.         mData = textutils.unserialize(info)
  224.         local mAcc = mData[name]
  225.         local mPwd = mData[pass]
  226.         local mBal = mData[bal]
  227.         newBal = math.floor(mBal + 1500)
  228.         rednet.send(id, textutils.serialize({"confirm", cAcc, cPwd, newBal}))
  229.         local file = fs.open("backups/serverlog","a")
  230.         file.write(date..","..time.."("..id.."): "..acc.." house "..amt.." access granted\n")
  231.         file.close()
  232.         print("("..id.."): User: "..acc.." house "..amt.." access granted")
  233.       elseif cBal < 1500 then
  234.         rednet.send(id, textutils.serialize({"declined"}))
  235.         local file = fs.open("backups/serverlog","a")
  236.         file.write(date..","..time.."("..id.."): "..acc.." house "..amt.." access denied, insufficient funds\n")
  237.         file.close()
  238.         print("("..id.."): User: "..acc.." house "..amt.." access denied, insufficient funds")
  239.       end
  240.     else rednet.send(id, textutils.serialize({"declined"}))
  241.       local file = fs.open("backups/serverlog","a")
  242.       file.write(date..","..time.."("..id.."): Unknown command "..cmd.." entered\n")
  243.       file.close()
  244.       print("("..id.."): Unknown command "..cmd.." entered")
  245.     end
  246.   end
  247. end
  248. else
  249.   local id = os.getComputerID()
  250.   fs.makeDir("server")
  251.   local file = fs.open("server/id","w")
  252.   file.write(id)
  253.   file.close()
  254.   local file = fs.open("backups/serverlog","a")
  255.   file.write(date..","..time.."("..id.."): Server Created\n")
  256.   file.close()
  257.   print("This Server's ID is "..id)
  258.   sleep(5)
  259. end
Add Comment
Please, Sign In to add comment