Skip to content

✨ - Refactor CanvasController and WSS classes + reset Canvas a get image #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions backend/controllers/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ class CanvasController {
height: 1024,
};

public static async init() {
// TODO: Initialize the canvas from the database or create a new one if it doesn't exist
/**
* VALIDATION
* * Check if the canvas exists in the database
*
* PROCESS
* * Create a new canvas if it doesn't exist
* * Load the canvas from the database
*
* RESPONSE
* * Set the canvas property
* * Log the canvas initialization
*/
}

/**
* Get the canvas image
* @server HTTP
Expand All @@ -39,7 +23,11 @@ class CanvasController {
req: express.Request,
res: express.Response
) {
// TODO: Send the canvas image as a response
res.status(200).json({
pixels: this._canvas.pixels,
width: this._canvas.width,
height: this._canvas.height,
});
}

/**
Expand All @@ -49,7 +37,10 @@ class CanvasController {
* @param socket The socket that sent the pixel data
* @param data The payload
*/
public static async placePixel(socket: SocketIO.Socket, [x, y, palette]: [number, number, number]) {
public static async placePixel(
socket: SocketIO.Socket,
[x, y, palette]: [number, number, number]
) {
// TODO: Place the pixel on the canvas
/**
* VALIDATION
Expand Down Expand Up @@ -80,22 +71,15 @@ class CanvasController {
req: express.Request,
res: express.Response
) {
// TODO: Reset the canvas and log the action
/**
* VALIDATION
* * Check if the user is an admin
*
* PROCESS
* * Reset the canvas in the database
* * Log the canvas reset
*
* RESPONSE
* * Send a success response
* * Broadcast the canvas reset to all clients
* * Send the updated leaderboard to all clients
* * Send the updated user data to all clients
* * Send the updated canvas to all clients
*/
// TODO: Log the canvas reset
console.log("Canvas reset");

this._canvas.changes = 0;
this._canvas.pixels.fill(0);

WSS.resetCanvas();

res.status(200).send("Canvas reset");
}

/**
Expand All @@ -109,8 +93,8 @@ class CanvasController {
req: express.Request,
res: express.Response
) {
const { height, width }: {height: number, width: number} = req.body;
const { height, width }: { height: number; width: number } = req.body;

if (width < 0 || height < 0) {
return res.status(400).send("Invalid canvas size");
} else if (width > 1024 || height > 1024) {
Expand All @@ -125,7 +109,7 @@ class CanvasController {
console.log(`Canvas size changed to ${width}x${height}`);

WSS.updateCanvasSize(width, height);

res.status(200).send("Canvas size changed");
}

Expand Down
18 changes: 15 additions & 3 deletions backend/server/Websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,30 @@ class WSS {
}

/**
* Sends an 'updateCanvas' event to all connected clients.
* @param canvas
* Sends an 'updateCanvasSize' event to all connected clients.
* @param width The new canvas width
* @param height The new canvas height
*/

static async updateCanvasSize(width:number, height:number) {
this.io.emit("canvas-size-update", width, height );
}

/**
* Sends a 'resetCanvas' event to all connected clients.
*/
static async resetCanvas(){
this.io.emit("canvas-reset");
}

/**
* Broadcast a message to everyone
* @param senderEmail The devinciEmail of the sender
* @param message The message
*/
static async broadcastMessage(senderEmail: string, message: string) {
this.io.emit("message", senderEmail, message);
}

}

export default WSS;
10 changes: 8 additions & 2 deletions common/docs/socket-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ Sent to everyone when a message is received from `message` endpoint.

## classement-update

Sent to everyone when a pixel changes from `place-pixel` endpoint.
Sent to everyone when the leaderboard changes.

| index | type | description |
| :---: | :---- | :----------------- |
| 0 | array | Top 10 leaderboard |

## canvas-size-update

Sent to everyone when a pixel changes from `place-pixel` endpoint.
Sent to everyone when the canvas sizes is changed.

| index | type | description |
| :---: | :----- | :------------ |
| 0 | number | Canvas width |
| 1 | number | Canvas height |

## canvas-reset

Sent to everyone when the canvas is reset.

> No parameter