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 ac2bb0e..9a04d44 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 @@ -1,62 +1,58 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat; import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService; +import java.util.ArrayList; import java.util.List; /** - * responsible for the transferring of messages from the server to the ChatModel - * or from the ChatViewController to the server + * responsible for the transferring of messages from the server to the ChatModel or from the + * ChatViewController to the server */ public class ChatController { private final String username; private final ClientService clientService; - private ChatModel chatModel; - - private int game_id; + private final ArrayList chatModelArrayList; public ChatController(String username, ClientService clientService) { this.username = username; this.clientService = clientService; + chatModelArrayList = new ArrayList<>(); + chatModelArrayList.add(new ChatModel(ChatType.GLOBAL, username)); } - public void createChat(int game_id) { - chatModel = new ChatModel(ChatModel.ChatType.GLOBAL, username); - this.game_id = game_id; + public void createLobbyChat(int lobbyId, ChatType chatType) { + ChatModel chatModel = new ChatModel(chatType, username, lobbyId); + chatModelArrayList.add(chatModel); } - public ChatModel getChatModel() { - return chatModel; - } - - /** - * method to send a message, the ChatViewController received to the server - */ - public void sendMessage(String msg, String username) { - Message message = new Message(Message.MessageType.GLOBAL, 0, username, null, msg); - onSendToNetwork(message); - } - - /** - * method to get all messages from the server - */ + /** method to get all messages from the server */ public Boolean receiveMessage() { List newMessages = clientService.getMessages(); if (!newMessages.isEmpty()) { for (Message msg : newMessages) { - chatModel.addMessage(msg); + switch (msg.getMessageType()) { + case ChatType.GLOBAL: + chatModelArrayList.get(0).addMessage(msg); + case ChatType.LOBBY: + ChatModel chatModel = chatModelArrayList.get(1); + if (chatModel != null && chatModel.lobbyId == msg.lobbyId) { + chatModel.addMessage(msg); + } + case ChatType.WHISPER: + // TODO: Check, if target person is user and if yes, iterate through all + // whisper chats. + } } return true; - } else { return false; } + } else { + return false; + } } - /** - * method to send a message to the server - * @param message - */ - public void onSendToNetwork(Message message) { + /** method to send a message to the server */ + public void sendMessageToNetwork(Message message) { clientService.sendMessage(message); } - } 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 0c2f075..6fd4177 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 @@ -5,31 +5,26 @@ import java.util.ArrayList; /** * ChatModel, stores the data for a specific chat * - * Holds the current state of a chat + *

Holds the current state of a chat */ - public class ChatModel { public ArrayList messages; - public ChatType chattype; + private ChatType chattype; public String username; public int count; - public enum ChatType { - GLOBAL, - LOBBY, - WHISPER - } + public int lobbyId; /** * Creates a new ChatModel, given a username of the client + * * @param chattype * @param username */ - public ChatModel(ChatType chattype, String username) { this.messages = new ArrayList(); this.chattype = chattype; @@ -37,8 +32,21 @@ public class ChatModel { this.count = 0; } + public ChatModel(ChatType chattype, String username, int lobbyId) { + this.messages = new ArrayList(); + this.chattype = chattype; + this.username = username; + this.count = 0; + this.lobbyId = lobbyId; + } + + public ChatType getChattype() { + return chattype; + } + /** - * method, used by the ChatViewController, to access all new messages, that are stored in the ChatModel + * method, used by the ChatViewController, to access all new messages, that are stored in the + * ChatModel */ public synchronized String viewNextMessage() { count--; @@ -47,8 +55,8 @@ public class ChatModel { } /** - * Adds a new message - * method used by the ChatController + * Adds a new message method used by the ChatController + * * @param msg */ public synchronized void addMessage(Message msg) { @@ -56,9 +64,7 @@ public class ChatModel { count++; } - /** - * method to send all current messages to the ChatViewController, if needed - */ + /** method to send all current messages to the ChatViewController, if needed */ public void addCompleteChat() { for (int i = 0; i < this.messages.size(); i++) { Message msg = this.messages.get(i); 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 new file mode 100644 index 0000000..c234da5 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatType.java @@ -0,0 +1,7 @@ +package ch.unibas.dmi.dbis.cs108.casono.client.chat; + +public enum ChatType { + GLOBAL, + LOBBY, + WHISPER +} 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 8fb2b0e..60cb568 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 @@ -6,33 +6,35 @@ 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 + * 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 final ChatType type; private final String message; public String user; public String timestamp; - public int game_id = 0; + public int lobbyId = 0; public String target = null; - public enum MessageType { - GLOBAL, LOBBY, WHISPER - }; /** * 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 lobbyId - lobby id, or null, if the typChatType * @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 timestamp, String message) { + public Message( + ChatType type, + int lobbyId, + String user, + String target, + String timestamp, + String message) { this.type = type; - this.game_id = game_id; + this.lobbyId = lobbyId; this.user = user; this.target = target; this.timestamp = timestamp; @@ -40,17 +42,18 @@ public class Message { } /** - * Constructor for creating the Messages of the current user, using this client -> time of writing is being recorded + * 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 lobbyId - 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) { + public Message(ChatType type, int lobbyId, String user, String target, String message) { this.type = type; - this.game_id = game_id; + this.lobbyId = lobbyId; this.user = user; this.target = target; this.message = message; @@ -59,54 +62,54 @@ public class Message { this.timestamp = now.format(formatter); } - public String getMessage() { return message; } - public MessageType getMessageType() { + public ChatType getMessageType() { return type; } - /* - * 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 toArgsString() { - return String.format("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 String.format( + "TYPE=%s GAME=%d USER=%s TARGET=%s TIME=%s TEXT=%s", + this.type.toString(), + this.lobbyId, + this.user, + this.target, + this.timestamp, + 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=(?