diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/bean/LoginBean.java b/S2/DevWeb/Projet/src/main/java/uppa/project/bean/LoginBean.java index babbe1d..7374386 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/bean/LoginBean.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/bean/LoginBean.java @@ -33,7 +33,6 @@ public class LoginBean implements Serializable { try { DAO userDao = factory.getDAOUser(); User[] user = userDao.findByField("username", username); - System.out.println(user.length); for (User u : user) { if (u.getUsername().equals(username) && u.verifyPassword(password)) { diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/ConnectedUsersWS.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/ConnectedUsersWS.java index af394cd..d961ec8 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/ConnectedUsersWS.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/ConnectedUsersWS.java @@ -21,22 +21,51 @@ import uppa.project.json.websocket.Message; public class ConnectedUsersWS { public static final HashMap users = new HashMap<>(); + public static final ArrayList anonymousUsers = new ArrayList<>(); + private static final Gson gson = new Gson(); + private static void broadcast(String message) { + // Send the message to all anonymous users + for (Session session : anonymousUsers) { + try { + session.getBasicRemote().sendText(message); + } catch (IOException e) { + e.printStackTrace(); + } + } + + // Send the message to all connected users + for (Session session : users.keySet()) { + try { + session.getBasicRemote().sendText(message); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + @OnOpen public void onOpen(Session session, @PathParam("user_id") String userId) throws DAOException { Message message; - final DAO userDAO = new Game_JPA_DAO_Factory().getDAOUser(); - int id = Integer.parseInt(userId); - User user = userDAO.findById(id); - // Send the new user to all connected users - message = new Message("addUser", gson.toJson(new SimpleUser(user))); - broadcast(message.toJson()); + if (id == 0) { + anonymousUsers.add(session); + } else { + // Get the new user + final DAO userDAO = new Game_JPA_DAO_Factory().getDAOUser(); + + User user = userDAO.findById(id); + + // Send the new user to all connected users + message = new Message("addUser", gson.toJson(new SimpleUser(user))); + broadcast(message.toJson()); + + // Send all connected users to the new user + users.put(session, user); + } - // Send all connected users to the new user - users.put(session, user); ArrayList connectedUsers = new ArrayList<>(); for (User u : users.values()) connectedUsers.add(new SimpleUser(u)); message = new Message("init", gson.toJson(connectedUsers)); @@ -44,11 +73,16 @@ public class ConnectedUsersWS { } @OnClose - public void onClose(Session session) { - users.remove(session); + public void onClose(Session session, @PathParam("user_id") String userId) { + int id = Integer.parseInt(userId); - Message message = new Message("removeUser", gson.toJson(new SimpleUser(users.get(session)))); - broadcast(message.toJson()); + if (id == 0) { + anonymousUsers.remove(session); + } else { + Message message = new Message("removeUser", gson.toJson(new SimpleUser(users.get(session)))); + users.remove(session); + broadcast(message.toJson()); + } } @OnError @@ -57,17 +91,38 @@ public class ConnectedUsersWS { } @OnMessage - public void onMessage(String message, Session session) { - // Do nothing + public void onMessage(String rawMessage, Session session) { + Message message = gson.fromJson(rawMessage, Message.class); + + if (message.getType().equals("invite")) { + SimpleInvitation invitation = gson.fromJson(message.getData(), SimpleInvitation.class); + + // Find session of the user who receive + for (Session s : users.keySet()) { + if (users.get(s).getId().intValue() == invitation.to.id) { + try { + s.getBasicRemote().sendText(message.toJson()); + } catch (IOException e) { + e.printStackTrace(); + } + break; + } + } + } } - private static void broadcast(String message) { - for (Session session : users.keySet()) { - try { - session.getBasicRemote().sendText(message); - } catch (IOException e) { - e.printStackTrace(); - } + private static class SimpleInvitation { + + public SimpleUser from; + + public SimpleUser to; + + public int game_id; + + public SimpleInvitation(SimpleUser from, SimpleUser to, int game_id) { + this.from = from; + this.to = to; + this.game_id = game_id; } } diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java new file mode 100644 index 0000000..c21fddd --- /dev/null +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java @@ -0,0 +1,41 @@ +package uppa.project.web.websocket; + +import jakarta.websocket.OnClose; +import jakarta.websocket.OnError; +import jakarta.websocket.OnMessage; +import jakarta.websocket.OnOpen; +import jakarta.websocket.Session; +import jakarta.websocket.server.PathParam; +import jakarta.websocket.server.ServerEndpoint; +import java.util.ArrayList; +import java.util.HashMap; +import uppa.project.database.dao.DAOException; +import uppa.project.database.pojo.Game; +import uppa.project.database.pojo.User; + +@ServerEndpoint(value = "/ws/game/{game_id}/{user_id}") +public class GameWS { + + public static final HashMap users = new HashMap<>(); + public static final HashMap> games = new HashMap<>(); + + @OnOpen + public void onOpen(Session session, @PathParam("game_id") String gameId, @PathParam("user_id") String userId) throws DAOException { + + } + + @OnClose + public void onClose(Session session, @PathParam("game_id") String gameId, @PathParam("user_id") String userId) { + + } + + @OnError + public void onError(Throwable throwable) { + throwable.printStackTrace(); + } + + @OnMessage + public void onMessage(String message, Session session) { + // Do nothing + } +} diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp index 5ae0a77..f03f79a 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp @@ -3,6 +3,7 @@ <%@taglib prefix="component" tagdir="/WEB-INF/tags/components" %> +
@@ -35,7 +36,17 @@
@@ -43,7 +54,10 @@
+ + +
\ No newline at end of file diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/components/connected-user-list.tag b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/components/connected-user-list.tag index 5890710..7d07c1d 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/components/connected-user-list.tag +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/components/connected-user-list.tag @@ -5,8 +5,6 @@ <%@taglib prefix="component" tagdir="/WEB-INF/tags/components" %> -<%@attribute name="anonymous"%> - @@ -58,6 +56,12 @@ users = users.filter(user => user.id !== data.id); updateUsers(); }); + ws.onMessage("invite", (data) => { + const {from, to, game_id} = data; + if (confirm(from.username + " vous a invité à rejoindre sa partie")) { + window.location.href = "${pageContext.request.contextPath}/game?id=" + game_id; + } + }) ws.onError((error) => console.error(error)); ws.onClose(() => console.log("Disconnected from the server"));