diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatController.java index ea035ed..1ebaf41 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatController.java @@ -3,14 +3,10 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat; 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.ui.chatui.ChatBoxController; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; -import javafx.application.Platform; import org.jspecify.annotations.Nullable; +import java.util.*; + /** * responsible for the transferring of messages from the server to the ChatModel or from the * ChatViewController to the server @@ -43,6 +39,12 @@ public class ChatController { private static final long REFRESH_TIME = 1000; + /** + * Constructor, adds TimerTask to be sent to the server regularly + * + * @param username + * @param clientService + */ public ChatController(String username, ClientService clientService) { this.username = username; chatClient = new ChatClient(clientService); @@ -53,17 +55,19 @@ public class ChatController { new TimerTask() { @Override public void run() { - Platform.runLater( - () -> { - clientService.ping(); - receiveMessage(); - }); + receiveMessage(); } }, 0, REFRESH_TIME); } + /** + * Method to be activated, if a lobby has be chosen. It will update the UI and add a new + * ChatModel to hold the Data for the Lobby Chat + * + * @param lobbyId + */ public void setLobbyChat(int lobbyId) { this.lobbyId = lobbyId; ChatModel lobbyChatModel = new ChatModel(ChatType.LOBBY, username, lobbyId, null); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatModel.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatModel.java index e1ce152..87c9b8f 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatModel.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatModel.java @@ -1,9 +1,10 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat; +import javafx.beans.property.IntegerProperty; +import javafx.beans.property.SimpleIntegerProperty; + import java.util.ArrayList; import java.util.function.Consumer; -import javafx.beans.property.IntegerProperty; -import javafx.beans.property.SimpleIntegerProperty; /** * ChatModel, stores the data for a specific chat @@ -28,6 +29,14 @@ public class ChatModel { public int lobbyId; + /** + * Constructs a new ChatModel for a specific chat type. + * + * @param chattype The type of chat (e.g., GLOBAL, LOBBY, or WHISPER). + * @param username The username of the current user. + * @param lobbyId The ID of the lobby, or -1 if not applicable. + * @param target The username of the whisper recipient, or null for other chat types. + */ public ChatModel(ChatType chattype, String username, int lobbyId, String target) { this.messages = new ArrayList(); this.chattype = chattype; @@ -37,34 +46,38 @@ public class ChatModel { this.target = target; } + /** + * Returns the type of chat this model represents. + * @return The {@link ChatType}. + */ public ChatType getChattype() { return chattype; } /** - * method, used by the ChatViewController, to access all new messages, that are stored in the - * ChatModel - */ - public synchronized String viewNextMessage() { - count.subtract(1); - Message msg = messages.getLast(); - return String.format("[%s] %s: %s", msg.timestamp, msg.sender, msg.getMessage()); - } - - /** - * Adds a new message method used by the ChatController + * Adds a new message to the history and notifies all registered listeners. + * This method is synchronized to ensure thread safety when updating the message list. * - * @param msg + * @param msg The {@link Message} to be added. */ public synchronized void addMessage(Message msg) { messages.add(msg); listeners.stream().forEach((l) -> l.accept(messages.getLast())); } + /** + * Registers a listener to be notified whenever a new message is added to this model. + * + * @param listener A {@link Consumer} that processes the new {@link Message}. + */ public void addListener(Consumer listener) { this.listeners.add(listener); } + /** + * Returns the target user for this chat, primarily used for whispers. + * @return The target username or null. + */ public String getTarget() { return target; } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatType.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatType.java index c234da5..b6a7167 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatType.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatType.java @@ -1,5 +1,6 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat; +/** Describing the Type of the Chat */ public enum ChatType { GLOBAL, LOBBY, diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/Message.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/Message.java index f396c81..f2ab7d2 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/Message.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/Message.java @@ -3,16 +3,15 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat; import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBodyBuilder; +import org.jspecify.annotations.NonNull; + import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Optional; -import java.util.regex.Pattern; -import org.jspecify.annotations.NonNull; /** - * Message Object for internal handling of Chat-Messages TODO: Should be used on both sides of the - * network + * Message Object for internal handling of Chat-Messages */ public class Message { private final ChatType type; @@ -23,13 +22,15 @@ public class Message { public String target = null; /** - * Constructor for creating Messages with all information given + * Constructs a Message with a provided timestamp. Typically used when + * reconstructing messages received from the server. * - * @param type - Either global, local or whisper - * @param lobbyId - lobby id, or null, if the typChatType - * @param sender - username - * @param target - username of the target user, for whisper chat - * @param message + * @param type The chat category (e.g., GLOBAL, LOBBY, or WHISPER). + * @param lobbyId The ID of the lobby, or -1 if not applicable. + * @param sender The username of the message creator. + * @param target The username of the recipient (required for whispers, otherwise null). + * @param timestamp The formatted time string (e.g., "HH:mm"). + * @param message The actual text content of the message. */ public Message( ChatType type, @@ -47,14 +48,14 @@ public class Message { } /** - * Constructor for creating the Messages of the current user, using this client -> time of - * writing is being recorded + * Constructs a new Message for the current user. Automatically generates + * a timestamp based on the local system time ("HH:mm"). * - * @param type - Either global, local or whisper - * @param lobbyId - lobby id, or null, if the type is global - * @param sender - username - * @param target - username of the target user, for whisper chat - * @param message + * @param type The chat category (e.g., GLOBAL, LOBBY, or WHISPER). + * @param lobbyId The ID of the lobby, or -1 if not applicable. + * @param sender The username of the current user. + * @param target The username of the recipient (for whispers). + * @param message The actual text content to be sent. */ public Message(ChatType type, int lobbyId, String sender, String target, String message) { this.type = type; @@ -67,18 +68,29 @@ public class Message { this.timestamp = now.format(formatter); } + /** + * Returns the text content of the message. + * + * @return The message string. + */ public String getMessage() { return message; } + /** + * Returns the type of chat this message belongs to. + * + * @return The {@link ChatType}. + */ public ChatType getMessageType() { return type; } /** - * Method to create the request representation of the message object, to be sent to the server + * Formats the message object into a string representation compatible with + * the network protocol arguments. * - * @return - request as specified in the network protocol, as String + * @return A formatted string containing all message attributes for server transmission. */ public String toArgsString() { String gameIdString = ""; @@ -97,109 +109,62 @@ public class Message { this.message); } - /** Pattern, to analyze the response String with the given parameters */ - public static Pattern msgRex = - Pattern.compile( - "TYPE=(?\\w+) " - + "(GAME=(?\\w+) )?" - + "USER=(?\\w+) " - + "(TARGET=(?\\w+) )?" - + "TIME=(?