Ejemplo n.º 1
0
  /**
   * Send a question to all clients of the quiz
   *
   * @param question The question to send
   * @return Number of sent questions
   * @throws RemoteException
   */
  private int sendQuestion(QuizQuestion question) {
    int i = 0;

    Console.println("Sending question to all clients in the game", Console.MSG_NORMAL);
    for (i = 0; i < this.clients.size(); i++) {
      QuizClientServices client = (QuizClientServices) this.clients.elementAt(i);

      try {
        question.setSender((QuizClientServices) this.clients.elementAt(0));
        client.display(question);
      } catch (RemoteException e) {
        System.err.println("RemoteException in Quiz.sendQuestion");
        System.err.println("Client=" + i);
        System.err.println(e.getMessage());
        e.printStackTrace();
      }
    }
    return i;
  }
Ejemplo n.º 2
0
  /**
   * Method checks all answers to a question
   *
   * @param question
   * @return Number of correct answers
   */
  private int checkAnswers(QuizQuestion question) {
    int correctAnswers = 0;
    Vector answers = this.servant.getAnswers();

    Console.println("Current question: " + question.getId(), Console.MSG_DEBUG);
    Console.println("Correct answer: #" + question.getCorrectAnswer(), Console.MSG_DEBUG);
    Console.println("There are " + answers.size() + " answers to be checked", Console.MSG_DEBUG);

    // Iterating over the available answers
    for (int i = 0; i < answers.size(); i++) {
      QuizAnswer answer = (QuizAnswer) answers.elementAt(i);

      String nick = null;
      QuizClientServices client = null;

      try {
        client = answer.getSender();
        if (this.servant.getConnectedClients().contains(client)) nick = client.getNickname();
      } catch (RemoteException e) {
        e.printStackTrace();
      }
      if (!this.servant.getConnectedClients().contains(client)) {
        continue;
      }

      // Just to be sure that the current answer is related to the current question (SimpleClient)
      if (answer.getQuestionId() != question.getId()) {
        Console.println(
            "Answer from client " + nick + " is not related to the current question",
            Console.MSG_NORMAL);
        continue;
      }

      Console.println(nick + " answered: #" + answer.getAnswer(), Console.MSG_DEBUG);

      // Check if the answer is correct
      if (answer.getAnswer() == question.getCorrectAnswer()) {

        try {
          // Updating client's score
          client.updateScore(question.getPoints());
          SystemMessage sysMsg = new SystemMessage();
          sysMsg.setOpCode(SystemMessage.RIGHT_ANSWER);
          sysMsg.setBody("Your answer was right. " + question.getPoints() + " points!");
          client.display(sysMsg);
        } catch (RemoteException e) {
          System.err.println(e.getMessage());
          e.printStackTrace();
        }

        Console.println(
            "Correct answer from " + nick + ". " + question.getPoints() + " points added.",
            Console.MSG_DEBUG);
        correctAnswers += 1;
      }
    }

    if (correctAnswers == 0) {
      Console.println("No correct answer", Console.MSG_DEBUG);
    }

    // Reset the servant's answer vector
    this.servant.clearAnswers();

    return correctAnswers;
  }