-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2583064
commit a0556ef
Showing
10 changed files
with
135 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Request, Response } from 'express'; | ||
import { | ||
getCurrentMatchingUsersCount, | ||
} from '../model/matching-model'; | ||
|
||
export async function getMatchingUsersCount(req: Request, res: Response) { | ||
try { | ||
const count = getCurrentMatchingUsersCount(); | ||
return res.status(200).json({ count }); | ||
} catch (err) { | ||
console.error(err); | ||
return res.status(500).json({ message: 'Unknown error when retrieving matching users count' }); | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Server, Socket } from "socket.io"; | ||
import { addUserToSearchPool, addUserToSocketMap, getSocketIdForUser, matchUsers, removeUserFromSearchPool, removeUserFromSocketMap } from "../model/matching-model"; | ||
|
||
export function initaliseData(socket: Socket) { | ||
const { userId } = socket.data; | ||
addUserToSocketMap(userId, socket.id); | ||
console.log(`User ${userId} connected via socket`); | ||
} | ||
|
||
|
||
// Handle user registration for matching | ||
export function handleRegisterForMatching(socket: Socket, io: Server) { | ||
const { userId } = socket.data; | ||
socket.on('registerForMatching', (criteria) => { | ||
if (criteria.difficulty && criteria.topic) { | ||
addUserToSearchPool(userId, criteria); | ||
console.log(`User ${userId} registered with criteria`, criteria); | ||
socket.emit('registrationSuccess', { message: `User ${userId} registered for matching successfully.` }); | ||
|
||
// Check if a match can be made for the new user | ||
const match = matchUsers(); | ||
if (match) { | ||
const { matchedUsers } = match; | ||
const [user1, user2] = matchedUsers; | ||
|
||
// Notify both clients of the match using the mapping | ||
const socketId1 = getSocketIdForUser(user1.userId); | ||
const socketId2 = getSocketIdForUser(user2.userId); | ||
|
||
if (socketId1) { | ||
io.sockets.sockets.get(socketId1)?.emit('matchFound', { matchedWith: user2.userId }); //INSERT SESSION ID HERE | ||
} | ||
if (socketId2) { | ||
io.sockets.sockets.get(socketId2)?.emit('matchFound', { matchedWith: user1.userId }); //INSERT SESSION ID HERE | ||
} | ||
|
||
// Disconnect both users | ||
if (socketId1) { | ||
io.sockets.sockets.get(socketId1)?.disconnect(true); | ||
} | ||
if (socketId2) { | ||
io.sockets.sockets.get(socketId2)?.disconnect(true); | ||
} | ||
|
||
// Remove users from the map | ||
removeUserFromSocketMap(user1.userId); | ||
removeUserFromSocketMap(user2.userId); | ||
} | ||
} else { | ||
socket.emit('error', 'Invalid matching criteria.'); | ||
} | ||
}); | ||
} | ||
|
||
export function handleDisconnect(socket: Socket) { | ||
// Handle disconnection | ||
const { userId } = socket.data; | ||
socket.on('disconnect', () => { | ||
console.log(`User ${userId} disconnected`); | ||
removeUserFromSearchPool(userId); | ||
|
||
// Remove the user from the map | ||
removeUserFromSocketMap(userId); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 1 addition & 5 deletions
6
...ing-service/src/routes/matching-routes.ts → matching-service/src/routes/api-routes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
import { Router } from 'express'; | ||
import { registerForMatching, getMatchingTime, getMatchingUsersCount } from '../controller/matching-controller'; | ||
import { validateJWT } from '../middleware/jwt-validation'; | ||
import { getMatchingUsersCount } from '../controller/api-controller'; | ||
|
||
const router = Router(); | ||
|
||
router.get('/', (req, res) => {res.send('Hello from matching service!')}); // Test route | ||
|
||
|
||
router.post('/match/register', validateJWT, registerForMatching); // Register for matching | ||
router.get('/match/count', getMatchingUsersCount); // Retrieve the count of users matching | ||
|
||
export const matchingRoutes = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Server, Socket } from "socket.io"; | ||
import { handleDisconnect, handleRegisterForMatching } from "../controller/socket-controller"; | ||
|
||
export function registerEventHandlers(socket: Socket, io: Server) { | ||
handleRegisterForMatching(socket, io); | ||
handleDisconnect(socket); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.