Ejemplo n.º 1
0
        // --- Listing building logic
        public List <String> AddSteamProductKeys(Platform platform, string input)
        {
            List <Listing> listings  = new List <Listing>();
            List <String>  addedKeys = new List <String>();

            input = input.Replace("\r\n", "\r");
            input = input.Replace("\n", "\r");

            List <String> lines = input.Split(new string[] { "\r" }, StringSplitOptions.RemoveEmptyEntries).ToList();

            Regex SubIDPriceKey = new Regex(@"^sub/([0-9]+)\t+([0-9,]+)\t+([0-9]+)\t+([^\t]+)(\t+[^\t]+)?$");
            Regex AppIDPriceKey = new Regex(@"^([0-9]+)\t+([0-9]+)(\t+[^\t]+)?$");
            //\t+([^\t]+)

            DateTime dateAdded = DateTime.Now;

            Match      match;
            bool       isGift   = false;
            string     key      = "";
            string     gameName = "";
            int        appId    = 0;
            int        price    = 0;
            int        subId    = 0;
            List <int> appIds   = new List <int>();

            foreach (String line in lines)
            {
                price    = 0;
                appId    = 0;
                gameName = "";
                key      = "";
                isGift   = false;
                subId    = 0;
                appIds   = new List <int>();

                if (AppIDPriceKey.Match(line).Success)
                {
                    match = AppIDPriceKey.Match(line);

                    appId = Int32.Parse(match.Groups[1].ToString());
                    price = Int32.Parse(match.Groups[2].ToString());

                    if (String.IsNullOrEmpty(match.Groups[3].Value))
                    {
                        isGift = true;
                    }
                    else
                    {
                        key = match.Groups[3].Value.Trim();
                    }
                }
                else if (SubIDPriceKey.Match(line).Success)
                {
                    match = SubIDPriceKey.Match(line);

                    subId = Int32.Parse(match.Groups[1].Value);

                    string[] separator = new string[] { "," };
                    foreach (string splitId in match.Groups[2].Value.Split(separator, StringSplitOptions.RemoveEmptyEntries))
                    {
                        appIds.Add(Int32.Parse(splitId));
                    }

                    if (String.IsNullOrEmpty(match.Groups[5].Value))
                    {
                        isGift = true;
                    }
                    else
                    {
                        key = match.Groups[5].Value.Trim();
                    }

                    gameName = match.Groups[4].Value;
                    price    = Int32.Parse(match.Groups[3].ToString());
                }
                else
                {
                    addedKeys.Add("Unable to parse: " + line);
                    continue;
                }

                Listing listing = null;

                if (appId != 0)
                {
                    listing = listings.Where(x => x.Product.AppID == appId).SingleOrDefault();

                    if (listing == null)
                    {
                        listing = GetListingByAppIDSteam(appId, platform.PlatformID);//GetListingByAppID(appId, platform.PlatformName);
                        if (listing != null)
                        {
                            listings.Add(listing);
                        }
                    }
                }
                else if (subId != 0)
                {
                    listing = listings.Where(x => x.Product.AppID == subId).SingleOrDefault();

                    if (listing == null)
                    {
                        listing = GetListingByAppIDSteam(subId, platform.PlatformID);//GetListingByAppID(subId, platform.PlatformName);
                        if (listing != null)
                        {
                            listings.Add(listing);
                        }
                    }
                }

                if (listing != null)
                {
                    listing.ListingPrice = price;
                    listing.AddProductKeyAndUpdateQuantity(new ProductKey(isGift, key));
                    listing.DateEdited = dateAdded;
                    //listingRepository.UpdateListing(listing);

                    addedKeys.Add(platform.PlatformName + ": " + listing.ListingName + "...+1!");
                }
                else
                {
                    listing = new Listing(gameName, price, dateAdded);
                    listing.AddPlatform(platform);
                    listing.AddProductKeyAndUpdateQuantity(new ProductKey(isGift, key));

                    Product product = new Product(appId, gameName);
                    //Add logic to get data from api on product info & product details

                    listing.AddProduct(product);

                    // insert this listing entry for now, as we build the listing with data gathered from Steam's store api
                    // we may need to build more listings recursively, we need this listing to be in the repository so it doesn't get stuck in a loop
                    //AddListing(listing);

                    if (appId != 0)
                    {
                        BuildListingWithAppID(listing, appId);
                    }
                    else if (appIds.Count != 0)
                    {
                        listing.Product.AppID = subId;
                        BuildListingWithPackageID(listing, appIds, gameName);
                    }

                    listings.Add(listing);
                    //UpdateListing(listing);

                    addedKeys.Add(platform.PlatformName + ": " + listing.ListingName + "...created!");
                }
            }

            foreach (Listing updatedListing in listings)
            {
                if (updatedListing.ListingID != 0)
                {
                    listingRepository.UpdateListing(updatedListing);
                }
                else
                {
                    listingRepository.InsertListing(updatedListing);
                }
            }

            SiteNotification notification = new SiteNotification();
            var url = String.Format("https://theafterparty.azurewebsites.net/store/date?month={0}&day={1}&year={2}", DateTime.Today.Month, DateTime.Today.Day, DateTime.Today.Year);

            notification.Notification     = "[new][/new] [url=" + url + "][gtext]" + addedKeys.Count() + "[/gtext][/url] items added to the [url=https://theafterparty.azurewebsites.net/store/newest]Co-op Shop[/url]!";
            notification.NotificationDate = DateTime.Now;
            siteRepository.InsertSiteNotification(notification);

            unitOfWork.Save();

            return(addedKeys);
        }
