Feat: Add chat box into the game screen #303
@@ -4,6 +4,7 @@ import ch.unibas.dmi.dbis.cs108.casono.client.ClientApp;
|
|||||||
import ch.unibas.dmi.dbis.cs108.casono.client.network.ChatClient;
|
import ch.unibas.dmi.dbis.cs108.casono.client.network.ChatClient;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
|
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui.ChatBoxController;
|
import ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui.ChatBoxController;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui.ChatViewController;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -14,6 +15,7 @@ import java.util.Set;
|
|||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@@ -39,7 +41,9 @@ public class ChatController {
|
|||||||
return chatBoxController;
|
return chatBoxController;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final ChatBoxController chatBoxController;
|
public void setChatBoxController(ChatBoxController chatBoxController) {this.chatBoxController = chatBoxController; }
|
||||||
|
|
||||||
|
private ChatBoxController chatBoxController;
|
||||||
private int lobbyId = -1;
|
private int lobbyId = -1;
|
||||||
private final Timer timer;
|
private final Timer timer;
|
||||||
private final Consumer<List<String>> serverEventListener;
|
private final Consumer<List<String>> serverEventListener;
|
||||||
@@ -61,6 +65,13 @@ public class ChatController {
|
|||||||
/** List of all users connected to the server, to safe them locally on the client */
|
/** List of all users connected to the server, to safe them locally on the client */
|
||||||
private final List<String> localUserList;
|
private final List<String> localUserList;
|
||||||
|
|
||||||
|
public List<String> getLocalUserList() {
|
||||||
|
return this.localUserList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final Map<ChatController.ChatKey, ChatViewController> activeChatControllers;
|
||||||
|
|
||||||
|
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,6 +89,7 @@ public class ChatController {
|
|||||||
this.chatBoxController = new ChatBoxController(username, this);
|
this.chatBoxController = new ChatBoxController(username, this);
|
||||||
this.logger = LogManager.getLogger(ChatController.class);
|
this.logger = LogManager.getLogger(ChatController.class);
|
||||||
this.serverEventListener = this::handleServerEvent;
|
this.serverEventListener = this::handleServerEvent;
|
||||||
|
this.activeChatControllers = new HashMap<>();
|
||||||
|
|
||||||
registerAsActiveController(clientService);
|
registerAsActiveController(clientService);
|
||||||
clientService.addEventListener(serverEventListener);
|
clientService.addEventListener(serverEventListener);
|
||||||
@@ -126,7 +138,7 @@ public class ChatController {
|
|||||||
}
|
}
|
||||||
ChatModel lobbyChatModel = new ChatModel(ChatType.LOBBY, username, lobbyId, null);
|
ChatModel lobbyChatModel = new ChatModel(ChatType.LOBBY, username, lobbyId, null);
|
||||||
chatModelMap.put(key, lobbyChatModel);
|
chatModelMap.put(key, lobbyChatModel);
|
||||||
this.chatBoxController.addChatTab("Lobby", lobbyChatModel);
|
this.chatBoxController.addChatTab("LOBBY", lobbyChatModel, ChatType.LOBBY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+33
-8
@@ -11,6 +11,8 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
@@ -27,7 +29,7 @@ public class ChatBoxController {
|
|||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
private ChatController chatController;
|
private final ChatController chatController;
|
||||||
|
|
||||||
@FXML private VBox chatBox;
|
@FXML private VBox chatBox;
|
||||||
|
|
||||||
@@ -43,7 +45,6 @@ public class ChatBoxController {
|
|||||||
|
|
||||||
@FXML private final Map<String, Tab> usernameTabMap;
|
@FXML private final Map<String, Tab> usernameTabMap;
|
||||||
|
|
||||||
private String ressource = "/ui-structure/components/chatui/chattab.fxml";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for the ChatBoxController, initializes the necessary fields and data structures
|
* Constructor for the ChatBoxController, initializes the necessary fields and data structures
|
||||||
@@ -73,11 +74,12 @@ public class ChatBoxController {
|
|||||||
*/
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
ChatModel globalChatModel = new ChatModel(ChatType.GLOBAL, username, -1, null);
|
ChatType global = ChatType.GLOBAL;
|
||||||
|
ChatModel globalChatModel = new ChatModel(global, username, -1, null);
|
||||||
chatController
|
chatController
|
||||||
.getChatModelMap()
|
.getChatModelMap()
|
||||||
.put(new ChatController.ChatKey(ChatType.GLOBAL), globalChatModel);
|
.put(new ChatController.ChatKey(global), globalChatModel);
|
||||||
addChatTab("GLOBAL", globalChatModel);
|
addChatTab("GLOBAL", globalChatModel, global);
|
||||||
addWhisperChatButton.setOnAction(event -> addWhisperChatButton.show());
|
addWhisperChatButton.setOnAction(event -> addWhisperChatButton.show());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,10 +168,11 @@ public class ChatBoxController {
|
|||||||
public void addWhisperChat(String target, ChatModel chatModel) {
|
public void addWhisperChat(String target, ChatModel chatModel) {
|
||||||
if (!activeWhisperChats.contains(target)) {
|
if (!activeWhisperChats.contains(target)) {
|
||||||
activeWhisperChats.add(target);
|
activeWhisperChats.add(target);
|
||||||
|
ChatType whisper = ChatType.WHISPER;
|
||||||
chatController
|
chatController
|
||||||
.getChatModelMap()
|
.getChatModelMap()
|
||||||
.put(new ChatController.ChatKey(ChatType.WHISPER, target), chatModel);
|
.put(new ChatController.ChatKey(whisper, target), chatModel);
|
||||||
addChatTab(target, chatModel);
|
addChatTab(target, chatModel, whisper);
|
||||||
} else {
|
} else {
|
||||||
chatTabPane.getSelectionModel().select(usernameTabMap.get(target));
|
chatTabPane.getSelectionModel().select(usernameTabMap.get(target));
|
||||||
}
|
}
|
||||||
@@ -184,7 +187,8 @@ public class ChatBoxController {
|
|||||||
* @param chatModel The {@link ChatModel} containing the data and logic for this specific chat.
|
* @param chatModel The {@link ChatModel} containing the data and logic for this specific chat.
|
||||||
* @throws RuntimeException If the FXML resource for the chat tab cannot be loaded.
|
* @throws RuntimeException If the FXML resource for the chat tab cannot be loaded.
|
||||||
*/
|
*/
|
||||||
public void addChatTab(String title, ChatModel chatModel) {
|
public void addChatTab(String title, ChatModel chatModel, ChatType chatType) {
|
||||||
|
String ressource = "/ui-structure/components/chatui/chattab.fxml";
|
||||||
URL resource = getClass().getResource(ressource);
|
URL resource = getClass().getResource(ressource);
|
||||||
FXMLLoader fxmlLoader = new FXMLLoader(resource);
|
FXMLLoader fxmlLoader = new FXMLLoader(resource);
|
||||||
runOnPlatformSynchronized(
|
runOnPlatformSynchronized(
|
||||||
@@ -193,6 +197,7 @@ public class ChatBoxController {
|
|||||||
ChatViewController chatViewController =
|
ChatViewController chatViewController =
|
||||||
new ChatViewController(
|
new ChatViewController(
|
||||||
this.chatController, chatModel, this.username);
|
this.chatController, chatModel, this.username);
|
||||||
|
chatController.activeChatControllers.put(new ChatController.ChatKey(chatType), chatViewController);
|
||||||
fxmlLoader.setController(chatViewController);
|
fxmlLoader.setController(chatViewController);
|
||||||
Node load = fxmlLoader.load();
|
Node load = fxmlLoader.load();
|
||||||
VBox.setVgrow(load, Priority.ALWAYS);
|
VBox.setVgrow(load, Priority.ALWAYS);
|
||||||
@@ -239,4 +244,24 @@ public class ChatBoxController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadChats() {
|
||||||
|
Map<ChatController.ChatKey, ChatModel> chatModelMap = chatController.getChatModelMap();
|
||||||
|
ChatController.ChatKey key = new ChatController.ChatKey(ChatType.GLOBAL);
|
||||||
|
ChatModel global = chatModelMap.get(key);
|
||||||
|
ChatViewController globalController = chatController.activeChatControllers.get(key);
|
||||||
|
for (Message msg : global.messages) {
|
||||||
|
globalController.showMessage(msg);
|
||||||
|
}
|
||||||
|
for (String user : chatController.getLocalUserList()) {
|
||||||
|
ChatController.ChatKey whisperUserKey = new ChatController.ChatKey(ChatType.WHISPER, user);
|
||||||
|
if (chatModelMap.containsKey(whisperUserKey)) {
|
||||||
|
ChatModel whisperChatModel = chatModelMap.get(whisperUserKey);
|
||||||
|
addWhisperChat(user, whisperChatModel);
|
||||||
|
for (Message msg : whisperChatModel.messages) {
|
||||||
|
chatController.activeChatControllers.get(whisperUserKey).showMessage(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user