81 lines
2.3 KiB
GDScript
81 lines
2.3 KiB
GDScript
extends Node
|
|
|
|
func save_cloud_data(data,collection_id):
|
|
var auth = Firebase.Auth.auth
|
|
if auth.localid:
|
|
var collection: FirestoreCollection = Firebase.Firestore.collection(collection_id)
|
|
var document = await collection.get_doc(auth.localid)
|
|
if document:
|
|
for key in data.keys():
|
|
document.add_or_update_field(key,data[key])
|
|
var update_task = await collection.update(document)
|
|
if update_task:
|
|
print("Document updated successfully")
|
|
else:
|
|
print("Failed to update document")
|
|
else:
|
|
await collection.add(auth.localid,data)
|
|
|
|
func load_cloud_data(collection_id):
|
|
var auth = Firebase.Auth.auth
|
|
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 str(GameGlobals.all_user_leaderboards[GameGlobals.user_id][GameGlobals.last_leaderboard_id])
|
|
|
|
func save_leaderboard_data():
|
|
var leaderboard_data = {
|
|
"high_score" : GameGlobals.high_score,
|
|
"money" : GameGlobals.money,
|
|
"deposited_money" : GameGlobals.deposited_money
|
|
}
|
|
var leaderboard_name = get_leaderboard_name()
|
|
var data = {
|
|
leaderboard_name : leaderboard_data
|
|
}
|
|
var collection = "leaderboard"
|
|
save_cloud_data(data,collection)
|
|
|
|
func load_leaderboard_data():
|
|
var collection = get_leaderboard_name()
|
|
load_cloud_data(collection)
|
|
|
|
func save_user_data():
|
|
var leaderboard_data = {
|
|
"last_hit_path" : GameGlobals.last_hit_path,
|
|
"current_match_id" : GameGlobals.current_match_id,
|
|
"current_round_id" : GameGlobals.current_round_id,
|
|
"enemies_killed" : GameGlobals.enemies_killed,
|
|
"held_guns" : SaveLoad.save_resource_path(GameGlobals.held_guns),
|
|
"gun_ammo" : GameGlobals.gun_ammo,
|
|
"ammo_reserve" : GameGlobals.ammo_reserve,
|
|
"current_gun_index" : GameGlobals.current_gun_index
|
|
}
|
|
|
|
var leaderboard_name = get_leaderboard_name()
|
|
|
|
var data ={
|
|
leaderboard_name : leaderboard_data
|
|
}
|
|
var collection = "user_data"
|
|
|
|
save_cloud_data(data,collection)
|
|
|
|
func save_persistent_data():
|
|
var data = {
|
|
"user_name" : GameGlobals.user_names[0],
|
|
"last_leaderboard_id" : GameGlobals.last_leaderboard_id,
|
|
"all_user_leaderboards" : GameGlobals.all_user_leaderboards[0]
|
|
}
|
|
|
|
var collection = "persistent_data"
|
|
|
|
save_cloud_data(data,collection)
|