From 9f4ddc63cb6d3141a9e78b0d098768a19e69be2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Sg=C3=A1netz?= Date: Tue, 22 Nov 2022 01:06:03 +0100 Subject: [PATCH] Mark questions with intro audio --- TriviaMurderPartyModder/Data/DataJet.cs | 68 ++++--- TriviaMurderPartyModder/Data/FinalRounder.cs | 2 +- TriviaMurderPartyModder/Data/Question.cs | 9 +- TriviaMurderPartyModder/Data/WorstDrawing.cs | 2 +- TriviaMurderPartyModder/Data/WorstResponse.cs | 2 +- .../MainWindow.FinalRound.cs | 159 +++++++++++++++ .../MainWindow.Question.cs | 45 ++++ .../MainWindow.WorstDrawing.cs | 22 ++ .../MainWindow.WorstResponse.cs | 22 ++ TriviaMurderPartyModder/MainWindow.xaml | 14 +- TriviaMurderPartyModder/MainWindow.xaml.cs | 192 ------------------ .../TriviaMurderPartyModder.csproj | 4 + 12 files changed, 312 insertions(+), 229 deletions(-) create mode 100644 TriviaMurderPartyModder/MainWindow.FinalRound.cs create mode 100644 TriviaMurderPartyModder/MainWindow.Question.cs create mode 100644 TriviaMurderPartyModder/MainWindow.WorstDrawing.cs create mode 100644 TriviaMurderPartyModder/MainWindow.WorstResponse.cs diff --git a/TriviaMurderPartyModder/Data/DataJet.cs b/TriviaMurderPartyModder/Data/DataJet.cs index 27a9e91..ec6f531 100644 --- a/TriviaMurderPartyModder/Data/DataJet.cs +++ b/TriviaMurderPartyModder/Data/DataJet.cs @@ -9,9 +9,7 @@ public string Contents { get { string dataPath = Path.Combine(Folder, "data.jet"); if (contents == null) { - if (File.Exists(dataPath)) - return contents = File.ReadAllText(dataPath); - return contents = defaultContents; + contents = (File.Exists(dataPath) ? File.ReadAllText(dataPath) : defaultContents); } return contents; } @@ -28,22 +26,15 @@ public DataJet(string path, int id, string defaultContents = null) { this.defaultContents = defaultContents; } - void Commit() { - Directory.CreateDirectory(Folder); - File.WriteAllText(Path.Combine(Folder, "data.jet"), contents); - } - - string ReplaceValue(int v, string value) { - int vEnd = contents.IndexOf(',', v + 2), qm2 = contents.LastIndexOf('"', vEnd - 1) - 1, qm1 = contents.LastIndexOf('"', qm2); - string oldValue = contents.Substring(qm1 + 1, qm2 - qm1); - contents = contents.Remove(v, vEnd - v).Insert(v, string.Format("\"v\":\"{0}\"", value)); - return oldValue; + public static void GetIfNotLoaded(ref DataJet jet, string dataFolder, int id, string defaultContents = null) { + if (jet == null) { + jet = new DataJet(dataFolder, id, defaultContents); + } } - void ReplaceValue(string name, string value) { - int pos = Contents.LastIndexOf(string.Format("\"{0}\"", name)); - if (pos != -1) - ReplaceValue(contents.LastIndexOf("\"v\"", pos), value); + public bool GetAudioFileActive(AudioType type) { + int pos = Contents.LastIndexOf($"\"Has{type}\""), v = contents.LastIndexOf("\"v\"", pos); + return v != -1 && GetValue(v).Equals("true"); } public void SetValue(string name, string value) { @@ -52,20 +43,22 @@ public void SetValue(string name, string value) { } public void SetValues(string[] names, string value) { - for (int i = 0; i < names.Length; ++i) + for (int i = 0; i < names.Length; i++) { ReplaceValue(names[i], value); + } Commit(); } public void SetAudioFile(AudioType type, string sourceFile) { string defaultName = type.ToString(); - int pos = Contents.LastIndexOf(string.Format("\"{0}\"", defaultName)), v = contents.LastIndexOf("\"v\"", pos); - if (contents.IndexOf('{', v, pos - v) != -1) // Has no file, there is an array element separation between - contents = contents.Insert(contents.LastIndexOf(',', pos - 4), string.Format(",\"v\":\"{0}\"", defaultName)); - else { + int pos = Contents.LastIndexOf($"\"{defaultName}\""), v = contents.LastIndexOf("\"v\"", pos); + if (contents.IndexOf('{', v, pos - v) != -1) { // Has no file, there is an array element separation between + contents = contents.Insert(contents.LastIndexOf(',', pos - 4), $",\"v\":\"{defaultName}\""); + } else { if (MessageBox.Show("There is already an audio file. Do you want to overwrite?", "Overwrite", - MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No) + MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No) { return; + } File.Delete(Path.Combine(Folder, ReplaceValue(v, defaultName) + ".ogg")); } SetValue("Has" + defaultName, "true"); @@ -77,9 +70,32 @@ public void RemoveAudioFile(AudioType type) { MessageBox.Show(type.ToString() + " audio was removed for the selected entry."); } - public static void Get(ref DataJet jet, string dataFolder, int id, string defaultContents = null) { - if (jet == null) - jet = new DataJet(dataFolder, id, defaultContents); + void Commit() { + Directory.CreateDirectory(Folder); + File.WriteAllText(Path.Combine(Folder, "data.jet"), contents); + } + + string GetValue(int afterIndex) { + int vEnd = contents.IndexOf(',', afterIndex + 2), + qm2 = contents.LastIndexOf('"', vEnd - 1) - 1, + qm1 = contents.LastIndexOf('"', qm2); + return contents.Substring(qm1 + 1, qm2 - qm1); + } + + string ReplaceValue(int v, string value) { + int vEnd = contents.IndexOf(',', v + 2), + qm2 = contents.LastIndexOf('"', vEnd - 1) - 1, + qm1 = contents.LastIndexOf('"', qm2); + string oldValue = contents.Substring(qm1 + 1, qm2 - qm1); + contents = contents.Remove(v, vEnd - v).Insert(v, $"\"v\":\"{value}\""); + return oldValue; + } + + void ReplaceValue(string name, string value) { + int pos = Contents.LastIndexOf($"\"{name}\""); + if (pos != -1) { + ReplaceValue(contents.LastIndexOf("\"v\"", pos), value); + } } } } \ No newline at end of file diff --git a/TriviaMurderPartyModder/Data/FinalRounder.cs b/TriviaMurderPartyModder/Data/FinalRounder.cs index b49bc53..d3c9a6e 100644 --- a/TriviaMurderPartyModder/Data/FinalRounder.cs +++ b/TriviaMurderPartyModder/Data/FinalRounder.cs @@ -31,7 +31,7 @@ public void ImportTopicAudio(string dataFolder, string audioFile) { if (audioFile == null) { return; } - DataJet.Get(ref jet, dataFolder, id, + DataJet.GetIfNotLoaded(ref jet, dataFolder, id, "{\"fields\":[{\"t\":\"B\",\"v\":\"false\",\"n\":\"HasQ\"},{\"t\":\"A\",\"n\":\"Q\"}]}"); jet.SetAudioFile(AudioType.Q, audioFile); } diff --git a/TriviaMurderPartyModder/Data/Question.cs b/TriviaMurderPartyModder/Data/Question.cs index 85d976c..d7b4e30 100644 --- a/TriviaMurderPartyModder/Data/Question.cs +++ b/TriviaMurderPartyModder/Data/Question.cs @@ -57,16 +57,21 @@ public string this[int key] { DataJet jet; + public bool GetIntroAudio(string dataFolder) { + DataJet.GetIfNotLoaded(ref jet, dataFolder, ID, defaultJet); + return jet.GetAudioFileActive(AudioType.Intro); + } + public void ImportAudio(string dataFolder, AudioType type, string audioFile) { if (audioFile == null) { return; } - DataJet.Get(ref jet, dataFolder, ID, defaultJet); + DataJet.GetIfNotLoaded(ref jet, dataFolder, ID, defaultJet); jet.SetAudioFile(type, audioFile); } public void RemoveAudio(string dataFolder, AudioType type) { - DataJet.Get(ref jet, dataFolder, ID, defaultJet); + DataJet.GetIfNotLoaded(ref jet, dataFolder, ID, defaultJet); jet.RemoveAudioFile(type); } } diff --git a/TriviaMurderPartyModder/Data/WorstDrawing.cs b/TriviaMurderPartyModder/Data/WorstDrawing.cs index c21651d..dea0ef2 100644 --- a/TriviaMurderPartyModder/Data/WorstDrawing.cs +++ b/TriviaMurderPartyModder/Data/WorstDrawing.cs @@ -23,7 +23,7 @@ public WorstDrawing(int id, string category) { public void ImportAudio(string dataFolder, string audioFile) { if (string.IsNullOrEmpty(audioFile)) return; - DataJet.Get(ref jet, dataFolder, ID, string.Format( + DataJet.GetIfNotLoaded(ref jet, dataFolder, ID, string.Format( "{{\"fields\":[{{\"t\":\"B\",\"v\":\"false\",\"n\":\"HasJokeAudio\"}},{{\"t\":\"S\",\"v\":\"{0}\",\"n\":\"QuestionText\"}}," + "{{\"t\":\"S\",\"v\":\"\",\"n\":\"AlternateSpellings\"}},{{\"t\":\"A\",\"n\":\"JokeAudio\"}}]}}", Category)); jet.SetAudioFile(AudioType.JokeAudio, audioFile); diff --git a/TriviaMurderPartyModder/Data/WorstResponse.cs b/TriviaMurderPartyModder/Data/WorstResponse.cs index 4077952..9c776b0 100644 --- a/TriviaMurderPartyModder/Data/WorstResponse.cs +++ b/TriviaMurderPartyModder/Data/WorstResponse.cs @@ -23,7 +23,7 @@ public void ImportAudio(string dataFolder, string audioFile) { if (audioFile == null) { return; } - DataJet.Get(ref jet, dataFolder, ID, string.Format( + DataJet.GetIfNotLoaded(ref jet, dataFolder, ID, string.Format( "{{\"fields\":[{{\"t\":\"B\",\"v\":\"false\",\"n\":\"HasBumperAudio\"}},{{\"t\":\"B\",\"v\":\"false\",\"n\":\"HasBumperType\"}}," + "{{\"t\":\"B\",\"v\":\"false\",\"n\":\"HasCorrectAudio\"}},{{\"t\":\"B\",\"v\":\"false\",\"n\":\"HasQuestionAudio\"}}," + "{{\"t\":\"S\",\"v\":\"\",\"n\":\"Suggestions\"}},{{\"t\":\"S\",\"v\":\"{0}\",\"n\":\"Category\"}}," + diff --git a/TriviaMurderPartyModder/MainWindow.FinalRound.cs b/TriviaMurderPartyModder/MainWindow.FinalRound.cs new file mode 100644 index 0000000..7cf206a --- /dev/null +++ b/TriviaMurderPartyModder/MainWindow.FinalRound.cs @@ -0,0 +1,159 @@ +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; + +using TriviaMurderPartyModder.Data; +using TriviaMurderPartyModder.Dialogs; +using TriviaMurderPartyModder.Files; +using TriviaMurderPartyModder.Properties; + +namespace TriviaMurderPartyModder { + public partial class MainWindow { + void SelectFinalQuestion(FinalRounder question) { + selectedTopic = question; + topicId.Text = question.ID.ToString(); + topic.Text = question.Text; + } + + void DeselectChoice() { + selectedChoice = null; + choiceCorrect.IsChecked = false; + choiceAnswer.Text = string.Empty; + } + + void FinalRoundSelection(object sender, RoutedPropertyChangedEventArgs e) { + TreeViewItem selected = (TreeViewItem)((TreeView)sender).SelectedItem; + if (selected is FinalRounder topic) { + DeselectChoice(); + SelectFinalQuestion(topic); + } else if (selected != null) { + selectedChoice = (FinalRounderChoice)selected; + choiceCorrect.IsChecked = selectedChoice.Correct; + choiceAnswer.Text = selectedChoice.Text; + SelectFinalQuestion((FinalRounder)selected.Parent); + } + } + + void AddTopic(object _, RoutedEventArgs e) { + FinalRounder newTopic = new FinalRounder(0, "New topic"); + finalRoundList.Add(newTopic); + newTopic.IsSelected = true; + topic.SelectAll(); + topic.Focus(); + } + + void AddTopicChoice(object _, RoutedEventArgs e) { + if (selectedTopic != null) { + FinalRounderChoice choice = new FinalRounderChoice(false, "New choice"); + selectedTopic.Items.Add(choice); + selectedTopic.IsExpanded = true; + choice.IsSelected = true; + choiceAnswer.SelectAll(); + choiceAnswer.Focus(); + finalRoundList.Unsaved = true; + } + } + + void AddTopicChoices(object _, RoutedEventArgs e) { + if (selectedTopic != null) { + BulkOption form = new BulkOption(); + bool? result = form.ShowDialog(); + if (result.HasValue && result.Value) { + selectedTopic.IsExpanded = true; + finalRoundList.Unsaved = true; + string[] correct = form.CorrectValues, incorrect = form.IncorrectValues; + for (int i = 0; i < correct.Length; ++i) { + FinalRounderChoice choice = new FinalRounderChoice(true, correct[i]); + selectedTopic.Items.Add(choice); + } + for (int i = 0; i < incorrect.Length; ++i) { + FinalRounderChoice choice = new FinalRounderChoice(false, incorrect[i]); + selectedTopic.Items.Add(choice); + } + } + } + } + + void AddTopicAudio(object _, RoutedEventArgs e) => + selectedTopic.ImportTopicAudio(finalRoundList.DataFolderPath, LoadAudio(questions, questionList)); + + void TopicIDChange(object sender, TextChangedEventArgs e) { + TextBox box = (TextBox)sender; + if (!int.TryParse(box.Text, out int id)) { + box.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0)); + return; + } + box.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255)); + if (selectedTopic != null) { + selectedTopic.ID = id; + finalRoundList.Unsaved = true; + } + } + + void TopicChange(object sender, TextChangedEventArgs e) { + if (selectedTopic != null) { + selectedTopic.Text = ((TextBox)sender).Text; + finalRoundList.Unsaved = true; + } + } + + void RemoveTopic(object _, RoutedEventArgs e) { + if (selectedTopic != null) { + DeselectChoice(); + finalRoundList.Remove(selectedTopic); + } + } + + void ChoiceCorrect(object sender, RoutedEventArgs e) { + if (selectedChoice != null) { + selectedChoice.Correct = ((CheckBox)sender).IsChecked.Value; + finalRoundList.Unsaved = true; + } + } + + void ChoiceText(object sender, TextChangedEventArgs e) { + if (selectedChoice != null) { + selectedChoice.Text = ((TextBox)sender).Text; + finalRoundList.Unsaved = true; + } + } + + void RemoveChoice(object _, RoutedEventArgs e) { + if (selectedChoice != null) { + selectedTopic.Items.Remove(selectedChoice); + DeselectChoice(); + finalRoundList.Unsaved = true; + } + } + + void FinalRoundImport(object _, RoutedEventArgs e) => finalRoundList.Import(true); + void FinalRoundImportLastSave(object _, RoutedEventArgs e) => + finalRoundList.ImportFrom(Settings.Default.lastFinalRound); + void FinalRoundMerge(object _, RoutedEventArgs e) => finalRoundList.Import(false); + void FinalRoundSave(object _, RoutedEventArgs e) => finalRoundList.Save(); + void FinalRoundSaveAs(object _, RoutedEventArgs e) => finalRoundList.SaveAs(); + + void FinalRoundReleaseCheck(object _, RoutedEventArgs e) { + string finalRoundFileDir = null; + if (finalRoundList.FileName != null) + finalRoundFileDir = finalRoundList.DataFolderPath; + for (int i = 0, end = finalRoundList.Count; i < end; ++i) { + for (int j = i + 1; j < end; ++j) { + if (finalRoundList[i].ID == finalRoundList[j].ID) { + FinalRounders.FinalRoundIssue(string.Format(Properties.Resources.multipleIDs, finalRoundList[i].ID)); + return; + } + } + if (finalRoundList[i].Items.Count < 3) { + FinalRounders.FinalRoundIssue(string.Format("{0} has less than 3 choices.", finalRoundList[i].Text)); + return; + } + if (finalRoundList.FileName != null && !Parsing.CheckAudio(finalRoundFileDir, finalRoundList[i].ID)) { + FinalRounders.FinalRoundIssue(string.Format(Properties.Resources.missingAudio, finalRoundList[i].ID)); + return; + } + } + MessageBox.Show(Properties.Resources.checkSuccess, Properties.Resources.checkResult); + } + } +} \ No newline at end of file diff --git a/TriviaMurderPartyModder/MainWindow.Question.cs b/TriviaMurderPartyModder/MainWindow.Question.cs new file mode 100644 index 0000000..fb9bfee --- /dev/null +++ b/TriviaMurderPartyModder/MainWindow.Question.cs @@ -0,0 +1,45 @@ +using System.Windows; +using System.Windows.Controls; + +using TriviaMurderPartyModder.Data; +using TriviaMurderPartyModder.Properties; + +namespace TriviaMurderPartyModder { + public partial class MainWindow { + void ImportQuestionAudio(AudioType type) { + if (!(questions.SelectedItem is Question question)) { + return; + } + question.ImportAudio(questionList.DataFolderPath, type, LoadAudio(questions, questionList)); + hasIntro.IsChecked = question.GetIntroAudio(questionList.DataFolderPath); + } + + void RemoveQuestionAudio(AudioType type) { + if (!(questions.SelectedItem is Question question)) { + return; + } + question.RemoveAudio(questionList.DataFolderPath, type); + hasIntro.IsChecked = question.GetIntroAudio(questionList.DataFolderPath); + } + + void QuestionSelected(object _, SelectionChangedEventArgs e) { + if (!(questions.SelectedItem is Question question)) { + return; + } + hasIntro.IsChecked = question.GetIntroAudio(questionList.DataFolderPath); + } + + void Questions_CellEditEnding(object _, DataGridCellEditEndingEventArgs e) => questionList.Unsaved = true; + void QuestionImport(object _, RoutedEventArgs e) => questionList.Import(true); + void QuestionImportLastSave(object _, RoutedEventArgs e) => questionList.ImportFrom(Settings.Default.lastQuestion); + void QuestionMerge(object _, RoutedEventArgs e) => questionList.Import(false); + void QuestionSave(object _, RoutedEventArgs e) => questionList.Save(); + void QuestionSaveAs(object _, RoutedEventArgs e) => questionList.SaveAs(); + void QuestionReleaseCheck(object _, RoutedEventArgs e) => ReleaseCheck(questionList); + void QuestionEqualize(object _, RoutedEventArgs e) => questionList.Equalize(); + void QuestionAudio(object _, RoutedEventArgs e) => ImportQuestionAudio(AudioType.Q); + void QuestionIntroAudio(object _, RoutedEventArgs e) => ImportQuestionAudio(AudioType.Intro); + void RemoveIntroAudio(object _, RoutedEventArgs e) => RemoveQuestionAudio(AudioType.Intro); + void QuestionRemove(object _, RoutedEventArgs e) => RemoveElement(questions, questionList); + } +} \ No newline at end of file diff --git a/TriviaMurderPartyModder/MainWindow.WorstDrawing.cs b/TriviaMurderPartyModder/MainWindow.WorstDrawing.cs new file mode 100644 index 0000000..1b8133c --- /dev/null +++ b/TriviaMurderPartyModder/MainWindow.WorstDrawing.cs @@ -0,0 +1,22 @@ +using System.Windows; +using System.Windows.Controls; + +using TriviaMurderPartyModder.Data; +using TriviaMurderPartyModder.Properties; + +namespace TriviaMurderPartyModder { + public partial class MainWindow { + void WorstDrawings_CellEditEnding(object _, DataGridCellEditEndingEventArgs e) => worstDrawingList.Unsaved = true; + void WorstDrawingImport(object _, RoutedEventArgs e) => worstDrawingList.Import(true); + void WorstDrawingImportLastSave(object _, RoutedEventArgs e) => + worstDrawingList.ImportFrom(Settings.Default.lastWorstDrawing); + void WorstDrawingMerge(object _, RoutedEventArgs e) => worstDrawingList.Import(false); + void WorstDrawingSave(object _, RoutedEventArgs e) => worstDrawingList.Save(); + void WorstDrawingSaveAs(object _, RoutedEventArgs e) => worstDrawingList.SaveAs(); + void WorstDrawingReleaseCheck(object _, RoutedEventArgs e) => ReleaseCheck(worstDrawingList); + void WorstDrawingAudio(object _, RoutedEventArgs e) => + ((WorstDrawing)worstDrawings.SelectedItem).ImportAudio(worstDrawingList.DataFolderPath, + LoadAudio(questions, questionList)); + void WorstDrawingRemove(object _, RoutedEventArgs e) => RemoveElement(worstDrawings, worstDrawingList); + } +} \ No newline at end of file diff --git a/TriviaMurderPartyModder/MainWindow.WorstResponse.cs b/TriviaMurderPartyModder/MainWindow.WorstResponse.cs new file mode 100644 index 0000000..d0314b9 --- /dev/null +++ b/TriviaMurderPartyModder/MainWindow.WorstResponse.cs @@ -0,0 +1,22 @@ +using System.Windows; +using System.Windows.Controls; + +using TriviaMurderPartyModder.Data; +using TriviaMurderPartyModder.Properties; + +namespace TriviaMurderPartyModder { + public partial class MainWindow { + void WorstResponses_CellEditEnding(object _, DataGridCellEditEndingEventArgs e) => worstResponseList.Unsaved = true; + void WorstResponseImport(object _, RoutedEventArgs e) => worstResponseList.Import(true); + void WorstResponseImportLastSave(object _, RoutedEventArgs e) => + worstResponseList.ImportFrom(Settings.Default.lastWorstResponse); + void WorstResponseMerge(object _, RoutedEventArgs e) => worstResponseList.Import(false); + void WorstResponseSave(object _, RoutedEventArgs e) => worstResponseList.Save(); + void WorstResponseSaveAs(object _, RoutedEventArgs e) => worstResponseList.SaveAs(); + void WorstResponseReleaseCheck(object _, RoutedEventArgs e) => ReleaseCheck(worstResponseList); + void WorstResponseAudio(object _, RoutedEventArgs e) => + ((WorstResponse)worstResponses.SelectedItem).ImportAudio(worstResponseList.DataFolderPath, + LoadAudio(questions, questionList)); + void WorstResponseRemove(object _, RoutedEventArgs e) => RemoveElement(worstResponses, worstResponseList); + } +} \ No newline at end of file diff --git a/TriviaMurderPartyModder/MainWindow.xaml b/TriviaMurderPartyModder/MainWindow.xaml index 64f48e2..557da06 100644 --- a/TriviaMurderPartyModder/MainWindow.xaml +++ b/TriviaMurderPartyModder/MainWindow.xaml @@ -5,7 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TriviaMurderPartyModder" mc:Ignorable="d" - Title="Trivia Murder Party modder" Height="450" Width="800" Closing="Window_Closing"> + Title="Trivia Murder Party modder" Height="500" Width="800" Closing="Window_Closing"> @@ -14,7 +14,7 @@ - + @@ -28,16 +28,18 @@ ToolTip="Make answers in different positions equally likely."/> - + - +