Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void HandlerUserInput(ClientHandler newClient) {
- newClient.isBusy = false;
- while (true) {
- serverMessage = "";
- command = null;
- try {
- command = in.readUTF();
- System.out.println("Command from client: " + command);
- if (command.matches("/refresh")) {
- if (gameRooms.isEmpty()) {
- out.writeUTF("No games found!");
- } else {
- for (GameRoom game : gameRooms) {
- if (game.isFull == false) {
- serverMessage = serverMessage + game.roomName + " Connected users : " + game.connectedClients.size() + " Host: " + game.host + "\n";
- }
- }
- out.writeUTF(serverMessage);
- }
- } else if (command.contains("/create")) {
- for (GameRoom game : gameRooms) {
- if (game.host.equals(newClient)) {
- out.writeUTF("You have already an active room!");
- newClient.isBusy = true;
- }
- }
- if (!newClient.isBusy) {
- try {
- for (GameRoom gm : gameRooms) {
- if (gm.roomName.matches(command.substring(8))) {
- nameBusy = true;
- break;
- }
- }
- if (!nameBusy) {
- gameRooms.add(new GameRoom(command.substring(8), 4, newClient));
- out.writeUTF("A new gameroom has been created. . . \n Searching for client to connect. . . ");
- newClient.isBusy = true;
- } else {
- out.writeUTF("Roomname already found in list.");
- }
- } catch (Exception e) {
- out.writeUTF("No roomname.");
- }
- }
- }
- else if (command.contains("/join")) {
- Boolean roomFound = true;
- if (!gameRooms.isEmpty()) {
- for (GameRoom gm : gameRooms) {
- try {
- if (gm.roomName.matches(command.substring(6))) {
- if (newClient.isBusy) {
- out.writeUTF("You are already connected to a room, write /leave to leave");
- break;
- } else if (gm.connectedClients.size() >= gm.maxUsers) {
- out.writeUTF("Room is full!");
- break;
- } else if (!gm.connectedClients.contains(newClient)) {
- newClient.isBusy = true;
- gm.connectedClients.add(newClient);
- out.writeUTF("Connecting to room: " + gm.roomName);
- break;
- }
- }
- else {
- roomFound = false;
- }
- }catch (Exception e){
- break;
- }
- }
- }else{
- if (!roomFound){
- out.writeUTF("Cannot find your requested room");
- } else {
- out.writeUTF("There were no rooms found in the list.");
- }
- }
- }
- else if (command.matches("/leave")) {
- try {
- if (!gameRooms.isEmpty() && newClient.isBusy) {
- for (GameRoom gm : gameRooms) {
- if (gm.connectedClients.contains(newClient)) {
- if (gm.host.equals(newClient)) {
- for (ClientHandler g : gm.connectedClients) {
- g.isBusy = false;
- }
- gameRooms.remove(gm);}
- gm.connectedClients.remove(newClient);
- newClient.isBusy = false;
- out.writeUTF("You left: " + gm.roomName);
- break;
- }
- }
- }
- else{
- out.writeUTF("Roomlist is empty or no room to leave.");
- }
- } catch (Exception e){
- e.printStackTrace();
- }
- } else if (command.matches("/help")) {
- out.writeUTF("/create [roomname] \t /join [roomname] \t /leave \t /refresh");
- }
- else{
- out.writeUTF("Could not find any matching commands for: " +command);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement