private static List <PlayerReplayModel> GetPlayerReplaysLocal(string levelName, int numOfRecords) { List <PlayerReplayModel> playerPlaybacks = new List <PlayerReplayModel>(); for (int i = 0; i < numOfRecords; i++) { string playerDataFilePath = _playerCompletedDataLocation + _playerDataFileName + i + ".json"; if (File.Exists(playerDataFilePath)) { PlayerReplayModel playerReplayModel = JsonUtility.FromJson <PlayerReplayModel> (File.ReadAllText(playerDataFilePath)); if (playerReplayModel.LevelName == levelName) { playerPlaybacks.Add(JsonUtility.FromJson <PlayerReplayModel> (File.ReadAllText(playerDataFilePath))); } } else { break; } } return(playerPlaybacks); }
static void SavePlayerReplayLocal(PlayerReplayModel playerReplay, int insertIndex, string playerDataLocation) { string lastItemFilePath; string nextToLastFilePath; string[] tempFiles; int numOfExistingModels; // serialize playerPlayback var bytes = System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(playerReplay)); // find out how many files we currently have tempFiles = Directory.GetFiles(playerDataLocation, "*.json", SearchOption.TopDirectoryOnly); numOfExistingModels = tempFiles.Length; try { // copy files down from insertIndex to make room for insertIndex item for (int i = numOfExistingModels; i > insertIndex; i--) { lastItemFilePath = playerDataLocation + _playerDataFileName + (i - 1) + ".json"; nextToLastFilePath = playerDataLocation + _playerDataFileName + i + ".json"; System.IO.File.Copy(lastItemFilePath, nextToLastFilePath, true); } // copy new file to insertIndex File.WriteAllBytes(playerDataLocation + _playerDataFileName + insertIndex + ".json", bytes); } catch (Exception e) { Debug.LogException(e); } // set NumOfCompletedRecords NumOfLocalRecords = Directory.GetFiles(_playerCompletedDataLocation, "*.json").Length; }
public static IEnumerator SavePlayerPlayback(PlayerReplayModel playerPlayback, Action <bool> action) { CheckConnection.Instance.StartCoroutine(CheckConnection.Instance.CheckInternetConnection((isConnected) => { if (isConnected) { _dataController.StartCoroutine(_dataController.AddReplay(playerPlayback, (success) => { if (success) { action(success); } })); } })); List <PlayerReplayModel> playerReplayModels = GetPlayerReplaysLocal(playerPlayback.LevelName, PlayerPrefs.GetInt(Constants.NUM_OF_LOCAL_GHOST_RECORDS)); if (playerReplayModels.Count == 0) { SavePlayerReplayLocal(playerPlayback, playerReplayModels.Count, _playerCompletedDataLocation); yield break; } for (int i = 0; i < playerReplayModels.Count; i++) { if (i < playerReplayModels.Count) { if (playerPlayback.ReplayTime < playerReplayModels[i].ReplayTime) { SavePlayerReplayLocal(playerPlayback, i, _playerCompletedDataLocation); yield break; } } } if (playerReplayModels.Count < PlayerPrefs.GetInt(Constants.NUM_OF_LOCAL_GHOST_RECORDS)) { SavePlayerReplayLocal(playerPlayback, playerReplayModels.Count, _playerCompletedDataLocation); } action(true); }