Пример #1
0
		public static void Submit(Answer a)
		{
			SqlConnection conn = Connection.Open();

            string answerText = a.Text;

            if (a.Text.Length > 500)
            {
                answerText = a.Text.Substring(0, 500);
            }

            string sql = SqlFormat.Format("INSERT INTO ANSWERS (qtext, priority, state, whosubmitted, qhour, qnumber) VALUES ('{0}', {1}, {2}, '{3}', {4}, {5})", answerText, a.Priority, a.State, a.Submitter, a.Hour, a.Number);
		
			lock (AnswerAccess.mLockCookie)
			{
				SqlCommand cmd = new SqlCommand(sql, conn);
			
				cmd.ExecuteNonQuery();

				sql = SqlFormat.Format("SELECT MAX(id) as HighestId FROM ANSWERS");

				cmd = new SqlCommand(sql, conn);

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    bool gotData = reader.Read();

                    a.ID = int.Parse(reader["HighestId"].ToString());
                }
			}
		}
Пример #2
0
		public static void Update(Answer a)
		{
			SqlConnection conn = Connection.Open();
//string sql = SqlFormat.Format("INSERT INTO ANSWERS (qtext, priority, whosubmitted, qhour, qnumber) VALUES ('{0}', {1}, '{2}', {3}, {4})", a.Text, a.Priority, a.Who, a.Hour, a.Number);
            string sql = SqlFormat.Format("UPDATE ANSWERS SET qtext='{0}', priority={1}, state={2}, whosubmitted='{3}', qpartial={4}, timecalledin='{5}', whocalledin='{6}' WHERE id={7}", 
				a.Text, a.Priority, a.State, a.Submitter, a.Partial ? 1 : 0, a.TimeCalledIn, a.WhoCalledIn, a.ID);

			SqlCommand cmd = new SqlCommand(sql, conn);
			
			cmd.ExecuteNonQuery();
		}
Пример #3
0
        public static void ShowToastForPartial(Answer partialAnswer)
        {
            string imageKey = GetImageForQuestion(partialAnswer.Hour, partialAnswer.Number, ToastFactory.PartialImages);

            ImageSource image = (ImageSource)ToastFactory.mImagesDictionary[imageKey];

            ToastNotification toast = new ToastNotification(image);
            toast.Indication = ToastNotification.TriviaIndication.PartialAnswer;
            toast.BodyText = string.Format("Partial confirmed for #{0}:{1}{2}", partialAnswer.Number, Environment.NewLine, partialAnswer.Text);

            toast.Show();
        }
Пример #4
0
        private static bool ShouldStopAnswerSubmission(Answer submittedAnswer, QuestionViewModel q, TriviaClient window)
        {
            string normalizedSubmittedAnswer = AnswerViewModel.GetNormalizedText(submittedAnswer.Text);

            AnswerViewModel duplicateAnswer = q.SubmittedAnswers.AsParallel().FirstOrDefault(otherAnswer => AnswerViewModel.GetNormalizedText(otherAnswer.Text).Equals(normalizedSubmittedAnswer, StringComparison.OrdinalIgnoreCase));

            if (duplicateAnswer == null)
            {
                return false;
            }

            string message;

            if (duplicateAnswer.HasBeenCalledIn)
            {
                if (duplicateAnswer.Text.Equals(q.Answer))
                {
                    message = string.Format("The answer '{0}' is already accepted as the correct answer.", duplicateAnswer.Text);
                }
                else if (duplicateAnswer.Partial)
                {
                    message = string.Format("The answer '{0}' has been identified as a partial.", duplicateAnswer.Text);
                }
                else
                {
                    message = string.Format("The answer '{0}' was already called in and was wrong.", duplicateAnswer.Text);
                }
            }
            else
            {
                message = string.Format("The answer '{0}' has already been submitted by {1} and is on the list to call in.", duplicateAnswer.Text, duplicateAnswer.Submitter);
            }

            MessageBox.Show(window, message, "Already Submitted", MessageBoxButton.OK);
            
            return true;
            
        }
Пример #5
0
        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);
                }));
        }
