Advertisement
jimthenerd1999

Untitled

Jul 13th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. --[[
  2. MCNET Bank Server Software
  3. Version 0.1
  4. ]]
  5.  
  6. --[[
  7. Request Format
  8.  
  9. local quest = {
  10. requestType (auth, trans, create),
  11. requestAction (deposit, withdraw),
  12. accountName,
  13. accountPin,
  14. additionalParameter (amount)
  15. }
  16.  
  17. The server will respond with either
  18. true (fuck you) or false (fuck you too)
  19. ]]
  20.  
  21. --Variables
  22.  
  23. local trustedClients = {}
  24.  
  25. --Functions
  26.  
  27. function isTrustedClient(id)
  28. for i = 1, table.getn(trustedClients) do
  29. if (id == trustedClients[i]) then
  30. return true
  31. end
  32. end
  33. return false
  34. end
  35.  
  36. function genAccountName(accountName)
  37. return "/accounts/" .. accountName
  38. end
  39.  
  40. function getAccountInfo(accountName)
  41. if (fs.exists(accountName)) then
  42. local account = fs.open(genAccountName(accountName), "r")
  43. if (account == nil) then return nil end
  44.  
  45. local pin = account.readLine()
  46. local balance = account.readLine()
  47. account.close()
  48.  
  49. return pin, balance
  50. end
  51.  
  52. return nil
  53. end
  54.  
  55. function updateAccountInfo(accountName, pin, balance)
  56. if (fs.exists(accountName)) then
  57. local account = fs.open(genAccountName(accountName), "w")
  58. if (account == nil) then return false end
  59.  
  60. account.writeLine(pin)
  61. account.writeLine(balance)
  62. account.flush()
  63. account.close()
  64.  
  65. return true
  66. end
  67.  
  68. return false
  69. end
  70.  
  71. function verifyPin(accountName, userPin)
  72. pin, balance = getAccountInfo(accountName)
  73. return (pin == userPin)
  74. end
  75.  
  76. function deposit(accountName, userPin, amount)
  77. if (verifyPin(accountName, userPin)) then
  78. pin, balance = getAccountInfo(genAccountName(accountName))
  79. updateAccountInfo(accountName, userPin, balance + amount)
  80.  
  81. return true
  82. end
  83.  
  84. return false
  85. end
  86.  
  87. function withdraw(accountName, userPin, amount)
  88. pin, balance = getAccountInfo(accountName)
  89. if (balance < amount) then
  90. return false
  91. end
  92.  
  93. return deposit(accountName, userPin, -1 * amount)
  94. end
  95.  
  96. function createAccount(accountName, userPin)
  97. if (fs.exists(genAccountName(accountName))) then
  98. return false
  99. end
  100.  
  101. local acnt = fs.open(genAccountName(accountName), "w")
  102. acnt.close();
  103.  
  104. return updateAccountInfo(accountName, userPin, 0)
  105. end
  106.  
  107. --The Actual Shit
  108.  
  109. while true do
  110. id, strRequest = rednet.receive()
  111. request = textutils.unserialize(strRequest)
  112.  
  113. if (not request == nil and isTrustedClient(id)) then
  114. if (table.getn(request) == 5) then
  115. if (request[1] == "auth") then
  116. if (verifyPin(request[3], request[4])) then
  117. rednet.send(id, "true")
  118. else
  119. rednet.send(id, "false")
  120. end
  121. end
  122.  
  123. if (request[1] == "trans") then
  124.  
  125. local accountName = request[3]
  126. local accountPin = request[4]
  127.  
  128. if (not verifyPin(accountName, accountPin)) then
  129. rednet.send(id, "false")
  130. else
  131. if (request[2] == "deposit") then
  132. deposit(accountName, accountName, accountPin, request[5])
  133. end
  134.  
  135. if (request[2] == "withdraw") then
  136. if (withdraw(accountName, accountPin, request[5])) then
  137. rednet.send(id, "true")
  138. end
  139. rednet.send(id, "false")
  140. end
  141. end
  142. end
  143.  
  144. if (request[1] == "create") then
  145. local accountName = request[3]
  146. local accountPin = request[4]
  147.  
  148. if (createAccount(accountName, accountPin)) then
  149. rednet.send(id, "true")
  150. else
  151. rednet.send(id, "false")
  152. end
  153. end
  154. end
  155. end
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement