mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-13 17:11:49 +00:00
feat(DevWeb): Create invitations to play game
This commit is contained in:
@@ -33,7 +33,6 @@ public class LoginBean implements Serializable {
|
||||
try {
|
||||
DAO<User> 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)) {
|
||||
|
||||
@@ -21,22 +21,51 @@ import uppa.project.json.websocket.Message;
|
||||
public class ConnectedUsersWS {
|
||||
|
||||
public static final HashMap<Session, User> users = new HashMap<>();
|
||||
public static final ArrayList<Session> 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<User> 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<User> 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<SimpleUser> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Session, User> users = new HashMap<>();
|
||||
public static final HashMap<Game, ArrayList<User>> 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
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
<%@taglib prefix="component" tagdir="/WEB-INF/tags/components" %>
|
||||
|
||||
<layout:base>
|
||||
|
||||
<component:hero>
|
||||
<div class="columns">
|
||||
<div class="column">
|
||||
@@ -35,7 +36,17 @@
|
||||
<button class="delete" aria-label="close"></button>
|
||||
</header>
|
||||
<section class="modal-card-body">
|
||||
TODO Liste des utilisateurs
|
||||
<table class="table is-fullwidth">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Utilisateur</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<footer class="modal-card-foot">
|
||||
<button class="button is-success">Fermer</button>
|
||||
@@ -43,7 +54,10 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<jsp:useBean id="user" scope="session" type="uppa.project.database.pojo.User"/>
|
||||
<script defer type="module">
|
||||
// Modal
|
||||
|
||||
// Modal
|
||||
document.querySelectorAll('.modal-trigger').forEach(($el) => {
|
||||
$el.addEventListener('click', () => {
|
||||
@@ -68,4 +82,76 @@
|
||||
});
|
||||
</script>
|
||||
|
||||
<script defer type="module">
|
||||
import WebsocketToolkit from "${pageContext.request.contextPath}/static/js/WebsocketToolkit.js";
|
||||
|
||||
let users = [];
|
||||
|
||||
function updateUsers() {
|
||||
const $tbody = document.querySelector('#user-list-modal tbody');
|
||||
$tbody.innerHTML = '';
|
||||
users.forEach(user => {
|
||||
const $tr = document.createElement('tr');
|
||||
const $tdUsername = document.createElement('td');
|
||||
const $tdAction = document.createElement('td');
|
||||
const $button = document.createElement('button');
|
||||
|
||||
$button.classList.add('button', 'is-success');
|
||||
$button.textContent = 'Inviter';
|
||||
$button.addEventListener('click', () => {
|
||||
const data = {
|
||||
from: {
|
||||
id: ${user.id},
|
||||
username: "${user.username}"
|
||||
},
|
||||
to: {...user},
|
||||
game_id: ${game.id}
|
||||
};
|
||||
const message = {
|
||||
type: "invite",
|
||||
data: JSON.stringify(data)
|
||||
};
|
||||
|
||||
ws.ws.send(JSON.stringify(message));
|
||||
})
|
||||
|
||||
|
||||
$tdUsername.textContent = user.username;
|
||||
$tdAction.appendChild($button);
|
||||
|
||||
$tr.appendChild($tdUsername);
|
||||
$tr.appendChild($tdAction);
|
||||
|
||||
$tbody.appendChild($tr);
|
||||
});
|
||||
}
|
||||
|
||||
// Websocket for users in the lobby
|
||||
const url = new URL(window.location.href);
|
||||
url.pathname = "${pageContext.request.contextPath}/ws/users/0";
|
||||
url.protocol = "ws:"
|
||||
url.searchParams.delete("id")
|
||||
|
||||
const ws = new WebsocketToolkit(url);
|
||||
ws.onOpen(() => console.log("Connected to the server"));
|
||||
ws.onMessage("init", (data) => {
|
||||
users = data;
|
||||
updateUsers();
|
||||
});
|
||||
ws.onMessage("addUser", (data) => {
|
||||
users.push(data);
|
||||
updateUsers();
|
||||
});
|
||||
ws.onMessage("removeUser", (data) => {
|
||||
users = users.filter(user => user.id !== data.id);
|
||||
updateUsers();
|
||||
});
|
||||
ws.onMessage("invite", (data) => {
|
||||
const {from, to, game} = data;
|
||||
console.log("User " + from.username + " invited " + to.username + " to play " + game.name);
|
||||
})
|
||||
ws.onError((error) => console.error(error));
|
||||
ws.onClose(() => console.log("Disconnected from the server"));
|
||||
</script>
|
||||
|
||||
</layout:base>
|
||||
@@ -5,8 +5,6 @@
|
||||
|
||||
<%@taglib prefix="component" tagdir="/WEB-INF/tags/components" %>
|
||||
|
||||
<%@attribute name="anonymous"%>
|
||||
|
||||
<table id="connected-user-list" class="table is-fullwidth">
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -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"));
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user