//--------------------------------------------------------------------------------------------------- #region Private Functions private async Task GetLeaderBoardData()// Attempts to fetch LeaderBoard data from db { if (await Internet.CheckInternetConnectivity()) { GameData.SetLeaderBoardJSON(await Database.LoadLeaderBoardData()); } }
private async Task SaveInfo() //Saves the current LeaderBoard players into a JSON string { try { if (await Internet.CheckInternetConnectivity()) { var dbData = gameData.GetLeaderBoardJSON(); if (Misc.IsStringValid(dbData))//If player loaded leaderboard data from db, then save { var jsonList = PrepareSaveData(); await Database.SaveLeaderBoard(jsonList); GameData.SetLeaderBoardJSON(string.Empty); Debug.Log($"Internet connection <color=blue>found</color>, Saved {nameof(LeaderBoard)} data to db."); } else//If player loaded leaderboard data from local storage, then compare local vs online data and then save { var onlineDataJSON = await Database.LoadLeaderBoardData(); if (Misc.IsStringValid(onlineDataJSON)) { var onlineData = JsonUtility.FromJson <ListOfPlayers>(onlineDataJSON); SortingAlgorithm(onlineData.leaderBList); var jsonList = PrepareSaveData(); await Database.SaveLeaderBoard(jsonList); } } } else { var jsonList = PrepareSaveData(); Debug.Log($"Internet connection <color=blue>NOT found</color>, Saved {nameof(LeaderBoard)} data to PlayerPrefs."); PlayerPrefs.SetString(key, jsonList); PlayerPrefs.Save(); //Debug.Log($"Saved the json data as: {PlayerPrefs.GetString(key)}, jsonData: {jsonList}"); } } catch (Exception e) { Misc.HandleException(e, gameData.GetExcLeaderBoardSaveInfo()); } }
//--------------------------------------------------------------------------------------------------- internal async Task SaveData() //Saves acquired data into a file { FileStream file = File.Create(Application.persistentDataPath + $"/{saveFileName}"); //TODO: Revisit this change later, will it work the same for db? //TODO: Consider instead, opening the file inside the try block and referencing an outside variable to it try { playerName = gameData.GetPlayerName(); saveFileName = playerName + GameData.saveFormat; bool internetConnection = await Internet.CheckInternetConnectivity(); if (internetConnection) { //Substitute with code to save data onto online db BinaryFormatter bf = new BinaryFormatter(); PlayerPrefs.SetInt(saveKey, 1); PlayerPrefs.Save(); bf.Serialize(file, data); Debug.Log("Game Saved. With Internet connection"); } else { BinaryFormatter bf = new BinaryFormatter(); PlayerPrefs.SetInt(saveKey, 1); PlayerPrefs.Save(); bf.Serialize(file, data); Debug.Log("Game Saved. Without Internet connection"); } } catch (Exception e) { Misc.HandleException(e, gameData.GetExcSaveData()); } finally { file.Close(); } }