protected bool SendToTwitchChat(string message, string userNickName)
    {
        // Within the messages, allow variables:
        // {0} = user’s nickname
        // {1} = Code (module number)
        if (message.RegexMatch(out Match match, @"^senddelayedmessage ([0-9]+(?:\.[0-9])?) (\S(?:\S|\s)*)$") && float.TryParse(match.Groups[1].Value, out float messageDelayTime))
        {
            ComponentHandle.StartCoroutine(SendDelayedMessage(messageDelayTime, string.Format(match.Groups[3].Value, userNickName, ComponentHandle.Code)));
            return(true);
        }

        if (!message.RegexMatch(out match, @"^(sendtochat|sendtochaterror|strikemessage) +(\S(?:\S|\s)*)$"))
        {
            return(false);
        }

        var chatMsg = string.Format(match.Groups[2].Value, userNickName, ComponentHandle.Code);

        switch (match.Groups[1].Value)
        {
        case "sendtochat":
            IRCConnection.Instance.SendMessage(chatMsg);
            return(true);

        case "sendtochaterror":
            ComponentHandle.CommandError(userNickName, chatMsg);
            return(true);

        case "strikemessage":
            StrikeMessage = chatMsg;
            return(true);

        default:
            return(false);
        }
    }
Example #2
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;
    }