-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from CS3219-AY2425S1/kervyn/clone-message-queue
Kervyn/clone message queue
- Loading branch information
Showing
11 changed files
with
360 additions
and
650 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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 } |
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,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 | ||
} |
Oops, something went wrong.