コード例 #1
0
        /// <summary>
        /// Submits the answer
        /// </summary>
        /// <returns>
        /// Task
        /// </returns>
        private async Task SubmitAnswer()
        {
            SubmitAnswerRequest req = new SubmitAnswerRequest
            {
                answerId = CurrentQuestion.AnswerIndices[SelectedAnswer]
            };
            SubmitAnswerResponse res;

            byte[] buf;

            int[] answerIndices = RandomAnswerIndices();

            try
            {
                // Send the submit answer request
                await Client.Send(SubmitAnswerRequest.CODE, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req)));

                // Get from the server the response
                buf = await Client.Recv();

                // Deserialize the response
                res = JsonConvert.DeserializeObject <SubmitAnswerResponse>(Encoding.UTF8.GetString(buf));

                // If an error occurred
                if (res.status == 1)
                {
                    // Show error
                    await DialogHost.Show(new Dialogs.MessageDialog {
                        Message = "Unable to submit answer!"
                    });

                    throw new Exception();
                }
                else
                {
                    // Highlight the correct answer
                    GetRadioButtonForAnswer(res.correctAnswerId).Foreground = Brushes.LightGreen;

                    // If was wrong, highlight in red the wrong answer
                    if (res.correctAnswerId != CurrentQuestion.AnswerIndices[SelectedAnswer])
                    {
                        radioButtons[SelectedAnswer].Foreground = Brushes.Red;
                    }
                }
            } catch
            {
                Dispatcher.Invoke(() =>
                {
                    // Close this window and re-show the auth window to connect to the server
                    new AuthWindow().Show();
                    Close();
                });
            }
        }
コード例 #2
0
        private async void TimerTick(object sneder, EventArgs e)
        {
            // If the time ran out, send an empty answer
            if (RemainingTime == 0)
            {
                // Stop the timer
                timer.Stop();

                DisableRadioButtons();

                SubmitAnswerRequest req = new SubmitAnswerRequest
                {
                    answerId = 5
                };
                SubmitAnswerResponse res;
                byte[] buf;

                int[] answerIndices = RandomAnswerIndices();

                try
                {
                    // Send the submit answer request
                    await Client.Send(SubmitAnswerRequest.CODE, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req)));

                    // Get from the server the response
                    buf = await Client.Recv();

                    // Deserialize the response
                    res = JsonConvert.DeserializeObject <SubmitAnswerResponse>(Encoding.UTF8.GetString(buf));

                    // If an error occurred
                    if (res.status == 1)
                    {
                        // Show error
                        await DialogHost.Show(new Dialogs.MessageDialog {
                            Message = "Unable to submit answer!"
                        });

                        throw new Exception();
                    }
                    else
                    {
                        // Highlight the correct answer
                        GetRadioButtonForAnswer(res.correctAnswerId).Foreground = Brushes.LightGreen;

                        // If was wrong, highlight in red the wrong answer
                        if (SelectedAnswer != -1 && res.correctAnswerId != CurrentQuestion.AnswerIndices[SelectedAnswer])
                        {
                            radioButtons[SelectedAnswer].Foreground = Brushes.Red;
                        }
                    }
                } catch
                {
                    Dispatcher.Invoke(() =>
                    {
                        // Close this window and re-show the auth window to connect to the server
                        new AuthWindow().Show();
                        Close();
                    });

                    return;
                }

                // Enable the button
                submitNextBtn.IsEnabled = true;

                // Change the button to the next button
                submitNextBtn.Content = "Next";
            }
            else
            {
                RemainingTime -= 1;
            }

            Update();
        }