package server.commands;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.quickserver.net.server.ClientHandler;
import server.constants.CommandList;
import server.protocol.ClientInfo;
import server.protocol.GameRoom;
import server.protocol.Message;
/**
* The Class Join. Handles the join command. Sets clients info and sends
* playerslist, dice, gamesheet and leaderboard to clients if game starts.
*/
public class Join implements Command {
private ClientInfo data;
private boolean wrongName;
public void execute(ClientHandler handler, GameRoom room,
List<String> splitted) throws IOException {
data = (ClientInfo) handler.getClientData();
wrongName = methods.chechForNameInUse(splitted.get(2), room);
if (wrongName) {
handler.sendClientObject(new Message(CommandList.NAMEINUSECMD));
return;
}
data.setName(splitted.get(2));
data.setJoinTime(new Date());
data.setRoom(room);
room.getGameSheet().addPlayer(handler);
room.addClient(handler);
methods.sendPlayers(handler, room);
if (room.getClients().size() == room.getSize()) {
room.setInProgress(true);
room.getGameSheet().instantiateGameSheet();
room.getDice().setCurOwner(data.getName());
methods.sendDice(handler, room);
methods.sendSheet(handler, room);
methods.sendLeaderboard(handler, room);
}
}
}