Snowdingerr

SnowCenter - Channel12

May 27th, 2021 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. -- Bank server
  2.  
  3. -- Channels
  4. local BANK_SERVER = 12
  5. local REGISTER_COMPUTER = 14
  6. local IMAGE_SERVER = 17
  7. local EVENT_MANAGER = 18
  8. local TURTLESCREEN_COMS = 19
  9. local CARDREADER = 21
  10. rednet.open("top")
  11.  
  12. -- Libs
  13.  
  14. -- Code
  15.  
  16. local databasepath = "db"
  17. local db
  18. local defaultbalance = 0
  19.  
  20. if not fs.exists(databasepath) then
  21.     database = {}
  22.     local file = fs.open(databasepath, "w")
  23.     file.write("{}")
  24.     file.close()
  25. else    
  26.     local file = fs.open(databasepath, "r")
  27.     database = textutils.unserialise(file.readAll())
  28.     file.close()
  29. end
  30.  
  31. print("initialized database with success")
  32.  
  33. while true do
  34.     local id, data = rednet.receive("bank")
  35.     print(textutils.serialise(data))
  36.     if data.type == "getbalance" then
  37.         print("getting balance of user ", data.user)
  38.         rednet.send(id, database[data.user], "bank")
  39.     elseif data.type == "setbalance" then
  40.         print("setting balance of " .. data.user .. " to " .. data.balance)
  41.         database[data.user].balance = data.balance
  42.         local file = fs.open(databasepath, "w")
  43.         file.write(textutils.serialise(database))
  44.         file.close()
  45.         rednet.send(id, nil, "bank")
  46.     elseif data.type == "registeruser" then
  47.         print("Registering user " .. data.user .. ":" .. data.name)
  48.         database[data.user] = {
  49.             name=data.name,
  50.             balance=defaultbalance
  51.         }
  52.         local file = fs.open(databasepath, "w")
  53.         file.write(textutils.serialise(database))
  54.         file.close()
  55.         rednet.send(id, nil, "bank")
  56.     end
  57. end
  58.  
  59.  
Add Comment
Please, Sign In to add comment