Ejemplo n.º 1
0
        private async Task Fish(CancellationToken cancellationToken)
        {
            m_mouth.Say(Translate.GetTranslate("manager", "LABEL_CASTING"));
            m_eyes.updateBackground();
            await m_hands.Cast(cancellationToken);

            m_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.
            CancellationTokenSource eyeCancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            CancellationToken       eyeCancelToken       = eyeCancelTokenSource.Token;
            Task <Win32.Point>      eyeTask = Task.Run(async() => await m_eyes.LookForBobber(eyeCancelToken));

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

            Random rnd = new Random();

            a_FishWait = rnd.Next(Properties.Settings.Default.FishWaitLow, Properties.Settings.Default.FishWaitHigh);
            bool fishHeard = await m_ears.Listen(
                a_FishWait,
                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)
            {
                m_fishingStats.RecordNotHeard();
                m_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?
                Win32.Point bobberPos = eyeTask.Result;

                if (bobberPos.x != 0 && bobberPos.y != 0)
                {
                    // bobber found
                    if (await m_eyes.SetMouseToBobber(bobberPos, cancellationToken))
                    {
                        // bobber is still there
                        Log.Information("Bobber databl: ({bx},{by})", bobberPos.x, bobberPos.y);
                        await m_hands.Loot();

                        m_mouth.Say(Translate.GetTranslate("manager", "LABEL_HEAR_FISH"));
                        m_fishingStats.RecordSuccess();
                        m_fishErrorLength = 0;
                        Log.Information("Fish success");
                        return;
                    }
                }
            }
            m_fishingStats.RecordBobberNotFound();
            m_fishErrorLength++;
        }
Ejemplo n.º 2
0
        private async Task Fish(CancellationToken cancellationToken)
        {
            m_mouth.Say(Translate.GetTranslate("manager", "LABEL_CASTING"));
            await m_hands.Cast(cancellationToken);

            m_mouth.Say(Translate.GetTranslate("manager", "LABEL_FINDING"));
            bool didFindFish = await m_eyes.LookForBobber(cancellationToken);

            if (!didFindFish)
            {
                m_fishingStats.RecordBobberNotFound();
                return;
            }

            // 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)
                {
                    m_mouth.Say(Translate.GetTranslate(
                                    "manager",
                                    "LABEL_WAITING",
                                    msecs / SECOND,
                                    Properties.Settings.Default.FishWait / SECOND));
                }
            });
            var uiUpdateTask = Task.Run(
                async() => await UpdateUIWhileWaitingToHearFish(progress, uiUpdateCancelToken),
                uiUpdateCancelToken);

            bool fishHeard = await m_ears.Listen(
                Properties.Settings.Default.FishWait,
                cancellationToken);

            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)
            {
                m_fishingStats.RecordNotHeard();
                return;
            }

            m_mouth.Say(Translate.GetTranslate("manager", "LABEL_HEAR_FISH"));
            await m_hands.Loot();

            m_fishingStats.RecordSuccess();
        }
Ejemplo n.º 3
0
 private async Task Resume()
 {
     m_mouth.Say(Translate.GetTranslate("frmMain", "LABEL_RESUMED"));
     m_managerEventHandler.Resumed();
     await RunBot();
 }
Ejemplo n.º 4
0
        private void TakeNextAction(Object myObject, EventArgs myEventArgs)
        {
            switch (GetActualState())
            {
            case FishingState.Start:
            {
                // We just start, going to Idle to begin bot loop
                SetActualState(FishingState.Idle);
                break;
            }

            case FishingState.Idle:
            {
                // We first check if another action is needed, foreach on all NeededAction enum values
                foreach (NeededAction neededAction in (NeededAction[])Enum.GetValues(typeof(NeededAction)))
                {
                    if (HasNeededAction(neededAction))
                    {
                        HandleNeededAction(neededAction);
                        return;
                    }
                }

                // If no other action required, we can cast !
                m_mouth.Say(Translate.GetTranslate("manager", "LABEL_CASTING"));
                SetActualState(FishingState.Casting);
                Hands.Cast();
                break;
            }

            case FishingState.Casting:
            {
                m_mouth.Say(Translate.GetTranslate("manager", "LABEL_START_FINDING"));
                SetActualState(FishingState.SearchingForBobber);
                m_eyes.StartLooking();     // <= The new state will be set in the Eyes
                break;
            }

            case FishingState.SearchingForBobber:
            {
                // We are just waiting for the Eyes
                m_mouth.Say(Translate.GetTranslate("manager", "LABEL_FINDING"));
                break;
            }

            case FishingState.WaitingForFish:
            {
                // We are waiting a detection from the Ears
                m_mouth.Say(Translate.GetTranslate("manager", "LABEL_WAITING", GetFishWaitTime() / 1000, Properties.Settings.Default.FishWait / 1000));

                if ((m_fishWaitTime += ACTION_TIMER_LENGTH) >= Properties.Settings.Default.FishWait)
                {
                    SetActualState(FishingState.Idle);
                    m_fishWaitTime = 0;
                }

                break;
            }

            default:
                break;
            }
        }