Skip to content

Commit

Permalink
Merge pull request #49 from CS3219-AY2425S1/kervyn/clone-message-queue
Browse files Browse the repository at this point in the history
Kervyn/clone message queue
  • Loading branch information
smolegz authored Nov 3, 2024
2 parents da4058c + cec9dd3 commit fb56977
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 650 deletions.
403 changes: 123 additions & 280 deletions message-queue/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion message-queue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"nodemon": "^3.1.7",
"mongodb": "^6.10.0",
"tsc-watch": "^6.2.0"
},
"devDependencies": {
Expand Down
8 changes: 5 additions & 3 deletions message-queue/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export const DIFFICULTY_QUEUE = "DIFFICULTY_QUEUE";
export const DIFFICULTY_ROUTING_KEY = "difficulty";
export const EXCHANGE = "difficulty_exchange"
export const HARD_ROUTING_KEY = "HARD_QUEUE";
export const MEDIUM_ROUTING_KEY = "MEDIUM_QUEUE";
export const EASY_ROUTING_KEY = "EASY_QUEUE";
export const COLLAB_EXCHANGE = "collab_exchange"
export const COLLAB_KEY = "collab"
export const HARD_ROUTING_KEY = "hard_queue";
export const MEDIUM_ROUTING_KEY = "medium_queue";
export const EASY_ROUTING_KEY = "easy_queue";
export const HARD = "Hard";
export const MEDIUM = "Medium";
export const EASY = "Easy";
Expand Down
25 changes: 25 additions & 0 deletions message-queue/src/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MongoClient } from "mongodb"

const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost:27017"
const DB_NAME = "matchmakingDB"

let db
const connectToMongoDB = async () => {
if (db) {
return db
}

try {
const client = await MongoClient.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
db = client.db(DB_NAME)
console.log("Connected to MongoDB")
return db
} catch (err) {
console.error("Failed to connect to MongoDB", err)
throw err
}
}
export { connectToMongoDB, db }
21 changes: 7 additions & 14 deletions message-queue/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
export const getRandomIntegerInclusive = (min, max) => {
min = Math.ceil(min)
max = Math.floor(max)
import crypto from "crypto"

return Math.floor(Math.random() * (max - min + 1)) + min
}

export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
import { UserData } from "./types"

export const deepEqual = (x, y) => {
return (x && y && typeof x === 'object' && typeof y === 'object') ?
(Object.keys(x).length === Object.keys(y).length) &&
Object.keys(x).reduce(function(isEqual, key) {
return isEqual && deepEqual(x[key], y[key]);
}, true) : (x === y);
}
export const generateSessionId = (user1Id: UserData, user2Id: UserData) => {
const combinedId = [user1Id.user_id, user2Id.user_id].sort().join("-") // e.g. user1-user2
const sessionId = crypto.createHash("sha256").update(combinedId).digest("hex")
return sessionId
}
Loading

0 comments on commit fb56977

Please sign in to comment.