Fix: Chat implementation completed

This commit is contained in:
Julian Kropff
2026-04-13 01:44:18 +02:00
parent 44785ee77f
commit 0bf24b9e0f
6 changed files with 45 additions and 21 deletions
@@ -89,7 +89,9 @@ public class ChatController {
public void setLobbyChat(int lobbyId) {
this.lobbyId = lobbyId;
ChatKey key = new ChatKey(ChatType.LOBBY);
if (chatModelMap.containsKey(key)) {
ChatModel existingModel = chatModelMap.get(key);
if (existingModel != null) {
existingModel.lobbyId = lobbyId;
return;
}
ChatModel lobbyChatModel = new ChatModel(ChatType.LOBBY, username, lobbyId, null);
@@ -115,7 +117,7 @@ public class ChatController {
chatModelMap.get(new ChatKey(ChatType.GLOBAL)).addMessage(msg);
break;
case ChatType.LOBBY:
if (msg.lobbyId == lobbyId) {
if (msg.lobbyId == getActiveLobbyChatId()) {
chatModelMap
.computeIfAbsent(
new ChatKey(ChatType.LOBBY),
@@ -155,6 +157,14 @@ public class ChatController {
}
}
private int getActiveLobbyChatId() {
ChatModel lobbyModel = chatModelMap.get(new ChatKey(ChatType.LOBBY));
if (lobbyModel != null) {
return lobbyModel.lobbyId;
}
return lobbyId;
}
/**
* Method to process the usernames of other users connected to the server, after they got polled
* by the {@link ChatClient}.
@@ -79,21 +79,11 @@ public class ChatClient {
public List<String> getUsers() {
logger.info("Asking server for list of users");
List<String> users = clientService.processCommand("LIST_USERS");
List<String> parameters = new ArrayList<String>();
for (int i = 0; i < users.size(); i++) {
String line = users.get(i);
if (line.equals("USERS")) {
continue;
} else if (line.equals("END")) {
break;
}
line = line.replaceFirst("^\t", "");
if (line.equals("USER")) {
String username = users.get(i + 1);
username = username.replaceFirst("^\t", "");
username = username.replaceFirst("^\t", "");
parameters.add(username);
List<String> parameters = new ArrayList<>();
for (String line : users) {
String trimmed = line == null ? "" : line.trim();
if (trimmed.startsWith("USERNAME=")) {
parameters.add(trimmed);
}
}
return parameters;
@@ -195,6 +195,8 @@ public class ClientService {
|| token.equals("LOBBY")
|| token.equals("PLAYERS")
|| token.equals("PLAYER")
|| token.equals("USERS")
|| token.equals("USER")
|| token.equals("CARDS")
|| token.equals("CARD");
}
@@ -174,7 +174,8 @@ public class ServerApp {
parserDispatcher.register("SEND_MESSAGE", new SendMessageParser());
commandRouter.register(
SendMessageRequest.class, new SendMessageHandler(responseDispatcher, userRegistry));
SendMessageRequest.class,
new SendMessageHandler(responseDispatcher, userRegistry, context.lobbyManager()));
parserDispatcher.register("GET_MESSAGE_COUNT", new GetMessageCountParser());
commandRouter.register(
@@ -1,6 +1,9 @@
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.send_message;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatType;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyId;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyManager;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.OkResponse;
@@ -8,6 +11,7 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatch
public class SendMessageHandler extends CommandHandler<SendMessageRequest> {
private final UserRegistry userRegistry;
private final LobbyManager lobbyManager;
/**
* Constructs a new SendMessageHandler with the required dispatcher and user registry.
@@ -15,9 +19,13 @@ public class SendMessageHandler extends CommandHandler<SendMessageRequest> {
* @param responseDispatcher The dispatcher used to send responses back to clients.
* @param userRegistry The registry containing all currently connected users.
*/
public SendMessageHandler(ResponseDispatcher responseDispatcher, UserRegistry userRegistry) {
public SendMessageHandler(
ResponseDispatcher responseDispatcher,
UserRegistry userRegistry,
LobbyManager lobbyManager) {
super(responseDispatcher);
this.userRegistry = userRegistry;
this.lobbyManager = lobbyManager;
}
/**
@@ -42,6 +50,19 @@ public class SendMessageHandler extends CommandHandler<SendMessageRequest> {
* @param message The {@link Message} object to be broadcast.
*/
public void broadcast(Message message) {
if (message != null
&& message.getMessageType() == ChatType.LOBBY
&& message.lobbyId >= 0
&& lobbyManager != null) {
lobbyManager.broadcast(
LobbyId.of(message.lobbyId),
username ->
userRegistry
.getByUsername(username)
.ifPresent(user -> user.enqueueMessage(message)));
return;
}
userRegistry.getAllUsers().forEach(user -> user.enqueueMessage(message));
}
}
@@ -25,8 +25,8 @@
<!-- Eingabebereich -->
<HBox fx:id="controlBar" spacing="10">
<!-- TODO: Größe des TextFields dynamisch anpassen -->
<TextField fx:id="inputField" promptText="Nachricht eingeben..." styleClass="gray-input-field" HBox.hgrow="ALWAYS" />
<Button fx:id="sendButton" styleClass="yellow-button" text="SENDEN" />
<TextField fx:id="inputField" promptText="Enter your message..." styleClass="gray-input-field" HBox.hgrow="ALWAYS" />
<Button fx:id="sendButton" styleClass="yellow-button" text="SEND" />
</HBox>
</children>