From 58ffe690fec6b4cf1161bb6bdb7af04d685f25c7 Mon Sep 17 00:00:00 2001 From: Florian Charlaix Date: Sun, 30 Jan 2022 17:41:43 +0100 Subject: [PATCH] Add command / support --- .../gui/controller/ApplicationController.java | 22 +++++++++++++++++-- src/fr/univ/lyon1/server/ConnectedClient.java | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/fr/univ/lyon1/gui/controller/ApplicationController.java b/src/fr/univ/lyon1/gui/controller/ApplicationController.java index 58f61c9..59f747d 100644 --- a/src/fr/univ/lyon1/gui/controller/ApplicationController.java +++ b/src/fr/univ/lyon1/gui/controller/ApplicationController.java @@ -8,6 +8,7 @@ import fr.univ.lyon1.common.command.Command; import fr.univ.lyon1.common.command.CommandType; import fr.univ.lyon1.common.exception.ChatException; import fr.univ.lyon1.common.exception.LoginInvalid; +import fr.univ.lyon1.common.exception.NotInChannel; import fr.univ.lyon1.common.exception.UnknownCommand; import fr.univ.lyon1.gui.handlers.ServerConfigurationHandler; import javafx.application.Platform; @@ -249,14 +250,31 @@ public class ApplicationController { if (this.currentChannel != null) { try { // Send the message to the server - this.sendObject(new Command(CommandType.message, List.of(new Message(textToSend, this.currentChannel)))); - } catch (IOException e) { + parsInput(textToSend); + } catch (ChatException | IOException e) { System.out.println("Can not send message to server : " + e.getMessage()); e.printStackTrace(); } } } + private void parsInput(String textToSend) throws UnknownCommand, NotInChannel, IOException { + if (textToSend.startsWith("/")) { + List args = Arrays.asList(textToSend.split(" ")); + String commandName = args.get(0).replace("/", ""); + CommandType commandType; + + try { + commandType = CommandType.valueOf(commandName); + } catch (IllegalArgumentException e) { + throw new UnknownCommand(commandName); + } + + this.sendObject(new Command(commandType, new ArrayList<>(args.subList(1, args.size())))); + } else + this.sendObject(new Command(CommandType.message, List.of(new Message(textToSend, this.currentChannel)))); + } + /** * Clear the current textarea message */ diff --git a/src/fr/univ/lyon1/server/ConnectedClient.java b/src/fr/univ/lyon1/server/ConnectedClient.java index 65c5ef2..7200881 100644 --- a/src/fr/univ/lyon1/server/ConnectedClient.java +++ b/src/fr/univ/lyon1/server/ConnectedClient.java @@ -162,7 +162,7 @@ public class ConnectedClient implements Runnable { send(new Command(CommandType.join, List.of((Channel) chan))); - server.broadcastMessage(new Message(chan, Server.getServerUser(), user.getUsername()+" joined the channel !"), -1); + server.broadcastMessage(new Message(chan, Server.getServerUser(), user.getUsername()+" joined the channel !\n"), -1); } /**