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