Skip to content

Commit

Permalink
refactor code and minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshidwivedi committed Jun 21, 2024
1 parent 973ddde commit a5d2fba
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 47 deletions.
2 changes: 1 addition & 1 deletion common/recoil/room/room.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export const useSetRoomId = () => {
setRoomId({id});
};
return handleSetRoomId
}
};
4 changes: 2 additions & 2 deletions common/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export declare global {
options: CtxOptions;
}

type Room =Map<string, Move[]>;
type Room = {users: Map<string, Move[]>; drawed: Move[]};

interface ServerToClientEvents {
room: (room: string)=> void;
room: (room: Room, usersToParse: string)=> void;
created: (roomId: string) => void;
joined:(roomId :string, failed?:boolean) => void;
user_draw: (move: Move, userId: string) => void;
Expand Down
8 changes: 7 additions & 1 deletion modules/home/components/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { FormEvent, useEffect, useState } from 'react';
import { useRouter } from 'next/router';
import { socket } from '@/common/lib/socket';
import { useSetRoomId } from '@/common/recoil/room';

const Home = () => {
const [roomId, setRoomId] = useState("");

const setAtomRoomId = useSetRoomId();

const router = useRouter();

useEffect(() => {
const handleCreated = (roomIdFromServer: string) => {
setAtomRoomId(roomIdFromServer);
router.push(roomIdFromServer);
};

const handleJoined = (roomIdFromServer: string, failed?: boolean) => {
if (!failed) {
setAtomRoomId(roomIdFromServer);
router.push(roomIdFromServer);
} else {
console.log("Failed to join room");
Expand All @@ -26,7 +32,7 @@ const Home = () => {
socket.off("created", handleCreated);
socket.off("joined", handleJoined);
};
}, [router]);
}, [router, setAtomRoomId]);

const handleCreateRoom = () => {
socket.emit("create_room");
Expand Down
2 changes: 1 addition & 1 deletion modules/room/components/MousePosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const MousePosition = () => {
socket.emit("mouse_move", getPos(docX,x), getPos(docY, y));
prevPosition.current = { x: docX, y: docY };
}
}, 300);
}, 25);

