Ejemplo n.º 1
0
 private void write(JsonGenerator jg, TwitterEntry entry) throws IOException {
   jg.writeStartObject();
   // can either do "jg.writeFieldName(...) + jg.writeNumber()", or this:
   jg.writeNumberField("id", entry.getId());
   jg.writeStringField("text", entry.getText());
   jg.writeNumberField("fromUserId", entry.getFromUserId());
   jg.writeNumberField("toUserId", entry.getToUserId());
   jg.writeStringField("langugeCode", entry.getLanguageCode());
   jg.writeEndObject();
   jg.close();
 }
Ejemplo n.º 2
0
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    String sid = req.getParameter("sid");
    String gameId = req.getParameter("game");

    HanabiUser user = s.getUserBySession(sid);
    HanabiGame game = s.getGame(gameId);

    JsonGenerator out = new JsonFactory().createJsonGenerator(resp.getOutputStream());

    out.writeStartObject();
    out.writeStringField("id", gameId);
    out.writeStringField("nextEvent", Integer.toString(game.events.getNextId()));
    out.writeNumberField("drawPile", game.drawPile.size());
    out.writeNumberField("hintsLeft", game.hintsLeft);
    out.writeNumberField("errorsMade", game.errorsMade);
    out.writeNumberField("turnNumber", game.turn);
    out.writeStringField("activePlayer", Integer.toString(game.activeSeat));

    out.writeFieldName("seats");
    out.writeStartArray();
    for (int i = 0; i < game.seats.size(); i++) {
      HanabiGame.Seat seat = game.seats.get(i);
      out.writeStartObject();
      out.writeStringField("seat", Integer.toString(i));
      out.writeStringField("playerName", seat.user.name);
      if (seat.user == user) {
        out.writeBooleanField("isYou", true);
      }
      out.writeFieldName("hand");
      out.writeStartArray();
      for (HanabiGame.Card c : seat.hand) {
        if (seat.user == user) {
          out.writeString("unknown");
        } else {
          out.writeString(c.toString());
        }
      }
      out.writeEndArray();
      out.writeEndObject();
    }
    out.writeEndArray(); // end seats

    out.writeFieldName("piles");
    out.writeStartObject();
    for (int i = 0; i < HanabiGame.SUIT_COUNT; i++) {
      HanabiGame.Card c = game.getPileTopCard(i);
      if (c != null) {
        out.writeStringField(c.getSuitName(), c.toString());
      }
    }
    out.writeEndObject(); // end piles

    out.writeFieldName("discards");
    out.writeStartArray();
    for (HanabiGame.Card c : game.discards) {
      out.writeString(c.toString());
    }
    out.writeEndArray();

    out.writeFieldName("hints");
    out.writeStartArray();
    for (HanabiGame.Hint h : game.hints) {
      out.writeStartObject();
      out.writeNumberField("turn", h.whenGiven);
      out.writeStringField("from", Integer.toString(h.from));
      out.writeStringField("to", Integer.toString(h.to));
      out.writeStringField("hintType", h.type.name());
      out.writeStringField("hint", h.getHintString());

      HanabiGame.Seat targetSeat = game.seats.get(h.to);
      out.writeFieldName("applies");
      out.writeStartArray();
      for (int slot = 0; slot < targetSeat.hand.size(); slot++) {
        if (targetSeat.whenReceived.get(slot).intValue() > h.whenGiven) {
          out.writeString("");
        } else if (h.affirms(targetSeat.hand.get(slot))) {
          out.writeString("Y");
        } else {
          out.writeString("N");
        }
      }
      out.writeEndArray();
      out.writeEndObject();
    }
    out.writeEndArray(); // end hints

    out.writeEndObject(); // end game
    out.close();
  }