From 3809b7e0e2bead01bf11a0a74c71d519710acd8f Mon Sep 17 00:00:00 2001 From: ginkelmath Date: Sat, 14 Mar 2026 14:33:17 +0100 Subject: [PATCH] Fix: update clientservice --- .../cs108/casono/client/chat/Message.java | 148 ++++++++++++++---- .../casono/client/network/ClientParser.java | 26 --- .../casono/client/network/ClientService.java | 57 +------ .../client/network/ResponseException.java | 9 -- .../client/network/ClientServiceTest.java | 4 +- 5 files changed, 118 insertions(+), 126 deletions(-) delete mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/network/ClientParser.java delete mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/network/ResponseException.java 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 44e2c45..d5b5a1c 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 @@ -1,57 +1,137 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat; import java.time.LocalTime; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Message Object for internal handling of Chat-Messages + * TODO: Should be used on both sides of the network + */ public class Message { + private final MessageType type; private String message; - public String name; - public int hourTime; - public int minuteTime; + public String user; + public String timestamp; public int game_id = 0; public String target = null; + public enum MessageType { + GLOBAL, LOBBY, WHISPER + }; - // For Global Chat + public static final String SEPERATOR = " "; - public Message(String message, String name) { - this.message = message; - this.name = name; - LocalTime now = LocalTime.now(); - this.hourTime = now.getHour(); - this.minuteTime = now.getMinute(); - } + /** + * Constructor for creating Messages with all information given + * @param type - Either global, local or whisper + * @param game_id - lobby id, or null, if the type is global + * @param user - username + * @param target - username of the target user, for whisper chat + * @param message + */ - // For Lobby Chat - - public Message(String message, String name, int game_id) { - this.message = message; - this.name = name; - LocalTime now = LocalTime.now(); - this.hourTime = now.getHour(); - this.minuteTime = now.getMinute(); - this.game_id = game_id; - } - - // For Whisper Chat - - public Message(String message, String name, int game_id, String target) { - this.message = message; - this.name = name; - LocalTime now = LocalTime.now(); - this.hourTime = now.getHour(); - this.minuteTime = now.getMinute(); + public Message(MessageType type, int game_id, String user, String target, String timestamp, String message) { + this.type = type; this.game_id = game_id; + this.user = user; this.target = target; + this.timestamp = timestamp; + this.message = message; } + /** + * Constructor for creating the Messages of the current user, using this client -> time of writing is being recorded + * @param type - Either global, local or whisper + * @param game_id - lobby id, or null, if the type is global + * @param user - username + * @param target - username of the target user, for whisper chat + * @param message + */ + + public Message(MessageType type, int game_id, String user, String target, String message) { + this.type = type; + this.game_id = game_id; + this.user = user; + this.target = target; + this.message = message; + LocalTime now = LocalTime.now(); + this.timestamp = now.toString(); + } + + public String getMessage() { return message; } - public String toString() { - return String.format("%s: %s", this.name, this.message); + public String getMessageType() { + return type.toString(); } - public void print() { - System.out.println(this.toString()); + /** + * Method to test the system + * @return - String representation of the Message instance, as for example "player1: Hello World" + */ + public String toString() { + return String.format("%s: %s", this.user, this.message); + } + + /** + * Method to create the request representation of the message object, to be sent to the server + * @return - request as specified in the network protocol, as String + */ + public String toRequest() { + String request = String.format("SEND_MESSAGE TYPE=%s GAME=%d USER=%s TARGET=%s TIME=%s TEXT=%s", + this.type.toString(), this.game_id, this.user, this.target, this.timestamp, this.message); + return request; + } + + + /** + * 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=(?