protected override async Task OnLoaded()
        {
            this.GameLowestRoleAllowedComboBox.ItemsSource   = ChatCommand.PermissionsAllowedValues;
            this.GameLowestRoleAllowedComboBox.SelectedIndex = 0;

            this.CurrencyTypeComboBox.ItemsSource            = ChannelSession.Settings.Currencies.Values;
            this.CurrencyRequirementTypeComboBox.ItemsSource = EnumHelper.GetEnumNames <CurrencyRequirementTypeEnum>();

            this.GameTypeComboBox.ItemsSource = EnumHelper.GetEnumNames <GameTypeEnum>();

            this.AddOutcomeRankGroupButton.IsEnabled = (ChannelSession.Settings.Currencies.Values.Where(c => c.IsRank).Count() > 0);

            this.OutcomeCommandsItemsControl.ItemsSource = this.outcomeCommandControls;
            this.OutcomeGroupsItemsControl.ItemsSource   = this.outcomeGroupControls;

            await this.GameStartedCommandControl.Initialize(this, null);

            await this.GameEndedCommandControl.Initialize(this, null);

            await this.UserJoinedCommandControl.Initialize(this, null);

            await this.NotEnoughUsersCommandControl.Initialize(this, null);

            await this.UserParticipatedCommandControl.Initialize(this, null);

            await this.LoseLeftoverProbabilityCommandControl.Initialize(this, null);

            if (this.command != null)
            {
                this.GameNameTextBox.Text        = this.command.Name;
                this.GameChatCommandTextBox.Text = this.command.CommandsString;

                this.GameCooldownTextBox.Text = this.command.Cooldown.ToString();
                this.GameLowestRoleAllowedComboBox.SelectedItem = EnumHelper.GetEnumName(this.command.Permissions);

                if (this.command is SinglePlayerGameCommand)
                {
                    this.GameTypeComboBox.SelectedItem = EnumHelper.GetEnumName(GameTypeEnum.SinglePlayer);
                }
                else if (this.command is IndividualProbabilityGameCommand)
                {
                    this.GameTypeComboBox.SelectedItem = EnumHelper.GetEnumName(GameTypeEnum.IndividualProbabilty);

                    IndividualProbabilityGameCommand ipCommand = (IndividualProbabilityGameCommand)this.command;

                    this.GameLengthTextBox.Text = ipCommand.GameLength.ToString();
                    this.GameMinimumParticipantsTextBox.Text = ipCommand.MinimumParticipants.ToString();

                    await this.GameStartedCommandControl.Initialize(this, ipCommand.GameStartedCommand);

                    await this.GameEndedCommandControl.Initialize(this, ipCommand.GameEndedCommand);

                    await this.UserJoinedCommandControl.Initialize(this, ipCommand.UserJoinedCommand);

                    await this.NotEnoughUsersCommandControl.Initialize(this, ipCommand.NotEnoughUsersCommand);
                }
                else if (this.command is OnlyOneWinnerGameCommand)
                {
                    this.GameTypeComboBox.SelectedItem = EnumHelper.GetEnumName(GameTypeEnum.OnlyOneWinner);

                    OnlyOneWinnerGameCommand oowCommand = (OnlyOneWinnerGameCommand)this.command;

                    this.GameLengthTextBox.Text = oowCommand.GameLength.ToString();
                    this.GameMinimumParticipantsTextBox.Text = oowCommand.MinimumParticipants.ToString();

                    await this.GameStartedCommandControl.Initialize(this, oowCommand.GameStartedCommand);

                    await this.GameEndedCommandControl.Initialize(this, oowCommand.GameEndedCommand);

                    await this.UserJoinedCommandControl.Initialize(this, oowCommand.UserJoinedCommand);

                    await this.NotEnoughUsersCommandControl.Initialize(this, oowCommand.NotEnoughUsersCommand);
                }
                else if (this.command is UserCharityGameCommand)
                {
                    this.GameTypeComboBox.SelectedItem = EnumHelper.GetEnumName(GameTypeEnum.UserCharity);

                    UserCharityGameCommand rucComand = (UserCharityGameCommand)this.command;

                    this.GiveToRandomUserCharityToggleButton.IsChecked = rucComand.GiveToRandomUser;

                    await this.UserParticipatedCommandControl.Initialize(this, rucComand.UserParticipatedCommand);
                }

                this.CurrencyTypeComboBox.SelectedItem            = this.command.CurrencyRequirement.GetCurrency();
                this.CurrencyRequirementTypeComboBox.SelectedItem = EnumHelper.GetEnumName(this.command.CurrencyRequirementType);
                this.CurrencyMinimumCostTextBox.Text = this.command.CurrencyRequirement.RequiredAmount.ToString();
                this.CurrencyMaximumCostTextBox.Text = this.command.CurrencyRequirement.MaximumAmount.ToString();

                if (this.command is OutcomeGameCommandBase)
                {
                    OutcomeGameCommandBase oCommand = (OutcomeGameCommandBase)this.command;

                    await this.LoseLeftoverProbabilityCommandControl.Initialize(this, oCommand.LoseLeftoverCommand);

                    this.outcomeCommandControls.Clear();
                    foreach (GameOutcome outcome in oCommand.Outcomes)
                    {
                        GameOutcomeCommandControl outcomeControl = new GameOutcomeCommandControl(outcome);
                        await outcomeControl.Initialize(this);

                        this.outcomeCommandControls.Add(outcomeControl);
                    }

                    this.outcomeGroupControls.Clear();
                    foreach (GameOutcomeGroup group in oCommand.Groups)
                    {
                        this.outcomeGroupControls.Add(new GameOutcomeGroupControl(group));
                    }
                }
            }
            else
            {
                this.GameCooldownTextBox.Text = "0";
            }
        }
