Skip to content

Commit

Permalink
1. Create a new Match
Browse files Browse the repository at this point in the history
2. Close the match
3. update the match states
  • Loading branch information
hackertron committed Oct 17, 2023
1 parent b6b9708 commit f6c8b40
Showing 1 changed file with 84 additions and 1 deletion.
85 changes: 84 additions & 1 deletion src/controllers/matchmaking.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// create matches from MatchGroup
import { MatchGroup } from "../models/matchGroup"
import { Match } from "../models/match";
import { Match, MatchDocument } from "../models/match";
import { User } from "../models/schema";
import mongoose from "mongoose";
/*
1. Create a new Match
2. Close the match
Expand Down Expand Up @@ -55,3 +56,85 @@ export const createMatch = async() => {
return matches;

}

// get match by matchID
export const getMatchbyMatchID = async (matchID: string) => {
const match = await Match.findOne({ _id: matchID });
return match;
}

// 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) {
return {
error: "Invalid status",
}
}
const match = await Match.findOne({ _id: matchID });

if(!match) {
throw new Error("Match not found");
}
try {
match.status = status;
await match.save();
return match;
} catch (error) {
console.error(error);
return {
error: "Match update failed",
};
}
}

export const updateUserCompleted = async(matchID: string, userID: mongoose.Types.ObjectId, updateUser = "user1"):Promise<MatchDocument | { error: string; }> => {
const match = await Match.findOne({ _id: matchID });
if(!match) {
throw new Error("Invalid matchID. Match not found");
}
if (match.user1._id === userID) {
match.user1Completed = true;
}
else if (match.user2._id === userID) {
match.user2Completed = true;
}
else {
const user = await User.findById({_id: userID});
if (user?.role === 'mod') {
if(updateUser === 'user1') {
match.user1Completed = true;
} else if (updateUser === 'user2') {
match.user2Completed = true;
}
else {
console.log("invalid user");
}
} else {
console.error("Cannot change completion status. No permission");
return {
error : "Cannot change completion status. No permission",
}
}
}
match.status = 'running';
return await match?.save();
}

export const markMatchCompleted = async(matchID: string) => {
let match = await Match.findById({_id: matchID});
if(!match) {
throw new Error("Match not found with provided matchID");
}
if(match.user1Completed === true && match.user2Completed === true) {
match.status = 'completed';
await User.updateMany({ _id: { $in: [match.user1._id, match.user2._id] } }, { matched: false });
match = await match.save()
}
return match;

}

0 comments on commit f6c8b40

Please sign in to comment.