Package scotlandyard.client.widget

Source Code of scotlandyard.client.widget.NewWaitingRoom

package scotlandyard.client.widget;

import java.io.Serializable;
import java.util.List;

import scotlandyard.client.network.comet.MyCometSerializer;
import scotlandyard.client.network.rpc.PreGameService;
import scotlandyard.client.network.rpc.PreGameServiceAsync;
import scotlandyard.shared.messaging.Message;
import scotlandyard.shared.messaging.Message.MessageType;

import net.zschech.gwt.comet.client.CometClient;
import net.zschech.gwt.comet.client.CometListener;
import net.zschech.gwt.comet.client.CometSerializer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;

public class NewWaitingRoom extends Composite {
  static final String COMET_SERVER_URL = GWT.getModuleBaseURL() + "server";
  private int counter = 0;
  private PreGameServiceAsync preGame = GWT.create(PreGameService.class);
  private Label lblPlayers[];
  private CometClient client;
  private Label lblHeader;
  /**
   * This constructor needs the game room name to send it to server and query the current players list and then update the labels list.
   * It also calls startCometSession method.
   * @param gameName
   */
  public NewWaitingRoom(String gameName) {
    this();
    lblHeader.setText(gameName);
    preGame.getPlayerList(gameName, new AsyncCallback<String[]>() {

      @Override
      public void onFailure(Throwable caught) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onSuccess(String[] result) {
        /* It is guaranteed that the last name is the player name itself
         * it's not necessary to add it now, because its going to be added
         * later by cometClient onMessage Method.
         */
        for (int i = 0; i < (result.length-1); i++) {
            addPlayerToList(result[i]);
        }
      }

    });
    startCometSession();
  }
 
  /**
   * @wbp.parser.constructor
   */
  public NewWaitingRoom() {
    lblPlayers = new Label[6];
   
    VerticalPanel verticalPanel = new VerticalPanel();
    verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    initWidget(verticalPanel);
    verticalPanel.setSize("1024px", "768px");
   
    AbsolutePanel absolutePanel = new AbsolutePanel();
    verticalPanel.add(absolutePanel);
    absolutePanel.setSize("500px", "480px");
   
    lblHeader = new Label("New label");
    absolutePanel.add(lblHeader, 10, 10);
    lblHeader.setSize("480px", "30px");
   
    TextArea taGameDescription = new TextArea();
    taGameDescription.setReadOnly(true);
    absolutePanel.add(taGameDescription, 200, 46);
    taGameDescription.setSize("278px", "272px");
   
    Button btnPronto = new Button("New button");
    btnPronto.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        //ASK TO START GAME
        preGame.readyStartGame(new AsyncCallback <Void>(){

          @Override
          public void onFailure(Throwable caught) {
            // TODO Auto-generated method stub
           
          }

          @Override
          public void onSuccess(Void result) {
            System.out.println("\tSucesso em pedir para come�ar o jogo");
          }
         
        });
      }
    });
    btnPronto.setText("Pronto");
    absolutePanel.add(btnPronto, 346, 331);
    btnPronto.setSize("132px", "24px");
   
    Button btnSair = new Button("New button");
    btnSair.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        //NOT YET IMPLEMENTED
      }
    });
    btnSair.setText("Sair");
    absolutePanel.add(btnSair, 346, 361);
    btnSair.setSize("132px", "24px");
   
    VerticalPanel verticalPanel_1 = new VerticalPanel();
    absolutePanel.add(verticalPanel_1, 10, 46);
    verticalPanel_1.setSize("184px", "188px");
   
    lblPlayers[0] = new Label("Open");
    lblPlayers[0].setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    verticalPanel_1.add(lblPlayers[0]);
    lblPlayers[0].setSize("180px", "20px");
   
     lblPlayers[1] = new Label("Open");
    lblPlayers[1].setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    verticalPanel_1.add(lblPlayers[1]);
    lblPlayers[1].setSize("180px", "20px");
   
     lblPlayers[2] = new Label("Open");
    lblPlayers[2].setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    verticalPanel_1.add(lblPlayers[2]);
    lblPlayers[2].setSize("180px", "20px");
   
     lblPlayers[3] = new Label("Open");
    lblPlayers[3].setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    verticalPanel_1.add(lblPlayers[3]);
    lblPlayers[3].setSize("180px", "20px");
   
     lblPlayers[4] = new Label("Open");
    lblPlayers[4].setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    verticalPanel_1.add(lblPlayers[4]);
    lblPlayers[4].setSize("180px", "20px");
   
     lblPlayers[5] = new Label("Open");
    lblPlayers[5].setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    verticalPanel_1.add(lblPlayers[5]);
    lblPlayers[5].setSize("180px", "20px");
   
    TextBox txtChatInput = new TextBox();
    txtChatInput.addKeyPressHandler(new KeyPressHandler() {
      public void onKeyPress(KeyPressEvent event) {
        if (event.getCharCode() == 13){
          System.out.println("PRESSIONOU ENTER!");
          client.stop();
        }
      }
    });
    absolutePanel.add(txtChatInput, 10, 443);
    txtChatInput.setSize("316px", "17px");
   
    TextArea taChatArea = new TextArea();
    taChatArea.setReadOnly(true);
    absolutePanel.add(taChatArea, 10, 331);
    taChatArea.setSize("324px", "94px");
  }

  /**
   * StartCometSession instantiates and starts a CometClient and its listener to receive messages from MyCometServlet.
   * Comet Listener onMessage method is used to automatically update the players list (labels) whenever a new player joins or leaves the game.
   * MyCometSerializer is used to allows user-defined GWT RPC serializable classes that extends the base Message class.
   */
  private void startCometSession() {
    CometListener listener = new CometListener() {
      public void onConnected(int heartbeat) {
        System.out.println("Cliente1: conectado");
      }

      public void onDisconnected() {
        System.out.println("Cliente1: desconectado");
      }

      public void onHeartbeat() {
        System.out.println("Cliente1: heartbeat");
      }

      public void onRefresh() {
        System.out.println("Cliente1: refresh");
      }

      public void onError(Throwable exception, boolean connected) {
        // warn the user of the connection error
        //exception.printStackTrace();
        System.out.println("Erro");
      }

      public void onMessage(List<? extends Serializable> messages) {
        System.out.println("Recebi uma mensagem:");
         for (Serializable message : messages) {
          System.out.println("\tMensagem: " + message);
          Message msg = (Message) message;
          MessageType type = msg.getMessageType();

          if (type == MessageType.CONNECT) {
            System.out.println("Adicionando Jogador:" + msg.getSenderName());
            addPlayerToList(msg.getSenderName());
          }
          else if (type == MessageType.DISCONNECT) {
          }
          else if (type == MessageType.START){
            start();
          }
        }
         System.out.println("Fim recebimento de mensagem (onMessage)\n");
      }
    };

    CometSerializer serializer = GWT.create(MyCometSerializer.class);
    client = new CometClient(COMET_SERVER_URL, serializer,
        listener);
    client.start();
  }
 
  //Add and Remove methods that updates on demand the player list.
 
  private void addPlayerToList(String playerName) {
    for (int i = 0; i < lblPlayers.length; i++) {
      if (lblPlayers[i].getText().equals("Open")) {
        lblPlayers[i].setText(playerName);
        counter++;
        return;
      }
    }
  }

  private void removePlayerOfList(String playerName) {
    for (int i = 0; i < lblPlayers.length; i++) {
      if (lblPlayers[i].getText().equals(playerName)) {
        lblPlayers[i].setText("Open");
        counter--;
        return;
      }
    }
  }
 
  private void start(){
    //System.out.println("CLIENTE: client.stop()");
    //client.stop();
    client.stop();
    preGame.stopCometServer(new AsyncCallback<Void>(){

      @Override
      public void onFailure(Throwable caught) {
        // TODO Auto-generated method stub
       
      }

      @Override
      public void onSuccess(Void result) {
        createTabuleiro();
      }
     
    });
  }
 
  private void createTabuleiro(){
    String[] players = new String[counter];
    for (int i = 0; i < counter; i++)
      players[i] = lblPlayers[i].getText();
   
    RootLayoutPanel.get().clear();
    RootLayoutPanel.get().add(new Game(players));
  }
}
TOP

Related Classes of scotlandyard.client.widget.NewWaitingRoom

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.