Skip to content

Commit 42a5ae4

Browse files
✨ classement
1 parent 79fa9f7 commit 42a5ae4

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

backend/controllers/Chat.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {Socket} from 'socket.io'
2+
import ChatMessagePayload from '../../common/requests/ChatMessagePayload'
3+
14
class ChatController {
25
/**
36
* Broadcasts a message to all connected clients
@@ -6,7 +9,7 @@ class ChatController {
69
* @param data The message data
710
* @param socket The client socket
811
*/
9-
public static async broadcastMessage(data: ChatMessagePayload, socket: SocketIO.Socket) {
12+
public static async broadcastMessage(data: ChatMessagePayload, socket: Socket) {
1013
// TODO: Broadcast the message to all clients
1114
/**
1215
* VALIDATION

backend/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ WSS.io.on("connection", (socket: Socket) => {
2222
const userAgent = socket.handshake.headers["user-agent"];
2323
console.log(`Socket connected from ${ip} using ${userAgent}`);
2424

25+
WSS.updateClassement(socket)
26+
2527
socket.on("place-pixel", (data) => CanvasController.placePixel(data, socket));
2628
socket.on("message", (data) => ChatController.broadcastMessage(data, socket));
2729

28-
socket.on('ev', (data) => {
29-
console.log(data)
30-
})
31-
3230
socket.on("disconnect", () => {
3331
console.log("Socket disconnected");
3432
});

backend/server/Websocket.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type http from "http";
22
import SocketIO from "socket.io";
3+
import { PrismaClient } from '@prisma/client'
4+
5+
const client = new PrismaClient()
36

47
class WSS {
58
public static io: SocketIO.Server;
@@ -11,6 +14,20 @@ class WSS {
1114
}
1215
});
1316
}
17+
18+
static async updateClassement(socket?: SocketIO.Socket) {
19+
const classement = await client.account.findMany({
20+
select: {
21+
devinciEmail: true,
22+
placedPixels: true
23+
},
24+
orderBy: {
25+
placedPixels: 'desc'
26+
}
27+
});
28+
if(!socket) this.io.emit('classementUpdate', classement)
29+
else socket.emit('classementUpdate', classement)
30+
}
1431
}
1532

1633
export default WSS;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface classementItem {
2+
devinciEmail: string;
3+
placedPixels: number;
4+
}
5+
6+
export default classementItem

frontend/src/App.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
// import { useState } from 'react'
2+
import { useEffect, useState } from 'react';
23
import './App.css'
34
import LoginComponent from './pages/login'
5+
import { socket } from './socket';
6+
import classementItem from '../../common/interfaces/classementItem.interface'
47

58
function App() {
9+
const [classement, setClassement] = useState<classementItem[]>([])
10+
const [isConnected, setIsConnected] = useState(socket.connected);
11+
12+
useEffect(() => {
13+
function onConnect() {
14+
setIsConnected(true);
15+
}
16+
17+
function onDisconnect() {
18+
setIsConnected(false);
19+
}
20+
21+
function onclassementUpdate(data: classementItem[]) {
22+
setClassement(data)
23+
}
24+
25+
socket.on('connect', onConnect);
26+
socket.on('disconnect', onDisconnect);
27+
socket.on('classementUpdate', onclassementUpdate);
28+
29+
return () => {
30+
socket.off('connect', onConnect);
31+
socket.off('disconnect', onDisconnect);
32+
socket.off('classementUpdate', onclassementUpdate);
33+
};
34+
}, []);
635

736
// affichage (render)
837
return (

frontend/src/socket.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { io } from 'socket.io-client';
2+
3+
// "undefined" means the URL will be computed from the `window.location` object
4+
const URL = process.env.NODE_ENV === 'production' ? undefined : 'http://localhost:3000';
5+
6+
export const socket = io(URL);

0 commit comments

Comments
 (0)