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.
This commit is contained in:
+24
-14
@@ -1,37 +1,43 @@
|
||||
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: [] });
|
||||
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) {
|
||||
// room is full
|
||||
if (Object.keys(room.players).length > 1) {
|
||||
return "room-full";
|
||||
} else {
|
||||
// only one user in room
|
||||
room.players[username].color = color;
|
||||
}
|
||||
} else {
|
||||
// add player in the room
|
||||
room.players = {};
|
||||
room.players[username].color = color;
|
||||
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);
|
||||
const io = new socket.Server(server, {
|
||||
cors: {
|
||||
origin: process.env.CORS_ALLOWED_HOST,
|
||||
},
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
let id = socket.id;
|
||||
@@ -57,6 +63,10 @@ function socketIOServerInit(server) {
|
||||
socket.emit("join-room-error", "room does not exist");
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("move", (roomID, moveData) => {
|
||||
socket.to(roomID).emit("opponent-move", moveData);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user