Advertisement
Susceptance

database_start

May 7th, 2022 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. accounts = {}
  2.  
  3. function save()
  4. local file = fs.open('accounts.txt',"w")
  5. file.write(textutils.serialize(accounts))
  6. file.close()
  7. end
  8.  
  9. if fs.exists('accounts.txt') == false then
  10. accounts = {['1111'] = {'Test man', 0}}
  11. save()
  12. end
  13.  
  14. file = fs.open('accounts.txt',"r")
  15. data = file.readAll()
  16. file.close()
  17. accounts = textutils.unserialise(data)
  18.  
  19. valueTable = {['minecraft:emerald'] = 400, ['minecraft:diamond'] = 200, ['minecraft:gold_ingot'] = 50, ['minecraft:iron_ingot'] = 20}
  20. gameCost = 99
  21.  
  22. function ServeRequest(operation, account, param)
  23. if operation == nil then
  24. return
  25. end
  26.  
  27. if accounts[account] == nil then
  28. return
  29. end
  30.  
  31. if account == nil then
  32. return
  33. end
  34.  
  35. if operation == 'account' then
  36. rednet.send(sender, textutils.serialise(accounts[account]), "C_ACC")
  37. print('Served account request '..account)
  38. elseif operation == 'depo' then
  39. item = param[1]
  40. amount = param[2]
  41.  
  42. if valueTable[item] ~= nil then
  43. accounts[account][2] = accounts[account][2] + valueTable[item] * amount
  44. print('Served depo request '..account)
  45. end
  46. elseif operation == 'with' then
  47. takeAmount = math.floor(accounts[account][2] / valueTable[param])
  48. accounts[account][2] = accounts[account][2] - valueTable[param] * takeAmount
  49.  
  50. rednet.send(sender, textutils.serialise({'withR', takeAmount}), "C_ACC")
  51. print('Served withdraw request '..account)
  52. elseif operation == 'add' then
  53. if accounts[account][2] + param >= 0 then
  54. accounts[account][2] = accounts[account][2] + param
  55.  
  56.  
  57. rednet.send(sender, textutils.serialise(true), "C_ACC")
  58. print('Edited balance of account '..account..' by '..param)
  59. else
  60. rednet.send(sender, textutils.serialise(false), "C_ACC")
  61. print('Failed to edit balance of account '..account..' by '..param)
  62. end
  63. end
  64. save()
  65. end
  66.  
  67. function ServeData()
  68. while true do
  69. sender,message,distance = rednet.receive("C_ACC")
  70. message = textutils.unserialise(message)
  71.  
  72. ServeRequest(message[1], message[2], message[3])
  73. end
  74. end
  75.  
  76. function ServeGames()
  77. while true do
  78. sender,message,distance = rednet.receive("C_GAME")
  79. message = textutils.unserialise(message)
  80.  
  81. if message[1] == 'add' then
  82. accounts[message[2]][2] = accounts[message[2]][2] + message[3]
  83. print('Edited balance of account '..message[2]..' by '..message[3])
  84. end
  85. save()
  86. end
  87. end
  88.  
  89. function Main()
  90. while true do
  91. print('new account: n')
  92. print('give/remove credit: c')
  93. print('view account: v')
  94. command = read()
  95. if command == 'n' then
  96. print('pin')
  97. pin = read()
  98. print('name')
  99. name = read()
  100. accounts[pin] = {name, 0}
  101.  
  102. print('Created account '..name..' with pin '..pin)
  103. end
  104.  
  105. if command == 'c' then
  106. print('pin')
  107. pin = read()
  108. print('amount')
  109. amount = read()
  110. print(accounts[pin][2])
  111. accounts[pin][2] = accounts[pin][2] + tonumber(amount)
  112.  
  113. print('Added '..amount..' credits to account '..pin)
  114. print('Total credits '..accounts[pin][2])
  115. end
  116.  
  117. if command == 'v' then
  118. print('pin or name')
  119. pin = read()
  120. amount = read()
  121. print('Name: '..accounts[pin][1])
  122. print('Pin: '..pin)
  123. print('Credits: '..accounts[pin][2])
  124. end
  125.  
  126. save()
  127. end
  128. end
  129.  
  130. rednet.open("top")
  131. rednet.host("C_ACC", "database")
  132.  
  133. parallel.waitForAll(ServeData, ServeGames, Main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement