Example #1
0
        private async void ParseCommand(string userCommand)
        {
            _cts?.Cancel();
            _cts = new CancellationTokenSource();

            MatchedItems.Clear();

            if (string.IsNullOrEmpty(userCommand))
            {
                return;
            }

            _log.Debug($"ParseCommand('{userCommand}')");

            var parts = userCommand.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Where(p => p.Length > 1).ToArray();

            //            Task.Run(() => ParseCommandAsync(userCommand, _cts.Token), _cts.Token);

            List <FolderMatch> survivors;

            try
            {
                survivors = await ParseCommandAsync(parts, _cts.Token);
            }
            catch (OperationCanceledException)
            {
                _log.Debug($"operation cancelled. search args:{userCommand}");
                return;
            }
            if (survivors == null)
            {
                return;
            }

            foreach (var item in survivors.OrderBy(f => f.MatchedFolder.Level).ThenBy(f => f.Position).Take(200))
            {
                MatchedItems.Add(new HyperJumpFolderViewModel(item.MatchedFolder, parts));
            }
            SelectedMatchedItem = MatchedItems.FirstOrDefault();
        }
        public MainWindowViewModel()
        {
            Services.Instance.AddMainWindowModel(this);
            Services.Instance.AddManualUserInput(ManualResolveViewModel);

            ImgProcessing = new ImageProcessor(SelectedShopRuleset);
            Services.Instance.ServerHandler.StartServer();

            Services.Instance.ServerHandler.OnImageReceived += (s, e) => { ImageSource = e; };
            ImgProcessing.OnImageParsed += OnImageParsed;

            Finalize = new ParametrizedCommand((obj) => {
                Purchase purchase = new Purchase(SelectedShopRuleset.Shop, ImgProcessing.CurrentParsingResult.Meta.PurchasedAt,
                                                 _matchedItems.Select(s => s.ItemPurchase).ToArray());
                purchase.FinalizePurchase();
                ClearButtonVisible    = true;
                FinalizeButtonVisible = false;
            });

            Clear = new ParametrizedCommand((obj) => {
                ImageSource                 = ResourceNames.TRANSPARENT_IMAGE;
                FinalizeButtonVisible       = false;
                SendToMTDBButtonVisible     = false;
                ClearButtonVisible          = false;
                ManualPurchaseButtonVisible = true;
                AnalyzeButtonVisible        = true;
                UnknownItems.Clear();
                MatchedItems.Clear();
            });

            Analyze = new ParametrizedCommand((obj) => {
                if (string.IsNullOrEmpty(ImageSource) || ImageSource == ResourceNames.TRANSPARENT_IMAGE)
                {
                    //TODO
                    return;
                }
                _ = ImgProcessing.Analyze(ImageSource);
            });

            SendToMTDB = new ParametrizedCommand(async(obj) => {
                using (CPCServer server = new CrossProcessCommunication().StartServer(5689)) {
                    server.StartListening();
                    await server.ListenForClient();
                    server.SendString(SenderHelper.GetString(ImgProcessing.CurrentParsingResult));
                    server.StopListening();
                }
            });

            OnMouseRightClickImage += (s, e) => {
                if (string.IsNullOrEmpty(ImageSource) || ImageSource == ResourceNames.TRANSPARENT_IMAGE)
                {
                    //TODO
                    return;
                }
                new Process {
                    StartInfo = new ProcessStartInfo(ImageSource)
                }.Start();
            };

            Instance = this;
        }