Skip to content

Commit

Permalink
get unverified urls of users in match making
Browse files Browse the repository at this point in the history
  • Loading branch information
hackertron committed Oct 19, 2023
1 parent 7f2023c commit 6835e38
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 67 deletions.
82 changes: 45 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^16.0.3",
"ethers": "^6.1.0",
"ethers": "^6.8.0",
"express": "^4.18.2",
"express-jwt": "^8.4.1",
"jest": "^29.6.1",
Expand All @@ -42,6 +42,7 @@
"@types/supertest": "^2.0.13",
"@types/swagger-jsdoc": "^6.0.1",
"@types/swagger-ui-express": "^4.1.4",
"ethers-typescript-typings": "^0.0.4",
"faker": "^5.5.3",
"nodemon": "^2.0.22",
"swagger-jsdoc": "^6.2.8",
Expand Down
47 changes: 31 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * as userControl from './users';
export * as urlControl from './urls';
export * as tagControl from './tags';
export * as likeControl from './likes';
export * as matchControl from './match';
3 changes: 3 additions & 0 deletions src/controllers/match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const createMatch = async (req: Request, res: Response) => {

}
1 change: 1 addition & 0 deletions src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const createUser = async (req: Request, res: Response) => {
try {
const user = await User.create({ walletAddress: address });
const token = generateToken(user);
const match = addToGroup(user, 1);
res.status(201).json({
user: user,
token,
Expand Down
39 changes: 28 additions & 11 deletions src/controllers/matchmaking.ts → src/lib/matchmaking.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// create matches from MatchGroup
import { MatchGroup } from "../models/matchGroup"
import { Match, MatchDocument } from "../models/match";
import { User } from "../models/schema";
import { User, Url } from "../models/schema";
import mongoose from "mongoose";
import { Request, Response } from 'express';
/*
Expand All @@ -21,30 +21,38 @@ export const createMatch = async(req: Request, res: Response) => {
for (const groupKey in MatchGroups) {
if (MatchGroups.hasOwnProperty(groupKey)) {
const group = MatchGroups[groupKey];
console.log("group : ", group);
console.log("group is ......... : ", group);
if (group.users.length >= 2) {
for(let i = 0; i < group.users.length; i++) {
for (let j = i+ 1; j < group.users.length; j++) {
const match = await Match.create(
{ user1: group.users[i]._id, user2: group.users[j]._id },
{ user1: group.users[i]._id, user2: group.users[j]._id, threshold: parseInt(groupKey) },
)
// update users matched field
await User.updateMany({ _id: { $in: [group.users[i]._id, group.users[j]._id] } }, { matched: true });
// remove users from matchgroup
const res = await MatchGroup.updateMany({}, { $pull: { users: {$in: [group.users[i]._id, group.users[j]._id]} } });
console.log("result : ", res);

// remove matched users from MatchGroups
// remove matched users from MatchGroups array
MatchGroups[groupKey].users.splice(i, 1);
MatchGroups[groupKey].users.splice(i, j);
matches.push(match);
}
}
}
else {
console.log("not enough users in group : ", group.users);
}
}
}
// Match remaining users across groups
let usersRemaining = [];
for (const groupkey in MatchGroups) {
const group = MatchGroups[groupkey];
usersRemaining.push(group);
if(group.users.length > 0) {
usersRemaining.push(group);
}
}
while (usersRemaining.length >= 2)
{
Expand All @@ -59,17 +67,26 @@ export const createMatch = async(req: Request, res: Response) => {

}

// populate match details from Match
export const populateMatch = async(matchObj : MatchDocument) => {
// get all the urls submitted by user1 where verified is false and limit is threshold
const user1Urls = await Url.find({ user: matchObj.user1, verified: false }).limit(matchObj.threshold);
const user2Urls = await Url.find({ user: matchObj.user2, verified: false }).limit(matchObj.threshold);
return { user1Urls: user1Urls, user2Urls: user2Urls };

}


// get match by matchID
export const getMatchbyMatchID = async (matchID: string) => {
const match = await Match.findOne({ _id: matchID });
return match;
if(!match) {
throw new Error("match with provided matchID not found");
}
const urls = await populateMatch(match);
return { match: match, urls: urls };
}

// get match by userID
export const getMatchbyUserID = async (userID: string) => {
const match = await Match.findOne({ user1: userID });
return match;
}

export const updateMatchStatus = async(matchID: string, status: string) :Promise<MatchDocument | { error: string; }> => {
if (status in ['ready', 'running', 'completed', 'deadlock'] === false) {
Expand Down
5 changes: 5 additions & 0 deletions src/models/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface MatchDocument extends Document {
user2: mongoose.Types.ObjectId;
user2Completed: boolean;
status: string;
threshold: number; // threshold is max limit of urls allowed to be validated, depnds on key in MatchGroup
createdAt: Date;
updatedAt: Date;
}
Expand All @@ -32,6 +33,10 @@ const MatchSchema = new mongoose.Schema<MatchDocument>({
enum: ['ready', 'running', 'completed', 'deadlock'],
default: 'ready',
},
threshold: {
type: Number,
default: 1,
},
createdAt: {
type: Date,
default: Date.now,
Expand Down
Loading

0 comments on commit 6835e38

Please sign in to comment.