public static async Task SaveAsync(RankingData ranking) { var new_ranks = ranking.Ranks.Take(10).ToList(); ranking.Ranks = new_ranks; var data = Newtonsoft.Json.JsonConvert.SerializeObject(ranking, Newtonsoft.Json.Formatting.None); IFolder rootFolder = FileSystem.Current.LocalStorage; var folder = await rootFolder.CreateFolderAsync("Ranking", CreationCollisionOption.OpenIfExists); var file = await folder.CreateFileAsync("ranking.drf", CreationCollisionOption.ReplaceExisting); await file.WriteAllTextAsync(data); }
public static RankingData Zero() { RankingData ranking = new RankingData() { Ranks = new List <RankData>(11) }; for (int i = 0; i < 10; i++) { var rank = new RankData() { Score = 0, RankedFileName = "" }; ranking.Ranks.Add(rank); } return(ranking); }
public static async Task <RankingData> LoadAsync() { var json = ""; IFolder rootFolder = FileSystem.Current.LocalStorage; var folder = await rootFolder.CreateFolderAsync("Ranking", CreationCollisionOption.OpenIfExists); var exists = await folder.CheckExistsAsync("ranking.drf"); if (exists == (ExistenceCheckResult.NotFound)) { return(RankingData.Zero()); } else if (exists == (ExistenceCheckResult.FileExists)) { var file = await folder.GetFileAsync("ranking.drf"); json = await file.ReadAllTextAsync(); } return(Newtonsoft.Json.JsonConvert.DeserializeObject <RankingData>(json)); }
public override async Task Load() { await base.Load(); _rankingData = await IORanking.LoadAsync(); if (_playData == null) { IORanking.SaveAsync(_rankingData).Wait(); return; } for (int i = 0; i < _rankingData.Ranks.Count; i++) { _rankingData.CurrentPlayData = _playData; var rankData = _rankingData.Ranks[i]; if (_playData.Score > rankData.Score) { //string fileName = System.Guid.NewGuid().ToString(); string fileName = DateTime.Now.ToString("yyyyMMddhhmiss"); string ext = ".drf"; _rankingData.Ranks.Insert(i, new RankData() { Score = _playData.Score, RankedFileName = fileName + ext, }); _rankingData.IsRankinCurrentPlayData = true; _rankingData.RankinIndex = i; IORanking.SaveAsync(_rankingData).Wait(); string json = Newtonsoft.Json.JsonConvert.SerializeObject(_playData, Newtonsoft.Json.Formatting.None); json = Encode.Deflate.Encode(json); Task.Run(async() => { IFolder rootFolder = FileSystem.Current.LocalStorage; var folder = await rootFolder.CreateFolderAsync("PlayData", CreationCollisionOption.OpenIfExists); var file = await folder.CreateFileAsync(fileName + ext, CreationCollisionOption.ReplaceExisting); await file.WriteAllTextAsync(json); }).Wait(); break; } } Task.Run(async() => { IFolder rootFolder = FileSystem.Current.LocalStorage; var folder = await rootFolder.CreateFolderAsync("PlayData", CreationCollisionOption.OpenIfExists); var files = await folder.GetFilesAsync(); var rankedFileNames = _rankingData.Ranks.Take(10).Select(f => f.RankedFileName).ToArray(); foreach (var file in files) { if (!rankedFileNames.Contains(file.Name)) { await file.DeleteAsync(); } } }).Wait(); }