Example #1
0
 @Override
 protected void handlePrivmsgMessage(
     Connection connection,
     String fromNickname,
     String text,
     List<String> textAsList,
     Message message,
     HandlerContext handlerContext)
     throws Exception {
   if (textAsList.size() < 2) {
     connection.send(
         Command.PRIVMSG,
         fromNickname,
         "Syntax: \"!pix <search terms>\" or \"!pix random\" to use a random word.");
     return;
   }
   if (isGameOngoing()) {
     connection.send(
         Command.PRIVMSG,
         fromNickname,
         "A game is already ongoing (started by " + mGameCreatedBy + ").");
     return;
   }
   String searchTerms = text.trim().substring(getCommand().length() + 1).trim();
   newGame(connection, fromNickname, searchTerms, handlerContext);
 }
Example #2
0
 @Override
 protected void handleChannelMessage(
     Connection connection,
     String channel,
     String fromNickname,
     String text,
     List<String> textAsList,
     Message message,
     HandlerContext handlerContext)
     throws Exception {
   if (!isGameOngoing()) {
     connection.send(
         Command.PRIVMSG,
         channel,
         fromNickname
             + ": No game is currently ongoing.  Privmsg me \"!pix <search terms>\" or \"!pix random\" to start one.");
     return;
   }
   if (text.trim().equals(getCommand())) {
     // Give current status
     connection.send(
         Command.PRIVMSG,
         channel,
         "Ongoing game started by "
             + mGameCreatedBy
             + " ("
             + mGuessCount
             + " guesses).  Guess the search terms with \"!pix <your guess>\".");
     // Show the current link
     connection.send(
         Command.PRIVMSG,
         channel,
         hideUrl(mSearchResults.get(mGuessCount % RESULT_SIZE).getLink()));
   } else {
     // Guess the word
     String guess = text.trim().substring(getCommand().length() + 1).trim();
     guess(connection, handlerContext, channel, fromNickname, guess);
   }
 }
Example #3
0
  private void newGame(
      Connection connection, String fromNickname, String searchTerms, HandlerContext handlerContext)
      throws IOException {
    boolean isRandom = RANDOM.equalsIgnoreCase(searchTerms);
    if (isRandom) {
      searchTerms = getRandomWord(handlerContext);
    } else if (!StringUtils.isAlphanumericSpace(searchTerms)) {
      connection.send(
          Command.PRIVMSG,
          fromNickname,
          "No punctuation allowed in the search terms.  Try another one.");
      return;
    }
    try {
      mSearchResultCount = queryGoogle(handlerContext, connection, searchTerms);
    } catch (IOException e) {
      Log.w(TAG, "newGame Could not query Google", e);
      connection.send(Command.PRIVMSG, fromNickname, "Oops something went wrong...");
      return;
    }
    if (mSearchResultCount == 0) {
      connection.send(
          Command.PRIVMSG, fromNickname, "This search has no results!  Try another one.");
      resetGame();
      return;
    }
    mSearchTerms = searchTerms;
    mGameCreatedBy = fromNickname;

    if (isRandom) {
      connection.send(Command.PRIVMSG, fromNickname, "New game started with a random word search.");
    } else {
      connection.send(
          Command.PRIVMSG,
          fromNickname,
          "New game started with the search \"" + searchTerms + "\".");
    }
  }
Example #4
0
  private void guess(
      Connection connection,
      HandlerContext handlerContext,
      String channel,
      String fromNickname,
      String guess)
      throws IOException {
    mGuessCount++;
    if (!StringUtils.stripAccents(mSearchTerms.toLowerCase(Locale.FRANCE))
        .equals(StringUtils.stripAccents(guess.toLowerCase(Locale.FRANCE)))) {
      // Lost
      connection.send(Command.PRIVMSG, channel, fromNickname + ": WRONG.");

      if (mGuessCount >= mSearchResultCount) {
        connection.send(
            Command.PRIVMSG,
            channel,
            "Well there are no more results.  You lose, after "
                + mGuessCount
                + " guesses!  The secret search was \""
                + mSearchTerms
                + "\"...  FAIL.");
        resetGame();
        return;
      }

      switch (mGuessCount) {
        case GUESSES_FIRST_HINT:
          int nbWords = mSearchTerms.split("\\s+").length;
          connection.send(
              Command.PRIVMSG,
              channel,
              "Ok since you guys suck, here's an hint: the search has "
                  + nbWords
                  + " word"
                  + (nbWords == 1 ? "." : "s."));
          break;

        case GUESSES_SECOND_HINT:
          connection.send(
              Command.PRIVMSG,
              channel,
              "Ok since you guys suck, here's another hint: the search looks like this: \""
                  + getSecondHint()
                  + "\".");
          break;

        case GUESSES_THIRD_HINT:
          connection.send(
              Command.PRIVMSG,
              channel,
              "Ok I'll give you one last hint: the search looks like this: \""
                  + getThirdHint()
                  + "\".");
          break;

        case GUESSES_MAX:
          connection.send(
              Command.PRIVMSG,
              channel,
              "Ok you guys suck too much.  You lose, after "
                  + mGuessCount
                  + " guesses!  The secret search was \""
                  + mSearchTerms
                  + "\"...  FAIL.");
          resetGame();
          return;
      }

      if (mGuessCount % RESULT_SIZE == 0) {
        // Fetch a new results page
        queryGoogle(handlerContext, connection, mSearchTerms);
      }

      connection.send(
          Command.PRIVMSG,
          channel,
          hideUrl(mSearchResults.get(mGuessCount % RESULT_SIZE).getLink()));
    } else {
      // Won
      connection.send(
          Command.PRIVMSG,
          channel,
          fromNickname
              + ": YES!  The secret search was \""
              + mSearchTerms
              + "\".  It was found in "
              + mGuessCount
              + " guesses.  Congrats!");
      resetGame();
    }
  }