Files
ChessHub/backend/socket.js
T
Moon Patel 4853e5edd9 THIS IS A MAJOR MILESTONE
Socket connection re-implemented. Everything is working fine now.
All thats left to do is to make the app robust and handle some edge cases.
TODO
- The friends list is maybe hardcoded or whatever I dont know, but the problem is
  it shows the same username no matter who the signed in user is. I need to fix that.
- Whenever the pawn reaches to tha last square for promotion an 'Invalid move' error is
  thrown by the Chess class. Need to fix that too.
- Need to disable the chessboard (so user cannot make any moves) until the opponent has joined the game.
- Set timers for the game.
- Need to show the loading states in various places in the app.
- Improve the backend such that a user cannot create more than one room at a time.
- Remove hardcoded values from various places.
- Make the app responsive. The UI is good on desktop but it is very bad on mobile.
- Add a feature to search and add friends.
- Destroying the rooms when the match ends and set timers for auto destruction after a certain time.
- Need some logic to save games in the history.
- Add some tests.
2023-07-03 01:12:47 +05:30

78 lines
2.3 KiB
JavaScript

const socket = require("socket.io");
// roomID => { timeLimit, players:[{username: {color}}] }
let activeRooms = new Map();
function createRoom(roomID, timeLimit) {
console.log(roomID, "created");
activeRooms.set(roomID, { timeLimit, players: {} });
console.log("Currently active rooms", activeRooms.size);
}
// structure of userDetails: {username,color}
function addUserToRoom(roomID, userDetails) {
console.log(userDetails);
let { username, color } = userDetails;
let room = activeRooms.get(roomID);
if (room.players[username]) {
room.players[username] = { color };
return "join-room-success";
}
if (Object.keys(room.players).length > 1) {
// room is full
console.log(activeRooms);
return "room-full";
} else {
room.players[username] = { color };
}
console.log(activeRooms);
return "join-room-success";
}
// initialize the socket server with the given http server instance
function socketIOServerInit(server) {
const io = new socket.Server(server, {
cors: {
origin: process.env.CORS_ALLOWED_HOST,
},
});
io.on("connection", (socket) => {
let id = socket.id;
console.log(socket.id, "connected");
socket.on("disconnect", (reason) => {
console.log(id, "disconnected due to", reason);
});
// data is the metadata of the user joining the room played between the users
// structure: {username,color}
socket.on("join-room", (roomID, data) => {
if (activeRooms.has(roomID)) {
let result = addUserToRoom(roomID, data);
if (result === "join-room-success") {
socket.join(roomID);
io.to(roomID).emit("new user joined the room");
socket.emit(result); // room joined successfully
} else {
socket.emit(result); // room is full
}
} else {
socket.emit("join-room-error", "room does not exist");
}
});
socket.on("move", (roomID, moveData) => {
socket.to(roomID).emit("opponent-move", moveData);
});
});
}
module.exports = {
createRoom,
addUserToRoom,
socketIOServerInit,
};