// Uploads the time data to the database, and returns the current top time list. public static Task <UserScore> UploadReplay( long time, LevelMap map, ReplayData replay) { // Get a client-generated unique id based on timestamp and random number. string key = FirebaseDatabase.DefaultInstance.RootReference.Push().Key; Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; string name = (auth.CurrentUser != null && !string.IsNullOrEmpty(auth.CurrentUser.DisplayName)) ? auth.CurrentUser.DisplayName : StringConstants.UploadScoreDefaultName; string userId = (auth.CurrentUser != null && !string.IsNullOrEmpty(auth.CurrentUser.UserId)) ? auth.CurrentUser.UserId : StringConstants.UploadScoreDefaultName; string replayPath = replay != null ? Storage_Replay_Root_Folder + GetPath(map) + key : null; var userScore = new Firebase.Leaderboard.UserScore( userId, name, time, DateTime.Now.Ticks / TimeSpan.TicksPerSecond, new Dictionary <string, object> { { Database_Property_ReplayPath, replayPath } }); UploadConfig config = new UploadConfig() { key = key, storagePath = replayPath, dbRankPath = GetDBRankPath(map) + key, dbSharedReplayPath = GetDBSharedReplayPath(map) + key, shareReplay = replay != null //TODO(chkuang): && GameOption.shareReplay }; if (replay == null) { // Nothing to upload, return user score to upload to leaderboard. return(Task.FromResult(userScore)); } else { return(UploadReplayData(userScore, replay, config) .ContinueWith(task => { if (config.shareReplay) { var dbRef = FirebaseDatabase.DefaultInstance.RootReference; return dbRef.Child(config.dbSharedReplayPath) .SetValueAsync(userScore.ToDictionary()); } else { return null; }; }).ContinueWith(task => userScore)); } }
// Returns the top times, given the level's database path and map id. public static Task <List <TimeData> > GetTopTimes(LevelMap map) { DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference(GetPath(map)); return(reference.GetValueAsync().ContinueWith(task => { return GetTimeList(task.Result); })); }
// Gets the path, given the level's database path and map id. // This path is used for both database and storage. private static string GetPath(LevelMap map) { if (!string.IsNullOrEmpty(map.DatabasePath)) { return(map.DatabasePath + "/"); } else { return("OfflineMaps/" + map.mapId + "/"); } }
// Uploads the time data to the database, and returns the current top time list. public Task <List <TimeData> > UploadTime(LevelMap map) { DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference(GetPath(map)); return(reference.RunTransaction( mutableData => UploadScoreTransaction(mutableData, this)) .ContinueWith(task => { return GetTimeList(task.Result); })); }
// Iterates through a map and spawns all the objects in it. public void SpawnWorld(LevelMap map) { foreach (MapElement element in map.elements.Values) { GameObject obj = PlaceTile(element); } worldMap.SetProperties(map.name, map.mapId, map.ownerId, map.DatabasePath); HasPendingEdits = false; // Set the camera to the start position: MapObjects.StartPosition startPos = FindObjectOfType <MapObjects.StartPosition>(); if (startPos) { CommonData.mainCamera.MoveCameraTo(startPos.gameObject.transform.position); } }
// Utility function to get the top shared replay record storage path from the database public static Task <string> GetBestSharedReplayPathAsync(LevelMap map) { DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference(GetDBSharedReplayPath(map)); return(reference.OrderByChild(Database_Property_Time).LimitToFirst(1).GetValueAsync() .ContinueWith((task) => { if (task.Result.ChildrenCount == 0) { return null; } UserScore replayScore = new UserScore(task.Result.Children.First()); if (replayScore != null && replayScore.OtherData != null && replayScore.OtherData.ContainsKey(Database_Property_ReplayPath)) { return replayScore.OtherData[Database_Property_ReplayPath] as string; } return null; })); }
// Gets the path for the top shared replays on the database, given the level's database path // and map id. Note that not everyone wants to share replay record private static string GetDBSharedReplayPath(LevelMap map) { return("Leaderboard/Map/" + GetPath(map) + Database_Replays_Postfix); }
// Gets the path for the top ranks on the database, given the level's database path // and map id. public static string GetDBRankPath(LevelMap map) { return("Leaderboard/Map/" + GetPath(map) + Database_Ranks_Postfix); }