Package com.poker.client

Source Code of com.poker.client.ThreeCardPokerEntryPoint$Contact

package com.poker.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.poker.client.LoginInfo;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.HasRpcToken;
import com.google.gwt.user.client.rpc.RpcTokenException;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.XsrfToken;
import com.google.gwt.user.client.rpc.XsrfTokenService;
import com.google.gwt.user.client.rpc.XsrfTokenServiceAsync;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.gwtfb.sdk.FBCore;
import com.gwtfb.client.Callback;
import com.gwtfb.client.JSOModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.Command;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import com.google.gwt.user.client.ui.DockPanel;
import com.poker.server.*;
import com.poker.shared.*;

public class ThreeCardPokerEntryPoint implements EntryPoint {
  private LoginInfo loginInfo = null;
  final private PopupPanel popupPanel = new PopupPanel();
    final private FBCore fbCore = GWT.create(FBCore.class);
    final private String APPID = "759183437442227";
    private boolean status = true;
    private boolean xfbml = true;
    private boolean cookie = true;

    private String fbUid = "";
    private String fbNickName = "";
    final Presenter presenter = new Presenter();
    final Graphics graphics = new Graphics(presenter);
    LoginServiceAsync loginService = GWT.create(LoginService.class);
  
    DockPanel dockpanel = new DockPanel();
 
    public class logedinCallback extends Callback<JavaScriptObject>{
    @Override
    public void onSuccess(JavaScriptObject response){
        popupPanel.hide();
       loginStatusCallback loginStatusCallback = new loginStatusCallback();
       fbCore.getLoginStatus(loginStatusCallback);
    }
  }
      
    public class logoutCallback extends Callback<JavaScriptObject>{
    @Override
    public void onSuccess(JavaScriptObject response){
       fbCore.getLoginStatus(new loginStatusCallback ());
    }
    @Override
    public void onFailure (Throwable Caught){
       Window.alert("logout failed: " + Caught);
    }
  }
      
    public class getUserInfoCallback extends Callback<JavaScriptObject> {
        @Override
        public void onSuccess(JavaScriptObject response) {
          JSOModel jso = response.cast();
          fbNickName = jso.get("name");
          loginGAE();
        }

        @Override
        public void onFailure(Throwable Caught) {
             Window.alert("get User Info failded!\n" + Caught.toString());
        }
      }

    class loginStatusCallback extends Callback<JavaScriptObject> {
        @Override
        public void onSuccess(JavaScriptObject response) {
           JSOModel jsm = response.cast();
             String status = jsm.get("status");
             if (status.equals("connected")) {
                 JSOModel res = jsm.getObject("authResponse");
                 fbUid = res.get("userID");
                 String userImage = "http://graph.facebook.com/" + fbUid + "/picture";
                 graphics.setUserImage(userImage);
                 String url = "/" + fbUid + "?fields=name,username";
                 fbCore.api(url, new getUserInfoCallback());
                 fbCore.api("/me/friends", new friendListCallback());
                 popupPanel.hide();
                
             } else if (status.equals("not_authorized")) {
                 graphics.setUserImage("");
                 popupPanel.show();
                 fbUid = "";
                 fbCore.login(new logedinCallback());
                
             } else {
                 graphics.setUserImage("");
                 popupPanel.show();
                 fbUid = "";
                 fbCore.login(new logedinCallback());
               
               }

        }
       
        @Override
        public void onFailure(Throwable Caught) {
             Window.alert("LoginStatus failed!");
        }

    }
   
    private void fbLogin() {
         popupPanel.setWidth("300px");
         popupPanel.setGlassEnabled(true);
         popupPanel.setAnimationEnabled(true);
         final VerticalPanel vertPanel = new VerticalPanel();
         final Label userLabel = new Label();
         userLabel.setText("Please login with your Facebook ID");
         final Button loginButton = new Button("Login");
         loginButton.setPixelSize(100, 35);
         loginButton.setStylePrimaryName("button");
       
         loginButton.addClickHandler(new ClickHandler() {
             @Override
             public void onClick(ClickEvent event) {              
               fbCore.getLoginStatus(new loginStatusCallback());
               popupPanel.hide();
             }
           });
         vertPanel.add(userLabel);
         vertPanel.add(loginButton);
         popupPanel.setWidget(vertPanel);
         popupPanel.center();        
    }
  
