public async Task<List<PlayItem>> Get(string username) { var cachedResult = Cache.Default.Get(Cache.PlaysKey(username)) as List<PlayItem>; if (cachedResult != null) { Debug.WriteLine("Served Plays from Cache."); return cachedResult; } var client = new BoardGameGeekClient(); var plays = await client.LoadPlays(username); var gameIds = new HashSet<int>(plays.Items.Select(g => g.GameId)); var gameDetails = await client.ParallelLoadGames(gameIds); var gameDetailsById = gameDetails.ToDictionary(g => g.GameId); var response = (from p in plays.Items orderby p.PlayDate descending let g = gameDetailsById[p.GameId] select new PlayItem { GameId = p.GameId, Name = p.Name, Image = g.Image, Thumbnail = g.Thumbnail, PlayDate = p.PlayDate, NumPlays = p.NumPlays, Comments = p.Comments }).ToList(); Cache.Default.Set(Cache.PlaysKey(username), response, DateTimeOffset.Now.AddSeconds(15)); return response; }
public async Task <List <PlayItem> > Get(string username, int?gameId = null) { var cachedResult = Cache.Default.Get(Cache.PlaysKey(username, gameId)) as List <PlayItem>; if (cachedResult != null) { Debug.WriteLine("Served Plays from Cache."); return(cachedResult); } var client = new BoardGameGeekClient(); var plays = await client.LoadPlays(username, 1, null, null, gameId); var gameIds = new HashSet <int>(plays.Items.Select(g => g.GameId)); var gameDetails = await client.ParallelLoadGames(gameIds); var gameDetailsById = gameDetails.ToDictionary(g => g.GameId); var response = (from p in plays.Items orderby p.PlayDate descending let g = gameDetailsById[p.GameId] select new PlayItem { GameId = p.GameId, Name = p.Name, Image = g.Image, Thumbnail = g.Thumbnail, PlayDate = p.PlayDate, NumPlays = p.NumPlays, Comments = p.Comments, Players = p.Players }).ToList(); Cache.Default.Set(Cache.PlaysKey(username), response, DateTimeOffset.Now.AddSeconds(15)); return(response); }
public async Task<Challenge> Get(int id) { var cachedResult = Cache.Default.Get(Cache.ChallengeKey(id)) as Challenge; if (cachedResult != null) { Debug.WriteLine("Served Collection from Cache."); return cachedResult; } var client = new BoardGameGeekClient(); var geeklist = await client.LoadGeekList(id); var options = ParseOptions(geeklist.Description); var goalPerGame = options.ContainsKey("GoalPerGame") ? int.Parse((string)options["GoalPerGame"]) : -1; var start = options.ContainsKey("Start") ? DateTime.Parse((string)options["Start"]) : (DateTime?)null; var end = options.ContainsKey("End") ? DateTime.Parse((string)options["End"]) : (DateTime?)null; var pageOne = await client.LoadPlays(geeklist.Username, 1, start, end); var plays = new List<PlayItem>(); List<Task<Plays>> tasks = new List<Task<Plays>>(); plays.AddRange(pageOne.Items); if (pageOne.Total > 100) { int remaining = pageOne.Total - 100; int page = 2; while (remaining > 0) { tasks.Add(client.LoadPlays(geeklist.Username, page, start, end)); page++; remaining -= 100; } } Challenge challenge = new Challenge() { GeekListId = id, Title = geeklist.Title, Username = geeklist.Username, Start = start != null ? start.Value.ToString("yyyy-MM-dd") : null, End = start != null ? end.Value.ToString("yyyy-MM-dd") : null, GoalPerGame = goalPerGame, Items = new List<ChallengeItem>() }; var games = from item in geeklist.Items select new { GameId = item.GameId, Name = item.Name, Description = item.Description }; var itemsById = new Dictionary<int, ChallengeItem>(); foreach (var item in games) { var gameOptions = ParseOptions(item.Description); var challengeItem = new ChallengeItem() { GameId = item.GameId, Name = item.Name }; if (gameOptions.ContainsKey("AltName")) { challengeItem.Name = (string)gameOptions["AltName"]; } if (gameOptions.ContainsKey("AdditionalGameId")) { challengeItem.AdditionalGameIds = new List<int>(); var list = gameOptions["AdditionalGameId"] as List<string>; if (list == null) { challengeItem.AdditionalGameIds.Add(int.Parse((string)gameOptions["AdditionalGameId"])); } else { challengeItem.AdditionalGameIds.AddRange(list.Select(i => int.Parse((string)i))); } } challenge.Items.Add(challengeItem); itemsById[item.GameId] = challengeItem; if (challengeItem.AdditionalGameIds != null) { foreach (var altGameId in challengeItem.AdditionalGameIds) { itemsById[altGameId] = challengeItem; } } } var additionalPages = await Task.WhenAll(tasks); foreach (var page in additionalPages) { plays.AddRange(page.Items); } foreach (var play in plays) { if (itemsById.ContainsKey(play.GameId)) { itemsById[play.GameId].PlayCount++; } } challenge.Complete = true; foreach (var game in challenge.Items) { if (game.PlayCount >= goalPerGame) { game.Complete = true; } else { challenge.Complete = false; } } Cache.Default.Set(Cache.ChallengeKey(id), challenge, DateTimeOffset.Now.AddSeconds(15)); return challenge; }
public async Task <Challenge> Get(int id) { var cachedResult = Cache.Default.Get(Cache.ChallengeKey(id)) as Challenge; if (cachedResult != null) { Debug.WriteLine("Served Collection from Cache."); return(cachedResult); } var client = new BoardGameGeekClient(); var geeklist = await client.LoadGeekList(id); var options = ParseOptions(geeklist.Description); var goalPerGame = options.ContainsKey("GoalPerGame") ? int.Parse((string)options["GoalPerGame"]) : -1; var start = options.ContainsKey("Start") ? DateTime.Parse((string)options["Start"]) : (DateTime?)null; var end = options.ContainsKey("End") ? DateTime.Parse((string)options["End"]) : (DateTime?)null; var pageOne = await client.LoadPlays(geeklist.Username, 1, start, end); var plays = new List <PlayItem>(); List <Task <Plays> > tasks = new List <Task <Plays> >(); plays.AddRange(pageOne.Items); if (pageOne.Total > 100) { int remaining = pageOne.Total - 100; int page = 2; while (remaining > 0) { tasks.Add(client.LoadPlays(geeklist.Username, page, start, end)); page++; remaining -= 100; } } Challenge challenge = new Challenge() { GeekListId = id, Title = geeklist.Title, Username = geeklist.Username, Start = start != null?start.Value.ToString("yyyy-MM-dd") : null, End = start != null?end.Value.ToString("yyyy-MM-dd") : null, GoalPerGame = goalPerGame, Items = new List <ChallengeItem>() }; var games = from item in geeklist.Items select new { GameId = item.GameId, Name = item.Name, Description = item.Description }; var itemsById = new Dictionary <int, ChallengeItem>(); foreach (var item in games) { var gameOptions = ParseOptions(item.Description); var challengeItem = new ChallengeItem() { GameId = item.GameId, Name = item.Name }; if (gameOptions.ContainsKey("AltName")) { challengeItem.Name = (string)gameOptions["AltName"]; } if (gameOptions.ContainsKey("AdditionalGameId")) { challengeItem.AdditionalGameIds = new List <int>(); var list = gameOptions["AdditionalGameId"] as List <string>; if (list == null) { challengeItem.AdditionalGameIds.Add(int.Parse((string)gameOptions["AdditionalGameId"])); } else { challengeItem.AdditionalGameIds.AddRange(list.Select(i => int.Parse((string)i))); } } challenge.Items.Add(challengeItem); itemsById[item.GameId] = challengeItem; if (challengeItem.AdditionalGameIds != null) { foreach (var altGameId in challengeItem.AdditionalGameIds) { itemsById[altGameId] = challengeItem; } } } var additionalPages = await Task.WhenAll(tasks); foreach (var page in additionalPages) { plays.AddRange(page.Items); } foreach (var play in plays) { if (itemsById.ContainsKey(play.GameId)) { itemsById[play.GameId].PlayCount++; } } challenge.Complete = true; foreach (var game in challenge.Items) { if (game.PlayCount >= goalPerGame) { game.Complete = true; } else { challenge.Complete = false; } } Cache.Default.Set(Cache.ChallengeKey(id), challenge, DateTimeOffset.Now.AddSeconds(15)); return(challenge); }