Пример #1
0
        private async Task RunBot()
        {
            _fishErrorLength         = 0;
            _fishingState            = FishingState.Fishing;
            _cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = _cancellationTokenSource.Token;
            var session           = new BotSession();

            try
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    // We first check if another action is needed, foreach on all NeededAction enum values
                    foreach (var neededAction in (NeededAction[])Enum.GetValues(typeof(NeededAction)))
                    {
                        if (HasNeededAction(neededAction))
                        {
                            await HandleNeededAction(neededAction, cancellationToken);
                        }
                    }

                    // If no other action required, we can cast !
                    await Fish(session, cancellationToken);

                    if (_fishErrorLength > 10)
                    {
                        Stop();
                    }
                }
            }
            catch (TaskCanceledException)
            {
                //ignore
            }
            finally
            {
                _cancellationTokenSource.Dispose();
                _cancellationTokenSource = null;
            }
        }
Пример #2
0
        private async Task Fish(BotSession session, CancellationToken cancellationToken)
        {
            _mouth.Say(Translate.GetTranslate("manager", "LABEL_CASTING"));
            _eyes.UpdateBackground();
            await _hands.Cast(cancellationToken);

            _mouth.Say(Translate.GetTranslate("manager", "LABEL_FINDING"));
            // Make bobber found async, so can check fishing sound in parallel, the result only important when we hear fish.
            // The position used for repositioning.
            var eyeCancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            var eyeCancelToken       = eyeCancelTokenSource.Token;
            var eyeTask = Task.Run(async() => await _eyes.LookForBobber(session, eyeCancelToken), eyeCancelToken);

            // Update UI with wait status
            var uiUpdateCancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            var uiUpdateCancelToken       = uiUpdateCancelTokenSource.Token;
            var progress = new Progress <long>(msecs =>
            {
                if (!uiUpdateCancelToken.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
                {
                    _mouth.Say(Translate.GetTranslate(
                                   "manager",
                                   "LABEL_WAITING",
                                   msecs / Second,
                                   _aFishWait / Second));
                }
            });
            var uiUpdateTask = Task.Run(
                async() => await UpdateUiWhileWaitingToHearFish(progress, uiUpdateCancelToken),
                uiUpdateCancelToken);

            var rnd = new Random();

            _aFishWait = rnd.Next(Properties.Settings.Default.FishWaitLow, Properties.Settings.Default.FishWaitHigh);
            var fishHeard = await _ears.Listen(
                _aFishWait,
                cancellationToken);

            //Log.Information("Ear result: "+a_FishWait.ToString());

            uiUpdateCancelTokenSource.Cancel();
            try {
                uiUpdateTask.GetAwaiter().GetResult(); // Wait & Unwrap
                // https://github.com/StephenCleary/AsyncEx/blob/dc54d22b06566c76db23af06afcd0727cac625ef/Source/Nito.AsyncEx%20(NET45%2C%20Win8%2C%20WP8%2C%20WPA81)/Synchronous/TaskExtensions.cs#L18
            } catch (TaskCanceledException) {
            } finally {
                uiUpdateCancelTokenSource.Dispose();
            }

            if (!fishHeard)
            {
                _fishingStats.RecordNotHeard();
                _fishErrorLength++;
                return;
            }

            // We heard the fish, let's check bobbers position
            if (!eyeTask.IsCompleted)
            {
                // the search is not finished yet, but fish is heard, we have 2 seconds left to find and hook it
                eyeTask.Wait(2000, cancellationToken);
                eyeCancelTokenSource.Cancel();
            }
            eyeCancelTokenSource.Dispose();

            if (eyeTask.IsCompleted)
            {
                // search is ended what's the result?
                var bobberPos = eyeTask.Result;

                if (bobberPos != null && bobberPos.X != 0 && bobberPos.Y != 0)
                {
                    // bobber found
                    if (await _eyes.SetMouseToBobber(session, bobberPos, cancellationToken))
                    {
                        // bobber is still there
                        Log.Information("Bobber databl: ({bx},{by})", bobberPos.X, bobberPos.Y);
                        await _hands.Loot();

                        _mouth.Say(Translate.GetTranslate("manager", "LABEL_HEAR_FISH"));
                        _fishingStats.RecordSuccess();
                        _fishErrorLength = 0;
                        Log.Information("Fish success");
                        return;
                    }
                }
            }
            _fishingStats.RecordBobberNotFound();
            _fishErrorLength++;
        }