return (
<motion.div
Expand Down
5 changes: 5 additions & 0 deletions modules/room/components/Room.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { useRoomId } from "@/common/recoil/room";
import RoomContextProvider from "../context/Room.context";
import Canvas from "./Canvas";
import { MousePosition } from "./MousePosition";
import MouseRenderer from "./MouseRenderer";
import { ToolBar } from "./ToolBar";

const Room = () => {
const roomId = useRoomId();

if(!roomId) return <div> No Room ID</div>

return (
<RoomContextProvider>
<div className="relative h-full w-full overflow-hidden">
Expand Down
2 changes: 1 addition & 1 deletion modules/room/components/UserMouse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const UserMouse = ({ userId }: { userId: string }) => {
<motion.div
className={`absolute top-0 left-0 text-blue-800 ${pos.x === -1 ? 'hidden' : ''} pointer-events-none`}
animate={{ x: pos.x + x, y: pos.y + y }}
transition={{ duration: 0.3, ease: "linear" }}
transition={{ duration: 0.1, ease: "linear" }}
>
<BsCursorFill className="-rotate-90" />
</motion.div>
Expand Down
7 changes: 6 additions & 1 deletion modules/room/helpers/Canvas.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ export const handleMove = (
}
};

export const drawOnUndo = (
export const drawAllMoves = (
ctx: CanvasRenderingContext2D,
movesWithoutUser: Move[],
savedMoves: Move[],
users: {[key: string]: Move[]}
) => {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

movesWithoutUser.forEach((move)=>{
handleMove(move,ctx);
});

Object.values(users).forEach((user)=>{
user.forEach((move)=> handleMove(move,ctx));
});
Expand Down
62 changes: 40 additions & 22 deletions modules/room/hooks/Canvas.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useCallback, useEffect, useState } from "react";
import { use, useCallback, useEffect, useState } from "react";
import { socket } from "@/common/lib/socket";
import { useOptions } from "@/common/recoil/options";
import { drawOnUndo, handleMove } from "../helpers/Canvas.helpers";
import { drawAllMoves, handleMove } from "../helpers/Canvas.helpers";
import usersAtom, { useUsers } from "@/common/recoil/users";
import { useBoardPosition } from "./useBoardPosition";
import { getPos } from "@/common/lib/getPos";
import { useSetRecoilState } from "recoil";

const movesWithoutUser: Move[] = [];
const savedMoves: Move[] = [];
let moves: [number, number][] = [];
let tempMoves: [number, number][] = [];

export const useDraw = (
ctx: CanvasRenderingContext2D | undefined,
Expand Down Expand Up @@ -38,7 +39,7 @@ export const useDraw = (

socket.emit("undo");

drawOnUndo(ctx, savedMoves, users);
drawAllMoves(ctx, movesWithoutUser, savedMoves, users);
handleEnd();
}
}, [ctx, handleEnd, users]);
Expand All @@ -64,6 +65,16 @@ export const useDraw = (
ctx.beginPath();
ctx.lineTo(getPos(x, movedX), getPos(y, movedY));
ctx.stroke();

tempMoves = [[getPos(x, movedX), getPos(y, movedY)]];
};

const handleDraw = (x: number, y: number) => {
if (!ctx || !drawing || blocked) return;

ctx.lineTo(getPos(x, movedX), getPos(y, movedY));
ctx.stroke();
tempMoves.push([getPos(x, movedX), getPos(y, movedY)]);
};

const handleEndDrawing = () => {
Expand All @@ -72,24 +83,21 @@ export const useDraw = (
setDrawing(false);

const move: Move = {
path: moves,
path: tempMoves,
options
};

savedMoves.push(move);
tempMoves = [];

socket.emit("draw", move);
moves = [];

drawAllMoves(ctx, movesWithoutUser, savedMoves, users);

handleEnd();
};

const handleDraw = (x: number, y: number) => {
if (!ctx || !drawing || blocked) return;

ctx.lineTo(getPos(x, movedX), getPos(y, movedY));
ctx.stroke();
moves.push([getPos(x, movedX), getPos(y, movedY)]);
};


return {
handleEndDrawing,
Expand All @@ -109,19 +117,29 @@ export const useSocketDraw = (
const setUsers = useSetRecoilState(usersAtom);

useEffect(()=> {
socket.emit("joined_room")
},[]);
if(ctx) socket.emit("joined_room")
},[ctx]);

useEffect(() => {
socket.on("room", (roomJSON) => {
const room: Room = new Map(JSON.parse(roomJSON));
socket.on("room", (room, usersToParse) => {
if(!ctx) return;

room.forEach((userMoves, userId) => {
if (ctx) userMoves.forEach((move) => handleMove(move, ctx));
handleEnd();
setUsers((prevUsers) => ({ ...prevUsers, [userId]: userMoves }));
const users= new Map<string, Move[]>(JSON.parse(usersToParse));

room.drawed.forEach((move) => {
handleMove(move, ctx);
movesWithoutUser.push(move);
});

users.forEach((userMoves, userId) => {
userMoves.forEach((move)=>handleMove(move, ctx) );
setUsers((prevUsers)=> ({...prevUsers, [userId]: userMoves}));
});
handleEnd();

});



return () => {
socket.off("room");
Expand Down Expand Up @@ -170,7 +188,7 @@ export const useSocketDraw = (
newUsers[userId] = newUsers[userId].slice(0, -1);
}
if (ctx) {
drawOnUndo(ctx, savedMoves, newUsers);
drawAllMoves(ctx,movesWithoutUser, savedMoves, newUsers);
handleEnd();
}
return newUsers;
Expand Down
51 changes: 33 additions & 18 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,30 @@ nextApp.prepare().then(async () => {
});

const rooms = new Map<string, Room>();
rooms.set("global", new Map());


const addMove = (roomId: string, socketId: string, move: Move) => {
const room = rooms.get(roomId);

if (!room?.has(socketId)) {
room?.set(socketId, [move]);
if (!room?.users.has(socketId)) {
room?.users.set(socketId, [move]);
}
room?.get(socketId)?.push(move);
room?.users.get(socketId)?.push(move);
};

const undoMove = (roomId: string, socketId: string) => {
const room = rooms.get(roomId);
room?.get(socketId)?.pop();
room?.users.get(socketId)?.pop();
};

const leaveRoom = (roomId: string, socketId: string) => {
const room= rooms.get(roomId);

const userMoves= room?.users.get(socketId)!;
room?.drawed.push(...userMoves);
room?.users.delete(socketId);

console.log("user left room");
};

io.on("connection", (socket) => {
Expand All @@ -51,8 +61,8 @@ nextApp.prepare().then(async () => {
} while (rooms.has(roomId));

socket.join(roomId);
rooms.set(roomId, new Map());
rooms.get(roomId)?.set(socket.id, []);
rooms.set(roomId, {users: new Map(), drawed: []});
rooms.get(roomId)?.users.set(socket.id, []);

io.to(socket.id).emit("created", roomId);

Expand All @@ -71,17 +81,23 @@ nextApp.prepare().then(async () => {

const roomId = getRoomId();
//add the socket to the room's map
rooms.get(roomId)?.set(socket.id, []);
io.to(socket.id).emit("room", JSON.stringify([...rooms.get(roomId)!]));

const room= rooms.get(roomId);
if(room) {
room.users.set(socket.id, []);

io.to(socket.id).emit("room", room, JSON.stringify([...room.users]));

socket.broadcast.to(roomId).emit("new_user", socket.id);
};

});

socket.on("leave_room", () => {
const roomId= getRoomId();
const user= rooms.get(roomId)?.get(socket.id);
if(user?.length ===0 ) rooms.get(roomId)?.delete(socket.id);
leaveRoom(roomId, socket.id);

io.to(roomId).emit("user_disconnected", socket.id);
});

socket.on("draw", (move) => {
Expand All @@ -106,15 +122,14 @@ nextApp.prepare().then(async () => {
});

socket.on("disconnect", () => {

io.to(getRoomId()).emit("user_disconnected", socket.id);

const user= rooms.get(getRoomId())?.get(socket.id);
//remove user from the room if they have no moves
//remove user from the room if they have no moves

leaveRoom(getRoomId(), socket.id );
io.to(getRoomId()).emit("user_disconnected", socket.id);



if(user?.length === 0) rooms.get(getRoomId())?.delete(socket.id);
console.log("disconnected");

});
});

Expand Down

0 comments on commit a5d2fba

Please sign in to comment.