    private void loginGAE(){
        if (!fbUid.equals("") || fbUid != null) {
                final String nameAndId = fbNickName + "@"+ fbUid;
                            
                   loginService.loginWithFBID(nameAndId, new AsyncCallback<LoginInfo>() {
                           public void onFailure(Throwable error) {
                           Window.alert(error.getMessage());
                           }                            
                           public void onSuccess(LoginInfo result) {                   
                               loginInfo = result;
                               if(loginInfo.isLoggedIn()) { 
                                    presenter.setId(loginInfo.getFbId());
                                  presenter.setNickName(loginInfo.getNickname());                   
                                  presenter.openGame(loginInfo.getToken());                                
                               }
                           }
                      });
                 }
       
    }
    // FB Login Over
 
    // FB Get Friend List
    MenuBar menu = new MenuBar();
    MenuBar friendList = new MenuBar(true);
    public static HashMap<String, String> friendsName = new HashMap<String, String>();
    private List<Contact> noAgainstList = new ArrayList<Contact>();
    private List<Contact> userTurnList = new ArrayList<Contact>();
    private List<Contact> opponentTurnList = new ArrayList<Contact>();
    private int counter = 0;
    String opponentId = "";

    public class Contact implements Comparable<Contact> {
        private String name;
        private String id;
        private int rank;
        private int rd;
        private Long gameId;
        public Contact(String name, String id, int rank, int rd, Long gameId) {
                this.name = name;
                this.id = id;
                this.rank = rank;
                this.rd=rd;
                this.gameId = gameId;
        }
        public String getName() {
                return name;
        }
        public String getId() {
                return id;
        }
        public int getRank() {
                return rank;
        }
        public int getrd(){
              return rd;
        }
        public Long getGameId() {
                return gameId;
        }
        public int compareTo(Contact other) {
                return name.compareTo(other.name);
        }
   }

   
    class friendListCallback extends Callback<JavaScriptObject> {
      @Override
        public void onSuccess(JavaScriptObject response){
        friendList = new MenuBar(true);
            JSOModel jso = response.cast();
            if (jso.hasKey("error")) {
                    Window.alert("Get Friend List Error");
                    return;
            }
            final JsArray array = jso.getArray("data");
            if (array != null) {
              for (int i = 0; i < array.length(); i++) {
                JSOModel j = array.get(i).cast();
                    final String name = j.get("name");
                    final String id = j.get("id");
                    friendsName.put(id, name);
                  
                    loginService.getIdInfo(fbUid, id, new AsyncCallback<OtherPlayerInfo>() {
                      @Override
                        public void onFailure(Throwable caught) {
                                Window.alert(caught.getMessage());
                        }
                      @Override
                        public void onSuccess(OtherPlayerInfo result) {
                        if (result.getExist() == 0) {
                                  noAgainstList.add(new Contact(name, id, result.getRank(), result.getrd(), result.getGameId()));
                        }
                        else{
                          // User Turn = 1, Opponent Turn = 2
                                switch (result.getTurn()) {
                               
                                case 1:
                                        userTurnList.add(new Contact(name, id, result.getRank(), result.getrd(),result.getGameId()));
                                        break;
                                case 2:
                                        opponentTurnList.add(new Contact(name, id, result.getRank(), result.getrd(),result.getGameId()));
                                        break;
                                }
                        }
                        counter++;
                          if (counter == array.length()) {
                                Collections.sort(noAgainstList);
                                Collections.sort(userTurnList);
                                Collections.sort(opponentTurnList);
                               
                                for (int i = 0; i < userTurnList.size(); i++) {
                                    final int tmp = i;
                                    final String cell = "<img src = 'http://graph.facebook.com/" + userTurnList.get(i).getId()
                                                    + "/picture' height = '50px' width = '50px'>" + userTurnList.get(i).getName() + " / "
                                                    + "["+(userTurnList.get(i).getRank()-2*userTurnList.get(i).getrd())+" , "+(userTurnList.get(i).getRank()+2*userTurnList.get(i).getrd())+"]" + " / Your Turn";
                                    SafeHtml friendCell = new SafeHtml() {
                                            @Override
                                            public String asString() {
                                                    return cell;
                                            }
                                    };
                                    friendList.addItem(friendCell, new Command() {
                                            @Override
                                            public void execute() {
                                                AsyncCallback<MatchInfo> callback = new AsyncCallback<MatchInfo>(){
                                                      @Override
                                                      public void onFailure(Throwable caught) {
                                                      }
                                                      @Override
                                                      public void onSuccess(MatchInfo result) {   //load the game from the server
                                                            presenter.loadGame(result.toString());
                                                     
                                                                     
                                               };
                                               opponentId = userTurnList.get(tmp).getId();
                                               loginService.LoadMatchByOpponentInDropdown(fbUid, opponentId, callback);
                                              
                                       }
                                   });
                               }
                               
                                for (int i = 0; i < opponentTurnList.size(); i++) {
                                    final String cell = "<img src = 'http://graph.facebook.com/" + opponentTurnList.get(i).getId()
                                                    + "/picture' height = '50px' width = '50px'>" + opponentTurnList.get(i).getName() + " / "
                                                    + "["+(opponentTurnList.get(i).getRank()-2*opponentTurnList.get(i).getrd())+" , "+(opponentTurnList.get(i).getRank()+2*opponentTurnList.get(i).getrd())+"]" + " / Opponent Turn";
                                    SafeHtml friendCell = new SafeHtml() {
                                            @Override
                                            public String asString() {
                                                    return cell;
                                            }
                                    };
                                    final int tmp = i;
                                    friendList.addItem(friendCell, new Command() {
                                            @Override
                                            public void execute() {
                                                    AsyncCallback<MatchInfo> callback = new AsyncCallback<MatchInfo>(){
                                                                  @Override
                                                                  public void onFailure(Throwable caught) {
                                                                  }
                                                                  @Override
                                                                  public void onSuccess(MatchInfo result) {   //load the game from the server
                                                                    presenter.loadGame(result.toString());
                                                                 
                                                                         
                                                   };
                                                   opponentId = opponentTurnList.get(tmp).getId();
                                                   loginService.LoadMatchByOpponentInDropdown(fbUid, opponentId, callback);
                                                  
                                           }
                                    });
                                }
                               
                                for (int i = 0; i < noAgainstList.size(); i++) {
                                    final int tmp = i;
                                    final String cell = "<img src = 'http://graph.facebook.com/" + noAgainstList.get(i).getId()
                                                    + "/picture' height = '50px' width = '50px'>" + noAgainstList.get(i).getName() + " / "
                                                    +"["+(noAgainstList.get(i).getRank()-2*noAgainstList.get(i).getrd())+" , "+(noAgainstList.get(i).getRank()+2*noAgainstList.get(i).getrd())+"]";
                                    SafeHtml friendCell = new SafeHtml() {
                                            @Override
                                            public String asString() {
                                                    return cell;
                                            }
                                    };
                                    friendList.addItem(friendCell, new Command() {
                                            @Override
                                            public void execute() {                                           
                                                 opponentId = noAgainstList.get(tmp).getId();
                                                 presenter.startMatch(opponentId);  
                                                 presenter.sendRequest();
                                    }});
                                }
                               
                                menu.addItem(new MenuItem("Friends", friendList));
                           
                          }
                      }
                     
                    });
              }
            }
           
      }
      @Override
        public void onFailure (Throwable Caught){
                Window.alert("Get friend list failed: " + Caught);
        }

    }
 
 
  public void onModuleLoad() {
    
         HorizontalPanel footer= new HorizontalPanel();
             presenter.initState(graphics);
             menu.setAutoOpen(true);
             menu.setAnimationEnabled(true);
             RootPanel.get().add(menu);
             footer.add(menu);     
             HTML likeHTML=new HTML();
             likeHTML.setHTML("<iframe src=\"https://www.facebook.com/plugins/like.php?href=http://10.socialgamescourse.appspot.com/\" scrolling=\"no\" frameborder=\"0\"style=\"border:none; width:260px; height:25px\"></iframe>");     
             VerticalPanel likePanel= new VerticalPanel();
             likePanel.add(likeHTML);
             footer.add(likePanel);
             Button logout = new Button("logout");
             logout.setPixelSize(65,25);
             logout.setStylePrimaryName("button");
             footer.add(logout);
             logout.addClickHandler(new ClickHandler(){
                 @Override
                 public void onClick(ClickEvent event){
                         fbCore.logout(new logoutCallback());
                 }
             });
             footer.setSpacing(5);
             dockpanel.add(footer, DockPanel.NORTH);
             dockpanel.add(graphics, DockPanel.CENTER);
             RootPanel.get().add(dockpanel);
             fbCore.init(APPID, status, cookie, xfbml);
             fbLogin();                        
    }
 

}
TOP

Related Classes of com.poker.client.ThreeCardPokerEntryPoint$Contact

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.