Ejemplo n.º 2
0
        public List <String> AddProductKeys(Platform platform, string input)
        {
            if (platform.PlatformName.ToLower().CompareTo("steam") == 0)
            {
                return(AddSteamProductKeys(platform, input));
            }

            List <String> addedKeys = new List <String>();

            input = input.Replace("\r\n", "\r");

            List <String> lines = input.Split(new string[] { "\r" }, StringSplitOptions.RemoveEmptyEntries).ToList();

            Regex IDPriceKey   = new Regex(@"^id/([0-9]+)\t+([0-9]+)\t+([^\t]+)$");
            Regex NamePriceKey = new Regex("^([^\t]+)\t+([0-9]+)\t+([^\t]+)$");
            Regex NamePrice    = new Regex("^([^\t]+)\t+([0-9]+)$");

            DateTime dateAdded = DateTime.Now;

            Match match;

            bool   isGift   = false;
            string key      = "";
            string gameName = "";
            int    price    = 0;

            foreach (String line in lines)
            {
                price    = 0;
                gameName = "";
                key      = "";
                isGift   = false;
                int p_id = 0;

                if (IDPriceKey.Match(line).Success)
                {
                    match = IDPriceKey.Match(line);

                    p_id  = Int32.Parse(match.Groups[1].ToString());
                    price = Int32.Parse(match.Groups[2].ToString());
                    key   = match.Groups[3].ToString();
                }
                else if (NamePriceKey.Match(line).Success)
                {
                    match = NamePriceKey.Match(line);

                    gameName = match.Groups[1].Value;
                    price    = Int32.Parse(match.Groups[2].ToString());
                    key      = match.Groups[3].ToString();
                }
                else if (NamePrice.Match(line).Success)
                {
                    match = NamePrice.Match(line);

                    gameName = match.Groups[1].Value;
                    price    = Int32.Parse(match.Groups[2].ToString());
                    isGift   = true;
                }

                Listing listing = null;

                if (String.IsNullOrEmpty(gameName) == false)
                {
                    listing = listingRepository.GetListings().Where(l => l.ContainsPlatform(platform) && object.Equals(l.Product.ProductName, gameName)).SingleOrDefault();
                }

                if (listing != null)
                {
                    listing.ListingPrice = price;
                    listing.AddProductKeyAndUpdateQuantity(new ProductKey(isGift, key));
                    listing.DateEdited = dateAdded;
                    listingRepository.UpdateListing(listing);

                    addedKeys.Add(platform.PlatformName + ": " + listing.ListingName + "...+1!");
                }
                else
                {
                    if (p_id != 0)
                    {
                        listing = listingRepository.GetListings().Where(l => l.Platforms.Any(p => p.PlatformID == platform.PlatformID) && l.Product.AppID == p_id && l.Product.IsSteamAppID == true).FirstOrDefault();
                    }

                    if (listing == null)
                    {
                        Product product = null;

                        if (p_id != 0)
                        {
                            product = listingRepository.GetProducts().FirstOrDefault(p => p.AppID == p_id && p.IsSteamAppID == true);

                            if (product == null)
                            {
                                product = new Product(p_id);

                                bool success = BuildOrUpdateSteamProduct(p_id, product);

                                if (success == false)
                                {
                                    product = null;
                                }
                            }
                        }

                        if (product != null)
                        {
                            listing = new Listing(product.ProductName, price, dateAdded);
                        }
                        else
                        {
                            product = new Product(gameName, false);
                            listing = new Listing(gameName, price, dateAdded);
                        }

                        listing.AddPlatform(platform);
                        listing.AddProduct(product);
                        listing.AddProductKeyAndUpdateQuantity(new ProductKey(isGift, key));

                        listingRepository.InsertListing(listing);

                        addedKeys.Add(platform.PlatformName + ": " + listing.ListingName + "...created!");
                    }
                    else
                    {
                        listing.ListingPrice = price;
                        listing.AddProductKeyAndUpdateQuantity(new ProductKey(isGift, key));
                        listing.DateEdited = dateAdded;

                        listingRepository.UpdateListing(listing);

                        addedKeys.Add(platform.PlatformName + ": " + listing.ListingName + "...+1!");
                    }
                }

                unitOfWork.Save();
            }

            SiteNotification notification = new SiteNotification();
            var url = String.Format("https://theafterparty.azurewebsites.net/store/date?month={0}&day={1}&year={2}", DateTime.Today.Month, DateTime.Today.Day, DateTime.Today.Year);

            notification.Notification     = "[new][/new] [url=" + url + "][gtext]" + addedKeys.Count() + "[/gtext][/url] items added to the [url=https://theafterparty.azurewebsites.net/store/newest]Co-op Shop[/url]!";
            notification.NotificationDate = DateTime.Now;
            siteRepository.InsertSiteNotification(notification);

            unitOfWork.Save();

            return(addedKeys);
        }