⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smsmanager.java

📁 Creat mobile game Creat mobile game Creat mobile game Creat mobile game
💻 JAVA
字号:
package net.frog_parrot.net;import java.io.*;import net.frog_parrot.checkers.MoveManager;/** * This class keeps track of transferring local and  * remote moves from one player to the other.. * * @author Carol Hamer */public class SMSManager {  //--------------------------------------------------------  //  static fields  /**   * The int to signal that the game is to begin.   */  public static final byte START_GAME_FLAG = -4;  /**   * The byte to signal that the game is to end.   */  public static final byte END_GAME_FLAG = -3;  /**   * The byte to signal the end of a turn.   */  public static final byte END_TURN_FLAG = -2;  /**   * The protocol string.   */  public static final String SMS_PROTOCOL = "sms://";  //--------------------------------------------------------  //  game instance fields  /**   * The class that directs the data from the communications   * module to game logic module and vice versa.   */  private MoveManager myManager;  /**   * The class that receives and reads the SMSMessages.   */  private SMSReceiver myReceiver;  //--------------------------------------------------------  //  data exchange instance fields  /**   * The data from the local player that is to    * be sent to the opponent.   */  private byte[] myMove;  /**   * The port number to send to.   */  private String myPortNum = "16474";  /**   * The phone number to send to.   */  private String myPhoneNum;  //--------------------------------------------------------  //  lifecycle  /**   * Start the processes to send and receive messages.   * @return whether the game was started by receiving   * an invitation from another player.   */  public boolean init(MoveManager manager)       throws IOException {    myManager = manager;    myReceiver = new SMSReceiver();    return(myReceiver.init(this));  }  /**   * Stop the receiver.     * This method is called alone from pauseApp or destroyApp   * since sending one last message is too time-consuming.   */  public void shutDown() {    if(myReceiver != null) {      myReceiver.shutDown();    }  }  /**   * Sets the current opponent if none is set, and verifies   * that subsequent messages came from the right opponent.   *   * @returns true if the message can be accepted.   */  public boolean checkPhoneNum(String phoneNumber) {    //System.out.println("checkPhoneNum-->myPhoneNum: " + myPhoneNum);    //System.out.println("checkPhoneNum-->phoneNumber: " + phoneNumber);    if(myPhoneNum == null) {      myPhoneNum = phoneNumber;      return true;    } else if (myPhoneNum.equals(phoneNumber)) {      return true;    } else {      return false;    }  }  /**   * This is called when the game is started by receiving    * an invitation from a remote player.  This triggers the    * game logic to let the local player make the first move.   */  public void receiveInvitation(String taunt) {    System.out.println("receiveInvitation-->taunt: " + taunt);    int state = myManager.getState();    if((state == MoveManager.NOT_STARTED)         || (state == MoveManager.FOUND_REMOTE_PLAYER)) {      //System.out.println("got a new invitation");      myManager.receiveInvitation(taunt, myPhoneNum.substring(6));    }  }  //--------------------------------------------------------  //  sending methods.  /**   * Send the game invitation SMS.   */  public void sendInvitation(String phoneNumber) {    myPhoneNum = SMS_PROTOCOL + phoneNumber;    byte[] invitation = new byte[1];    invitation[0] = START_GAME_FLAG;    invitation = addTaunt(invitation);    SMSSender sender = new SMSSender(myPhoneNum, myPortNum,         invitation, myManager);    sender.start();  }  /**   * Send the message to the other   * player that this player has quit.   */  public void sendGameOver() {    byte[] data = new byte[1];    data[0] = END_GAME_FLAG;    data = addTaunt(data);    SMSSender sender = new SMSSender(myPhoneNum, myPortNum,         data, myManager);    sender.start();  }  /**   * Records the local move in a byte array to prepare it    * to be sent to the remote player.   */  public void setLocalMove(byte[] move) {    if(myMove == null) {      myMove = new byte[5];      System.arraycopy(move, 0, myMove, 0, 4);      myMove[4] = END_TURN_FLAG;    } else {      // here we're dealing with the case of a       // series of jumps.  This isn't the typical case, so       // it shouldn't be too inefficient to just       // create a new, larger array each time       // we enlarge the move payload.      byte[] newMove = new byte[myMove.length + 4];      System.arraycopy(myMove, 0, newMove, 0, myMove.length);      System.arraycopy(move, 0, newMove, myMove.length - 1, 4);      newMove[newMove.length - 1] = END_TURN_FLAG;      myMove = newMove;    }  }  /**   * Sends the current local move to the remote player   * then clears the move data.   */  public void sendLocalMove() {    //System.out.println("sendLocalMove-->move: " + myMove);    if(myMove != null) {      myMove = addTaunt(myMove);      //System.out.println("sendLocalMove-->length: " + myMove.length);      SMSSender sender = new SMSSender(myPhoneNum, myPortNum,           myMove, myManager);      myMove = null;      sender.start();    }  }  //--------------------------------------------------------  //  receiving methods  /**   * Pass the remote player's move data to the game logic.   */  public void setMove(byte[] move) {    //System.out.println("SMSManager-->setMove");    myManager.receiveRemoteMove(move);  }  /**   * Pass the remote player's message to the display.   */  /*  public void receiveTaunt(String taunt) {    myManager.receiveTaunt(taunt);  }  */  /**   * Signal that the remote player's turn is over.   */  public void endTurn(String taunt) {    myManager.endRemoteTurn(taunt);  }  /**   * Signal that the remote player has ended the game.   */  public void receiveGameOver(String taunt) {    myManager.receiveGameOver(taunt);  }  //--------------------------------------------------------  //  utilities  /**   * Adds the taunt message data to the message being sent.   */  public byte[] addTaunt(byte[] data) {    String taunt = myManager.getTaunt();    if(taunt != null) {      try {	byte[] tauntdata = taunt.getBytes("utf-8");	byte[] retData = new byte[tauntdata.length + data.length];	System.arraycopy(data, 0, retData, 0, data.length);	System.arraycopy(tauntdata, 0, retData, data.length, 			 tauntdata.length);	return retData;      } catch (UnsupportedEncodingException e) {	// if the encoding is not supported, ignore message.      }    }    return data;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -