Added a networking process that updates the username in the ChatController of the ClientModel
This commit is contained in:
parent
28b0c08ddb
commit
6ebc4495e7
@ -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
|
||||
*/
|
||||
|
||||
@ -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");
|
||||
}
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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";
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
Reference in New Issue
Block a user