Advertisement
gravitowl

[GraviBank] database

Jan 12th, 2021 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. rednet.open("top")
  2.  
  3. local requestTypes = {"userExists", "checkPassword", "getBalance", "getLoan"}
  4.  
  5. local postTypes = {"newUser", "newPassword", "removeUser", "changeBalance", "changeLoan"}
  6.  
  7. local dbDir = "disk/database.txt"
  8.  
  9. function readTable(dir)
  10. local file = fs.open(dir, "r")
  11. local data = file.readAll()
  12. file.close()
  13. return textutils.unserialize(data)
  14. end
  15.  
  16. function saveTable(table, dir)
  17. local file = fs.open(dir, "w")
  18. file.write(textutils.serialize(table))
  19. file.close()
  20. end
  21.  
  22. if not fs.exists(dbDir) then
  23. saveTable({}, dbDir)
  24. end
  25.  
  26. function handleRequests(data)
  27. local requestType = "false_request"
  28. for i, type in pairs(requestTypes) do
  29. if type == data[1] then
  30. requestType = type
  31. end
  32. end
  33.  
  34. if requestType == "false_request" then
  35. print("false_request TRUE")
  36. return "false_request"
  37. end
  38.  
  39. if requestType == "userExists" then
  40. local db = readTable(dbDir)
  41. if db[data[2]] then
  42. print("userExists TRUE")
  43. return "true"
  44. else
  45. print("userExists FALSE")
  46. return "false"
  47. end
  48.  
  49.  
  50. elseif requestType == "checkPassword" then
  51. local db = readTable(dbDir)
  52.  
  53. if db[data[2]] then
  54. if db[data[2]].password == data[3] then
  55. print("checkPassword TRUE")
  56. return "true"
  57. else
  58. print("checkPassword FALSE")
  59. return "false"
  60. end
  61. else
  62. print("checkPassword FALSE")
  63. return "false"
  64. end
  65.  
  66. elseif requestType == "getBalance" then
  67. local db = readTable(dbDir)
  68.  
  69. if db[data[2]] then
  70. print("getBalance TRUE")
  71. return db[data[2]].bal
  72. else
  73. print("getBalance FALSE")
  74. return "false"
  75. end
  76.  
  77. elseif requestType == "getLoan" then
  78. local db = readTable(dbDir)
  79.  
  80. if db[data[2]] then
  81. print("getLoan TRUE")
  82. return db[data[2]].loan
  83. else
  84. print("getLoan FALSE")
  85. return "false"
  86. end
  87. end
  88. end
  89.  
  90. function handlePosts(data)
  91. local postType = "false_post"
  92. for i, type in pairs(postTypes) do
  93. if type == data[1] then
  94. postType = type
  95. end
  96. end
  97.  
  98. if requestType == "false_post" then
  99. print("false_post TRUE")
  100. return "false_post"
  101. end
  102.  
  103. if postType == "newUser" then
  104. local db = readTable(dbDir)
  105. local exists = handleRequests({"userExists", data[2]})
  106.  
  107. if exists == "true" then
  108. print("newUser FALSE")
  109. return "false"
  110. else
  111. if data[3] ~= "" then
  112. db[data[2]] = {}
  113. db[data[2]].password = data[3]
  114. db[data[2]].bal = 0
  115. db[data[2]].loan = 0
  116. saveTable(db, dbDir)
  117. print("newUser TRUE")
  118. return "true"
  119. end
  120. end
  121.  
  122. elseif postType == "removeUser" then
  123. local db = readTable(dbDir)
  124. local exists = handleRequests({"userExists", data[2]})
  125.  
  126. if exists == "true" then
  127. db[data[2]] = nil
  128. saveTable(db, dbDir)
  129. print("removeUser TRUE")
  130. return "true"
  131. else
  132. print("removeUser FALSE")
  133. return "false"
  134. end
  135.  
  136. elseif postType == "newPassword" then
  137. local db = readTable(dbDir)
  138. local exists = handleRequests({"userExists", data[2]})
  139.  
  140. if exists == "true" then
  141. db[data[2]].password = data[3]
  142. saveTable(db, dbDir)
  143. print("newPassword TRUE")
  144. return "true"
  145. else
  146. print("newPassword FALSE")
  147. return "false"
  148. end
  149.  
  150. elseif postType == "changeBalance" then
  151. local db = readTable(dbDir)
  152. local exists = handleRequests({"userExists", data[2]})
  153.  
  154. if exists == "true" then
  155. db[data[2]].bal = db[data[2]].bal + data[3]
  156. saveTable(db, dbDir)
  157. print("changeBalance TRUE")
  158. return "true"
  159. else
  160. print("changeBalance FALSE")
  161. return "false"
  162. end
  163.  
  164. elseif postType == "changeLoan" then
  165. local db = readTable(dbDir)
  166. local exists = handleRequests({"userExists", data[2]})
  167.  
  168. if exists == "true" then
  169. db[data[2]].loan = db[data[2]].loan + data[3]
  170. saveTable(db, dbDir)
  171. print("changeLoan TRUE")
  172. return "true"
  173. else
  174. print("changeLoan FALSE")
  175. return "false"
  176. end
  177. end
  178. end
  179.  
  180. while true do
  181. local senderID, receivedMessage = rednet.receive("graviBank")
  182. if type(receivedMessage) ~= "table" then
  183. print("isTable FALSE")
  184. rednet.send(senderID, {"unrecognized_datatype"}, "graviBank")
  185. else
  186. if receivedMessage[1] == "request" then
  187. rednet.send(senderID, {handleRequests(receivedMessage[2])}, "graviBank")
  188. elseif receivedMessage[1] == "post" then
  189. rednet.send(senderID, {handlePosts(receivedMessage[2])}, "graviBank")
  190. else
  191. print("isCorrectMessagetype FALSE")
  192. rednet.send(senderID, {"unrecognized_messagetype"}, "graviBank")
  193. end
  194. end
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement