Coti5432

Lotto Mainframe

Nov 27th, 2021 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. rednet.open("bottom")
  2. local databasePath = "db"
  3. local database
  4.  
  5. if not fs.exists(databasePath) then
  6. database = {}
  7. local file = fs.open(databasePath, "w")
  8. file.write("{}")
  9. file.close()
  10. else
  11. local file = fs.open(databasePath, "r")
  12. database = textutils.unserialise(file.readAll())
  13. file.close()
  14. end
  15.  
  16. print("Database loaded.")
  17.  
  18. while true do
  19. local id, data = rednet.receive("otto")
  20. print(textutils.serialise(data))
  21. if data.type == "getPlayerBalance" then
  22. print("Fetching balance for ", data.player)
  23. rednet.send(id, database[data.player], "otto")
  24. elseif data.type == "setPlayerBalance" then
  25. print("Setting balance for ", data.player, " to ", data.balance)
  26. database[data.player].balance = data.balance
  27. local file = fs.open(databasePath, "w")
  28. file.write(textutils.serialise(database))
  29. file.close()
  30. rednet.send(id, nil, "otto")
  31. elseif data.type == "addPlayer" then
  32. print("Adding player: #"..data.player, data.name)
  33. database[data.player] = {
  34. name=data.name,
  35. balance=0
  36. }
  37. local file = fs.open(databasePath, "w")
  38. file.write(textutils.serialise(database))
  39. file.close()
  40. rednet.send(id, nil, "otto")
  41.  
  42. end
  43. end
  44.  
Add Comment
Please, Sign In to add comment