Added a networking process that updates the username in the ChatController of the ClientModel

This commit is contained in:
Seraina 2022-04-17 15:11:01 +02:00
parent 28b0c08ddb
commit 6ebc4495e7
6 changed files with 30 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GUI; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GUI;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatApp; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatApp;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatController;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
@ -70,6 +71,10 @@ public class Client {
} }
public void changeUsername (String newName) {
ChatController.getClient().setUsername(newName);
}
/** /**
* Sends a message to the Server in a formatted way COMND$msg * Sends a message to the Server in a formatted way COMND$msg
*/ */

View File

@ -58,6 +58,9 @@ public class JClientProtocolParser {
c.positionSetter(msg.substring(6)); c.positionSetter(msg.substring(6));
break; break;
case Protocol.serverDeliversLobbyList: case Protocol.serverDeliversLobbyList:
case Protocol.changedUserName:
c.changeUsername(msg.substring(6));
break;
default: default:
System.out.println("Received unknown command"); System.out.println("Received unknown command");
} }

View File

@ -17,8 +17,9 @@ public class ChatApp extends Application {
public static final Logger LOGGER = LogManager.getLogger(ChatApp.class); public static final Logger LOGGER = LogManager.getLogger(ChatApp.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
private static ClientModel clientModel; private static ClientModel clientModel;
private static ChatController chatController; private static ChatController chatController;
private ClientModel cModel;
public ChatApp() { public ChatApp() {
super(); super();
@ -36,6 +37,14 @@ public class ChatApp extends Application {
chatController = chatC; chatController = chatC;
} }
public void setcModel(ClientModel cModel) {
this.cModel = cModel;
}
public ClientModel getcModel() {
return cModel;
}
public static void setClientModel(ClientModel clientM) { public static void setClientModel(ClientModel clientM) {
clientModel = clientM; clientModel = clientM;
} }
@ -60,7 +69,7 @@ public class ChatApp extends Application {
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
LOGGER.info("made it here"); LOGGER.info("made it here");
this.setClientModel(clientModel); this.setcModel(clientModel);
URL resource = ChatApp.class.getResource( URL resource = ChatApp.class.getResource(
"splitPaneChatView.fxml"); "splitPaneChatView.fxml");
LOGGER.info("1"); LOGGER.info("1");

View File

@ -42,7 +42,7 @@ public class ChatController implements Initializable {
@FXML @FXML
private TextArea chatMsgField; private TextArea chatMsgField;
private ClientModel client; private static ClientModel client;
private SimpleBooleanProperty whisperTargetChosen; private SimpleBooleanProperty whisperTargetChosen;
private String cmd; private String cmd;
@ -56,8 +56,8 @@ public class ChatController implements Initializable {
whisperTargetChosen = new SimpleBooleanProperty(); whisperTargetChosen = new SimpleBooleanProperty();
cmd = "CHATA$"; cmd = "CHATA$";
} }
public ChatController(ClientModel client) { public ChatController(ClientModel c) {
this.client = client; client = c;
whisperTargetChosen = new SimpleBooleanProperty(); whisperTargetChosen = new SimpleBooleanProperty();
cmd = "CHATA"; cmd = "CHATA";
@ -73,8 +73,8 @@ public class ChatController implements Initializable {
*/ */
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
setClient(ChatApp.getClientModel()); setClient(ChatApp.getClientModel());
ChatApp.setChatController(this); ChatApp.setChatController(this);
vBoxChatMessages.getChildren().addListener(new ListChangeListener<Node>() { vBoxChatMessages.getChildren().addListener(new ListChangeListener<Node>() {
@Override @Override
public void onChanged(Change<? extends Node> c) { public void onChanged(Change<? extends Node> c) {
@ -157,7 +157,7 @@ public class ChatController implements Initializable {
/** /**
* @return the client who's chat controller this is * @return the client who's chat controller this is
*/ */
public ClientModel getClient() { public static ClientModel getClient() {
return client; return client;
} }

View File

@ -187,7 +187,10 @@ public class Protocol {
*/ */
public static final String serverDeliversLobbyList = "LLIST"; //todo: do we need this? public static final String serverDeliversLobbyList = "LLIST"; //todo: do we need this?
/**
* Informs Client, that their username has been changed
*/
public static final String changedUserName = "CHNAM";
} }

View File

@ -126,6 +126,7 @@ public class ClientHandler implements Runnable {
public void changeUsername(String newName) { public void changeUsername(String newName) {
String helper = this.getClientUserName(); String helper = this.getClientUserName();
this.clientUserName = nameDuplicateChecker.checkName(newName); this.clientUserName = nameDuplicateChecker.checkName(newName);
sendMsgToClient(Protocol.changedUserName + "$" + newName);
broadcastAnnouncementToAll(helper + " has changed their nickname to " + clientUserName); broadcastAnnouncementToAll(helper + " has changed their nickname to " + clientUserName);
try { try {
getLobby().getGame().getGameState().changeUsername(helper,newName); getLobby().getGame().getGameState().changeUsername(helper,newName);