Skip to content

Commit

Permalink
Mark questions with intro audio
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Nov 22, 2022
1 parent 1200817 commit 9f4ddc6
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 229 deletions.
68 changes: 42 additions & 26 deletions TriviaMurderPartyModder/Data/DataJet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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");
Expand All @@ -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);
}
}
}
}
2 changes: 1 addition & 1 deletion TriviaMurderPartyModder/Data/FinalRounder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 7 additions & 2 deletions TriviaMurderPartyModder/Data/Question.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion TriviaMurderPartyModder/Data/WorstDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion TriviaMurderPartyModder/Data/WorstResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}}," +
Expand Down
159 changes: 159 additions & 0 deletions TriviaMurderPartyModder/MainWindow.FinalRound.cs
Original file line number Diff line number Diff line change
@@ -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<object> 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);
}
}
}
45 changes: 45 additions & 0 deletions TriviaMurderPartyModder/MainWindow.Question.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
22 changes: 22 additions & 0 deletions TriviaMurderPartyModder/MainWindow.WorstDrawing.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 9f4ddc6

Please sign in to comment.