Пример #6
0
        private void mSubmitAnswerTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            QuestionViewModel question = (QuestionViewModel)this.DataContext;

            int priority = 3;
            bool submitNow = false;

            // Control-5 for a 5 priority answer, etc
            if ((Keyboard.Modifiers & ModifierKeys.Control) != 0)
            {
                switch (e.Key)
                {
                    case Key.D1:
                        priority = 1;
                        submitNow = true;
                        break;
                    case Key.D2:
                        priority = 2;
                        submitNow = true;
                        break;
                    case Key.D3:
                        priority = 3;
                        submitNow = true;
                        break;
                    case Key.D4:
                        priority = 4;
                        submitNow = true;
                        break;
                    case Key.D5:
                        priority = 5;
                        submitNow = true;
                        break;
                }
            }

            if (e.Key == Key.Return && (Keyboard.Modifiers & ModifierKeys.Shift) == 0
                || submitNow)
            {
                e.Handled = true;

                Answer answer = new Answer();
                answer.Text = this.mSubmitAnswerTextbox.Text.Trim();
                answer.Hour = question.Identifier.Hour;
                answer.Number = question.Identifier.Number;
                answer.Priority = priority;
                answer.Submitter = TriviaClient.PlayerName;
                answer.State = priority + 1;  // Middle column (3-star) is State 4

                this.mSubmitAnswerTextbox.Clear();

                TriviaCommands.SubmitAnswer.Execute(answer, null);
            }
        }
Пример #7
0
        public AnswerViewModel GetAnswerViewModel(Answer answer)
        {
            Contract.Requires(answer != null);

            QuestionViewModel question;

            bool found = this.TryGetQuestion(answer, out question);

            if (!(found))
            {
                throw new InvalidOperationException("Could not find Answer, but it surely must be there??");
            }

            return question.SubmittedAnswers.Single(vm => vm.ID == answer.ID);
        }
Пример #8
0
        /// <summary>
        /// A question may not always exist, for example, if someone submits an answer for an hour that's not loaded from the server yet (i.e. a past hour).  So try to get the hour if it exists.
        /// </summary>
        public bool TryGetQuestion(Answer answerToFindQuestionFor, out QuestionViewModel question)
        {
            Contract.Requires(answerToFindQuestionFor != null);

            return this.TryGetQuestion(answerToFindQuestionFor.Hour, answerToFindQuestionFor.Number,  out question);
        }
Пример #9
0
        void mOrchestrator_SubmitAnswerResponseReceived(Answer answer)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (this.IsActive)  // Only take action if the caller is idle
                    {
                        return;
                    }
                   
                    // Show a notification if an answer came in while the Caller tab is active
                    TabItem selectedTab = (TabItem)this.mTabs.SelectedItem;
                    
                    if (selectedTab == null || (string)selectedTab.Header != "Caller")
                    {
                        return;
                    }

                    if (this.mAnswersReceivedWhileInactiveCount == 0)
                    {
                        string originalTitle = this.Title;

                        EventHandler activatedHandler = null;

                        activatedHandler = (object sender, EventArgs e) =>
                            {
                                //FlashingWindow.StopFlashingWindow(this);
                                this.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.None; // nothing
                                this.mAnswersReceivedWhileInactiveCount = 0;
                                this.Activated -= activatedHandler;  // Detach
                                this.Title = originalTitle;
                            };

                        this.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Error; // red
                        this.TaskbarItemInfo.ProgressValue = 1D;

                        this.Activated += activatedHandler;  // Attach
                    }

                    this.mAnswersReceivedWhileInactiveCount++;
                    
                    this.Title = string.Format("{0} answer{1} to call in", this.mAnswersReceivedWhileInactiveCount, this.mAnswersReceivedWhileInactiveCount > 1 ? "s" : "");
                }));
        }
Пример #10
0
        public AnswerViewModel(Answer a)
        {
            Contract.Requires(a != null);

            this.hour= a.Hour;
            this.id = a.ID;
            this.number = a.Number;
            this.partial = a.Partial;
            this.priority = a.Priority;
            this.state = a.State;
            this.submitter = a.Submitter;
            this.text = a.Text;
            this.timeCalledIn = a.TimeCalledIn;
            this.whoCalledIn = a.WhoCalledIn;
        }
Пример #11
0
        public Answer CreateModel()
        {
            Answer a = new Answer(this.hour, this.number, this.text, this.priority, this.state, this.submitter, this.partial, this.timeCalledIn, this.whoCalledIn, this.id);

            return a;
        }
Пример #12
0
 public void Update(Answer updatedAnswer)
 {
     // Here are the properties that could change
     this.Text = updatedAnswer.Text;  // Might change due to Edit Partial
     this.Partial = updatedAnswer.Partial;
     this.TimeCalledIn = updatedAnswer.TimeCalledIn;
     this.WhoCalledIn = updatedAnswer.WhoCalledIn;
     this.State = updatedAnswer.State;
 }