/// <summary> /// Check is exist id /// </summary> /// <remarks> ///<table style="width:100%"> ///<tr> /// <td>ERROR#</td> /// <td>ERROR CODE</td> /// <td>ERROR DESCRIPTION</td> ///</tr> ///<tr> /// <td>9006</td> /// <td>DATA_BASE_ERROR</td> /// <td>Data base error.</td> ///</tr> ///<tr> /// <td>5001</td> /// <td>INVALID_QUESTION_ID</td> /// <td>QustionId length less than 0</td> ///</tr> ///<tr> /// <td>5002</td> /// <td>INVALID_USER_ID</td> /// <td>User Id is not valid</td> ///</tr> ///<tr> /// <td>9009</td> /// <td>INVALID_OPTION_ID</td> /// <td>Option Id is not valid</td> ///</tr> ///<tr> /// <td>9010</td> /// <td>INCORRECT_TEXT_LENGTH</td> /// <td>Question Id is not valid</td> ///</tr> ///</table> /// </remarks> /// <param name="answerRequest"></param> /// <returns></returns> public Errors SubmitAnswer(SubmitAnswerRequest answerRequest) { try { if (!_dbManager.isExistUserId(answerRequest.UserId)) { return(Errors.NOT_EXIST_USER_ID); } if (!_dbManager.isExistQuestionId(answerRequest.QuestionId)) { return(Errors.NOT_EXIST_QUESTION_ID); } if (!_dbManager.IsExistOptionId(answerRequest.OptionId)) { return(Errors.NOT_EXIST_OPTION_ID); } _dbManager.SubmitAnswer(answerRequest); return(Errors.OK); } catch (Exception ex) { _logger.Error("", ex); return(Errors.DATA_BASE_ERROR); } }
public override Task <Empty> SubmitAnswer(SubmitAnswerRequest request, ServerCallContext context) { try { var game = _manager.TryGetQuiz(request.Id); var questions = game.Quiz.Questions[game.CurrentQuestion - 1]; var answer = questions?.Answer.FirstOrDefault(x => x.Description == request.Answer); if (answer != null && answer.IsCorrect) { var player = game.Users.FirstOrDefault(x => x.Id == request.UserId); if (player != null) { player.Score += new Random().Next(5, 20); // XD } } return(Task.FromResult(new Empty())); } catch (GameNotFoundException) { context.Status = new Status(StatusCode.NotFound, "Quiz does not exist"); return(Task.FromResult(new Empty())); } }
public IHttpActionResult SubmitAnswer([FromBody] SubmitAnswerRequest answerRequest) { if (answerRequest.QuestionId <= 0) { return(Content(HttpStatusCode.BadRequest, new ErrorResponse(Errors.INVALID_QUESTION_ID))); } if (answerRequest.UserId <= 0) { return(Content(HttpStatusCode.BadRequest, new ErrorResponse(Errors.INVALID_USER_ID))); } if ((answerRequest.OptionId != null && answerRequest.OptionId <= 0)) { return(Content(HttpStatusCode.BadRequest, new ErrorResponse(Errors.INVALID_OPTION_ID))); } if ((answerRequest.Text == null) && (answerRequest.OptionId == null)) { return(Content(HttpStatusCode.BadRequest, new ErrorResponse(Errors.INCORRECT_TEXT_LENGTH))); } var result = _answersManager.SubmitAnswer(answerRequest); if (result == Errors.OK) { return(Ok()); } else { return(Content(HttpStatusCode.BadRequest, result)); } }
public void SubmitAnswer(SubmitAnswerRequest submitAnswerRequest) { using (var con = OpenConnection()) { var sqlQuery = @"INSERT INTO Answers (OptionId, QuestionId, Text, UserId) VALUES( @submitAnswerRequest.OptionId, @submitAnswerRequest.QuestionId, @submitAnswerRequest.Text, @submitAnswerRequest.UserId);"; IDbConnection db = con; db.Execute(sqlQuery, new { submitAnswerRequest.QuestionId, submitAnswerRequest.UserId, submitAnswerRequest.OptionId, submitAnswerRequest.Text }); } }
private void submitAnswer(int ans) { this.questions++; byte[] response = new byte[500]; SubmitAnswerRequest req = new SubmitAnswerRequest(); req.answerId = ans; SubmitAnswerResponse res = new SubmitAnswerResponse(); byte[] request = serializer.buildMessage <SubmitAnswerRequest>(ref req, 22); Client.client_socket.Send(request); res = serializer.DeAssembleMessage <SubmitAnswerResponse>(response); if (res.status == 1) { getQuestion(); } }
private async Task PlayGame(ISessionService sessionService, IPlayerService playerService, IQuestionService questionService, IAnswerService answerService, int playerAndCardCount) { var session = await sessionService.Create(); var players = new List <PlayerDto>(); session.CardAmount = playerAndCardCount; for (var i = 0; i < session.CardAmount; i++) { var player = await playerService.Create(i.ToString(), session.Id, i == 0); players.Add(player); } await sessionService.Start(session.Id, session.CardAmount); var questionIndex = 0; foreach (var player in players) { var questions = new List <string>(); for (var i = 0; i < session.CardAmount; i++) { questions.Add(questionIndex.ToString()); questionIndex++; } await questionService.SubmitQuestions(player.Id, questions); } await questionService.AssignQuestions(session.Id); var answerIndex = 0; foreach (var player in players) { var questions = await questionService.GetQuestionsToAnswer(player.Id); Assert.Equal(session.CardAmount, questions.Count); await answerService.SubmitAnswers(player.Id, questions.Select(x => { var request = new SubmitAnswerRequest { Answer = x.Content, QuestionId = x.Id }; answerIndex++; return(request); }).ToList()); } await answerService.AssignAnswersAndQuestions(session.Id); var allQuestionAnswerDtos = new List <QuestionAnswerDto>(); foreach (var player in players) { var answerQuestions = await answerService.GetQuestionAnswersForPlayer(player.Id); Assert.True(answerQuestions.DistinctBy(x => x.Question.AssignedAnswerId).Count() == answerQuestions.Count); Assert.True(answerQuestions.DistinctBy(x => x.Answer.Id).Count() == answerQuestions.Count); Assert.True(answerQuestions.All(x => answerQuestions.All(y => y.Answer.Id != x.Question.AssignedAnswerId))); allQuestionAnswerDtos.AddRange(answerQuestions); } var question = allQuestionAnswerDtos.First().Question; var readQuestions = new HashSet <Guid> { question.Id }; while (true) { var answer = allQuestionAnswerDtos.First(x => x.Answer.Id == question.AssignedAnswerId); Assert.NotEqual(answer.Answer.Content, question.Content); question = answer.Question; Assert.DoesNotContain(question.Id, readQuestions); readQuestions.Add(question.Id); if (readQuestions.Count == allQuestionAnswerDtos.Count) { break; } } Assert.Equal(session.CardAmount * session.CardAmount, allQuestionAnswerDtos.Select(x => x.Question.AssignedAnswerId).Distinct().Count()); }