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.GUI;
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;
@ -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
*/

View File

@ -58,6 +58,9 @@ public class JClientProtocolParser {
c.positionSetter(msg.substring(6));
break;
case Protocol.serverDeliversLobbyList:
case Protocol.changedUserName:
c.changeUsername(msg.substring(6));
break;
default:
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 BudaLogConfig l = new BudaLogConfig(LOGGER);
private static ClientModel clientModel;
private static ClientModel clientModel;
private static ChatController chatController;
private ClientModel cModel;
public ChatApp() {
super();
@ -36,6 +37,14 @@ public class ChatApp extends Application {
chatController = chatC;
}
public void setcModel(ClientModel cModel) {
this.cModel = cModel;
}
public ClientModel getcModel() {
return cModel;
}
public static void setClientModel(ClientModel clientM) {
clientModel = clientM;
}
@ -60,7 +69,7 @@ public class ChatApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
LOGGER.info("made it here");
this.setClientModel(clientModel);
this.setcModel(clientModel);
URL resource = ChatApp.class.getResource(
"splitPaneChatView.fxml");
LOGGER.info("1");

View File

@ -42,7 +42,7 @@ public class ChatController implements Initializable {
@FXML
private TextArea chatMsgField;
private ClientModel client;
private static ClientModel client;
private SimpleBooleanProperty whisperTargetChosen;
private String cmd;
@ -56,8 +56,8 @@ public class ChatController implements Initializable {
whisperTargetChosen = new SimpleBooleanProperty();
cmd = "CHATA$";
}
public ChatController(ClientModel client) {
this.client = client;
public ChatController(ClientModel c) {
client = c;
whisperTargetChosen = new SimpleBooleanProperty();
cmd = "CHATA";
@ -73,8 +73,8 @@ public class ChatController implements Initializable {
*/
@Override
public void initialize(URL location, ResourceBundle resources) {
setClient(ChatApp.getClientModel());
ChatApp.setChatController(this);
setClient(ChatApp.getClientModel());
ChatApp.setChatController(this);
vBoxChatMessages.getChildren().addListener(new ListChangeListener<Node>() {
@Override
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
*/
public ClientModel getClient() {
public static ClientModel getClient() {
return client;
}

View File

@ -187,7 +187,10 @@ public class Protocol {
*/
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) {
String helper = this.getClientUserName();
this.clientUserName = nameDuplicateChecker.checkName(newName);
sendMsgToClient(Protocol.changedUserName + "$" + newName);
broadcastAnnouncementToAll(helper + " has changed their nickname to " + clientUserName);
try {
getLobby().getGame().getGameState().changeUsername(helper,newName);