boolean exists = false;
Iterator<Player> it = playerList.iterator();
while (it.hasNext() && !exists) {
Friend friend = it.next();
if (friend.getName().equals(fromName)) {
exists = true;
}
}
if (!exists) {
Iterator<Player> ite = invitedPlayerList.iterator();
while (ite.hasNext() && !exists) {
Player invitedPlayer = ite.next();
if (invitedPlayer.getName().equals(fromName)) {
playerList.add(invitedPlayer);
FriendStatus status = FriendStatus.valueOf(networkInput.split(" ")[2]);
invitedPlayer.setStatus(status);
Chat.getSingleton().addChatter(GAMELOUNGE_NAME, invitedPlayer);
String message = GAME_CHANGED_TOKEN + "@@@";
message += gameFileName + "@@@";
message += gameName + "@@@";
message += gamePlayers + "@@@";
message += gameTeams + "@@@";
message += gameDescription;
NetworkAdapter.getSingleton().sendMessage(message, fromIp, port);
NetworkAdapter.getSingleton().sendMessage(JOIN_TOKEN + " " + getCurrentUser().getName() + " " + getCurrentUser().getStatus(), fromIp, port);
fireStateChanged();
exists = true;
}
}
}
} else if (networkInput.startsWith(GatheringLoungeModel.I_HAVE_INVITED_TOKEN)) {
String invitedPlayerName = networkInput.split(" ")[1];
String invitedPlayerAlias = networkInput.split(" ")[2];
InetAddress invitedPlayerIp = null;
try {
invitedPlayerIp = Inet4Address.getByName(networkInput.split(" ")[3]);
} catch (UnknownHostException ex) {
Logger.getLogger(GatheringLoungeModel.class.getName()).log(Level.SEVERE, null, ex);
}
int invitedPlayerPort = Integer.parseInt(networkInput.split(" ")[4]);
FriendStatus invitedPlayerStatus = FriendStatus.valueOf(networkInput.split(" ")[5]);
Player player = new Player(invitedPlayerName, invitedPlayerAlias, invitedPlayerIp);
player.setStatus(invitedPlayerStatus);
invitedPlayerList.add(player);
} else if (networkInput.startsWith(GAME_STARTED_TOKEN)) {
if (getCurrentUser().getStatus() == FriendStatus.IN_GAMELOUNGE) {
leaveGameLounge();
} else {
receivingFase = true;
Chat.getSingleton().addChatMessage(GAMELOUNGE_NAME, "GameInfo", "Teams set. All receive the game!");
getCurrentUser().setStatus(FriendStatus.IN_GAMELOUNGE);
for (Friend friend : getCurrentUser().getFriends()) {
NetworkAdapter.getSingleton().sendMessage(AbstractLoungeModel.STATUS_CHANGE_TOKEN + " " + getCurrentUser().getStatus(),
friend.getIp().getHostAddress(), friend.getName());
}
for (Player player : playerList) {
if (player.getStatus() != FriendStatus.IS_HOSTING) {
player.setStatus(FriendStatus.IN_GAMELOUNGE);
}
}
amountReady = 1;
fireStateChanged();
requestGame();
}
} else if (networkInput.startsWith(KICK_TOKEN)) {
leaveGameLounge();
} else if (networkInput.startsWith(STATUS_CHANGE_TOKEN)) {
String status = networkInput.split(" ")[1];
FriendStatus newState = FriendStatus.valueOf(status);
for (Friend friend : playerList) {
if (friend.getName().equals(fromName)) {
if (newState == FriendStatus.IN_GAMELOUNGE && !receivingFase) {
amountReady--;
} else if (newState == FriendStatus.READY || newState == FriendStatus.IS_HOSTING) {
amountReady++;
}
friend.setStatus(newState);
}
}
fireStateChanged();
if (amountReady == gamePlayers && receivingFase && isHosting && newState == FriendStatus.READY) {
for (Player player : playerList) {
NetworkAdapter.getSingleton().sendMessage(GAME_REALLY_STARTED_TOKEN, player.getIp().getHostAddress(), player.getName());
}
startStartTimer();
}
} else if (networkInput.startsWith(GAME_REALLY_STARTED_TOKEN)) {
startStartTimer();
} else if (networkInput.startsWith(LEAVE_TOKEN)) {
Iterator<Player> iterator = playerList.iterator();
while (iterator.hasNext()) {
Friend friend = iterator.next();
if (friend.getName().equals(fromName)) {
if (friend.getStatus() == FriendStatus.READY) {
amountReady--;
}
friend.setStatus(FriendStatus.IN_GATHERINGLOUNGE);
Chat.getSingleton().removeChatter(GAMELOUNGE_NAME, friend);
iterator.remove();
}
}
fireStateChanged();
} else if (networkInput.startsWith("LEFT: ")) {
Iterator<Player> iterator = playerList.iterator();
while (iterator.hasNext()) {
Friend friend = iterator.next();
if (friend.getName().equals(fromName)) {
if (friend.getStatus() == FriendStatus.READY) {
amountReady--;
}
friend.setStatus(FriendStatus.OFFLINE);
Chat.getSingleton().removeChatter(GAMELOUNGE_NAME, friend);
iterator.remove();
}
}
fireStateChanged();
} else if (networkInput.startsWith(HOST_TOKEN)) {
JOptionPane.showMessageDialog(null, "The host has left! You are the host now!",
"You're hosting!",
JOptionPane.INFORMATION_MESSAGE);
if (getCurrentUser().getStatus() == FriendStatus.READY) {
amountReady--; // Compensering voor de statuschange
}
getCurrentUser().setStatus(FriendStatus.IS_HOSTING);
for (Friend friend : getCurrentUser().getFriends()) {
NetworkAdapter.getSingleton().sendMessage(STATUS_CHANGE_TOKEN + " " + getCurrentUser().getStatus(),
friend.getIp().getHostAddress(), friend.getName());
}
for (Player player : playerList) {
NetworkAdapter.getSingleton().sendMessage(STATUS_CHANGE_TOKEN + " " + getCurrentUser().getStatus(),
player.getIp().getHostAddress(), player.getName());