gravitowl

[GraviBank] PayATM

Jan 13th, 2021 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. -- Variables
  2. local diskDrive = peripheral.wrap("bottom")
  3.  
  4. rednet.open("back")
  5.  
  6. local w, h = term.getSize()
  7. local select = 1
  8. local payAmount
  9. local menustate = "select"
  10.  
  11. -- Functions
  12.  
  13. function printCentered(str, ypos)
  14. term.setCursorPos(w/2 - #str/2, ypos)
  15. term.write(str)
  16. end
  17.  
  18. function printRight(str, ypos)
  19. term.setCursorPos(w - #str, ypos)
  20. term.write(str)
  21. end
  22.  
  23. function printLeft(str, ypos)
  24. term.setCursorPos(#str, ypos)
  25. term.write(str)
  26. end
  27.  
  28. function readTable(dir)
  29. local file = fs.open(dir, "r")
  30. local data = file.readAll()
  31. file.close()
  32. return textutils.unserialize(data)
  33. end
  34.  
  35. function checkPassword(password)
  36. if password == nil then
  37. return "false"
  38. end
  39. local data = readTable("disk/userData.txt")
  40. local username = data.username
  41. rednet.broadcast({"request", {"checkPassword", username, password}}, "graviBank")
  42. local senderID, message = rednet.receive("graviBank")
  43. return message[1]
  44. end
  45.  
  46. function hasEnoughMoney(amount)
  47. local data = readTable("disk/userData.txt")
  48. local username = data.username
  49. rednet.broadcast({"request", {"getBalance", username}}, "graviBank")
  50. local senderID, message = rednet.receive("graviBank")
  51. if message[1] >= amount then
  52. return true
  53. else
  54. return false
  55. end
  56. end
  57.  
  58. -- Menus
  59.  
  60. ---- Drawing menus
  61.  
  62. function drawSelect()
  63. printCentered("PAYMENT", h/2 - 1)
  64.  
  65. if payAmount == nil then
  66. printCentered("Amount: ", h/2)
  67. term.setCursorPos(w/2, h/2+1)
  68. payAmount = read()
  69. else
  70. printCentered("Amount: ", h/2)
  71. printCentered(payAmount, h/2+1)
  72. end
  73.  
  74.  
  75. if select == 1 then
  76. printCentered(">CONFIRM< CANCEL", h - 2)
  77. elseif select ==2 then
  78. printCentered("CONFIRM >CANCEL<", h - 2)
  79. end
  80. end
  81.  
  82. function drawPassword()
  83. if not diskDrive.isDiskPresent() then
  84. printCentered("No card in the system.", h/2-1)
  85. printCentered("Returning to start menu.", h/2)
  86. os.sleep(0.5)
  87. os.reboot()
  88. return
  89. end
  90.  
  91. if not hasEnoughMoney(tonumber(payAmount)) then
  92. printCentered("Not enough balance.", h/2)
  93. os.sleep(0.5)
  94. os.reboot()
  95. end
  96.  
  97. printCentered("Password:", h/2-1)
  98. printCentered("__________", h/2+1)
  99.  
  100. term.setCursorPos(w/2 - 10/2, h/2)
  101. local password = read("*")
  102. local isCorrect = checkPassword(password)
  103.  
  104. if isCorrect[1] == "false" then
  105. return
  106. else
  107. menustate = "pay"
  108. end
  109. end
  110.  
  111. function drawPay()
  112. printCentered("PAID", h/2)
  113. end
  114.  
  115. function drawCancel()
  116. printCentered("CANCELLED", h/2)
  117. end
  118.  
  119. function drawHeader()
  120. printCentered("GRAVIBANK ATM", 1)
  121. printCentered(string.rep("-", w + 1), 2)
  122. printRight("GraviBank system made by gravitowl.", h)
  123. end
  124.  
  125. ---- Menu state
  126.  
  127. local mopt = {
  128. ["select"] = {
  129. options = {"password", "cancel"},
  130. draw = drawSelect
  131. },
  132. ["cancel"] = {
  133. options = {"start"},
  134. draw = drawCancel
  135. },
  136. ["password"] = {
  137. options = {},
  138. draw = drawPassword
  139. },
  140. ["pay"] = {
  141. options = {"start"},
  142. draw = drawPay
  143. }
  144. }
  145.  
  146. ---- Run functions
  147.  
  148. function runMenu()
  149. while true do
  150. term.clear()
  151. drawHeader()
  152. mopt[menustate].draw()
  153. if #mopt[menustate].options > 0 then
  154. local id, key = os.pullEvent("key")
  155. if key == keys.up and select > 1 then select = select-1
  156. elseif key == keys.down and select < #mopt[menustate].options then select = select + 1
  157. elseif key == keys.enter then
  158. if mopt[menustate].options[select] == "quit" then break end
  159. menustate = mopt[menustate].options[select]
  160. select = 1
  161. end
  162. end
  163. end
  164. end
  165.  
  166. runMenu()
Add Comment
Please, Sign In to add comment