Spaces:
Running
Running
| const asyncHandler = require("express-async-handler"); | |
| const Room = require("../models/RoomModel"); | |
| const Question = require("../models/QuestionsModel"); | |
| const Leaderboard = require("../models/LeaderboardModel"); | |
| const Round = require("../models/RoundsModel"); | |
| const { getRoomLeaderBoardR1R2, getRound3Leaderboard } = require("../helpers/LeaderBoardPreFetch"); | |
| const Team = require("../models/TeamModel"); | |
| const User = require("../models/UserModel"); | |
| const PreviousRoomModel = require("../models/PreviousRoomModel"); | |
| const CollaborativeLeaderboardModel = require("../models/CollaborativeLeaderboardModel"); | |
| const UserQuestionModel = require("../models/UserQuestionModel"); | |
| //Random array retunrer | |
| const randomUniqueArray = (n, array) => { | |
| var arr = []; | |
| while (arr.length < n) { | |
| var r = Math.floor(Math.random() * array.length) + 0; | |
| if (arr.indexOf(array[r]) === -1) arr.push(array[r]); | |
| } | |
| return arr; | |
| } | |
| //MULTI BIG FUNCTION :: INITIAL ASSIGNMENT | |
| module.exports.roundFourAssign = asyncHandler(async (req, res) => { | |
| try { | |
| let date = new Date(); | |
| let roundNumber = 4; | |
| let round = await Round.findOne({ Round: roundNumber }); | |
| if (!round.Enabled) { | |
| res.send("Round is disabled"); | |
| return; | |
| } | |
| if (date >= round.StartTime && date <= round.EndTime) { | |
| //Router Should be open | |
| let leaderboardR3 = await getRound3Leaderboard() | |
| //here 1 represents round is 1 | |
| if (checkTeamIdInTop3(leaderboardR3, req.user.TeamID)) { | |
| //if team is in top 5 teams of Round 1 | |
| //set leaderboard of team | |
| let board = await Leaderboard.findOne({ TeamID: req.team._id, Round: roundNumber }); | |
| let board2 = await Leaderboard.findOne({ TeamID: req.team._id, Round: 2 }); | |
| let board3 = await CollaborativeLeaderboardModel.findOne({ | |
| $or: [{ TeamID1: req.team._id }, { TeamID2: req.team._id }] | |
| }); | |
| if (board === null) { | |
| board = new Leaderboard({ | |
| TeamID: req.team._id, | |
| TeamName: req.team.TeamName, | |
| Points: board3.Points+board2.Points, | |
| Questions: { | |
| Solved: [], | |
| Unsolved: [] | |
| }, | |
| Round: 4 | |
| }); | |
| await board.save(); | |
| board= await Leaderboard.findOne({ TeamID: req.team._id, Round: roundNumber }); | |
| } | |
| if (!board.Enabled) { | |
| res.send("This round is no longer available"); | |
| return; | |
| } | |
| //ASSIGNING ROOMID | |
| let room = await Room.findOne({ Round: roundNumber }); | |
| board=await Leaderboard.findOne({TeamID:req.team._id,Round:roundNumber}); | |
| if (board.RoomID === undefined) { | |
| board.RoomID = room._id; | |
| let team = await Team.findOne({ _id: req.team._id }); | |
| team.RoomID = room._id; | |
| let user = await User.findOne({ _id: req.user._id }); | |
| user.RoomID = room._id; | |
| let PreviousRoom = new PreviousRoomModel({ | |
| TeamID: req.team._id, | |
| RoomID: room._id, | |
| Round: roundNumber | |
| }); | |
| await PreviousRoom.save(); | |
| await team.save(); | |
| await user.save(); | |
| } | |
| //ASSIGNING QUESTIONS | |
| //ASSIGNING QUESTIONS | |
| if (board.Questions.Unsolved.length === 0) { | |
| let room = await Room.findOne({ _id: board.RoomID }) | |
| let questions = room.Questions; | |
| board.Questions.Unsolved = questions; | |
| //ASSIGNING HINTADDRESS FROM 0 TO 12 | |
| arr = [] | |
| let countr = 1; | |
| for (let i = 0; i < board.Questions.Unsolved.length; i++) { | |
| var r = Math.floor(Math.random() * 5) + 1; | |
| if (arr.indexOf(r) === -1) { | |
| arr.push(r); | |
| let question = await Question.findOne({ _id: board.Questions.Unsolved[i] }); | |
| let UserQuestion = await UserQuestionModel.findOne({ QuestionNumber: question.QuestionNumber, TeamID: req.team._id }); | |
| if (UserQuestion === null) { | |
| UserQuestion = new UserQuestionModel({ | |
| TeamID: req.team._id, | |
| QuestionID: question._id, | |
| QuestionNumber: question.QuestionNumber, | |
| Question: question.Question, | |
| Description: question.Description, | |
| Answer: question.Answer, | |
| Hint: question.Hint, | |
| HintAddress: r, | |
| Round: question.Round, | |
| Points: question.Points, | |
| RoomID: question.RoomID, | |
| num: countr | |
| }) | |
| await UserQuestion.save(); | |
| } | |
| } | |
| else { | |
| i--; | |
| } | |
| } | |
| await board.save(); | |
| } | |
| //SENDING QUESTIONS AS RESPONSE | |
| let UnSqsnsID = board.Questions.Unsolved; | |
| let SqsnsID = board.Questions.Solved; | |
| let qsns = { Solved: [], Unsolved: [] } | |
| //for unsolved | |
| for (let i in UnSqsnsID) { | |
| let question = await UserQuestionModel.findOne({ QuestionID: UnSqsnsID[i], TeamID: req.team._id }) | |
| if (question) { | |
| let obj = { | |
| QuestionNumber: question.QuestionNumber, | |
| Question: question.Question, | |
| Description: question.Description, | |
| HintAddress: question.HintAddress, | |
| Points: question.Points, | |
| num: question.num | |
| } | |
| qsns.Unsolved.push(obj); | |
| break; | |
| } | |
| } | |
| //for solved | |
| for (let i in SqsnsID) { | |
| let question = await UserQuestionModel.findOne({ QuestionID: SqsnsID[i], TeamID: req.team._id }) | |
| if (question) { | |
| let obj = { | |
| QuestionNumber: question.QuestionNumber, | |
| Question: question.Question, | |
| Description: question.Description, | |
| HintAddress: question.HintAddress, | |
| Points: question.Points, | |
| num: question.num | |
| } | |
| qsns.Solved.push(obj); | |
| } | |
| } | |
| res.json({ | |
| Questions: qsns, | |
| ServerTime: new Date(), | |
| RoundEnd: round.EndTime, | |
| status: true | |
| }) | |
| } | |
| else { | |
| res.send("You are not qualified for this round"); | |
| } | |
| } | |
| else { | |
| //router should be closed | |
| res.send("Round is closed"); | |
| } | |
| } | |
| catch (error) { | |
| console.log(error) | |
| res.status(500).json({ | |
| message: error.message | |
| }) | |
| } | |
| }) | |
| function checkTeamIdInTop3(leaderboard, teamId) { | |
| // Get the top 5 array elements | |
| const top3 = leaderboard.slice(0, 3); | |
| // Check if the teamId exists in the top 5 | |
| for (let i = 0; i < top3.length; i++) { | |
| if (top3[i].TeamID1.toString() == teamId.toString()) { | |
| return true; | |
| } | |
| else if (top3[i].TeamID2.toString() == teamId.toString()) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| async function all_member_room_assign(team, RoomID) { | |
| let members = team.Members; | |
| for (let i in members) { | |
| let member = await User.findOne({ _id: members[i] }); | |
| member.RoomID = RoomID; | |
| await member.save(); | |
| } | |
| } |