gravitowl

[GraviBank] ATM UI

Jan 12th, 2021 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. -- Variables
  2. local COMPUTER_ID = os.getComputerID()
  3. local TURTLE_ID = 130
  4.  
  5. local diskDrive = peripheral.wrap("right")
  6.  
  7. rednet.open("back")
  8.  
  9. w,h = term.getSize()
  10. local select = 1
  11. local selectedAmount = 1
  12. local menustate = "start"
  13.  
  14. -- Functions
  15.  
  16. function printCentered(str, ypos)
  17. term.setCursorPos(w/2 - #str/2, ypos)
  18. term.write(str)
  19. end
  20.  
  21. function printRight(str, ypos)
  22. term.setCursorPos(w - #str, ypos)
  23. term.write(str)
  24. end
  25.  
  26. function printLeft(str, ypos)
  27. term.setCursorPos(#str, ypos)
  28. term.write(str)
  29. end
  30.  
  31. function readTable(dir)
  32. local file = fs.open(dir, "r")
  33. local data = file.readAll()
  34. file.close()
  35. return textutils.unserialize(data)
  36. end
  37.  
  38. function checkPassword(password)
  39. if password == nil then
  40. return "false"
  41. end
  42. local data = readTable("disk/userData.txt")
  43. local username = data.username
  44. rednet.broadcast({"request", {"checkPassword", username, password}}, "graviBank")
  45. local senderID, message = rednet.receive("graviBank")
  46. return message
  47. end
  48.  
  49. function hasEnoughMoney(amount)
  50. local data = readTable("disk/userData.txt")
  51. local username = data.username
  52. rednet.broadcast({"request", {"getBalance", username}}, "graviBank")
  53. local senderID, message = rednet.receive("graviBank")
  54. if message[1] >= amount then
  55. return true
  56. else
  57. return false
  58. end
  59. end
  60.  
  61. -- Menus
  62.  
  63. ---- Drawing Menus
  64.  
  65. function drawStart()
  66. printCentered("Start", h/2)
  67. printCentered("Get Balance", h/2+2)
  68.  
  69. local ypos = h/2 + 1
  70. if select == 2 then
  71. ypos = h/2+3
  72. end
  73.  
  74. printCentered("-----------", ypos)
  75. end
  76.  
  77. function drawMain()
  78. printLeft(" 1", 4)
  79. printLeft(" 4", 9)
  80. printLeft(" 8", 14)
  81.  
  82. printRight("16 ", 4)
  83. printRight("32 ", 9)
  84. printRight("64 ", 14)
  85.  
  86. printCentered("Go Back", h - 2)
  87.  
  88. if select == 1 then
  89. printLeft(">", 4)
  90. selectedAmount = 1
  91. elseif select == 2 then
  92. printLeft(">", 9)
  93. selectedAmount = 4
  94. elseif select == 3 then
  95. printLeft(">", 14)
  96. selectedAmount = 8
  97. elseif select == 4 then
  98. printRight("<", 4)
  99. selectedAmount = 16
  100. elseif select == 5 then
  101. printRight("<", 9)
  102. selectedAmount = 32
  103. elseif select == 6 then
  104. printRight("<", 14)
  105. selectedAmount = 64
  106. elseif select == 7 then
  107. printCentered("-----------", h - 1)
  108. end
  109. end
  110.  
  111. function drawPay()
  112. printCentered("Getting "..selectedAmount.." diamonds ready..", 4)
  113. rednet.send(TURTLE_ID, {"request", {"diamond", selectedAmount}}, "gATM")
  114. local senderID, receivedMessage = rednet.receive("gATM")
  115. printCentered("Getting diamonds ready was "..receivedMessage[1], 6)
  116. if receivedMessage[1] == "unsuccesful" then
  117. printCentered("Stopping program...", 8)
  118. os.sleep(0.5)
  119. os.reboot()
  120. return
  121. end
  122. local data = readTable("disk/userData.txt")
  123. local username = data.username
  124. rednet.broadcast({"post", {"changeBalance", username, -selectedAmount}}, "graviBank")
  125.  
  126. printCentered("Diamonds should be received", 8)
  127. printCentered("Thanks for using this Gravibank ATM", 10)
  128. os.sleep(0.5)
  129. os.reboot()
  130. end
  131.  
  132. function drawPassword()
  133. if not diskDrive.isDiskPresent() then
  134. printCentered("No card in the system.", h/2-1)
  135. printCentered("Returning to start menu.", h/2)
  136. os.sleep(0.5)
  137. os.reboot()
  138. return
  139. end
  140.  
  141. if not hasEnoughMoney(selectedAmount) then
  142. printCentered("Not enough balance.", h/2)
  143. os.sleep(0.5)
  144. os.reboot()
  145. end
  146.  
  147. printCentered("Password:", h/2-1)
  148. printCentered("__________", h/2+1)
  149.  
  150. term.setCursorPos(w/2 - 10/2, h/2)
  151. local password = read("*")
  152. local isCorrect = checkPassword(password)
  153.  
  154. if isCorrect[1] == "false" then
  155. return
  156. else
  157. menustate = "pay"
  158. end
  159. end
  160.  
  161. function drawBalance()
  162. if not diskDrive.isDiskPresent() then
  163. printCentered("No card in the system.", h/2)
  164. os.sleep(0.5)
  165. os.reboot()
  166. end
  167. printCentered("Balance on your account:", h/2 - 2)
  168.  
  169. local data = readTable("disk/userData.txt")
  170. local username = data.username
  171. rednet.broadcast({"request", {"getBalance", username}}, "graviBank")
  172. local senderID, message = rednet.receive("graviBank")
  173.  
  174. printCentered("$"..message[1], h/2)
  175.  
  176. printCentered("Go Back", h-2)
  177. printCentered("-----------", h - 1)
  178. end
  179.  
  180. function drawHeader()
  181. printCentered("GRAVIBANK ATM", 1)
  182. printCentered(string.rep("-", w + 1), 2)
  183. printRight("GraviBank system made by gravitowl.", h)
  184. end
  185.  
  186. ---- Menu State
  187. local mopt = {
  188. ["start"] = {
  189. options = {"main", "balance"},
  190. draw = drawStart
  191. },
  192. ["main"] = {
  193. options = {"password", "password", "password", "password", "password", "password", "start"},
  194. draw = drawMain
  195. },
  196. ["password"] = {
  197. options = {},
  198. draw = drawPassword
  199. },
  200. ["check"] = {
  201. options = {},
  202. draw = drawCheck
  203. },
  204. ["pay"] = {
  205. options = {},
  206. draw = drawPay
  207. },
  208. ["balance"] = {
  209. options = {"start"},
  210. draw = drawBalance
  211. }
  212. }
  213.  
  214. ---- Run Functions
  215.  
  216. function runMenu()
  217. while true do
  218. term.clear()
  219. drawHeader()
  220. mopt[menustate].draw()
  221. if #mopt[menustate].options > 0 then
  222. local id, key = os.pullEvent("key")
  223. if key == keys.up and select > 1 then select = select-1
  224. elseif key == keys.down and select < #mopt[menustate].options then select = select + 1
  225. elseif key == keys.enter then
  226. if mopt[menustate].options[select] == "quit" then break end
  227. menustate = mopt[menustate].options[select]
  228. select = 1
  229. end
  230. end
  231. end
  232. end
  233.  
  234. runMenu()
Add Comment
Please, Sign In to add comment