Ejemplo n.º 1
0
 @Path("/")
 @GET
 @Produces(MediaType.APPLICATION_JSON)
 public QuestionResponseDto getQuestion() {
   return dozerMapper.map(
       questionManager.loadQuestions().getRandomQuestion(), QuestionResponseDto.class);
 }
Ejemplo n.º 2
0
  @Path("/{username}/create")
  @PUT
  public javax.ws.rs.core.Response addQuestionForUser(@PathParam("username") String userName) {
    List<Game> gamesAllReadyPlayed = gameUserDataManager.getGames(userName);
    List<Question> allQuestions = questionManager.loadQuestions().getQuestions();

    if (gamesAllReadyPlayed.size() == allQuestions.size()) {
      // NOTHING TO DO : the player allready answered all questions
      return javax.ws.rs.core.Response.ok().build();
    }

    Question randomQuestion = getRandomQuestion(gamesAllReadyPlayed, allQuestions);

    Game game = new Game();
    game.setId(randomQuestion.getId());
    game.setType(ResponseType.NEED_RESPONSE);

    try {
      gameUserDataManager.addOrUpdateGame(userName, game);
    } catch (StorageException e) {
      LOGGER.error("unable to store result in mongoDb: {}", e.getMessage());
    }

    questionsNotifier.notifyListener(userName, randomQuestion);

    return javax.ws.rs.core.Response.ok().build();
  }
Ejemplo n.º 3
0
 @Path("/{username}")
 @GET
 @Produces(MediaType.APPLICATION_JSON)
 public AllQuestionResponseDto getAllQuestions(@PathParam("username") String username) {
   List<Game> games =
       gameUserDataManager.getGamesByResultType(username, ResponseType.NEED_RESPONSE);
   AllQuestionResponseDto results = new AllQuestionResponseDto();
   for (Game game : games) {
     results.addQuestion(
         dozerMapper.map(
             questionManager.loadQuestions().getQuestionById(game.getId()),
             QuestionResponseDto.class));
   }
   return results;
 }
Ejemplo n.º 4
0
  @Path("/{username}/reply")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseResponseDto giveResponse(ResponseRequestDto responseDto) {
    Response res = dozerMapper.map(responseDto, Response.class);

    ResponseResponseDto response = new ResponseResponseDto();

    Set<ConstraintViolation<Response>> constraintViolations = validator.validate(res);
    if (constraintViolations.size() != 0) {
      LOGGER.error("Invalid response from user");
      response.setResponseType(ResponseType.INVALID);
      return response;
    }
    Question question = questionManager.loadQuestions().getQuestionById(res.getId());
    List<String> answers = question.getAnswers();
    List<String> responses = responseDto.getResponses();

    response.setId(res.getId());
    response.setAnswer(responses);
    response.setResponseType(ResponseType.FAIL);

    if (answers.size() == responses.size()) {
      // TODO : gerer le cas des majuscules/minuscules
      if (answers.containsAll(responses)) {
        response.setResponseType(ResponseType.SUCCESS);
      }
    }

    Game game = gameUserDataManager.getGameById(responseDto.getUserName(), res.getId());

    if (game == null) {
      game = new Game();
      game.setId(response.getId());
    }
    game.setGivenAnswers(response.getAnswer());
    game.setType(response.getResponseType());
    try {
      gameUserDataManager.addOrUpdateGame(responseDto.getUserName(), game);
    } catch (StorageException e) {
      LOGGER.error("unable to store result in mongoDb: {}", e.getMessage());
    }

    return response;
  }