Example #1
0
    public IEnumerator RespondToCommand(string userNickName, string message, ICommandResponseNotifier responseNotifier, IRCConnection connection)
    {
        _responded = false;
        _processingTwitchCommand = true;
        if (Solved)
        {
            responseNotifier.ProcessResponse(CommandResponse.NoResponse);
            _processingTwitchCommand = false;
            yield break;
        }

        _currentResponseNotifier = responseNotifier;
        _currentUserNickName     = userNickName;

        int beforeStrikeCount = StrikeCount;

        IEnumerator subcoroutine = null;

        if (message.StartsWith("send to module ", StringComparison.InvariantCultureIgnoreCase))
        {
            message = message.Substring(15);
        }
        else
        {
            subcoroutine = RespondToCommandCommon(message, userNickName);
        }

        if (subcoroutine == null || !subcoroutine.MoveNext())
        {
            if (_responded)
            {
                yield break;
            }

            try
            {
                subcoroutine = RespondToCommandInternal(message);
            }
            catch (Exception e)
            {
                HandleModuleException(e);
                yield break;
            }

            bool moved = false;
            if (subcoroutine != null)
            {
                try
                {
                    moved = subcoroutine.MoveNext();

                    if (moved && modInfo.DoesTheRightThing)
                    {
                        _responded = true;
                    }
                }
                catch (Exception e)
                {
                    HandleModuleException(e);
                    yield break;
                }
            }

            if (subcoroutine == null || !moved || Solved || beforeStrikeCount != StrikeCount)
            {
                if (Solved || beforeStrikeCount != StrikeCount)
                {
                    IEnumerator focusDefocus = BombCommander.Focus(Selectable, FocusDistance, FrontFace);
                    while (focusDefocus.MoveNext())
                    {
                        yield return(focusDefocus.Current);
                    }
                    yield return(new WaitForSeconds(0.5f));

                    responseNotifier.ProcessResponse(Solved ? CommandResponse.EndComplete : CommandResponse.EndError);

                    focusDefocus = BombCommander.Defocus(Selectable, FrontFace);
                    while (focusDefocus.MoveNext())
                    {
                        yield return(focusDefocus.Current);
                    }
                    yield return(new WaitForSeconds(0.5f));
                }
                else
                {
                    ComponentHandle.CommandInvalid(userNickName);
                    responseNotifier.ProcessResponse(CommandResponse.NoResponse);
                }

                _currentResponseNotifier = null;
                _currentUserNickName     = null;
                _processingTwitchCommand = false;
                yield break;
            }
        }

        responseNotifier.ProcessResponse(CommandResponse.Start);

        IEnumerator focusCoroutine = BombCommander.Focus(Selectable, FocusDistance, FrontFace);

        while (focusCoroutine.MoveNext())
        {
            yield return(focusCoroutine.Current);
        }

        yield return(new WaitForSeconds(0.5f));

        int  previousStrikeCount = StrikeCount;
        bool parseError          = false;
        bool needQuaternionReset = false;
        bool hideCamera          = false;
        bool exceptionThrown     = false;

        while (previousStrikeCount == StrikeCount && !Solved)
        {
            try
            {
                if (!subcoroutine.MoveNext())
                {
                    break;
                }
                else
                {
                    _responded = true;
                }
            }
            catch (Exception e)
            {
                exceptionThrown = true;
                HandleModuleException(e);
                break;
            }

            object currentValue = subcoroutine.Current;
            if (currentValue is string currentString)
            {
                if (currentString.Equals("strike", StringComparison.InvariantCultureIgnoreCase))
                {
                    _delegatedStrikeUserNickName     = userNickName;
                    _delegatedStrikeResponseNotifier = responseNotifier;
                }
                else if (currentString.Equals("solve", StringComparison.InvariantCultureIgnoreCase))
                {
                    _delegatedSolveUserNickName     = userNickName;
                    _delegatedSolveResponseNotifier = responseNotifier;
                }
                else if (currentString.Equals("unsubmittablepenalty", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (TwitchPlaySettings.data.UnsubmittablePenaltyPercent <= 0)
                    {
                        continue;
                    }

                    int penalty = Math.Max((int)(modInfo.moduleScore * TwitchPlaySettings.data.UnsubmittablePenaltyPercent), 1);
                    ComponentHandle.leaderboard.AddScore(_currentUserNickName, -penalty);
                    IRCConnection.SendMessage(TwitchPlaySettings.data.UnsubmittableAnswerPenalty, _currentUserNickName, ComponentHandle.idText.text, modInfo.moduleDisplayName, penalty, penalty > 1 ? "s" : "");
                }
                else if (currentString.StartsWith("strikemessage ", StringComparison.InvariantCultureIgnoreCase) &&
                         currentString.Substring(14).Trim() != string.Empty)
                {
                    StrikeMessage = currentString.Substring(14);
                }
                else if (currentString.Equals("parseerror", StringComparison.InvariantCultureIgnoreCase))
                {
                    parseError = true;
                    break;
                }
                else if (currentString.Equals("trycancel", StringComparison.InvariantCultureIgnoreCase) &&
                         Canceller.ShouldCancel)
                {
                    Canceller.ResetCancel();
                    break;
                }
                else if (currentString.StartsWith("sendtochat ", StringComparison.InvariantCultureIgnoreCase) &&
                         currentString.Substring(11).Trim() != string.Empty)
                {
                    IRCConnection.SendMessage(currentString.Substring(11));
                }
                else if (currentString.StartsWith("sendtochaterror ", StringComparison.InvariantCultureIgnoreCase) &&
                         currentString.Substring(16).Trim() != string.Empty)
                {
                    ComponentHandle.CommandError(userNickName, currentString.Substring(16));
                }
                else if (currentString.StartsWith("add strike", StringComparison.InvariantCultureIgnoreCase))
                {
                    OnStrike(null);
                }
                else if (currentString.Equals("multiple strikes", StringComparison.InvariantCultureIgnoreCase))
                {
                    DisableOnStrike = true;
                }
                else if (currentString.StartsWith("award strikes ", StringComparison.CurrentCultureIgnoreCase))
                {
                    if (int.TryParse(currentString.Substring(14), out int awardStrikeCount))
                    {
                        StrikeCount += awardStrikeCount;
                        AwardStrikes(_currentUserNickName, _currentResponseNotifier, awardStrikeCount);
                        DisableOnStrike = false;
                    }
                }
                else if (currentString.StartsWith("autosolve", StringComparison.InvariantCultureIgnoreCase))
                {
                    HandleModuleException(new Exception(currentString));
                    break;
                }
                else if (currentString.ToLowerInvariant().EqualsAny("detonate", "explode"))
                {
                    AwardStrikes(_currentUserNickName, _currentResponseNotifier, BombCommander.StrikeLimit - BombCommander.StrikeCount);
                    BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(string.Empty, modInfo.moduleDisplayName);
                    break;
                }
                else if (currentString.ToLowerInvariant().EqualsAny("elevator music", "hold music", "waiting music"))
                {
                    if (_musicPlayer == null)
                    {
                        _musicPlayer = MusicPlayer.StartRandomMusic();
                    }
                }
                else if (currentString.ToLowerInvariant().Equals("hide camera"))
                {
                    if (!hideCamera)
                    {
                        BombMessageResponder.moduleCameras?.Hide();
                        BombMessageResponder.moduleCameras?.HideHUD();
                        IEnumerator hideUI = BombCommander.twitchBombHandle.HideMainUIWindow();
                        while (hideUI.MoveNext())
                        {
                            yield return(hideUI.Current);
                        }
                    }
                    hideCamera = true;
                }
            }
            else if (currentValue is Quaternion localQuaternion)
            {
                BombCommander.RotateByLocalQuaternion(localQuaternion);
                needQuaternionReset = true;
            }
            else if (currentValue is string[] currentStrings)
            {
                if (currentStrings.Length >= 1)
                {
                    if (currentStrings[0].ToLowerInvariant().EqualsAny("detonate", "explode"))
                    {
                        AwardStrikes(_currentUserNickName, _currentResponseNotifier, BombCommander.StrikeLimit - BombCommander.StrikeCount);
                        switch (currentStrings.Length)
                        {
                        case 2:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(currentStrings[1], modInfo.moduleDisplayName);
                            break;

                        case 3:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(currentStrings[1], currentStrings[2]);
                            break;

                        default:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(string.Empty, modInfo.moduleDisplayName);
                            break;
                        }
                        break;
                    }
                }
            }
            yield return(currentValue);
        }

        if (!_responded && !exceptionThrown)
        {
            ComponentHandle.CommandInvalid(userNickName);
        }

        if (needQuaternionReset)
        {
            BombCommander.RotateByLocalQuaternion(Quaternion.identity);
        }

        if (hideCamera)
        {
            BombMessageResponder.moduleCameras?.Show();
            BombMessageResponder.moduleCameras?.ShowHUD();
            IEnumerator showUI = BombCommander.twitchBombHandle.ShowMainUIWindow();
            while (showUI.MoveNext())
            {
                yield return(showUI.Current);
            }
        }

        if (_musicPlayer != null)
        {
            _musicPlayer.StopMusic();
            _musicPlayer = null;
        }

        if (parseError)
        {
            responseNotifier.ProcessResponse(CommandResponse.NoResponse);
        }
        else
        {
            if (!Solved && (previousStrikeCount == StrikeCount))
            {
                responseNotifier.ProcessResponse(CommandResponse.EndNotComplete);
            }

            yield return(new WaitForSeconds(0.5f));
        }

        IEnumerator defocusCoroutine = BombCommander.Defocus(Selectable, FrontFace);

        while (defocusCoroutine.MoveNext())
        {
            yield return(defocusCoroutine.Current);
        }

        yield return(new WaitForSeconds(0.5f));

        _currentResponseNotifier = null;
        _currentUserNickName     = null;
        _processingTwitchCommand = false;
    }
    public IEnumerator RespondToCommand(string userNickName, string message)
    {
        TryCancel  = false;
        _responded = false;
        _zoom      = false;
        _processingTwitchCommand = true;
        if (Solved)
        {
            _processingTwitchCommand = false;
            yield break;
        }

        _currentUserNickName = userNickName;

        int beforeStrikeCount = StrikeCount;

        IEnumerator subcoroutine = null;

        if (message.StartsWith("send to module ", StringComparison.InvariantCultureIgnoreCase))
        {
            message = message.Substring(15);
        }
        else
        {
            subcoroutine = RespondToCommandCommon(message, userNickName);
        }

        if (subcoroutine == null || !subcoroutine.MoveNext())
        {
            if (_responded)
            {
                yield break;
            }

            if (_zoom)
            {
                message = message.Substring(4).Trim();
            }

            try
            {
                subcoroutine = RespondToCommandInternal(message);
            }
            catch (Exception e)
            {
                HandleModuleException(e);
                yield break;
            }

            bool moved = false;
            if (subcoroutine != null)
            {
                try
                {
                    moved = subcoroutine.MoveNext();
                    if (moved && modInfo.DoesTheRightThing)
                    {
                        //Handle No-focus API commands. In order to focus the module, the first thing yielded cannot be one of the things handled here, as the solver will yield break if
                        //it is one of these API commands returned.
                        switch (subcoroutine.Current)
                        {
                        case string currentString:
                            if (SendToTwitchChat(currentString, userNickName) && !currentString.StartsWith("strikemessage", StringComparison.InvariantCultureIgnoreCase))
                            {
                                yield break;
                            }
                            break;
                        }
                        _responded = true;
                    }
                }
                catch (Exception e)
                {
                    HandleModuleException(e);
                    yield break;
                }
            }

            if (subcoroutine == null || !moved || Solved || beforeStrikeCount != StrikeCount)
            {
                if (Solved || beforeStrikeCount != StrikeCount)
                {
                    IRCConnection.Instance.SendMessage("Please submit an issue at https://github.com/samfun123/KtaneTwitchPlays/issues regarding module !{0} ({1}) attempting to solve prematurely.", ComponentHandle.Code, ComponentHandle.HeaderText);
                    if (modInfo != null)
                    {
                        modInfo.DoesTheRightThing = false;
                        ModuleData.DataHasChanged = true;
                        ModuleData.WriteDataToFile();
                    }

                    IEnumerator focusDefocus = BombCommander.Focus(Selectable, FocusDistance, FrontFace);
                    while (focusDefocus.MoveNext())
                    {
                        yield return(focusDefocus.Current);
                    }
                    yield return(new WaitForSeconds(0.5f));

                    focusDefocus = BombCommander.Defocus(Selectable, FrontFace);
                    while (focusDefocus.MoveNext())
                    {
                        yield return(focusDefocus.Current);
                    }
                    yield return(new WaitForSeconds(0.5f));
                }
                else if (!_responded)
                {
                    ComponentHandle.CommandInvalid(userNickName);
                }

                _currentUserNickName     = null;
                _processingTwitchCommand = false;
                yield break;
            }
        }

        IEnumerator focusCoroutine = BombCommander.Focus(Selectable, FocusDistance, FrontFace);

        while (focusCoroutine.MoveNext())
        {
            yield return(focusCoroutine.Current);
        }

        yield return(new WaitForSeconds(0.5f));

        IEnumerator unzoom = null;

        if (_zoom)
        {
            IEnumerator zoom = BombMessageResponder.moduleCameras?.ZoomCamera(BombComponent, 1);
            unzoom = BombMessageResponder.moduleCameras?.UnzoomCamera(BombComponent, 1);
            if (zoom == null || unzoom == null)
            {
                _zoom = false;
            }
            else
            {
                while (zoom.MoveNext())
                {
                    yield return(zoom.Current);
                }
            }
        }

        int  previousStrikeCount = StrikeCount;
        bool parseError          = false;
        bool needQuaternionReset = false;
        bool hideCamera          = false;
        bool exceptionThrown     = false;
        bool trycancelsequence   = false;

        while ((previousStrikeCount == StrikeCount && !Solved) || DisableOnStrike)
        {
            try
            {
                if (!subcoroutine.MoveNext())
                {
                    break;
                }
                else
                {
                    _responded = true;
                }
            }
            catch (Exception e)
            {
                exceptionThrown = true;
                HandleModuleException(e);
                break;
            }

            object currentValue = subcoroutine.Current;
            if (currentValue is string currentString)
            {
                if (currentString.Equals("strike", StringComparison.InvariantCultureIgnoreCase))
                {
                    _delegatedStrikeUserNickName = userNickName;
                }
                else if (currentString.Equals("solve", StringComparison.InvariantCultureIgnoreCase))
                {
                    _delegatedSolveUserNickName = userNickName;
                }
                else if (currentString.Equals("unsubmittablepenalty", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (TwitchPlaySettings.data.UnsubmittablePenaltyPercent <= 0)
                    {
                        continue;
                    }

                    int penalty = Math.Max((int)(modInfo.moduleScore * TwitchPlaySettings.data.UnsubmittablePenaltyPercent), 1);
                    Leaderboard.Instance.AddScore(_currentUserNickName, -penalty);
                    IRCConnection.Instance.SendMessage(TwitchPlaySettings.data.UnsubmittableAnswerPenalty, _currentUserNickName, Code, modInfo.moduleDisplayName, penalty, penalty > 1 ? "s" : "");
                }
                else if (currentString.Equals("parseerror", StringComparison.InvariantCultureIgnoreCase))
                {
                    parseError = true;
                    break;
                }
                else if (currentString.RegexMatch(out Match match, "^trycancel((?: (?:.|\\n)+)?)$"))
                {
                    if (CoroutineCanceller.ShouldCancel)
                    {
                        CoroutineCanceller.ResetCancel();
                        if (!string.IsNullOrEmpty(match.Groups[1].Value))
                        {
                            IRCConnection.Instance.SendMessage($"Sorry @{userNickName}, {match.Groups[1].Value.Trim()}");
                        }
                        break;
                    }
                }
                else if (currentString.RegexMatch(out match, "^trycancelsequence((?: (?:.|\\n)+)?)$"))
                {
                    trycancelsequence = true;
                    yield return(currentValue);

                    continue;
                }
                else if (currentString.RegexMatch(out match, "^trywaitcancel ([0-9]+(?:\\.[0-9])?)((?: (?:.|\\n)+)?)$") && float.TryParse(match.Groups[1].Value, out float waitCancelTime))
                {
                    yield return(new WaitForSecondsWithCancel(waitCancelTime, false));

                    if (CoroutineCanceller.ShouldCancel)
                    {
                        CoroutineCanceller.ResetCancel();
                        if (!string.IsNullOrEmpty(match.Groups[2].Value))
                        {
                            IRCConnection.Instance.SendMessage($"Sorry @{userNickName}, {match.Groups[2].Value.Trim()}");
                        }
                        break;
                    }
                }
                // Commands that allow messages to be sent to the chat.
                else if (SendToTwitchChat(currentString, userNickName))
                {
                    //handled
                }
                else if (currentString.StartsWith("add strike", StringComparison.InvariantCultureIgnoreCase))
                {
                    OnStrike(null);
                }
                else if (currentString.Equals("multiple strikes", StringComparison.InvariantCultureIgnoreCase))
                {
                    DisableOnStrike = true;
                }
                else if (currentString.Equals("end multiple strikes", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (previousStrikeCount == StrikeCount)
                    {
                        DisableOnStrike = false;
                        if (Solved)
                        {
                            OnPass(null);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else if (currentString.StartsWith("autosolve", StringComparison.InvariantCultureIgnoreCase))
                {
                    HandleModuleException(new Exception(currentString));
                    break;
                }
                else if (currentString.ToLowerInvariant().EqualsAny("detonate", "explode"))
                {
                    AwardStrikes(_currentUserNickName, BombCommander.StrikeLimit - BombCommander.StrikeCount);
                    BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(string.Empty, modInfo.moduleDisplayName);
                    break;
                }
                else if (currentString.RegexMatch(out match, "^(end |toggle )?(?:elevator|hold|waiting) music$"))
                {
                    if (match.Groups.Count > 1 && _musicPlayer != null)
                    {
                        _musicPlayer.StopMusic();
                        _musicPlayer = null;
                    }
                    else if (!currentString.StartsWith("end ", StringComparison.InvariantCultureIgnoreCase) && _musicPlayer == null)
                    {
                        _musicPlayer = MusicPlayer.StartRandomMusic();
                    }
                }
                else if (currentString.ToLowerInvariant().Equals("hide camera"))
                {
                    if (!hideCamera)
                    {
                        BombMessageResponder.moduleCameras?.Hide();
                        BombMessageResponder.moduleCameras?.HideHUD();
                        IEnumerator hideUI = BombCommander.twitchBombHandle.HideMainUIWindow();
                        while (hideUI.MoveNext())
                        {
                            yield return(hideUI.Current);
                        }
                    }
                    hideCamera = true;
                }
                else if (currentString.Equals("cancelled", StringComparison.InvariantCultureIgnoreCase) && CoroutineCanceller.ShouldCancel)
                {
                    CoroutineCanceller.ResetCancel();
                    TryCancel = false;
                    break;
                }
                else
                {
                    if (TwitchPlaySettings.data.EnableDebuggingCommands)
                    {
                        DebugHelper.Log($"Unprocessed string: {currentString}");
                    }
                }
            }
            else if (currentValue is KMSelectable selectable1)
            {
                if (HeldSelectables.Contains(selectable1))
                {
                    DoInteractionEnd(selectable1);
                    HeldSelectables.Remove(selectable1);
                }
                else
                {
                    DoInteractionStart(selectable1);
                    HeldSelectables.Add(selectable1);
                }
            }
            else if (currentValue is IList <KMSelectable> selectables)
            {
                foreach (KMSelectable selectable in selectables)
                {
                    yield return(DoInteractionClick(selectable));

                    if ((previousStrikeCount != StrikeCount && !DisableOnStrike) || Solved || (trycancelsequence && CoroutineCanceller.ShouldCancel))
                    {
                        break;
                    }
                }
                if (trycancelsequence && CoroutineCanceller.ShouldCancel)
                {
                    CoroutineCanceller.ResetCancel();
                    break;
                }
            }
            else if (currentValue is Quaternion localQuaternion)
            {
                BombCommander.RotateByLocalQuaternion(localQuaternion);
                //Whitelist perspective pegs as it only returns Quaternion.Euler(x, 0, 0), which is compatible with the RotateCamaraByQuaternion.
                if (BombComponent.GetComponent <KMBombModule>()?.ModuleType.Equals("spwizPerspectivePegs") ?? false)
                {
                    BombCommander.RotateCameraByLocalQuaternion(BombComponent, localQuaternion);
                }
                needQuaternionReset = true;
            }
            else if (currentValue is Quaternion[] localQuaternions)
            {
                if (localQuaternions.Length == 2)
                {
                    BombCommander.RotateByLocalQuaternion(localQuaternions[0]);
                    BombCommander.RotateCameraByLocalQuaternion(BombComponent, localQuaternions[1]);
                    needQuaternionReset = true;
                }
            }
            else if (currentValue is string[] currentStrings)
            {
                if (currentStrings.Length >= 1)
                {
                    if (currentStrings[0].ToLowerInvariant().EqualsAny("detonate", "explode"))
                    {
                        AwardStrikes(_currentUserNickName, BombCommander.StrikeLimit - BombCommander.StrikeCount);
                        switch (currentStrings.Length)
                        {
                        case 2:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(currentStrings[1], modInfo.moduleDisplayName);
                            break;

                        case 3:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(currentStrings[1], currentStrings[2]);
                            break;

                        default:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(string.Empty, modInfo.moduleDisplayName);
                            break;
                        }
                        break;
                    }
                }
            }
            yield return(currentValue);

            if (CoroutineCanceller.ShouldCancel)
            {
                TryCancel = true;
            }

            trycancelsequence = false;
        }

        if (!_responded && !exceptionThrown)
        {
            ComponentHandle.CommandInvalid(userNickName);
        }

        if (needQuaternionReset)
        {
            BombCommander.RotateByLocalQuaternion(Quaternion.identity);
            BombCommander.RotateCameraByLocalQuaternion(BombComponent, Quaternion.identity);
        }

        if (hideCamera)
        {
            BombMessageResponder.moduleCameras?.Show();
            BombMessageResponder.moduleCameras?.ShowHUD();
            IEnumerator showUI = BombCommander.twitchBombHandle.ShowMainUIWindow();
            while (showUI.MoveNext())
            {
                yield return(showUI.Current);
            }
        }

        if (_musicPlayer != null)
        {
            _musicPlayer.StopMusic();
            _musicPlayer = null;
        }

        if (DisableOnStrike)
        {
            DisableOnStrike = false;
            BombMessageResponder.moduleCameras?.UpdateStrikes(true);
            if (Solved)
            {
                OnPass(null);
            }
            AwardStrikes(_currentUserNickName, StrikeCount - previousStrikeCount);
        }

        if (!parseError)
        {
            yield return(new WaitForSeconds(0.5f));
        }

        if (_zoom)
        {
            while (unzoom?.MoveNext() ?? false)
            {
                yield return(unzoom.Current);
            }
        }

        IEnumerator defocusCoroutine = BombCommander.Defocus(Selectable, FrontFace);

        while (defocusCoroutine.MoveNext())
        {
            yield return(defocusCoroutine.Current);
        }

        yield return(new WaitForSeconds(0.5f));

        _currentUserNickName     = null;
        _processingTwitchCommand = false;
    }