Chat adjustments:

-Multiple lines are now cut off before sending, and are not printed to sender chat
-"something went wrong while whispering" is now printed to chat rather than console
-Chat message is sent to lobby rather than broadcast, from the start
-whispering person sees who they whisper to
This commit is contained in:
Jonas 2022-04-18 20:17:24 +02:00
parent a6fac4c1b3
commit c89eb7c5de
4 changed files with 14 additions and 15 deletions

View File

@ -10,7 +10,6 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.JServerProtocolParser;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.io.*; import java.io.*;
@ -32,7 +31,7 @@ public class Client {
public ClientPinger clientPinger; public ClientPinger clientPinger;
private ChatApp chatApp; private ChatApp chatApp;
private GUI chatGUi; private GUI chatGui;
/** /**
* Saves the position of the client, gets refreshed everytime the client gets a vote request. * Saves the position of the client, gets refreshed everytime the client gets a vote request.
@ -62,8 +61,8 @@ public class Client {
} }
sendMsgToServer(Protocol.clientLogin + "$" + systemName); sendMsgToServer(Protocol.clientLogin + "$" + systemName);
this.chatApp = new ChatApp(new ClientModel(systemName, this)); this.chatApp = new ChatApp(new ClientModel(systemName, this));
this.chatGUi = new GUI(this.chatApp); this.chatGui = new GUI(this.chatApp);
chatGUi.setName(systemName); chatGui.setName(systemName);
clientPinger = new ClientPinger(this, this.socket); clientPinger = new ClientPinger(this, this.socket);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -251,7 +250,7 @@ public class Client {
cP.start(); cP.start();
client.userInputListener(); //this one blocks. client.userInputListener(); //this one blocks.
LOGGER.info("7.1"); LOGGER.info("7.1");
Thread guiThread = new Thread(client.chatGUi); Thread guiThread = new Thread(client.chatGui);
LOGGER.info("8"); LOGGER.info("8");
guiThread.start(); guiThread.start();
LOGGER.info("9"); LOGGER.info("9");

View File

@ -56,7 +56,7 @@ public class ChatController implements Initializable {
public ChatController() { //TODO: why does this get called public ChatController() { //TODO: why does this get called
super(); super();
whisperTargetChosen = new SimpleBooleanProperty(); whisperTargetChosen = new SimpleBooleanProperty();
cmd = "CHATA$"; cmd = Protocol.chatMsgToLobby + "$";
LOGGER.info("ChatController empty constructor used"); LOGGER.info("ChatController empty constructor used");
} }
@ -102,19 +102,21 @@ public class ChatController implements Initializable {
}); });
/** /**
* Initialize what heppens when the sen button is pressed * Initialize what happens when the sen button is pressed
*/ */
sendButton.setOnAction(new EventHandler<ActionEvent>() { sendButton.setOnAction(new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
String msg = chatMsgField.getText(); String msg = chatMsgField.getText().split("\\R")[0]; //cut off extra lines, if present.
if (!msg.isEmpty()) { if (!msg.isEmpty()) {
client.getClient().sendMsgToServer(cmd.toString() + msg); client.getClient().sendMsgToServer(cmd.toString() + msg);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg); LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Label l = new Label(client.getUsername() + " (you): " + msg); Label l;
if (cmd.contains(whisper)) { if (cmd.startsWith(whisper)) {
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH)); l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else { } else {
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER)); l.setBackground(Background.fill(Color.LAVENDER));
} }
vBoxChatMessages.getChildren().add(l); vBoxChatMessages.getChildren().add(l);

View File

@ -275,7 +275,7 @@ public class ClientHandler implements Runnable {
Protocol.printToClientChat + "$" + this.getClientUserName() + " whispers: " + msg); Protocol.printToClientChat + "$" + this.getClientUserName() + " whispers: " + msg);
/*sendMsgToClient( /*sendMsgToClient(
Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": " Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": "
+ msg);*/ // no confirmation messge needed. will be colorcoded in gui + msg);*/ // no confirmation message needed. will be colorcoded in gui
LOGGER.info( LOGGER.info(
Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": " Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": "
+ msg); + msg);
@ -301,7 +301,7 @@ public class ClientHandler implements Runnable {
} }
/** /**
* Decode a whisper mesage * Decode a whisper message
* *
* @param msg to decode. the command has been removed from the front * @param msg to decode. the command has been removed from the front
* @return a String[] containing the target at index 0 and the message at index 1. * @return a String[] containing the target at index 0 and the message at index 1.

View File

@ -51,8 +51,6 @@ public class JServerProtocolParser {
String[] targetAndMsg = h.decodeWhisper(msg.substring(6)); String[] targetAndMsg = h.decodeWhisper(msg.substring(6));
String targetName = targetAndMsg[0]; String targetName = targetAndMsg[0];
String chatMsg = targetAndMsg[1]; String chatMsg = targetAndMsg[1];
System.out.println(targetName);
System.out.println(chatMsg);
for (ClientHandler c : ClientHandler.getConnectedClients()) { for (ClientHandler c : ClientHandler.getConnectedClients()) {
if (c.getClientUserName().equals(targetName)) { if (c.getClientUserName().equals(targetName)) {
target = c; target = c;
@ -61,7 +59,7 @@ public class JServerProtocolParser {
assert target != null; assert target != null;
h.whisper(chatMsg, target); h.whisper(chatMsg, target);
} catch (Exception ignored) { } catch (Exception ignored) {
h.sendAnnouncementToClient("Something went wrong."); h.sendMsgToClient(Protocol.printToClientChat + "$Something went wrong while whispering.");
} }
break; break;
case Protocol.clientLogin: case Protocol.clientLogin: