Example #1
0
        public async Task Logout()
        {
            _username = null;
            _password = null;
            _session  = null;
            SecureStorage.RemoveAll();

            await GamesDb.Delete();

            await PlaysDb.Delete();

            await VideosDb.Delete();
        }
Example #2
0
        private async Task Init()
        {
            Mapper         = CreateMapper();
            RankDb         = new RankingDb();
            GamesDb        = new GamesDb();
            VideosDb       = new VideosDb();
            PlaysDb        = new PlaysDb();
            _httpClient    = new HttpClient();
            _bggApiClient  = new BoardGameGeekXmlApi2Client(_httpClient);
            _unofficialBgg = new UnofficialBggClient(_httpClient, _bggApiClient);

            if (!string.IsNullOrEmpty(await Username()))
            {
                var c = new BggCredentials()
                {
                    Username  = await Username(),
                    Password  = await Password(),
                    SessionId = await Session()
                };

                _unofficialBgg.Credentials = c;
            }
        }
        /// <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);
        }
Example #4
0
 public async Task <Game> StoreGame(Game game)
 {
     return(await GamesDb.AddOrUpdateGame(game));
 }
Example #5
0
 public async Task <IList <Game> > RetrieveGames()
 {
     return((IList <Game>) await GamesDb.GetAllGames());
 }
Example #6
0
 public async Task <Game> RetrieveGame(int id)
 {
     return(await GamesDb.GetGame(id));
 }