Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. public void HandlerUserInput(ClientHandler newClient) {
  2.         newClient.isBusy = false;
  3.         while (true) {
  4.  
  5.             serverMessage = "";
  6.             command = null;
  7.  
  8.             try {
  9.  
  10.                 command = in.readUTF();
  11.                 System.out.println("Command from client: " + command);
  12.  
  13.                 if (command.matches("/refresh")) {
  14.                     if (gameRooms.isEmpty()) {
  15.                         out.writeUTF("No games found!");
  16.                     } else {
  17.  
  18.                         for (GameRoom game : gameRooms) {
  19.                             if (game.isFull == false) {
  20.                                 serverMessage = serverMessage + game.roomName + " Connected users : " + game.connectedClients.size() + "  Host: " + game.host + "\n";
  21.                             }
  22.                         }
  23.                         out.writeUTF(serverMessage);
  24.                     }
  25.                 } else if (command.contains("/create")) {
  26.  
  27.                     for (GameRoom game : gameRooms) {
  28.                         if (game.host.equals(newClient)) {
  29.                             out.writeUTF("You have already an active room!");
  30.                             newClient.isBusy = true;
  31.                         }
  32.                     }
  33.                     if (!newClient.isBusy) {
  34.                         try {
  35.                             for (GameRoom gm : gameRooms) {
  36.                                 if (gm.roomName.matches(command.substring(8))) {
  37.                                     nameBusy = true;
  38.                                     break;
  39.                                 }
  40.                             }
  41.                             if (!nameBusy) {
  42.                                 gameRooms.add(new GameRoom(command.substring(8), 4, newClient));
  43.                                 out.writeUTF("A new gameroom has been created. . . \n Searching for client to connect. . . ");
  44.                                 newClient.isBusy = true;
  45.                             } else {
  46.                                 out.writeUTF("Roomname already found in list.");
  47.                             }
  48.                         } catch (Exception e) {
  49.                             out.writeUTF("No roomname.");
  50.                         }
  51.                     }
  52.                 }
  53.                 else if (command.contains("/join")) {
  54.                     Boolean roomFound = true;
  55.                     if (!gameRooms.isEmpty()) {
  56.                         for (GameRoom gm : gameRooms) {
  57.                             try {
  58.                                 if (gm.roomName.matches(command.substring(6))) {
  59.                                     if (newClient.isBusy) {
  60.                                         out.writeUTF("You are already connected to a room, write /leave to leave");
  61.                                         break;
  62.                                     } else if (gm.connectedClients.size() >= gm.maxUsers) {
  63.                                         out.writeUTF("Room is full!");
  64.                                         break;
  65.                                     } else if (!gm.connectedClients.contains(newClient)) {
  66.                                         newClient.isBusy = true;
  67.                                         gm.connectedClients.add(newClient);
  68.                                         out.writeUTF("Connecting to room: " + gm.roomName);
  69.                                         break;
  70.                                     }
  71.                                 }
  72.                                 else {
  73.                                     roomFound = false;
  74.                                 }
  75.                             }catch (Exception e){
  76.  
  77.                                 break;
  78.                             }
  79.  
  80.                         }
  81.  
  82.                     }else{
  83.                         if (!roomFound){
  84.                             out.writeUTF("Cannot find your requested room");
  85.                         } else {
  86.                             out.writeUTF("There were no rooms found in the list.");
  87.                         }
  88.                     }
  89.  
  90.                 }
  91.                 else if (command.matches("/leave")) {
  92.                     try {
  93.                         if (!gameRooms.isEmpty() && newClient.isBusy) {
  94.                             for (GameRoom gm : gameRooms) {
  95.                                 if (gm.connectedClients.contains(newClient)) {
  96.                                     if (gm.host.equals(newClient)) {
  97.                                         for (ClientHandler g : gm.connectedClients) {
  98.                                             g.isBusy = false;
  99.                                         }
  100.                                         gameRooms.remove(gm);}
  101.                                     gm.connectedClients.remove(newClient);
  102.                                     newClient.isBusy = false;
  103.                                     out.writeUTF("You left: " + gm.roomName);
  104.                                     break;
  105.                                 }
  106.                             }
  107.                         }
  108.                         else{
  109.                             out.writeUTF("Roomlist is empty or no room to leave.");
  110.                         }
  111.                     } catch (Exception e){
  112.                         e.printStackTrace();
  113.                     }
  114.  
  115.  
  116.                 } else if (command.matches("/help")) {
  117.                     out.writeUTF("/create [roomname] \t /join [roomname] \t /leave \t /refresh");
  118.                 }
  119.                 else{
  120.                     out.writeUTF("Could not find any matching commands for: " +command);
  121.                 }
  122.  
  123.             } catch (Exception e) {
  124.                 e.printStackTrace();
  125.             }
  126.         }
  127.  
  128.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement