Пример #1
0
        public override async Task LoadAsync()
        {
            IsLoading = true;
            if (ItemSelecionado?.Descricao == null)
            {
                var resultado = await BGGClient.GetItemDetalhe(idItem);

                ItemSelecionado = resultado;
            }

            IsLoading = false;
        }
Пример #2
0
        private async void ExecuteBuscaCommand(object obj)
        {
            /*
             * //Aqui ocorre a busca
             * var resultado = new List<ItemLista>()
             * {
             *  new ItemLista()
             *  {
             *      Id = "218704",
             *      Thumbnail = new ItemThumbnail(){
             *          Value = "https://cf.geekdo-images.com/images/pic2823310_t.jpg"
             *      },
             *      Nome = new ItemNome()
             *      {
             *          Value = "nome do livro"
             *      },
             *      AnoPublicacao = new ItemAnoPublicacao()
             *      {
             *          Value = "2017"
             *      }
             *  },
             *  new ItemLista()
             *  {
             *      Id = "218704",
             *      Thumbnail = new ItemThumbnail(){
             *          Value = "https://cf.geekdo-images.com/images/pic2823310_t.jpg"
             *      },
             *      Nome = new ItemNome()
             *      {
             *          Value = "nome do livro"
             *      },
             *      AnoPublicacao = new ItemAnoPublicacao()
             *      {
             *          Value = "2017"
             *      }
             *  }
             * };
             */
            IsLoading = true;
            var resultado = await BGGClient.GetItems(QueryBusca);

            //Lista.Clear();

            foreach (var itemResultado in resultado)
            {
                Lista.Add(itemResultado);
            }

            IsLoading = false;
        }
Пример #3
0
        private async static Task FetchUser(BGGClient client, string username, bool retry)
        {
            var request = new BGGCollectionRequest {
                Username = username
            };

            var task     = client.GetCollectionAsync(request);
            var response = await task;

            if (response.StatusCode == HttpStatusCode.Accepted)
            {
                if (retry)
                {
                    Console.WriteLine("Waiting to get collection for {0}", username);
                    Thread.Sleep(TimeSpan.FromSeconds(30));
                    await FetchUser(client, username, false);

                    return;
                }
                else
                {
                    Console.WriteLine("Failed to fetch collection at second attempt for {0}", username);
                }
            }

            var collection = response.Data;

            lock (LockObject)
            {
                foreach (var game in collection.Items)
                {
                    var id = game.Id;

                    GameNames[id] = game.Name;

                    if (game.Owned)
                    {
                        var owners = Owners.GetOrCreate(id, () => new List <string>());
                        owners.Add(username);
                    }

                    if (game.WantToPlay)
                    {
                        var players = WantToPlay.GetOrCreate(id, () => new List <string>());
                        players.Add(username);
                    }
                }
            }
        }
Пример #4
0
        private async static Task FetchUser(BGGClient client, string username, bool retry)
        {
            var request = new BGGCollectionRequest { Username = username };

            var task = client.GetCollectionAsync(request);
            var response = await task;

            if (response.StatusCode == HttpStatusCode.Accepted)
            {
                if (retry)
                {
                    Console.WriteLine("Waiting to get collection for {0}", username);
                    Thread.Sleep(TimeSpan.FromSeconds(30));
                    await FetchUser(client, username, false);
                    return;
                }
                else
                {
                    Console.WriteLine("Failed to fetch collection at second attempt for {0}", username);
                }
            }

            var collection = response.Data;

            lock (LockObject)
            {
                foreach (var game in collection.Items)
                {
                    var id = game.Id;

                    GameNames[id] = game.Name;

                    if (game.Owned)
                    {
                        var owners = Owners.GetOrCreate(id, () => new List<string>());
                        owners.Add(username);
                    }

                    if (game.WantToPlay)
                    {
                        var players = WantToPlay.GetOrCreate(id, () => new List<string>());
                        players.Add(username);
                    }
                }
            }
        }
        public override async Task LoadAsync()
        {
            IsLoading = true;
            if (Lista.Count == 0)
            {
                Lista.Clear();
                var resultado = await BGGClient.GetItems();



                foreach (var item in resultado)
                {
                    Lista.Add(item);
                }
            }
            IsLoading = false;
        }
Пример #6
0
        private async static Task <BGGCollection> FetchCollection(BGGClient client, BggDbContext db, DateTime now, string username, bool retry)
        {
            var existingUser = db.Users.SingleOrDefault(user => user.Username == username);

            if (existingUser != null && now - existingUser.CollectionLastFetched <= TimeSpan.FromDays(7))
            {
                return(null);
            }

            var request = new BGGCollectionRequest {
                Username = username, ExcludeSubtype = BGGSubtype.BoardGameExpansion, Stats = true
            };
            var task     = client.GetCollectionAsync(request);
            var response = await task;

            if (response.StatusCode == HttpStatusCode.Accepted)
            {
                if (retry)
                {
                    Console.WriteLine("Waiting to get collection for {0}", username);
                    Thread.Sleep(TimeSpan.FromSeconds(30));
                    return(await FetchCollection(client, db, now, username, false));
                }
                else
                {
                    Console.WriteLine("Failed to fetch collection at second attempt for {0}", username);
                    throw new InvalidOperationException("Couldn't fetch collection");
                }
            }

            if (existingUser == null)
            {
                db.Users.Add(new User {
                    Username = username, CollectionLastFetched = now
                });
            }
            else
            {
                existingUser.CollectionLastFetched = now;
            }

            db.SaveChanges();

            return(response.Data);
        }
Пример #7
0
        private static void Main(string[] args)
        {
            var client = new BGGClient();

            var tasks = Usernames.Select(username => FetchUser(client, username, true)).ToArray();

            Task.WaitAll(tasks);

            foreach (var wantToPlay in WantToPlay)
            {
                var id = wantToPlay.Key;

                List <string> owners;
                if (Owners.TryGetValue(id, out owners))
                {
                    Console.WriteLine("{0}\t{1}\t{2}", GameNames[id], string.Join(",", wantToPlay.Value), string.Join(",", owners));
                }
            }
        }
Пример #8
0
        private static void Main(string[] args)
        {
            var client = new BGGClient();

            var tasks = Usernames.Select(username => FetchUser(client, username, true)).ToArray();

            Task.WaitAll(tasks);

            foreach (var wantToPlay in WantToPlay)
            {
                var id = wantToPlay.Key;

                List<string> owners;
                if (Owners.TryGetValue(id, out owners))
                {
                    Console.WriteLine("{0}\t{1}\t{2}", GameNames[id], string.Join(",", wantToPlay.Value), string.Join(",", owners));
                }
            }
        }
Пример #9
0
        public static void Main(string[] args)
        {
            var now = DateTime.UtcNow;

            var client = new BGGClient();

            var db = new BggDbContext();

            var task = FetchCollection(client, db, now, Username, true);

            if (task.Result == null)
            {
                return;
            }
            var collection    = task.Result.Items;
            var collectionIds = collection.Select(game => game.Id).ToList();

            FetchGames(collectionIds, db, now, client);

            StoreRatings(db, collection, Username);
        }
Пример #10
0
        private static void FetchGames(ICollection <int> collectionIds, BggDbContext db, DateTime now, BGGClient client)
        {
            var newGames = collectionIds.Except(db.Games.Select(game => game.Id));
            var oldGames =
                db.Games.Where(
                    game => collectionIds.Contains(game.Id) && SqlFunctions.DateDiff("day", game.LastUpdate, now) > 7)
                .Select(game => game.Id);

            var gamesToFetch = newGames.Concat(oldGames).ToList();

            for (var start = 0; start < gamesToFetch.Count; start += ItemsBatchSize)
            {
                var itemsToFetch = gamesToFetch.Skip(start).Take(ItemsBatchSize).ToList();

                var itemsRequest = new BGGThingsRequest {
                    Id = itemsToFetch, Stats = true
                };
                var items = client.GetThingsAsync(itemsRequest).Result.Data.Items;

                var games =
                    items.Select(
                        item =>
                        new Game
                {
                    Id             = item.Id,
                    LastUpdate     = now,
                    Name           = item.Names.Single(name => name.Type == PrimaryNameType).Value,
                    Rating         = item.AverageRating,
                    BayesRating    = item.BayesAverageRating,
                    Weight         = item.AverageWeight,
                    MinimumPlayers = item.MinimumPlayers,
                    MaximumPlayers = item.MaximumPlayers,
                    PlayingTime    = item.PlayingTime,
                    MinimumAge     = item.MinimumAge
                });

                foreach (var game in games)
                {
                    var oldGame = db.Games.FirstOrDefault(old => old.Id == game.Id);
                    if (oldGame == null)
                    {
                        db.Games.Add(game);
                    }
                    else
                    {
                        db.Entry(oldGame).CurrentValues.SetValues(game);
                    }
                }

                db.SaveChanges();
            }
        }