Exemplo n.º 2
0
        private static async Task Version5Upgrade(int version, string filePath)
        {
            if (version < 5)
            {
                LegacyDesktopChannelSettings legacySettings = await SerializerHelper.DeserializeFromFile <LegacyDesktopChannelSettings>(filePath);

                DesktopChannelSettings settings = await SerializerHelper.DeserializeFromFile <DesktopChannelSettings>(filePath);

                await ChannelSession.Services.Settings.Initialize(settings);

                foreach (string quote in legacySettings.quotesInternal)
                {
                    settings.UserQuotes.Add(new UserQuoteViewModel(quote));
                }

                List <CommandBase> commands = new List <CommandBase>();
                commands.AddRange(settings.ChatCommands);
                commands.AddRange(settings.InteractiveCommands);
                commands.AddRange(settings.EventCommands);
                commands.AddRange(settings.TimerCommands);

                UserCurrencyViewModel currency = settings.Currencies.Values.FirstOrDefault(c => !c.IsRank);
                if (currency == null)
                {
                    currency = settings.Currencies.Values.FirstOrDefault();
                }

                foreach (CommandBase command in commands)
                {
                    foreach (ActionBase action in command.Actions)
                    {
                        if (action is InteractiveAction)
                        {
                            InteractiveAction nAction = (InteractiveAction)action;
#pragma warning disable CS0612 // Type or member is obsolete
                            if (nAction.AddUserToGroup)
                            {
                                nAction.InteractiveType = InteractiveActionTypeEnum.MoveUserToGroup;
                            }
                            else
                            {
                                nAction.InteractiveType = InteractiveActionTypeEnum.MoveGroupToScene;
                            }
                            nAction.SceneID = nAction.MoveGroupToScene;
#pragma warning restore CS0612 // Type or member is obsolete
                        }

                        if (currency != null)
                        {
                            if (action is ChatAction)
                            {
                                ChatAction nAction = (ChatAction)action;
                                nAction.ChatText = nAction.ChatText.Replace("$usercurrencyname", currency.Name);
                            }
                            else if (action is CurrencyAction)
                            {
                                CurrencyAction nAction = (CurrencyAction)action;
                                nAction.ChatText = nAction.ChatText.Replace("$usercurrencyname", currency.Name);
                            }
                            else if (action is OBSStudioAction)
                            {
                                OBSStudioAction nAction = (OBSStudioAction)action;
                                if (!string.IsNullOrEmpty(nAction.SourceText))
                                {
                                    nAction.SourceText = nAction.SourceText.Replace("$usercurrencyname", currency.Name);
                                }
                            }
                            else if (action is OverlayAction)
                            {
                                OverlayAction nAction = (OverlayAction)action;
                                if (!string.IsNullOrEmpty(nAction.Text))
                                {
                                    nAction.Text = nAction.Text.Replace("$usercurrencyname", currency.Name);
                                }
                            }
                            else if (action is TextToSpeechAction)
                            {
                                TextToSpeechAction nAction = (TextToSpeechAction)action;
                                nAction.SpeechText = nAction.SpeechText.Replace("$usercurrencyname", currency.Name);
                            }
                            else if (action is XSplitAction)
                            {
                                XSplitAction nAction = (XSplitAction)action;
                                if (!string.IsNullOrEmpty(nAction.SourceText))
                                {
                                    nAction.SourceText = nAction.SourceText.Replace("$usercurrencyname", currency.Name);
                                }
                            }
                        }
                    }
                }

                foreach (GameCommandBase game in settings.GameCommands)
                {
                    if (game is IndividualProbabilityGameCommand)
                    {
                        IndividualProbabilityGameCommand individualGame = (IndividualProbabilityGameCommand)game;
                        DesktopSettingsUpgrader.SetAllGameChatActionsToWhispers(individualGame.UserJoinedCommand);
                        DesktopSettingsUpgrader.SetAllGameChatActionsToWhispers(individualGame.LoseLeftoverCommand);
                        foreach (GameOutcome outcome in individualGame.Outcomes)
                        {
                            DesktopSettingsUpgrader.SetAllGameChatActionsToWhispers(outcome.ResultCommand);
                        }
                    }
                    else if (game is OnlyOneWinnerGameCommand)
                    {
                        OnlyOneWinnerGameCommand oneWinnerGame = (OnlyOneWinnerGameCommand)game;
                        DesktopSettingsUpgrader.SetAllGameChatActionsToWhispers(oneWinnerGame.UserJoinedCommand);
                    }
                }

                await ChannelSession.Services.Settings.Save(settings);
            }
        }
        private async void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            await this.RunAsyncOperation(async() =>
            {
                if (string.IsNullOrEmpty(this.GameNameTextBox.Text))
                {
                    await MessageBoxHelper.ShowMessageDialog("Name is missing");
                    return;
                }

                if (string.IsNullOrEmpty(this.GameChatCommandTextBox.Text))
                {
                    await MessageBoxHelper.ShowMessageDialog("Commands is missing");
                    return;
                }

                if (this.GameChatCommandTextBox.Text.Any(c => !Char.IsLetterOrDigit(c) && !Char.IsWhiteSpace(c)))
                {
                    await MessageBoxHelper.ShowMessageDialog("Commands can only contain letters and numbers");
                    return;
                }

                foreach (PermissionsCommandBase command in ChannelSession.AllChatCommands)
                {
                    if (this.command != command && this.GameNameTextBox.Text.Equals(command.Name))
                    {
                        await MessageBoxHelper.ShowMessageDialog("There already exists a chat command with the same name");
                        return;
                    }
                }

                IEnumerable <string> commandStrings = this.GetCommandStrings();
                if (commandStrings.GroupBy(c => c).Where(g => g.Count() > 1).Count() > 0)
                {
                    await MessageBoxHelper.ShowMessageDialog("Each command string must be unique");
                    return;
                }

                foreach (PermissionsCommandBase command in ChannelSession.AllChatCommands)
                {
                    if (command.IsEnabled && this.GetExistingCommand() != command)
                    {
                        if (commandStrings.Any(c => command.Commands.Contains(c)))
                        {
                            await MessageBoxHelper.ShowMessageDialog("There already exists a chat command that uses one of the command strings you have specified");
                            return;
                        }
                    }
                }

                int cooldown = 0;
                if (!int.TryParse(this.GameCooldownTextBox.Text, out cooldown) || cooldown < 0)
                {
                    await MessageBoxHelper.ShowMessageDialog("Cooldown must be 0 or greater");
                    return;
                }

                if (this.GameLowestRoleAllowedComboBox.SelectedIndex < 0)
                {
                    await MessageBoxHelper.ShowMessageDialog("A permission level must be selected");
                    return;
                }

                if (this.GameTypeComboBox.SelectedIndex < 0)
                {
                    await MessageBoxHelper.ShowMessageDialog("A game type must be selected");
                    return;
                }
                GameTypeEnum gameType = EnumHelper.GetEnumValueFromString <GameTypeEnum>((string)this.GameTypeComboBox.SelectedItem);

                if (this.CurrencyTypeComboBox.SelectedIndex < 0 || this.CurrencyRequirementTypeComboBox.SelectedIndex < 0)
                {
                    await MessageBoxHelper.ShowMessageDialog("A currency type and requirement must be selected");
                    return;
                }

                int minimum = 0;
                int maximum = 0;

                CurrencyRequirementTypeEnum currencyRequirementType = EnumHelper.GetEnumValueFromString <CurrencyRequirementTypeEnum>((string)this.CurrencyRequirementTypeComboBox.SelectedItem);
                if (currencyRequirementType != CurrencyRequirementTypeEnum.NoCurrencyCost && (!int.TryParse(this.CurrencyMinimumCostTextBox.Text, out minimum) || minimum < 0))
                {
                    await MessageBoxHelper.ShowMessageDialog("The currency minimum must be 0 or greater");
                    return;
                }

                if (currencyRequirementType == CurrencyRequirementTypeEnum.MinimumAndMaximum && (!int.TryParse(this.CurrencyMaximumCostTextBox.Text, out maximum) || maximum <= minimum))
                {
                    await MessageBoxHelper.ShowMessageDialog("The currency maximum must be greater than the minimum");
                    return;
                }

                int gameLength      = 0;
                int minParticipants = 0;
                if (this.MultiplayerGameDetailsGrid.Visibility == Visibility.Visible)
                {
                    if (!int.TryParse(this.GameLengthTextBox.Text, out gameLength) || gameLength < 0)
                    {
                        await MessageBoxHelper.ShowMessageDialog("Game length must be 0 or greater");
                        return;
                    }

                    if (!int.TryParse(this.GameMinimumParticipantsTextBox.Text, out minParticipants) || minParticipants < 1)
                    {
                        await MessageBoxHelper.ShowMessageDialog("Minimum participants must be 1 or greater");
                        return;
                    }
                }

                List <GameOutcome> outcomes           = new List <GameOutcome>();
                List <GameOutcomeGroup> outcomeGroups = new List <GameOutcomeGroup>();
                if (gameType == GameTypeEnum.SinglePlayer || gameType == GameTypeEnum.IndividualProbabilty)
                {
                    foreach (GameOutcomeCommandControl commandControl in this.outcomeCommandControls)
                    {
                        GameOutcome outcome = await commandControl.GetOutcome();
                        if (outcome == null)
                        {
                            return;
                        }
                        outcomes.Add(outcome);
                    }

                    if (outcomes.Select(o => o.Name).GroupBy(n => n).Where(g => g.Count() > 1).Count() > 0)
                    {
                        await MessageBoxHelper.ShowMessageDialog("Each outcome must have a unique name");
                        return;
                    }

                    foreach (GameOutcomeGroupControl groupControl in this.outcomeGroupControls)
                    {
                        GameOutcomeGroup group = await groupControl.GetOutcomeGroup();
                        if (group == null)
                        {
                            return;
                        }
                        outcomeGroups.Add(group);
                        for (int i = 0; i < outcomes.Count; i++)
                        {
                            group.Probabilities[i].OutcomeName = outcomes[i].Name;
                        }
                    }
                }

                UserRole permissionsRole = EnumHelper.GetEnumValueFromString <UserRole>((string)this.GameLowestRoleAllowedComboBox.SelectedItem);

                if (this.command != null)
                {
                    ChannelSession.Settings.GameCommands.Remove(this.command);
                }

                UserCurrencyRequirementViewModel currencyRequirement = new UserCurrencyRequirementViewModel((UserCurrencyViewModel)this.CurrencyTypeComboBox.SelectedItem, minimum, maximum);
                if (gameType == GameTypeEnum.SinglePlayer)
                {
                    this.command = new SinglePlayerGameCommand(this.GameNameTextBox.Text, this.GetCommandStrings(), permissionsRole, cooldown, currencyRequirement, currencyRequirementType, outcomes,
                                                               outcomeGroups, this.LoseLeftoverProbabilityCommandControl.GetCommand());
                }
                else if (gameType == GameTypeEnum.IndividualProbabilty)
                {
                    IndividualProbabilityGameCommand ipCommand = new IndividualProbabilityGameCommand(this.GameNameTextBox.Text, this.GetCommandStrings(), permissionsRole, cooldown,
                                                                                                      currencyRequirement, currencyRequirementType, outcomes, outcomeGroups, this.LoseLeftoverProbabilityCommandControl.GetCommand(), gameLength, minParticipants);
                    ipCommand.GameStartedCommand    = this.GameStartedCommandControl.GetCommand();
                    ipCommand.GameEndedCommand      = this.GameEndedCommandControl.GetCommand();
                    ipCommand.UserJoinedCommand     = this.UserJoinedCommandControl.GetCommand();
                    ipCommand.NotEnoughUsersCommand = this.NotEnoughUsersCommandControl.GetCommand();
                    this.command = ipCommand;
                }
                else if (gameType == GameTypeEnum.OnlyOneWinner)
                {
                    OnlyOneWinnerGameCommand oowCommand = new OnlyOneWinnerGameCommand(this.GameNameTextBox.Text, this.GetCommandStrings(), permissionsRole, cooldown, currencyRequirement,
                                                                                       gameLength, minParticipants);
                    oowCommand.GameStartedCommand    = this.GameStartedCommandControl.GetCommand();
                    oowCommand.GameEndedCommand      = this.GameEndedCommandControl.GetCommand();
                    oowCommand.UserJoinedCommand     = this.UserJoinedCommandControl.GetCommand();
                    oowCommand.NotEnoughUsersCommand = this.NotEnoughUsersCommandControl.GetCommand();
                    this.command = oowCommand;
                }
                else if (gameType == GameTypeEnum.UserCharity)
                {
                    UserCharityGameCommand ucCommand = new UserCharityGameCommand(this.GameNameTextBox.Text, this.GetCommandStrings(), permissionsRole, cooldown, currencyRequirement,
                                                                                  currencyRequirementType, this.GiveToRandomUserCharityToggleButton.IsChecked.GetValueOrDefault());
                    ucCommand.UserParticipatedCommand = this.UserParticipatedCommandControl.GetCommand();
                    this.command = ucCommand;
                }

                ChannelSession.Settings.GameCommands.Add(this.command);

                await ChannelSession.SaveSettings();

                this.Close();
            });
        }