Files
fps_project_1/scripts/leaderboard.gd

45 lines
1.4 KiB
GDScript

extends Node
func save_leaderboard_data():
var auth = Firebase.Auth.auth
var COLLECTION_ID = get_leaderboard_name()
if auth.localid:
var collection: FirestoreCollection = Firebase.Firestore.collection(COLLECTION_ID)
var data : Dictionary = {
"user_name" : GameGlobals.user_names[GameGlobals.user_id],
"leaderboard" : get_leaderboard_name(),
"high_score" : GameGlobals.high_score,
"money" : GameGlobals.money,
"deposited_money" : GameGlobals.deposited_money
}
print("DATA: ",data)
var document = await collection.get_doc(auth.localid)
if document:
for key in data.keys():
document.add_or_update_field(key,data[key])
var task = await collection.update(document)
if task:
print("Document updated successfully")
else:
print("Failed to update document")
else:
await collection.add(auth.localid,data)
func load_leaderboard_data():
var auth = Firebase.Auth.auth
var COLLECTION_ID = get_leaderboard_name()
if auth.localid:
var collection : FirestoreCollection = Firebase.Firestore.collection(COLLECTION_ID)
var document = await collection.get_doc(auth.localid)
if document:
print(document)
return document
else:
print("Failed to load document from Firebase")
func get_leaderboard_name():
return "leaderboard_" + str(GameGlobals.all_user_leaderboards[GameGlobals.user_id][GameGlobals.last_leaderboard_id])