Spaces:
Running
Running
| const asyncHandler = require("express-async-handler"); | |
| const Room = require("../models/RoomModel"); | |
| const Question = require("../models/QuestionsModel"); | |
| const User = require("../models/UserModel"); | |
| const Team = require("../models/TeamModel"); | |
| const { questionSolved } = require("../sockets/QuestionSolved"); | |
| const Leaderboard = require("../models/LeaderboardModel"); | |
| const Round = require("../models/RoundsModel"); | |
| const PreviousRoomModel = require("../models/PreviousRoomModel"); | |
| const UserQuestionModel = require("../models/UserQuestionModel"); | |
| //Static variable | |
| let count = 0; | |
| //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.initialAssign = asyncHandler(async (req, res) => { | |
| try { | |
| let date = new Date(); | |
| let round2 = await Round.findOne({ Round: 1 }); | |
| if (!round2.Enabled) { | |
| res.send("Round is disabled"); | |
| return; | |
| } | |
| if (date >= round2.StartTime && date <= round2.EndTime) { | |
| //ASSIGNING ROUND | |
| let board1 = await Leaderboard.findOne({ TeamID: req.team._id,Round: 0 }) | |
| let board=await Leaderboard.findOne({ TeamID: req.team._id,Round: 1 }) | |
| if (board === null) { | |
| board = new Leaderboard({ | |
| TeamID: req.team._id, | |
| TeamName: req.team.TeamName, | |
| Points: board1.Points, | |
| Round: 1, | |
| Questions: { | |
| Solved: [], | |
| Unsolved: [] | |
| } | |
| }); | |
| } | |
| let round = 1; | |
| if (!board.Enabled) { | |
| res.send("This round is no longer available"); | |
| return; | |
| } | |
| //ASSIGNING ROOMID | |
| // console.log(count) | |
| let rooms = await Room.find({ Round: round }); | |
| if (board.RoomID === undefined) { | |
| board.RoomID = rooms[count]._id; | |
| req.team.RoomID = rooms[count]._id; | |
| req.user.RoomID = rooms[count]._id; | |
| let team = await Team.findOne({ _id: req.team._id }); | |
| team.RoomID = rooms[count]._id; | |
| let PreviousRoom = new PreviousRoomModel({ | |
| TeamID: req.team._id, | |
| RoomID: rooms[count]._id, | |
| Round: round | |
| }); | |
| await PreviousRoom.save(); | |
| await team.save(); | |
| await all_member_room_assign(req.team, rooms[count]._id); | |
| count++; | |
| } | |
| if (count === rooms.length) { | |
| count = 0; | |
| } | |
| //ASSIGNING QUESTIONS | |
| let temp = await Round.findOne({ Round: round }); | |
| let roundQs = temp.Questions | |
| if (board.Questions.Unsolved.length === 0) { | |
| let room = await Room.findOne({ _id: board.RoomID }) | |
| let questions = room.Questions; | |
| board.Questions.Unsolved = randomUniqueArray(roundQs, questions); | |
| // console.log(board.Questions.Unsolved) | |
| //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() * 12) + 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++ | |
| }) | |
| console.log(countr) | |
| await UserQuestion.save(); | |
| } | |
| } | |
| else{ | |
| i--; | |
| } | |
| } | |
| await board.save(); | |
| console.log("board saved") | |
| } | |
| //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); | |
| } | |
| } | |
| //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, | |
| RoundEnd: round2.EndTime, | |
| ServerTime: new Date(), | |
| status: true | |
| }) | |
| } | |
| else { | |
| res.send("Round is Closed") | |
| } | |
| } | |
| catch (error) { | |
| console.log(error) | |
| res.status(500).json({ | |
| message: error.message | |
| }) | |
| } | |
| }) | |
| 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] }); | |
| if(member) | |
| { | |
| member.RoomID = RoomID; | |
| await member.save(); | |
| } | |
| } | |
| } |