Advertisement
Guest User

startup

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