private void TextBox_LostFocus(object sender, RoutedEventArgs e) { TextBox tb = (TextBox)sender; QuestionViewModel q = (QuestionViewModel)this.DataContext; int newPoints; if (int.TryParse(tb.Text.Trim(), out newPoints)) { // For speed, don't change the original question, copy it (changing the original question will trigger a bunch of events we don't care about here) QuestionChanges changes = new QuestionChanges(q.Identifier) { Points = newPoints }; TriviaCommands.UpdateQuestionPoints.Execute(changes, Window.GetWindow(this).Owner); } }
public QuestionChanges GetChangesMade() { QuestionChanges changes = new QuestionChanges(this.mQuestion.Identifier); Question editedQuestion = this.mQuestion.CreateModel(); // Easier to work with the Model than the ViewModel if (this.mSnapshotOfQuestionPriorToEdits.Answer != editedQuestion.Answer) { changes.Answer = editedQuestion.Answer; } if (this.mSnapshotOfQuestionPriorToEdits.AudioTriviaFilename != editedQuestion.AudioTriviaFilename) { changes.AudioTriviaFilename = editedQuestion.AudioTriviaFilename; } if (this.mSnapshotOfQuestionPriorToEdits.Correct != editedQuestion.Correct) { changes.Correct = editedQuestion.Correct; } if (this.mSnapshotOfQuestionPriorToEdits.Open != editedQuestion.Open) { changes.Open = editedQuestion.Open; } if (this.mSnapshotOfQuestionPriorToEdits.PhoneBankOperator != editedQuestion.PhoneBankOperator) { changes.PhoneBankOperator = editedQuestion.PhoneBankOperator; } if (this.mSnapshotOfQuestionPriorToEdits.Points != editedQuestion.Points) { changes.Points = editedQuestion.Points; } if (this.mSnapshotOfQuestionPriorToEdits.Text != editedQuestion.Text) { changes.Text = editedQuestion.Text; } return changes; }
public static void Start(ClientOrchestrator orchestrator, TriviaViewModel viewModel, CommandBindingCollection bindings, TriviaClient window) { Contract.Requires(orchestrator != null); bindings.Add(new CommandBinding(TriviaCommands.WrongAnswer, (object source, ExecutedRoutedEventArgs e) => { AnswerViewModel a = (AnswerViewModel)e.Parameter; a.WhoCalledIn = TriviaClient.PlayerName; orchestrator.SendUpdateAnswerRequest(a.CreateModel(), false); })); bindings.Add(new CommandBinding(TriviaCommands.CorrectAnswer, (object source, ExecutedRoutedEventArgs e) => { CorrectAnswerData cad = (CorrectAnswerData)e.Parameter; AnswerViewModel a = cad.Answer; a.WhoCalledIn = TriviaClient.PlayerName; orchestrator.SendUpdateAnswerRequest(a.CreateModel(), false); QuestionChanges questionChanges = new QuestionChanges(new QuestionId(a.Hour, a.Number)) { Open = false, Correct = true, Answer = a.Text, PhoneBankOperator = cad.PhoneBankOperator }; // Update the correct answer orchestrator.SendUpdateQuestionRequest(questionChanges); })); bindings.Add(new CommandBinding(TriviaCommands.PartialAnswer, (object source, ExecutedRoutedEventArgs e) => { AnswerViewModel a = (AnswerViewModel)e.Parameter; a.WhoCalledIn = TriviaClient.PlayerName; a.Partial = true; orchestrator.SendUpdateAnswerRequest(a.CreateModel(), true); // Flag as a partial })); bindings.Add(new CommandBinding(TriviaCommands.EditAnswer, (object source, ExecutedRoutedEventArgs e) => { // An edited answer was already modified by the command sender, so just update it to the server // Note: this is usually done by fixing the text of a partial answer AnswerViewModel a = (AnswerViewModel)e.Parameter; orchestrator.SendUpdateAnswerRequest(a.CreateModel(), false); // Flag not "partial changed". We don't know it to be the case, but that's the behavior we want })); bindings.Add(new CommandBinding(TriviaCommands.ShowSubmitAnswerDialog, (object source, ExecutedRoutedEventArgs e) => { QuestionViewModel question = (QuestionViewModel)e.Parameter; SubmitAnswerDialog submitAnswerDialog = new SubmitAnswerDialog(question); submitAnswerDialog.Owner = window; bool? submitted = submitAnswerDialog.ShowDialog(); if (submitted == true) { // 2014 - don't think this is true, but preserving the comment... Must execute the command against "this" current window so the commandbindings are hit TriviaCommands.SubmitAnswer.Execute(submitAnswerDialog.AnswerSubmitted, null); // Using "null" as the target means we avoid weird bugs where the event actually doesn't fire } })); bindings.Add(new CommandBinding(TriviaCommands.SubmitAnswer, (object source, ExecutedRoutedEventArgs e) => { Answer a = (Answer)e.Parameter; QuestionViewModel q; if (!(viewModel.TryGetQuestion(a, out q))) { throw new InvalidOperationException("Couldn't get question for answer submitted, that's really odd..."); } if (ContestConfiguration.PreventDuplicateAnswerSubmission && CommandHandler.ShouldStopAnswerSubmission(a, q, window)) { return; } orchestrator.SendSubmitAnswerRequest(a); // Inform the server // If you're submitting an answer, it's likely to assume you're researching the question if (!(q.IsBeingResearched)) { q.IsBeingResearched = true; TriviaCommands.ResearcherChanged.Execute(q, null); } })); bindings.Add(new CommandBinding(TriviaCommands.OpenQuestion, (object source, ExecutedRoutedEventArgs e) => { QuestionId questionId = (QuestionId)e.Parameter; orchestrator.SendOpenQuestionRequest(questionId.Hour, questionId.Number); })); bindings.Add(new CommandBinding(TriviaCommands.EditQuestion, (object source, ExecutedRoutedEventArgs e) => { QuestionViewModel question = (QuestionViewModel)e.Parameter; EditingQuestionData data = new EditingQuestionData() { HourNumber = question.Identifier.Hour, QuestionNumber = question.Identifier.Number, WhoEditing = TriviaClient.PlayerName }; // Tell everyone else that "I'm editing this question, so back off!" orchestrator.SendEditingQuestionMessage(data); EditQuestionDialog dialog = new EditQuestionDialog(orchestrator, question, false); dialog.Owner = window; bool? result = dialog.ShowDialog(); if (!(result.HasValue) || result.Value == false) { data.WhoEditing = null; // Signal that the edit was cancelled orchestrator.SendEditingQuestionMessage(data); // If the user made a change to the question (in-memory viewmodel), should refresh from the server (it's just the easiest thing to do) if (dialog.GetChangesMade().Changes != QuestionChanges.Change.None) { orchestrator.SendGetCompleteHourDataRequest(question.Identifier.Hour); } return; // Cancelled / Closed without saving } QuestionChanges changes = dialog.GetChangesMade(); if (changes.Changes == QuestionChanges.Change.None) { return; // No changes actually were made even though the user clicked the Save button } orchestrator.SendUpdateQuestionRequest(changes); })); bindings.Add(new CommandBinding(TriviaCommands.UpdateQuestionPoints, (object source, ExecutedRoutedEventArgs e) => { // Only Beerpigs use this QuestionChanges changes = (QuestionChanges)e.Parameter; orchestrator.SendUpdateQuestionRequest(changes); })); bindings.Add(new CommandBinding(TriviaCommands.PlayAudioTrivia, (object source, ExecutedRoutedEventArgs e) => { string audioTriviaFileName = (string)e.Parameter; CommandHandler.PlayAudioTrivia(audioTriviaFileName, window); })); bindings.Add(new CommandBinding(TriviaCommands.ShowAddNoteDialog, (object source, ExecutedRoutedEventArgs e) => { QuestionViewModel question = (QuestionViewModel)e.Parameter; Note note = new Note(); note.Submitter = TriviaClient.PlayerName; note.HourNumber = question.Identifier.Hour; note.QuestionNumber = question.Identifier.Number; AddNoteDialog dialog = new AddNoteDialog(note); dialog.Owner = window; bool? result = dialog.ShowDialog(); if (!(result.HasValue) || result.Value == false) { return; // Cancelled / Closed without saving } note.Text = note.Text.Trim(); // Trim it. // Save the note orchestrator.SendAddNoteMessage(note); // If you're adding a note, it's likely to assume you're researching the question if (!(question.IsBeingResearched)) { question.IsBeingResearched = true; TriviaCommands.ResearcherChanged.Execute(question, null); } })); bindings.Add(new CommandBinding(TriviaCommands.CorrectAnswerWithoutSubmission, (object source, ExecutedRoutedEventArgs e) => { QuestionViewModel question = (QuestionViewModel)e.Parameter; Answer answer = new Answer(); answer.Hour = question.Identifier.Hour; answer.Number = question.Identifier.Number; answer.WhoCalledIn = TriviaClient.PlayerName; AnswerViewModel answerViewModel = new AnswerViewModel(answer); // Show the "blank" dialog CorrectAnswerDialog dialog = new CorrectAnswerDialog(answerViewModel); dialog.Owner = window; bool? result = dialog.ShowDialog(); if (!(result.HasValue) || result.Value == false) { return; // Cancelled / Closed without saving } QuestionChanges changes = new QuestionChanges(question.Identifier) { Open = false, Correct = true, Answer = dialog.Answer.Text, PhoneBankOperator = dialog.PhoneBankOperator }; // Update the correct answer orchestrator.SendUpdateQuestionRequest(changes); })); bindings.Add(new CommandBinding(TriviaCommands.ResearcherChanged, (object source, ExecutedRoutedEventArgs e) => { QuestionViewModel questionThatChanged = (QuestionViewModel)e.Parameter; if (questionThatChanged.IsBeingResearched == false) { // User said they're not researching this question (and by extension, aren't researching *anything* anymore) orchestrator.SendResearcherChangedRequest( new ResearcherChange { HourNumber = 0, // Signaling that current user is researching nothing QuestionNumber = 0, // Signaling that current user is researching nothing Name = TriviaClient.PlayerName }); } else { orchestrator.SendResearcherChangedRequest( new ResearcherChange { HourNumber = questionThatChanged.Identifier.Hour, QuestionNumber = questionThatChanged.Identifier.Number, Name = TriviaClient.PlayerName }); } })); bindings.Add(new CommandBinding(TriviaCommands.ShowEditPartialsDialog, (object source, ExecutedRoutedEventArgs e) => { ListCollectionView partials = (ListCollectionView)e.Parameter; EditPartialsDialog dialog = new EditPartialsDialog(); dialog.DataContext = partials; dialog.Owner = window; dialog.ShowDialog(); })); bindings.Add(new CommandBinding(TriviaCommands.ShowCombinePartialsDialog, (object source, ExecutedRoutedEventArgs e) => { QuestionViewModel question = (QuestionViewModel)e.Parameter; string combinationAnswer = string.Join("\n", question.PartialAnswers.Cast<AnswerViewModel>().Select(avm => avm.Text)); SubmitAnswerDialog submitAnswerDialog = new SubmitAnswerDialog(question, combinationAnswer); submitAnswerDialog.Owner = window; bool? submitted = submitAnswerDialog.ShowDialog(); if (submitted == true) { // 2014, don't think this is true, ubt pres3erving the original comment..... Must execute the command against "this" current window so the commandbindings are hit TriviaCommands.SubmitAnswer.Execute(submitAnswerDialog.AnswerSubmitted, null); // Using "null" as the target means we avoid weird bugs where the event actually doesn't fire } })); bindings.Add(new CommandBinding(TriviaCommands.ShowAddLinkDialog, (object source, ExecutedRoutedEventArgs e) => { AddLinkDialog dialog = new AddLinkDialog(); dialog.Owner = window; bool? result = dialog.ShowDialog(); if (!(result.HasValue) || result.Value == false) { return; // Cancelled / Closed without saving } dialog.Link.Description = dialog.Link.Description.Trim(); dialog.Link.Url = dialog.Link.Url.Trim(); // Save the link orchestrator.SendAddLinkRequest(dialog.Link); })); bindings.Add(new CommandBinding(TriviaCommands.DeleteNote, (object source, ExecutedRoutedEventArgs e) => { orchestrator.SendDeleteNoteMessage((Note)e.Parameter); })); }
public static void Update(QuestionChanges q) { if (q == null) { throw new ArgumentNullException("Question is null"); } if (q.Hour == 0 || q.Number == 0) { throw new ArgumentOutOfRangeException("Hour and Number cannot be zero"); } string sql = string.Format("UPDATE QUESTIONS SET"); List<string> updates = new List<string>(); updates.Add("qnumber=" + q.Number); // make sure something gets set, even if the question is a whole bunch of null/unchanged fields if ((q.Changes & QuestionChanges.Change.Text) != QuestionChanges.Change.None) { updates.Add(SqlFormat.Format("qtext='{0}'", q.Text)); } if ((q.Changes & QuestionChanges.Change.Answer) != QuestionChanges.Change.None) { updates.Add(SqlFormat.Format("answer='{0}'", q.Answer)); } if ((q.Changes & QuestionChanges.Change.Points) != QuestionChanges.Change.None) { updates.Add(SqlFormat.Format("points={0}", q.Points)); } if ((q.Changes & QuestionChanges.Change.AudioTriviaFilename) != QuestionChanges.Change.None) { updates.Add(SqlFormat.Format("audiotriviafilename='{0}'", q.AudioTriviaFilename)); } if ((q.Changes & QuestionChanges.Change.Open) != QuestionChanges.Change.None) { updates.Add(SqlFormat.Format("qopen={0}", q.Open.Value ? 1 : 0)); } if ((q.Changes & QuestionChanges.Change.Correct) != QuestionChanges.Change.None) { updates.Add(SqlFormat.Format("correct={0}", q.Correct.Value ? 1 : 0)); } if ((q.Changes & QuestionChanges.Change.PhoneBankOperator) != QuestionChanges.Change.None) { updates.Add(SqlFormat.Format("phoneop='{0}'", q.PhoneBankOperator)); } string updateSets = string.Join(", ", updates); // Only set non-null fields (so if one person with the question open changes a field and another person with the same question open changes a different field, they don't do complete overwrites) sql = sql + " " + updateSets + string.Format(" WHERE qhour={0} AND qnumber={1}", q.Hour, q.Number); // These are the only updatable fields //old sql = SqlFormat.Format("UPDATE QUESTIONS SET qtext='{0}', answer='{1}', points={2}, audiotriviafilename='{3}', qopen={4}, correct={5}, phoneop='{6}' WHERE qhour={7} AND qnumber={8}", q.Text, q.Answer, q.Points, q.AudioTriviaFilename, q.Open ? '1' : '0', q.Correct ? '1' : '0', q.PhoneBankOperator, q.Hour, q.Number); SqlConnection conn = Connection.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); }