diff --git a/ChatApp/src/client/ClientConsole.java b/ChatApp/src/client/ClientConsole.java index 61b75ee..8c94b81 100644 --- a/ChatApp/src/client/ClientConsole.java +++ b/ChatApp/src/client/ClientConsole.java @@ -4,7 +4,9 @@ import java.net.InetSocketAddress; import java.net.SocketException; import java.time.Instant; import java.util.List; +import java.util.Map; import java.util.Scanner; +import java.util.function.Consumer; import network.protocol.ANSIColor; import network.protocol.packets.ServerResponsePacket; @@ -65,50 +67,72 @@ public class ClientConsole implements ClientListener { this.inputThread.join(); } - private void visitMessage(String message) { - try { - if (message.startsWith("/")) { - if (message.startsWith("/createRoom ")) { - String roomName = message.substring(12).trim(); - this.client.SendCreateRoom(roomName); - } else if (message.startsWith("/create ")) { - String roomName = message.substring(8).trim(); - this.client.SendCreateRoom(roomName); - } else if (message.equals("/listRooms") || message.equals("/list")) { - this.client.RequestRoomList(); - } else if (message.startsWith("/joinRoom ")) { - String roomName = message.substring(10).trim(); - this.client.SendJoinRoom(roomName); - } else if (message.startsWith("/join ")) { - String roomName = message.substring(6).trim(); - this.client.SendJoinRoom(roomName); - } else if (message.equals("/leaveRoom") || message.equals("/leave")) { - this.client.SendLeaveRoom(); - } else if (message.equals("/room")) { - this.client.RequestActualRoom(); - } - else if (message.equals("/bye")) { - this.client.close(); - } else if (message.equals("/help")) { - System.out.println("Available commands:"); - System.out.println("\t/createRoom "); - System.out.println("\t/listRooms"); - System.out.println("\t/joinRoom "); - System.out.println("\t/leaveRoom"); - System.out.println("\t/room"); - System.out.println("\t/bye"); - System.out.println("\t/help"); - } else { - System.out.println(ANSIColor.formatString("&rUnknown command&n")); - } - } else { - this.client.SendChatMessage(message); - } - System.out.print("\033[1A\r\033[2K"); // weird sequence to clear the line (but it works !) - System.out.flush(); - } catch (Exception e) { - e.printStackTrace(); + private void printHelp(Map, Consumer> commands) { + System.out.println("Available commands:"); + for (var entry : commands.entrySet()) { + List commandNames = entry.getKey(); + System.out.println("\t" + commandNames.get(0) + (commandNames.size() == 2 ? " or " + commandNames.get(1) : "")); } + // the clearLine eats the last line if a new line is not skipped + System.out.println("\n"); + } + + private void visitMessage(String message) { + if (!message.startsWith("/")) { + this.client.SendChatMessage(message); + clearLine(); + return; + } + + final Map, Consumer> commands = Map.of( + List.of("/createRoom ", "/create "), (args) -> this.client.SendCreateRoom(args), + List.of("/listRooms", "/list"), (args) -> this.client.RequestRoomList(), + List.of("/joinRoom ", "/join "), (args) -> this.client.SendJoinRoom(args), + List.of("/leaveRoom", "/leave"), (args) -> this.client.SendLeaveRoom(), + List.of("/actualRoom", "/room"), (args) -> this.client.RequestActualRoom(), + List.of("/goodbye", "/bye"), (args) -> this.client.close(), + List.of("/help"), (args) -> {}); + + boolean commandFound = false; + + for (var entry : commands.entrySet()) { + List commandNames = entry.getKey(); + Consumer runnable = entry.getValue(); + for (String cmd : commandNames) { + String[] cmdParts = cmd.split(" "); + if (cmdParts.length > 1) { + // the command expects arguments + if (message.startsWith(cmdParts[0] + " ")) { + runnable.accept(message.substring(cmdParts[0].length() + 1).trim()); + commandFound = true; + } + } else { + // the command does not expect arguments + if (message.equals(cmd)) { + runnable.accept(message.substring(cmd.length()).trim()); + commandFound = true; + } + } + } + if (commandFound) + break; + } + + if (commandFound) { + if (message.equals("/help")) + printHelp(commands); + clearLine(); + return; + } + + System.out.println(ANSIColor.formatString("&rUnknown command&n")); + printHelp(commands); + clearLine(); + } + + private void clearLine() { + System.out.print("\033[1A\r\033[2K"); // weird sequence to clear the line (but it works !) + System.out.flush(); } private void stop() {