benthomas7777

BM-Bank Client

Sep 2nd, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. os.loadAPI("crypt")
  2. os.loadAPI("gui")
  3.  
  4. local textStart = gui.textStart
  5. local modemSide = "back"
  6. local diskSide = "right"
  7. local serverId = 3
  8. local withdrawId = 14
  9. local version = "CC-Bank-0.2.0-Compatible"
  10.  
  11. -- Import GUI functions
  12. local drawHeader, drawButtons, drawButton, drawError, center = gui.drawHeader, gui.drawButtons, gui.drawButton, gui.drawError, gui.center
  13. local waitForMouse, waitForButton, waitForChar, waitForKey, waitForEnter = gui.waitForMouse, gui.waitForButton, gui.waitForChar, gui.waitForKey, gui.waitForEnter
  14. local waitForDisk = gui.waitForDisk
  15.  
  16. function otherAmount()
  17. local amount = ""
  18. term.clear() drawHeader()
  19. term.setCursorPos(center(textStart, "Amount : MC$ ") + 12, textStart)
  20. return read()
  21. end
  22.  
  23. function balance(acc, pin)
  24. term.clear() drawHeader() drawButtons()
  25.  
  26. drawButton(4, "Cancel")
  27. drawButton(5, "MC Dollars")
  28.  
  29. local mult = nil
  30. while true do
  31. local button = waitForButton()
  32. if button == 4 then return
  33. elseif button == 5 then mult = 1 break
  34. end
  35. end
  36.  
  37. local senderId, response = nil, nil
  38. rednet.open(modemSide)
  39.  
  40. rednet.send(serverId, version .. textutils.serialize( { "BALANCE", acc, crypt.hashPassword(pin) } ) )
  41.  
  42. while true do
  43. senderId, response = rednet.receive(5)
  44. if senderId == serverId then break end
  45. end
  46. rednet.close(modemSide)
  47.  
  48. term.clear() drawHeader() drawButtons()
  49. drawButton(8, "OK")
  50.  
  51. if response ~= "-" then
  52. local displayBalance = math.floor(tonumber(response) * mult * 100) / 100
  53. center(textStart, "Balance: " .. displayBalance)
  54. else
  55. drawError("Transaction Error")
  56. end
  57.  
  58. while true do
  59. local button = waitForButton()
  60. if button == 8 then return end
  61. end
  62. end
  63.  
  64. function transfer(acc, pin)
  65. term.clear() drawHeader() drawButtons()
  66.  
  67. drawButton(1, "MC$ 100")
  68. drawButton(2, "MC$ 200")
  69. drawButton(3, "MC$ 500")
  70. drawButton(5, "MC$ 1000")
  71. drawButton(6, "MC$ 2000")
  72. drawButton(7, "MC$ 5000")
  73.  
  74. drawButton(4, "Cancel")
  75. drawButton(8, "Other Amount")
  76.  
  77. local amountS = nil
  78. while true do
  79. local button = waitForButton()
  80. if button == 1 then amountS = "100" break
  81. elseif button == 2 then amountS = "200" break
  82. elseif button == 3 then amountS = "500" break
  83. elseif button == 5 then amountS = "1000" break
  84. elseif button == 6 then amountS = "2000" break
  85. elseif button == 7 then amountS = "5000" break
  86. elseif button == 4 then return
  87. elseif button == 8 then amountS = otherAmount() break
  88. end
  89. end
  90.  
  91. term.clear() drawHeader() drawButtons()
  92.  
  93. local amount = tonumber(amountS)
  94. if amount ~= nil and (amount % 1) == 0 then
  95. term.clear() drawHeader()
  96. term.setCursorPos(center(textStart, "Amount : MC$ ") + 12, textStart)
  97. term.write(amountS)
  98. term.setCursorPos(center(textStart + 1, "Account : ") + 10, textStart + 1)
  99. local account = read()
  100.  
  101. drawButtons()
  102. drawButton(4, "Cancel")
  103. drawButton(8, "Confirm")
  104.  
  105. while true do
  106. local button = waitForButton()
  107. if button == 4 then return
  108. elseif button == 8 then break
  109. end
  110. end
  111.  
  112. local senderId, response = nil, nil
  113. rednet.open(modemSide)
  114.  
  115. rednet.send(serverId, version .. textutils.serialize( { "TRANSFER", acc, crypt.hashPassword(pin), account, amountS } ) )
  116.  
  117. while true do
  118. senderId, response = rednet.receive(5)
  119. if senderId == serverId then break end
  120. end
  121. rednet.close(modemSide)
  122.  
  123. term.clear() drawHeader() drawButtons()
  124.  
  125. if response ~= "-" then
  126. center(textStart, "Transfer Successful")
  127. else
  128. drawError("Transaction Error")
  129. end
  130.  
  131. else
  132. term.clear() drawHeader() drawButtons()
  133. drawError("Invalid Amount")
  134.  
  135. --[[
  136. while true do
  137. local button = waitForButton()
  138. if button == 8 then return end
  139. end
  140. --]]
  141. end
  142.  
  143. drawButton(8, "OK")
  144. while true do
  145. local button = waitForButton()
  146. if button == 8 then return end
  147. end
  148. end
  149.  
  150. function withdraw(acc, pin)
  151. term.clear() drawHeader() drawButtons()
  152.  
  153. drawButton(1, "MC$ 100")
  154. drawButton(2, "MC$ 200")
  155. drawButton(3, "MC$ 500")
  156. drawButton(5, "MC$ 1000")
  157. drawButton(6, "MC$ 2000")
  158. drawButton(7, "MC$ 5000")
  159.  
  160. drawButton(4, "Cancel")
  161. drawButton(8, "Other Amount")
  162.  
  163. local amountS = nil
  164. while true do
  165. local button = waitForButton()
  166. if button == 1 then amountS = "100" break
  167. elseif button == 2 then amountS = "200" break
  168. elseif button == 3 then amountS = "500" break
  169. elseif button == 5 then amountS = "1000" break
  170. elseif button == 6 then amountS = "2000" break
  171. elseif button == 7 then amountS = "5000" break
  172. elseif button == 4 then return
  173. elseif button == 8 then amountS = otherAmount() break
  174. end
  175. end
  176.  
  177. term.clear() drawHeader() drawButtons()
  178.  
  179. local amount = tonumber(amountS)
  180. if amount ~= nil and (amount % 100) == 0 then
  181. term.clear() drawHeader()
  182. term.setCursorPos(center(textStart, "Amount : MC$ ") + 12, textStart)
  183. term.write(amountS)
  184. drawButtons()
  185. drawButton(4, "Cancel")
  186. drawButton(8, "Confirm")
  187.  
  188. while true do
  189. local button = waitForButton()
  190. if button == 4 then return
  191. elseif button == 8 then break
  192. end
  193. end
  194.  
  195. local senderId, response = nil, nil
  196. rednet.open(modemSide)
  197.  
  198. rednet.send(serverId, version .. textutils.serialize( { "WITHDRAW", acc, crypt.hashPassword(pin), amountS } ) )
  199.  
  200. while true do
  201. senderId, response = rednet.receive(5)
  202. if senderId == serverId then break end
  203. end
  204. rednet.close(modemSide)
  205.  
  206. term.clear() drawHeader()
  207.  
  208. if response ~= "-" then
  209. center(textStart, "Withdrawing " .. (amount) .. " MC$")
  210. rednet.open(modemSide)
  211. rednet.send(withdrawId, version .. textutils.serialize( { "WITHDRAW", (amount) .. "" } ) )
  212.  
  213. while true do
  214. senderId, response = rednet.receive(5)
  215. if senderId == withdrawId then break end
  216. end
  217. rednet.close(modemSide)
  218.  
  219. term.clear() drawHeader()
  220. if response ~= "-" then
  221. center(textStart, "Please take your cash")
  222. sleep(2)
  223. return
  224. else
  225. drawButtons()
  226. drawError("Withdrawal Error")
  227. end
  228. else
  229. drawError("Transaction Error")
  230. end
  231.  
  232. else
  233. term.clear() drawHeader() drawButtons()
  234. drawError("Invalid Amount")
  235.  
  236. --[[
  237. while true do
  238. local button = waitForButton()
  239. if button == 8 then return end
  240. end
  241. --]]
  242. end
  243.  
  244. drawButton(8, "OK")
  245. while true do
  246. local button = waitForButton()
  247. if button == 8 then return end
  248. end
  249. end
  250.  
  251. function bank(acc, pin)
  252. while true do
  253. term.clear() drawHeader() drawButtons()
  254.  
  255. drawButton(4, "Return Card")
  256. drawButton(5, "Balance Check")
  257. drawButton(6, "Withdrawal")
  258. drawButton(7, "Deposit")
  259. drawButton(8, "Transfer")
  260.  
  261. local button = waitForButton()
  262. if button == 4 then return
  263. elseif button == 5 then balance(acc, pin)
  264. elseif button == 6 then withdraw(acc, pin)
  265. --elseif button == 7 then deposit(acc, pin)
  266. elseif button == 8 then transfer(acc, pin)
  267. end
  268. end
  269. end
  270.  
  271. function begin()
  272. local acc, pin
  273. local version = "CC-Bank-0.2.0-Compatible"
  274. local serverId = 3
  275.  
  276. term.clear() drawHeader()
  277.  
  278. center(textStart, "Insert keycard or press ENTER")
  279. center(textStart + 1, "to transact without a keycard")
  280. parallel.waitForAny(waitForEnter, waitForDisk)
  281.  
  282. term.clear() drawHeader()
  283.  
  284. term.setCursorPos(center(textStart, "Account No : ") + 13, textStart)
  285.  
  286. if disk.hasData(diskSide) then
  287. acc = disk.getLabel(diskSide)
  288. term.write(acc)
  289. else
  290. acc = read()
  291. end
  292.  
  293. term.setCursorPos(center(textStart + 1, "PIN : ") + 6, textStart + 1)
  294. pin = read("*")
  295.  
  296. sleep(1)
  297.  
  298. local senderId, response = nil, nil
  299. rednet.open(modemSide)
  300.  
  301. rednet.send(serverId, version .. textutils.serialize( { "LOGIN", acc, crypt.hashPassword(pin) } ) )
  302.  
  303. while true do
  304. senderId, response = rednet.receive(5)
  305. if senderId == serverId then break end
  306. end
  307. rednet.close(modemSide)
  308.  
  309. term.clear() drawHeader()
  310.  
  311. if responsem ~= "-" then
  312. bank(acc, pin)
  313.  
  314. term.clear() drawHeader()
  315.  
  316. disk.eject(diskSide)
  317. center(textStart, "Thank you for banking with BM")
  318. center(textStart + 1, "Please take your keycard")
  319.  
  320. sleep(2)
  321.  
  322. else
  323. drawError("Login Error")
  324. end
  325.  
  326. while true do
  327. local button = waitForButton()
  328. if button == 8 then return end
  329. end
  330. end
  331.  
  332. while true do
  333. begin()
  334. end
Advertisement
Add Comment
Please, Sign In to add comment