public async Task Should_retrieve_logged_plays_for_user() { PlaysResponse response = await bgg.GetPlaysAsync(new PlaysRequest(USERNAME)); Assert.True(response.Succeeded); PlaysResponse.PlaysCollection collection = response.Result; Assert.NotNull(collection); Assert.Equal(USERNAME, collection.Username); Assert.Equal(1266617, collection.UserId); Assert.Equal(1, collection.Total); Assert.Equal(1, collection.Page); Assert.Equal("https://boardgamegeek.com/xmlapi/termsofuse", collection.TermsOfUse); Assert.Single(collection.Plays); PlaysResponse.Play play = collection.Plays[0]; Assert.Equal(34275125, play.Id); Assert.Equal(new DateTime(2019, 3, 7), play.Date); Assert.Equal(1, play.Quantity); Assert.Equal(35, play.Length); Assert.False(play.Incomplete); Assert.False(play.NowInStats); Assert.Equal("hillsboro", play.Location); Assert.Equal("this is just comments field", play.Comments); Assert.Equal(2, play.Players.Count); PlaysResponse.Item item = play.Item; Assert.Equal("Isle of Skye: From Chieftain to King", item.Name); Assert.Equal("thing", item.ObjectType); Assert.Equal(176494, item.ObjectId); Assert.Contains("boardgame", item.SubTypes); PlaysResponse.Player player1 = play.Players[0]; Assert.Equal(USERNAME, player1.Username); Assert.Equal(1266617, player1.UserId); Assert.Equal("Jake Bruun", player1.Name); Assert.Equal("0", player1.StartPosition); Assert.Equal("green", player1.Color); Assert.Equal("42", player1.Score); Assert.False(player1.New); Assert.Equal(0, player1.Rating); Assert.True(player1.Win); PlaysResponse.Player player2 = play.Players[1]; Assert.Equal("", player2.Username); Assert.Equal(0, player2.UserId); Assert.Equal("Robyn", player2.Name); Assert.Equal("0", player2.StartPosition); Assert.Equal("red", player2.Color); Assert.Equal("34", player2.Score); Assert.False(player2.New); Assert.Equal(0, player2.Rating); Assert.False(player2.Win); }
/// <summary> /// Fetches (from BGG) plays for the current logged in user. Stores play data and games in db. /// </summary> /// <returns>List of Plays</returns> public async Task <IList <Play> > FetchPlays() { PlaysRequest request = new PlaysRequest(await Username()); PlaysResponse response = await _bggApiClient.GetPlaysAsync(request); PlaysResponse.PlaysCollection collection = response.Result; int pages = (int)Math.Ceiling(collection.Total / (double)100); List <PlaysResponse.Play> allPlays = new List <PlaysResponse.Play>(); allPlays.AddRange(collection.Plays); // Okay we've got page 1 containing the first 100 logged plays for (int i = 2; i <= pages; i++) { request = new PlaysRequest(await Username(), page: i); // delay for a bit to prevent slamming the bgg server with requests Thread.Sleep(300); response = await _bggApiClient.GetPlaysAsync(request); allPlays.AddRange(response.Result.Plays); } var plays = Mapper.Map <List <Play> >(allPlays); List <int> incomplete = new List <int>(); // Further processing. The game objects are (very) incomplete foreach (var p in plays) { var g = await RetrieveGame(p.Game.Id); p.User = await this.Username(); //If the game isn't already in the db, add it to the list to fetch if (g == null) { incomplete.Add(p.Game.Id); } else { p.GameId = g.Id; p.Game = g; } } if (incomplete.Any()) { //bulk fetch "missing" (from TCK db) games var missingGames = await FetchGame(incomplete.ToArray()); foreach (var g in missingGames) { var x = await GamesDb.AddOrUpdateGame(g); //update the game in the play var p = plays.Where(p => p.GameId == g.Id); foreach (var pp in p) { pp.Game = x; pp.GameId = x.Id; } } } //Add plays to db foreach (var p in plays) { await PlaysDb.AddOrUpdateItemAsync(p); } return(plays); }