return;
}
// check if they're connecting with the same name as a ghost player
for (Enumeration<Player> i = game.getPlayers(); i.hasMoreElements();) {
Player player = i.nextElement();
if (player.getName().equals(name)) {
if (player.isGhost()) {
returning = true;
player.setGhost(false);
// switch id
connId = player.getId();
conn.setId(connId);
}
}
}
if (!returning) {
// Check to avoid duplicate names...
name = correctDupeName(name);
send(connId, new Packet(Packet.COMMAND_SERVER_CORRECT_NAME, name));
}
// right, switch the connection into the "active" bin
connectionsPending.removeElement(conn);
connections.addElement(conn);
connectionIds.put(new Integer(conn.getId()), conn);
// add and validate the player info
if (!returning) {
int team = Player.TEAM_NONE;
for (Player p : game.getPlayersVector()) {
if (p.getTeam() > team) {
team = p.getTeam();
}
}
team++;
Player newPlayer = new Player(connId, name);
newPlayer.setTeam(Math.min(team, 5));
game.addPlayer(connId, newPlayer);
validatePlayerInfo(connId);
}
// if it is not the lounge phase, this player becomes an observer
Player player = getPlayer(connId);
if ((game.getPhase() != IGame.Phase.PHASE_LOUNGE) && (null != player) && (game.getEntitiesOwnedBy(player) < 1)) {
player.setObserver(true);
}
// send the player the motd
sendServerChat(connId, motd);
// send info that the player has connected
send(createPlayerConnectPacket(connId));
// tell them their local playerId
send(connId, new Packet(Packet.COMMAND_LOCAL_PN, new Integer(connId)));
// send current game info
sendCurrentInfo(connId);
try {
InetAddress[] addresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
for (InetAddress addresse : addresses) {
sendServerChat(connId, "Machine IP is " + addresse.getHostAddress());
}
} catch (UnknownHostException e) {
// oh well.
}
// Send the port we're listening on. Only useful for the player
// on the server machine to check.
sendServerChat(connId, "Listening on port " + serverSocket.getLocalPort());
// Get the player *again*, because they may have disconnected.
player = getPlayer(connId);
if (null != player) {
StringBuffer buff = new StringBuffer();
buff.append(player.getName()).append(" connected from ").append(getClient(connId).getInetAddress());
String who = buff.toString();
System.out.print("s: player #");
System.out.print(connId);
System.out.print(", ");
System.out.println(who);