예제 #1
0
        protected virtual bool CheckProduct(ReleaseProductInfo productInfo, Proxy proxy, CancellationToken cancelToken, out List <string> availableSizes)
        {
            Random r = new Random();

            availableSizes = new List <string>();

            return(r.Next(0, 100) > 50);
        }
예제 #2
0
        public async Task <bool> Start(CancellationToken cancelToken)
        {
            bool ret = true;

            lock (m_lock)
            {
                if (m_state == ReleaseTaskState.Working)
                {
                    ret = false;
                }
                m_state = ReleaseTaskState.Working;
            }

            if (ret)
            {
                OnPropertyChanged("State");

                await Task.Run(() =>
                {
                    Log.Clear();

                    ReleaseProductInfo productInfo = new ReleaseProductInfo()
                    {
                        ProductLink = Release.ProductLink,
                        KeyWords    = Release.KeyWords.ToList()
                    };

                    ProductMonitorTask productMonitorTask = new ProductMonitorTask()
                    {
                        Period      = Release.Footsite.Settings.ProductMonitorPeriod,
                        ProductInfo = productInfo,
                        Proxy       = Release.GlobalProxy
                    };

                    Thread monitorThread = new Thread(() => MonitorProduct(cancelToken, Log, productMonitorTask));
                    monitorThread.Start();

                    List <CheckoutTask> checkoutTasks = new List <CheckoutTask>();

                    int taskId          = 0;
                    ProxyPool proxyPool = new ProxyPool(Release.Proxies.ToList(), 1);

                    foreach (ReleaseCheckoutProfile profile in Release.Profiles)
                    {
                        CheckoutTaskCcProfile checkoutTaskCcProfile = new CheckoutTaskCcProfile(new ReleaseCheckoutProfile(profile)
                        {
                            CheckoutProfile = new CheckoutProfile(profile.CheckoutProfile)
                        }, Release.Footsite.Settings.PurchaseLimitPerProfile);
                        Profiles.Add(checkoutTaskCcProfile);

                        for (int i = 0; i < profile.TasksCount; i++)
                        {
                            CheckoutTask checkoutTask = new CheckoutTask()
                            {
                                Id                    = taskId++,
                                Size                  = profile.Size,
                                VariantId             = profile.VariantId,
                                Profile               = checkoutTaskCcProfile,
                                ProxyPool             = proxyPool,
                                ProductAvailableEvent = productMonitorTask.ProductAvailableEvent,
                                Footsite              = Release.Footsite,
                                ProductInfo           = productInfo
                            };
                            Logger.LogEvent(checkoutTask.Log, $"TASK {checkoutTask.Id}", "Initializing...");

                            checkoutTaskCcProfile.CheckoutTasks.Add(checkoutTask);
                            checkoutTasks.Add(checkoutTask);
                        }
                    }

                    RestockMonitorTask restockMonitorTask = new RestockMonitorTask()
                    {
                        Period        = TimeSpan.FromMilliseconds(Release.Footsite.Settings.ProductMonitorPeriod),
                        CheckoutTasks = checkoutTasks,
                        ProductInfo   = productInfo,
                    };

                    Thread restockMonitorThread = new Thread(() => RestockMonitor(Log, restockMonitorTask, cancelToken));
                    restockMonitorThread.Start();

                    CheckoutTasksWatcher checkoutTasksWatcher = new CheckoutTasksWatcher(checkoutTasks);

                    Task.Run(() =>
                    {
                        foreach (CheckoutTask checkoutTask in checkoutTasks)
                        {
                            if (cancelToken.IsCancellationRequested)
                            {
                                break;
                            }

                            Thread checkoutThread = new Thread(() => Checkout(cancelToken, checkoutTask));
                            checkoutThread.Start();
                        }
                    });

                    checkoutTasksWatcher.FinishedEvent.WaitOne(cancelToken);

                    productMonitorTask.CompleteEvent.Set();
                    restockMonitorTask.CompleteEvent.Set();
                });

                TierControl.ReleaseQuota(Release.TasksCount, Release.Footsite.Type);

                State = ReleaseTaskState.Idle;
            }

            return(ret);
        }