Advertisement
Guest User

startup

a guest
Mar 6th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 KB | None | 0 0
  1. local mon = peripheral.wrap("right")
  2. authSet = 3
  3.  
  4.  mon.clear()
  5.  mon.setCursorPos(1,1)
  6.  mon.setTextScale(0.6)
  7.  term.setBackgroundColor(colors.blue)
  8.  term.clear()
  9.  term.setCursorPos(1,1)
  10.  term.setTextColor(colors.white)
  11.  print "Bank Server 3.1 Status:"
  12.  print "--------------------------------------"
  13.  term.setCursorPos(3,4)
  14.  print("Current Auth Code > "..authSet)
  15.  
  16.  term.redirect(mon)
  17.  term.setTextColor(colors.white)
  18.  term.setBackgroundColor(colors.black)
  19.  print("Server Online.")
  20.  
  21. function authF()
  22.  authSet = authSet * 2 + 7
  23. end
  24. side = "top" -- Rednet Modem sidem (End of Setup)
  25. rednet.open(side)
  26. -- Functions
  27. function drawStart()
  28.  id, message = rednet.receive()
  29.  if type(message) == "string" then
  30.   if message == "Login" then
  31.     login()
  32.   elseif message == "Balance" then
  33.     balance()
  34.   elseif message == "Transfer" then
  35.     transfer()
  36.   elseif message == "Create" then
  37.     create()
  38.   elseif message == "Delete" then
  39.     delete()
  40.   elseif message == "Rename" then
  41.     rename()
  42.   elseif message == "Passcode" then
  43.     passcode()
  44.   elseif message == "Add" then
  45.     add()
  46.   elseif message == "Auth" then
  47.     auth()
  48.   elseif message == "Withdraw" then
  49.     withdraw()
  50.   else
  51.    rednet.send(id,"Command Not Found")
  52.    drawStart()
  53.   end
  54.  else
  55.   rednet.send(id,"Data is Not String")
  56.  end
  57. end
  58. function login()
  59.  id, user = rednet.receive()
  60.  id, pass = rednet.receive()
  61.  mon.write(user .. " is trying to login.")
  62.  if type(pass) == "string" then
  63.   if type(user) == "string" then
  64.    if fs.exists("user/"..user) == true then
  65.      h = fs.open("user/"..user, "r")
  66.      passReal = h.readAll()
  67.      h.close()
  68.      if pass == passReal then
  69.        rednet.send(id,"Login Successful")
  70.        mon.write(user.." was logged in.")
  71.        drawStart()
  72.      else
  73.        mon.write(user.." failed to login.")
  74.        rednet.send(id,"Password Incorrect")
  75.        drawStart()
  76.      end
  77.    else
  78.      rednet.send(id,"Unknown User")
  79.      drawStart()
  80.    end
  81.   else
  82.    rednet.send(id,"User Is Not String")  
  83.   end
  84.  else
  85.    rednet.send(id,"Password Is Not String")
  86.  end
  87. end
  88. function balance()
  89.  id, user = rednet.receive()
  90.  if type(user) == "string" then
  91.   h = fs.open("$/"..user, "r")
  92.   bal = h.readAll()
  93.   h.close()
  94.   rednet.send(id,bal)
  95.   mon.write(user.." retrieved their balance.")
  96.   drawStart()
  97.  else
  98.   rednet.send(id,"User is Not String")
  99.  end
  100. end
  101. function transfer()
  102.  id, user1 = rednet.receive()
  103.  id, user2 = rednet.receive()
  104.  id, am = rednet.receive()
  105.  if type(user1) == "string" and type(user2) == "string" and type(am) == "string" then
  106.    if fs.exists("user/"..user1) and fs.exists("user/"..user2)then
  107.     h = fs.open("$/"..user1, "r")
  108.     bal1 = h.readAll()
  109.     h.close()
  110.     if tonumber(bal1) >= tonumber(am) then
  111.       h = fs.open("$/"..user2, "r")
  112.       bal2 = h.readAll()
  113.       h.close()
  114.       bal1 = bal1 - am
  115.       bal2 = bal2 + am
  116.       h = fs.open("$/"..user1, "w")
  117.       h.write(bal1)
  118.       h.close()
  119.       h = fs.open("$/"..user2, "w")
  120.       h.write(bal2)
  121.       h.close()
  122.       rednet.send(id,"Funds Transfered")
  123.       mon.write(user.. " transfered funds.")
  124.       drawStart()
  125.     elseif bal1 <= am then
  126.       rednet.send(id,"Not Enough Funds")
  127.       --term.setCursorPos(1,3)
  128.       --print(user1)
  129.       --print(user2)
  130.       --print(bal1)
  131.       --print(bal2)
  132.       --print(am)
  133.       drawStart()
  134.     else
  135.       rednet.send(id,"An Error Occurred")
  136.       drawStart()
  137.     end
  138.   else
  139.     rednet.send(id," User(s) Not Found")
  140.     drawStart()
  141.   end
  142.  else
  143.    rednet.send(id,"Data Is Not String")
  144.  end
  145. end
  146. function create()
  147.  id, user = rednet.receive()
  148.  id, pass = rednet.receive()
  149.  if type(user) == "string" and type(pass) then
  150.   if fs.exists("user/"..user) then
  151.     rednet.send(id,"User Exists")
  152.   else
  153.     h = fs.open("user/"..user,"w")
  154.     h.write(pass)
  155.     h.close()
  156.     h = fs.open("$/"..user,"w")
  157.     h.write("0")
  158.     h.close()
  159.     rednet.send(id,"Account Created")
  160.     mon.write("Account created.")
  161.     drawStart()
  162.   end
  163.  else
  164.   rednet.send(id,"Data Is Not String")
  165.  end
  166.  drawStart()
  167. end
  168. function delete()
  169. end
  170. function rename()
  171. end
  172. function passcode()
  173. end
  174. function add()
  175.  id, user = rednet.receive()
  176.  id, amt = rednet.receive()
  177.  id, auth1 = rednet.receive()
  178.  if type(user) == "string" and type(amt) == "string" and type(auth1) == "string" then
  179.   if tonumber(auth1) == authSet then
  180.    if fs.exists("user/"..user) then
  181.      h = fs.open("$/"..user, "r")
  182.      bal = h.readAll()
  183.      h.close()
  184.      balNew = bal + amt
  185.      h = fs.open("$/"..user, "w")
  186.      h.write(balNew)
  187.      h.close()
  188.      rednet.send(id,"$"..amt.." Added to "..user)
  189.      drawStart()
  190.    else
  191.      rednet.send(id,"No Such User")
  192.      drawStart()
  193.    end
  194.   else
  195.    rednet.send(id,"Authorization Code Not Accepted")
  196.    drawStart()
  197.   end
  198.  else
  199.   rednet.send(id,"Data Is Not String")
  200.  end
  201. end
  202. function auth()
  203.  authF()
  204.  rednet.send(id,"New Auth Code Found")
  205.  drawStart()
  206. end
  207. function withdraw()
  208.   id, logU = rednet.receive()
  209.   local a = fs.open("$/"..user, "r")
  210.   rednet.send(id, "Need Amount")
  211.   id, msg = rednet.receive()
  212.   local actual = a.readAll()
  213.   if actual < msg then
  214.     rednet.send(id, "Not enough funds!")
  215.   elseif actual >= msg then
  216.     final = actual - msg
  217.     a.close()
  218.     local b = fs.open("$/"..user, "w")
  219.     b.write(final)
  220.     b.close()
  221.     rednet.send(id, "Withdraw complete.")
  222.   end
  223. end
  224. -- Program
  225. drawStart()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement