📄 tictactoegamecontrol.java
字号:
if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("expected " + TicTacToeGameControl.SESSION_CONFIG_REQUEST_SENT + " got " + command); } } } else if (COMMAND_START_GAME_REQUEST.equals(command)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("gameControl : received " + TicTacToeGameControl.COMMAND_START_GAME_REQUEST + " received"); } if ((getProtocolState() == TicTacToeGameControl.SESSION_CONFIG_ACCEPT_SENT) || (getProtocolState() == TicTacToeGameControl.SESSION_ENDED)) { setProtocolState(TicTacToeGameControl.SESSION_START_REQUEST_RECEIVED); //do any processing before we accept to start the voice convo boolean success = sendCommand(TicTacToeGameControl.COMMAND_START_GAME_ACCEPT); setProtocolState(TicTacToeGameControl.SESSION_START_ACCEPT_SENT); setProtocolState(TicTacToeGameControl.SESSION_PLAYING); /** remote player will make first move */ this.localPlayerMoves.clear(); this.remotePlayerMoves.clear(); this.allMoves.clear(); setPlayersTurn(TicTacToeGameControl.REMOTE_PLAYER); } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("expected " + TicTacToeGameControl.SESSION_CONFIG_ACCEPT_SENT + " got " + command); } } } else if (COMMAND_START_GAME_ACCEPT.equals(command)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("gameControl : received " + TicTacToeGameControl.COMMAND_START_GAME_ACCEPT + " received"); } if (getProtocolState() == TicTacToeGameControl.SESSION_START_REQUEST_SENT) { updateAckThread(command); this.localPlayerMoves.clear(); this.remotePlayerMoves.clear(); this.allMoves.clear(); // this is the last command received before starting voice // transmission/reception. signal thread to start sending TTTdata setProtocolState(TicTacToeGameControl.SESSION_START_ACCEPT_RECEIVED); setProtocolState(TicTacToeGameControl.SESSION_PLAYING); setPlayersTurn(TicTacToeGameControl.LOCAL_PLAYER); /* local player makes first move */ //let the ui pick up this event } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("expected " + TicTacToeGameControl.SESSION_START_REQUEST_SENT + " got " + command); } } } else if (COMMAND_END_GAME_REQUEST.equals(command)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("gameControl : received " + TicTacToeGameControl.COMMAND_END_GAME_REQUEST + " received"); } if (getProtocolState() == TicTacToeGameControl.SESSION_PLAYING) { setProtocolState(TicTacToeGameControl.SESSION_END_REQUEST_RECEIVED); sendCommand(TicTacToeGameControl.COMMAND_END_GAME_ACCEPT); setProtocolState(TicTacToeGameControl.SESSION_END_ACCEPT_SENT); setProtocolState(TicTacToeGameControl.SESSION_ENDED); localActionEndGame(); } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("expected " + TicTacToeGameControl.SESSION_PLAYING + " got " + command); } } } else if (COMMAND_END_GAME_ACCEPT.equals(command)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("gameControl : received " + TicTacToeGameControl.COMMAND_END_GAME_ACCEPT + " received"); } if (getProtocolState() == TicTacToeGameControl.SESSION_END_REQUEST_SENT) { updateAckThread(command); setProtocolState(TicTacToeGameControl.SESSION_END_ACCEPT_RECEIVED); //hang up call setProtocolState(TicTacToeGameControl.SESSION_ENDED); } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("expected " + TicTacToeGameControl.SESSION_END_REQUEST_SENT + " got " + command); } } } else if (COMMAND_DISCONNECT_REQUEST.equals(command)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("gameControl : receive " + TicTacToeGameControl.COMMAND_DISCONNECT_REQUEST + " received"); } // this is not garunteed... need to change if (getProtocolState() == getProtocolState()) {//this.SESSION_ENDED) { setProtocolState(TicTacToeGameControl.SESSION_DISCONNECT_REQUEST_RECEIVED); sendCommand(TicTacToeGameControl.COMMAND_DISCONNECT_ACCEPT); setProtocolState(TicTacToeGameControl.SESSION_DISCONNECT_ACCEPT_SENT); //disconnect from host - close window no more calls from this tab localActionEndSession(); setProtocolState(TicTacToeGameControl.SESSION_DISCONNECTED); } else { // the other side is going down... kill session // TODO: if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("expected SESSION_TTT_ENDED - got " + command); } } } else if (COMMAND_DISCONNECT_ACCEPT.equals(command)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("gameControl : received " + command + " received"); } if (getProtocolState() == getProtocolState()) {//this.SESSION_ENDED) { updateAckThread(command); setProtocolState(TicTacToeGameControl.SESSION_DISCONNECT_ACCEPT_RECEIVED); //disconnect from host - close window no more calls from this tab localActionEndSession(); setProtocolState(TicTacToeGameControl.SESSION_DISCONNECTED); } else { // the other side is going down... kill session // TODO: if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("expected SESSION_TTT_ENDED - got " + command); } } } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.warning("got strange command " + command + " is local:"+isLocallyInitiated()); } } LOG.setLevel(Level.INFO); } private boolean sendCommand(final String command) { return sendCommand(command, null, null); } private void sendCommand(final String command, final String ackCommand) { sendCommand(command, ackCommand, null); } /** * Sends a command on the vijxta dialog pipe. This does not change the * Protocol state. We have to remember to do this ourselves. */ private boolean sendCommand(final String command, final String ackCommand, final HashMap<String, MessageElement> elementMap) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("sendCommand : " + command); } final DialogMessage msg = (DialogMessage) this.templateMessage.clone(); final StringMessageElement commandElement = new StringMessageElement( TAG_SESSION_COMMAND, command, null); msg.addMessageElement(TAG_SESSION_COMMAND, commandElement); if (elementMap != null) { final Iterator<String> keys = elementMap.keySet().iterator(); for (String elementName : elementMap.keySet()) { final MessageElement element = elementMap.get(elementName); msg.addMessageElement(elementName, element); } } if (ackCommand != null) { if (this.messageAckThread==null){ this.messageAckThread = new MessageAckThread(ackCommand); this.messageAckThread.start(); this.messageAckThread.waitForStart(); } else { System.out.println("ack thread should be null!"); } } return this.tttDialog.dispatch(msg); } /** * message has been received... determine if its the correct response */ protected void updateAckThread(final String command) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Checking AckThread"); } if (this.messageAckThread != null) { this.messageAckThread.setMessageAcknowledged(command); if (this.messageAckThread.isThreadEnded()) { this.messageAckThread = null; } } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("MessageAckThread is null!!"); } } } /** * This method updates the current protocol state. Also signals the UI * of a protocol state change. */ private void setProtocolState(final int protocolState) { this.protocolState = protocolState; if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("ProtocolStateChanged to " + this.sessionStateTable.get(new Integer(protocolState))); } this.tttView.protocolStateChanged(protocolState); } /** * Returns the protocol state */ public int getProtocolState() { return this.protocolState; } /** * Returns the current time in milliseconds to wait for a message respose * before session shutdown. */ public long getMessageAckTimeout() { return (this.messageAckTimeout > TicTacToeGameControl.MINIMUM_MESSAGE_ACK_TIMEOUT) ? this.messageAckTimeout : DEFAULT_MESSAGE_ACK_TIMEOUT; } public void setMessageAckTimeout(final long timeInMilliseconds) { if (timeInMilliseconds > MINIMUM_MESSAGE_ACK_TIMEOUT) { this.messageAckTimeout = timeInMilliseconds; } } /** * Message ack thread has timed out and no message ack has been received. * End session. */ protected void localActionMessageGoneUnAcknowledged() { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info(" localActionMessageGoneUnAcknowledged "); } localActionEndSession(); /** Normally we'd just call sendcommand(END_SESSION) and wait for a * end END_SEESION_ACCEPT then go down gracefully. Since we aren't * receiving command messages we choose to just exit hard. */ sendCommand(TicTacToeGameControl.COMMAND_DISCONNECT_REQUEST); this.setProtocolState(TicTacToeGameControl.SESSION_CONNECTED); } private void localActionEndSession() { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("localActionEndSession"); } } public int getRemotePlayerTotalWins() { return this.remotePlayerTotalWins; } public int getLocalPlayerTotalWins() { return this.localPlayerTotalWins; } protected void setRemotePlayerTotalWins(final int remotePlayerTotalWins) { this.remotePlayerTotalWins = remotePlayerTotalWins; } protected void setLocalPlayerTotalWins(final int localPlayerTotalWins) { this.localPlayerTotalWins = localPlayerTotalWins; } public void viewActionNewGame() { if (getProtocolState() == TicTacToeGameControl.SESSION_ENDED) { sendCommand(TicTacToeGameControl.COMMAND_START_GAME_REQUEST, TicTacToeGameControl.COMMAND_START_GAME_ACCEPT); setProtocolState(TicTacToeGameControl.SESSION_START_REQUEST_SENT); } } private void setPlayersTurn(final int player) { this.playersTurn = player; this.tttView.setCurrentPlayersTurn(player); } public int getPlayersTurn() { return this.playersTurn; } public void destruct() { this.tttDialog.removeListener(this); sendCommand(TicTacToeGameControl.COMMAND_DISCONNECT_REQUEST); setProtocolState(TicTacToeGameControl.SESSION_DISCONNECTED); } /** * not pretty or speedy but ..eh for now */ private int getRemotePlayerBoardState() { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("start getremotePlayerBoardState"); } if (this.allMoves.containsAll(Arrays.asList(TicTacToeGameControl.draw))) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("getremotePlayerBoardState GAME_DRAW"); } return TicTacToeGameControl.GAME_DRAW; } for (String[] element : TicTacToeGameControl.wins) { if (this.remotePlayerMoves.containsAll(Arrays.asList(element))) { this.winningSet = element; if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("getRemotePlayerBoardState GAME_WON"); } return TicTacToeGameControl.GAME_WON; } } return GAME_IN_PLAY; } private int getLocalPlayerBoardState() { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("start getlocalplayerboradstate"); } if (this.allMoves.containsAll(Arrays.asList(TicTacToeGameControl.draw))) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("getLocalPlayerBoardState GAME_DRAW");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -