Example #1
0
  /** Show publish dialog. */
  public void publish(String msg) {
    JSONObject data = new JSONObject();
    data.put("method", new JSONString("stream.publish"));
    data.put("message", new JSONString(msg));

    JSONObject attachment = new JSONObject();
    attachment.put("name", new JSONString("Mancala"));
    attachment.put("caption", new JSONString("The Board Game"));
    attachment.put("description", new JSONString(msg));
    attachment.put("href", new JSONString("http://10.mymancala.appspot.com"));
    data.put("attachment", attachment);

    JSONObject actionLink = new JSONObject();
    actionLink.put("text", new JSONString("Mancala"));
    actionLink.put("href", new JSONString("http://10.mymancala.appspot.com"));

    JSONArray actionLinks = new JSONArray();
    actionLinks.set(0, actionLink);
    data.put("action_links", actionLinks);

    data.put("user_message_prompt", new JSONString("Share your thoughts about Mancala"));

    /*
     * Execute facebook method
     */
    fbCore.ui(data.getJavaScriptObject(), new Callback());
  }
Example #2
0
        @Override
        public void onSuccess(String[] result) {
          try {

            // graphics.addItemToMatchList("", "");

            final String[] fResult = result;

            // graphics.clearMatchDisplay();

            String method = "fql.query";
            String fql =
                "SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";

            JSONObject query = new JSONObject();
            query.put("method", new JSONString(method));
            query.put("query", new JSONString(fql));

            // Execute query
            fbCore.api(
                query.getJavaScriptObject(),
                new Callback<JavaScriptObject>() {
                  public void onSuccess(JavaScriptObject response) {
                    List<ContactInfo> contacts = Lists.newArrayList();
                    try {
                      JSONObject friends = new JSONObject(response);
                      for (int i = 0; i < friends.size(); i++) {
                        JSONObject jso = (JSONObject) new JSONObject(response).get(i + "");
                        String id =
                            jso.get("uid")
                                .toString()
                                .substring(1, jso.get("uid").toString().length() - 1);
                        String name =
                            jso.get("name")
                                .toString()
                                .substring(1, jso.get("name").toString().length() - 1);
                        String picUrl =
                            jso.get("pic_square")
                                .toString()
                                .substring(1, jso.get("pic_square").toString().length() - 1);

                        contacts.add(new ContactInfo(id, name, picUrl));
                      }
                      Collections.sort(contacts);

                      List<ContactInfo> contactsWithOngoingMatch = Lists.newArrayList();
                      if (fResult != null) {
                        for (String matchInfoString : fResult) {
                          if (matchInfoString == null) {
                            continue;
                          }
                          MatchInfo mI = MatchInfo.deserialize(matchInfoString);

                          String opponent;
                          if (mI.getNorthPlayerId().equals(userId))
                            opponent = mI.getSouthPlayerId();
                          else opponent = mI.getNorthPlayerId();
                          ContactInfo matchOngoingContact;
                          if (opponent.equals("AI")) {
                            matchOngoingContact =
                                new ContactInfo(
                                    "AI",
                                    "Computer",
                                    "https://dl.dropboxusercontent.com/u/6568643/MancalaFb/robot.gif",
                                    "",
                                    null);
                          } else {
                            ContactInfo hCI = new ContactInfo(opponent, "", "", "", null);
                            matchOngoingContact = contacts.get(contacts.indexOf(hCI)).copy();
                          }

                          String turnText;
                          if (mI.getUserIdOfWhoseTurnItIs().equals(userId))
                            turnText = messages.yourTurn();
                          else turnText = messages.theirTurn();
                          matchOngoingContact.setTurn(turnText);

                          matchOngoingContact.setMatchId(Long.valueOf(mI.getMatchId()));

                          State state = State.deserialize(mI.getState());
                          if (state.isGameOver()) {
                            matchOngoingContact.setTurn("");
                            matchOngoingContact.setMatchId(null);
                          }

                          matchOngoingContact.setMatchId(Long.valueOf(mI.getMatchId()));
                          contactsWithOngoingMatch.add(matchOngoingContact);

                          // PlayerColor usersSide = mI.getNorthPlayerId().equals(userId) ?
                          // PlayerColor.N : PlayerColor.S;
                          //
                          // State state = State.deserialize(mI.getState());
                          // if (state.isGameOver()) {
                          // if (state.winner() == null) {
                          // turnText = messages.tie();
                          // }
                          // else {
                          // if (state.winner().equals(usersSide))
                          // turnText = messages.youWon(state.score() + "", (48 - state.score()) +
                          // "");
                          // else
                          // turnText = messages.youLost((48 - state.score()) + "", state.score() +
                          // "");
                          // }
                          // }

                        }

                        // make them alphabetical
                        Collections.sort(contactsWithOngoingMatch);
                        // shift the contacts with a match to the top
                        int i = 0;
                        for (ContactInfo cI : contactsWithOngoingMatch) {
                          contacts.remove(cI);
                          contacts.add(i, cI);
                          i++;
                        }
                      }

                      graphics.setContactsInList(contacts);
                    } catch (Exception e) {
                      graphics.windowAlert(e.toString());
                    }
                  }
                });

          } catch (Exception e) {
            graphics.windowAlert(e.toString());
          }
        }