* packet.
* @param packet -
* the <code>Packet</code> to be processed.
*/
protected synchronized void handle(int connId, Packet packet) {
Player player = game.getPlayer(connId);
// Check player. Please note, the connection may be pending.
if ((null == player) && (null == getPendingConnection(connId))) {
System.err.print("Server does not recognize player at connection ");
System.err.println(connId);
return;
}
// System.out.println("s(" + cn + "): received command");
if (packet == null) {
System.out.println("server.connection.handle: got null packet");
return;
}
// act on it
switch (packet.getCommand()) {
case Packet.COMMAND_CLOSE_CONNECTION:
// We have a client going down!
IConnection c = getConnection(connId);
if (c != null) {
c.close();
}
break;
case Packet.COMMAND_CLIENT_NAME:
receivePlayerName(packet, connId);
break;
case Packet.COMMAND_PLAYER_UPDATE:
receivePlayerInfo(packet, connId);
validatePlayerInfo(connId);
send(createPlayerUpdatePacket(connId));
break;
case Packet.COMMAND_PLAYER_READY:
receivePlayerDone(packet, connId);
send(createPlayerDonePacket(connId));
checkReady();
break;
case Packet.COMMAND_REROLL_INITIATIVE:
receiveInitiativeRerollRequest(packet, connId);
send(createPlayerDonePacket(connId));
break;
case Packet.COMMAND_CHAT:
String chat = (String) packet.getObject(0);
if (chat.startsWith("/")) {
processCommand(connId, chat);
} else {
sendChat(player.getName(), chat);
}
// Easter eggs. Happy April Fool's Day!!
if (DUNE_CALL.equalsIgnoreCase(chat)) {
sendServerChat(DUNE_RESPONSE);
} else if (STAR_WARS_CALL.equalsIgnoreCase(chat)) {
sendServerChat(STAR_WARS_RESPONSE);
} else if ( INVADER_ZIM_CALL.equalsIgnoreCase(chat) ){
sendServerChat(INVADER_ZIM_RESPONSE);
}
break;
case Packet.COMMAND_ENTITY_MOVE:
receiveMovement(packet, connId);
break;
case Packet.COMMAND_ENTITY_DEPLOY:
receiveDeployment(packet, connId);
break;
case Packet.COMMAND_DEPLOY_MINEFIELDS:
receiveDeployMinefields(packet, connId);
break;
case Packet.COMMAND_ENTITY_ATTACK:
receiveAttack(packet, connId);
break;
case Packet.COMMAND_ENTITY_ADD:
receiveEntityAdd(packet, connId);
resetPlayersDone();
transmitAllPlayerDones();
break;
case Packet.COMMAND_ENTITY_UPDATE:
receiveEntityUpdate(packet, connId);
resetPlayersDone();
transmitAllPlayerDones();
break;
case Packet.COMMAND_ENTITY_MODECHANGE:
receiveEntityModeChange(packet, connId);
break;
case Packet.COMMAND_ENTITY_SYSTEMMODECHANGE:
receiveEntitySystemModeChange(packet, connId);
break;
case Packet.COMMAND_ENTITY_AMMOCHANGE:
receiveEntityAmmoChange(packet, connId);
break;
case Packet.COMMAND_ENTITY_REMOVE:
receiveEntityDelete(packet, connId);
resetPlayersDone();
transmitAllPlayerDones();
break;
case Packet.COMMAND_SENDING_GAME_SETTINGS:
if (receiveGameOptions(packet, connId)) {
resetPlayersDone();
transmitAllPlayerDones();
send(createGameSettingsPacket());
receiveGameOptionsAux(packet, connId);
}
break;
case Packet.COMMAND_SENDING_MAP_SETTINGS:
if ( game.getPhase().isBefore(Phase.PHASE_DEPLOYMENT)){
MapSettings newSettings = (MapSettings) packet.getObject(0);
if (!mapSettings.equalMapGenParameters(newSettings)) {
sendServerChat("Player " + player.getName() + " changed mapsettings");
}
mapSettings = newSettings;
newSettings = null;
mapSettings.replaceBoardWithRandom(MapSettings.BOARD_RANDOM);
resetPlayersDone();
transmitAllPlayerDones();
send(createMapSettingsPacket());
}
break;
case Packet.COMMAND_SENDING_PLANETARY_CONDITIONS:
// MapSettings newSettings = (MapSettings) packet.getObject(0);
if ( game.getPhase().isBefore(Phase.PHASE_DEPLOYMENT) ){
PlanetaryConditions conditions = (PlanetaryConditions) packet.getObject(0);
sendServerChat("Player " + player.getName() + " changed planetary conditions");
game.setPlanetaryConditions(conditions);
resetPlayersDone();
transmitAllPlayerDones();
send(createPlanetaryConditionsPacket());
}