Commit 1bc6ae89 authored by Corentin-Boulanger's avatar Corentin-Boulanger Committed by alexandre.ducarne
Browse files

Send lists to users & chat messages

parent 04a1c6a2
......@@ -50,10 +50,14 @@ public class DataCoreClient {
this.localUserHeavy = localUserHeavy;
}
public void setLocalUserLight(UserLight localUserLight) {
this.localUserLight = localUserLight;
public UserLight getLocalUserLight() {
return localUserLight;
}
public void setLocalUserLight(UserLight localUserLight) {
this.localUserLight = localUserLight;
}
public IComToData getClientComToDataImpl() {
return comToDataClientImpl;
}
......
......@@ -68,11 +68,19 @@ public class MessageClientController {
// TODO send to the correct game by retrieving message.gameID
wsHandler.sendMessage(message);
}
public void processData(MessageMeta message) {
switch(message.getClass().toString()) {
case "ChatMessageToClientMessage" :
ChatMessageToClientMessage chatMessage = (ChatMessageToClientMessage) message;
this.comCore.getComToDataClient().receiveChatMessage(chatMessage.getMessage());
}
}
public void processData(MessageMeta message) {
switch (message.getClass().toString()) {
case "ChatMessageToClientMessage":
ChatMessageToClientMessage chatMessage = (ChatMessageToClientMessage) message;
this.comCore.getComToDataClient().receiveChatMessage(chatMessage.getMessage());
break;
case "ServerStateOnConnectionMessage":
ServerStateOnConnectionMessage serverStateMessage = (ServerStateOnConnectionMessage) message;
this.comCore.getComToDataClient().receivePlayersnGames(serverStateMessage.getUsersConnected(),
serverStateMessage.getGamesOnline());
break;
}
}
}
......@@ -52,7 +52,7 @@ public interface IComToData {
* @param gameID : the UUID of the game in which the message was sent
* @return the message to transfer
*/
Message receiveNewChatMessage(Message chatMessage, UUID gameID);
void receiveNewChatMessage(Message chatMessage, UUID gameID) throws Exception;
/**
* Get all the participants (players & spectators) of a game.
......
......@@ -51,19 +51,7 @@ public class ComToDataServerImpl implements IComToData {
}
@Override
public Message receiveNewChatMessage(String message, Timestamp timestamp, UUID userID, UUID gameID) throws Exception {
ListIterator<UserLight> itUser = dataCoreServer.getListConnectedUsers().listIterator();
Message newMessage = new Message(timestamp, message, null);
boolean userFound = false;
while(itUser.hasNext()&&!userFound){
UserLight currentUser = itUser.next();
if (currentUser.getId()==userID) {
userFound = true;
newMessage.setAuthor(currentUser.convertToUserZero());
}
}
if(!userFound)
throw new Exception("Author not found");
public void receiveNewChatMessage(Message newMessage, UUID gameID) throws Exception {
ListIterator<GameHeavy> itGame = dataCoreServer.getListGameHeavy().listIterator();
boolean gameFound = false;
while(itGame.hasNext()&&!gameFound){
......@@ -75,7 +63,6 @@ public class ComToDataServerImpl implements IComToData {
}
if(!gameFound)
throw new Exception("Game not found");
return newMessage;
}
@Override
......
package main.java.server.network;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import main.java.client.network.messages.clientToServer.ChatMessageToServerMessage;
import main.java.client.network.messages.clientToServer.LoginUserToServerMessage;
import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.UserLight;
import main.java.common.message.MessageMeta;
import main.java.server.network.messages.serverToClient.ChatMessageToClientMessage;
import main.java.common.dataModel.Message;
......@@ -71,7 +74,7 @@ public class MessageServerController {
* @param message the message to send
*/
public void sendListsToNewUser(ServerStateOnConnectionMessage message) {
wsController.sendToAll(message);
wsController.sendToUser(message, message.getReceiver().getId().toString());
}
/**
......@@ -83,23 +86,40 @@ public class MessageServerController {
wsController.sendToAll(message);
}
public void processData(MessageMeta message) {
switch(message.getClass().toString()) {
case "LoginUserToServerMessage":
LoginUserToServerMessage loginMessage = (LoginUserToServerMessage) message;
MessageServerController.getInstance().getComCore().getComToData().addAuthenticatedPlayer(loginMessage.getUserToAdd());
break;
case "ChatMessageToServerMessage":
ChatMessageToServerMessage chatMessage = (ChatMessageToServerMessage) message;
comCore.getComToData().receiveNewChatMessage(chatMessage.getChatMessage(), chatMessage.getGameId());
this.sendChatMessageToClients(new ChatMessageToClientMessage(chatMessage.getChatMessage()), chatMessage.getGameId());
break;
}
}
public void processData(MessageMeta message) {
switch (message.getClass().toString()) {
case "LoginUserToServerMessage":
// notify the data module of the new user
LoginUserToServerMessage loginMessage = (LoginUserToServerMessage) message;
MessageServerController.getInstance().getComCore().getComToData()
.addAuthenticatedPlayer(loginMessage.getUserToAdd());
// send lists to the new user
List<GameLight> gamesOnline = comCore.getComToData().getListGames();
List<UserLight> usersConnected = comCore.getComToData().getListUsers();
ServerStateOnConnectionMessage msg = new ServerStateOnConnectionMessage(loginMessage.getUserToAdd(),
usersConnected, gamesOnline);
MessageServerController.getInstance().sendListsToNewUser(msg);
break;
case "ChatMessageToServerMessage":
ChatMessageToServerMessage chatMessage = (ChatMessageToServerMessage) message;
try {
comCore.getComToData().receiveNewChatMessage(chatMessage.getChatMessage(), chatMessage.getGameId());
}
catch (Exception e) {
System.out.println(e.getMessage());
}
this.sendChatMessageToClients(new ChatMessageToClientMessage(chatMessage.getChatMessage()),
chatMessage.getGameId());
break;
}
}
public void sendChatMessageToClients(ChatMessageToClientMessage chatMessage, UUID gameId) {
//FIXME : when handling roomchat available, handle it to send chat message only on this chat room
wsController.sendMessage(chatMessage);
wsController.sendToRoom(chatMessage, gameId.toString());
}
public void sendNewGameNotification(NewGameNotificationToClientMessage message) {
......
......@@ -34,12 +34,12 @@ public class WebSocketController {
simpMessagingTemplate.convertAndSend(TOPIC_PREFIX + "clientMessages", message);
}
private void sendToUser(MessageMeta message, String userId) {
public void sendToUser(MessageMeta message, String userId) {
LOGGER.info("Message sent to user with id : " + userId);
simpMessagingTemplate.convertAndSend(TOPIC_PREFIX + userId + DESTINATION_USER_SUFFIX, message);
}
private void sendToRoom(MessageMeta message, String roomId) {
public void sendToRoom(MessageMeta message, String roomId) {
LOGGER.info("Message sent to whole room with id : " + roomId);
simpMessagingTemplate.convertAndSend(TOPIC_PREFIX + roomId + DESTINATION_ROOM_SUFFIX, message);
}
......
......@@ -7,19 +7,19 @@ import main.java.common.message.MessageMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class ServerStateOnConnectionMessage extends MessageMeta {
private UserLight receiver;
private ArrayList<UserLight> usersConnected;
private ArrayList<GameLight> gamesOnline;
private List<UserLight> usersConnected;
private List<GameLight> gamesOnline;
@Autowired(required = false)
private ComCoreClient comCoreClient;
public ServerStateOnConnectionMessage(UserLight receiver, ArrayList<UserLight> usersConnected,
ArrayList<GameLight> gamesOnline) {
public ServerStateOnConnectionMessage(UserLight receiver, List<UserLight> usersConnected,
List<GameLight> gamesOnline) {
this.receiver = receiver;
this.usersConnected = usersConnected;
this.gamesOnline = gamesOnline;
......@@ -36,24 +36,23 @@ public class ServerStateOnConnectionMessage extends MessageMeta {
this.receiver = receiver;
}
public ArrayList<UserLight> getUsersConnected() {
public List<UserLight> getUsersConnected() {
return usersConnected;
}
public void setUsersConnected(ArrayList<UserLight> usersConnected) {
public void setUsersConnected(List<UserLight> usersConnected) {
this.usersConnected = usersConnected;
}
public ArrayList<GameLight> getGamesOnline() {
public List<GameLight> getGamesOnline() {
return gamesOnline;
}
public void setGamesOnline(ArrayList<GameLight> gamesOnline) {
public void setGamesOnline(List<GameLight> gamesOnline) {
this.gamesOnline = gamesOnline;
}
@Override
public void processData() {
comCoreClient.getComToDataClient().receivePlayersnGames(getUsersConnected(), getGamesOnline());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment