1 package net.sourceforge.simplegamenet.connectaline; 2 3 import java.awt.*; 4 import java.io.Serializable; 5 import net.sourceforge.simplegamenet.specs.gui.GamePanel; 6 import net.sourceforge.simplegamenet.specs.gui.UtilityPanelFactory; 7 import net.sourceforge.simplegamenet.specs.model.ClientEngine; 8 import net.sourceforge.simplegamenet.specs.model.GamePlayerClient; 9 import net.sourceforge.simplegamenet.specs.to.GameSettings; 10 import net.sourceforge.simplegamenet.specs.to.PlayerSettings; 11 import net.sourceforge.simplegamenet.specs.tools.ColoredPlayerSettings; 12 13 public class CALPlayerClient extends GamePlayerClient implements CALPacketCodes { 14 15 private int[] colorIndexArray; 16 private Color[] colors; 17 private CALPlayField cALPlayField; 18 private CALPanel cALPanel; 19 private Integer[] participantsOrder; 20 21 public CALPlayerClient(ClientEngine clientEngine) { 22 super(clientEngine); 23 } 24 25 public GamePanel createGamePanel(UtilityPanelFactory utilityPanelFactory) { 26 cALPanel = new CALPanel(this, clientEngine.getGameSettings(), 27 utilityPanelFactory.createChatClientPanel()); 28 CALSettings cALSettings = (CALSettings) clientEngine.getGameSettings(); 29 int cAlPlayStyle = cALSettings.getPlayStyle(); 30 int cALLengthOfAline = cALSettings.getLengthOfALine(); 31 cALPanel.setCALSettings(cAlPlayStyle, cALLengthOfAline); 32 cALPanel.setMessageCALMessagePanel("Waiting for game to start."); 33 return cALPanel; 34 } 35 36 public void playerRemoved(PlayerSettings playerSettings) { 37 if (clientEngine.isGamePlaying() 38 && playerSettings.getPlayingState() == PlayerSettings.PARTICIPATING) { 39 cALPanel.setMessageCALMessagePanel("The game is remise."); 40 } 41 } 42 43 public void receiveData(Serializable data) { 44 CALPacket cALPacket = (CALPacket) data; 45 CALSettings cALSettings = (CALSettings) clientEngine.getGameSettings(); 46 ColoredPlayerSettings coloredPlayerSettings; 47 switch (cALPacket.getCalPacketCode()) { 48 case INIT: 49 int cAlPlayStyle = cALSettings.getPlayStyle(); 50 int cALLengthOfAline = cALSettings.getLengthOfALine(); 51 participantsOrder = (Integer[]) cALPacket.cALData; 52 colorIndexArray = new int[participantsOrder.length]; 53 colors = new Color[participantsOrder.length]; 54 for (int i = 0; i < participantsOrder.length; i++) { 55 coloredPlayerSettings = (ColoredPlayerSettings) 56 clientEngine.getPlayerSettingsMap().getPlayerSettings( 57 participantsOrder[i]); 58 colorIndexArray[i] = coloredPlayerSettings.getColorsIndex(); 59 colors[i] = coloredPlayerSettings.getColor(); 60 } 61 cALPlayField = new CALPlayField(cALSettings.getNumberOfColumns(), 62 cALSettings.getNumberOfRows(), 63 cALSettings.getLengthOfALine(), 64 participantsOrder); 65 cALPanel.setCALPlayerPanel(); 66 cALPanel.updateMoveCALPlayFieldPanel(); 67 cALPanel.setCALSettings(cAlPlayStyle, cALLengthOfAline); 68 moveOnToNextParticipant(); 69 break; 70 case MAKE_MOVE_BY_PLAYER: 71 int[] coordinates = (int[]) cALPacket.cALData; 72 cALPlayField.makeMove(coordinates[0], coordinates[1], 73 cALPlayField.getParticipantAtTurnPlayerID()); 74 cALPanel.updateMoveCALPlayFieldPanel(); 75 cALPanel.nextCALPlayerPanel(); 76 moveOnToNextParticipant(); 77 break; 78 case MAKE_MOVE_BY_CLIENT: 79 break; 80 case WINNER: 81 Integer participantAtTurn = 82 cALPlayField.getParticipantAtTurnPlayerID(); 83 coordinates = (int[]) cALPacket.cALData; 84 cALPanel.setMessageCALMessagePanel(clientEngine.getPlayerSettingsMap() 85 .getPlayerSettings(participantAtTurn) 86 .getNickname() + 87 " is the winner!"); 88 cALPlayField.makeMove(coordinates[0], coordinates[1], 89 participantAtTurn); 90 cALPanel.updateMoveCALPlayFieldPanel(); 91 cALPanel.removePlayerLabelsPlayerPanel(); 92 break; 93 case REMISE: 94 participantAtTurn = cALPlayField.getParticipantAtTurnPlayerID(); 95 coordinates = (int[]) cALPacket.cALData; 96 cALPanel.setMessageCALMessagePanel("The game is remise."); 97 cALPlayField.makeMove(coordinates[0], coordinates[1], 98 participantAtTurn); 99 cALPanel.updateMoveCALPlayFieldPanel(); 100 cALPanel.removePlayerLabelsPlayerPanel(); 101 break; 102 case PLAY_FIELD: 103 cALPlayField = (CALPlayField) cALPacket.cALData; 104 participantsOrder = cALPlayField.getParticipantsOrder(); 105 colorIndexArray = new int[participantsOrder.length]; 106 colors = new Color[participantsOrder.length]; 107 for (int i = 0; i < participantsOrder.length; i++) { 108 coloredPlayerSettings = (ColoredPlayerSettings) 109 clientEngine.getPlayerSettingsMap().getPlayerSettings( 110 participantsOrder[i]); 111 colorIndexArray[i] = coloredPlayerSettings.getColorsIndex(); 112 colors[i] = coloredPlayerSettings.getColor(); 113 } 114 cALPanel.setCALPlayerPanel(); 115 cALPanel.updateMoveCALPlayFieldPanel(); 116 moveOnToNextParticipant(); 117 break; 118 default: 119 System.out.println("Unrecognized packet"); 120 } 121 } 122 123 public CALSettings getCALSettings() { 124 return (CALSettings) clientEngine.getGameSettings(); 125 } 126 127 public void gameSettingsUpdated(GameSettings outdatedGameSettings, 128 GameSettings updatedGameSettings) { 129 CALSettings cALSettings = (CALSettings) updatedGameSettings; 130 cALPanel.cALSettingsUpdated(cALSettings); 131 cALPlayField = new CALPlayField(cALSettings.getNumberOfColumns(), 132 cALSettings.getNumberOfRows(), 133 cALSettings.getLengthOfALine(), 134 participantsOrder); 135 cALPanel.setMessageCALMessagePanel("Game settings updated."); 136 } 137 138 private void moveOnToNextParticipant() { 139 Integer participantAtTurn = cALPlayField.getParticipantAtTurnPlayerID(); 140 if (clientEngine.getPlayerID().equals(participantAtTurn)) { 141 cALPanel.handleTurn(); 142 } else { 143 cALPanel.setOtherParticipantAtTurn(clientEngine.getPlayerSettingsMap() 144 .getPlayerSettings(participantAtTurn) 145 .getNickname()); 146 } 147 } 148 149 public Integer getParticipantAtTurnPlayerID() { 150 Integer participantAtTurn = cALPlayField.getParticipantAtTurnPlayerID(); 151 return participantAtTurn; 152 } 153 154 public int getCALPlayerAmount() { 155 return clientEngine.getPlayerSettingsMap().getParticipantsAmount(); 156 } 157 158 public String getCALPlayerNickname(Integer CALPlayerID) { 159 return clientEngine.getPlayerSettingsMap() 160 .getPlayerSettings(CALPlayerID).getNickname(); 161 } 162 163 public Integer getCALPlayerID() { 164 return clientEngine.getPlayerID(); 165 } 166 167 public void sendCALPacket(CALPacket cALPacket) { 168 clientEngine.sendData(cALPacket); 169 } 170 171 public Integer[] getParticipantsOrder() { 172 return participantsOrder; 173 } 174 175 public CALPlayField getCALPlayField() { 176 return cALPlayField; 177 } 178 179 public boolean isGameStarted() { 180 return clientEngine.isGamePlaying(); 181 } 182 183 public int[] getColorIndexArray() { 184 return colorIndexArray; 185 } 186 187 public Color[] getColors() { 188 return colors; 189 } 190 191 public void gameAborting() { 192 cALPanel.removePlayerLabelsPlayerPanel(); 193 cALPanel.setMessageCALMessagePanel("Game aborted."); 194 } 195 }