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, questionSolvedlvl3 } = require("../sockets/QuestionSolved"); | |
| const Leaderboard = require("../models/LeaderboardModel"); | |
| const Round = require("../models/RoundsModel"); | |
| const CollaborativeLeaderboardModel = require("../models/CollaborativeLeaderboardModel"); | |
| const { round4End, teamRoundOver } = require("../sockets/RoundStartEnd"); | |
| const { updateLeaderBoard } = require("../sockets/LeaderBoardUpdated"); | |
| const UserQuestionModel = require("../models/UserQuestionModel"); | |
| exports.validateAnswer = asyncHandler(async (req, res) => { | |
| try { | |
| //In data from frontend we will recieve qs no. and answer enterd by user | |
| //data must contain QuestionNumber and Answer | |
| let data = req.body; | |
| let question = await Question.findOne({ QuestionNumber: data.QuestionNumber }) | |
| let round = await Round.findOne({ Round: question.Round }); | |
| let date = new Date(); | |
| if (!round.Enabled) { | |
| res.send("Round is disabled"); | |
| return; | |
| } | |
| if (date >= round.StartTime && date <= round.EndTime) { | |
| let board = await Leaderboard.findOne({ TeamID: req.team._id, Round: question.Round }) | |
| let mainBoard=await Leaderboard.findOne({TeamID:req.team._id,Round:0}) | |
| if (question.Round === 3) { | |
| board = await CollaborativeLeaderboardModel.findOne({ | |
| $or: [{ TeamID1: req.team._id }, { TeamID2: req.team._id }] | |
| }); | |
| } | |
| if ((data.Answer).trim() === (question.Answer).trim()) { | |
| let x = board.Questions.Unsolved.indexOf(question._id); | |
| let y = board.Questions.Solved.indexOf(question._id); | |
| if (x === -1 && y !== -1) { | |
| res.json({ | |
| message: "Question already solved", | |
| status: false | |
| }) | |
| return; | |
| } | |
| else if (x === -1) { | |
| res.json({ | |
| message: "Question not found", | |
| status: false | |
| }) | |
| return; | |
| } | |
| let ques2=await UserQuestionModel.findOne({TeamID:req.team._id,QuestionNumber:question.QuestionNumber,Round:question.Round}) | |
| board.Questions.Unsolved.splice(x, 1); | |
| // console.log(board.Questions.Unsolved); | |
| board.Questions.Solved.push(question._id); | |
| board.Points += question.Points; | |
| mainBoard.Points += question.Points; | |
| await mainBoard.save(); | |
| if(question.Round === 3){ | |
| board.Team1Points += question.Points; | |
| board.Team2Points += question.Points; | |
| questionSolvedlvl3(req, req.user, ques2, board) | |
| } | |
| board.Time = Date.now(); | |
| await board.save(); | |
| if(question.Round !== 3) | |
| updateLeaderBoard(req) | |
| questionSolved(req, req.user, ques2); | |
| if (board.Questions.Unsolved.length === 0 && question.Round === 4 && question.QuestionNumber===94) { | |
| round.Enabled = false; | |
| await round.save(); | |
| round4End(); | |
| } | |
| else if(board.Questions.Unsolved.length === 0){ | |
| board.Enabled = false; | |
| await board.save(); | |
| teamRoundOver(req.user,question.Round) | |
| } | |
| res.json({ | |
| message: "CORRECT ANSWER !", | |
| status: true | |
| }) | |
| } | |
| else { | |
| res.json({ | |
| message: "INCORRECT ANSWER !", | |
| status: false | |
| }) | |
| } | |
| } | |
| else { | |
| res.json({ | |
| message: "Round has not started yet", | |
| status: false | |
| }) | |
| return; | |
| } | |
| } | |
| catch (error) { | |
| console.log(error); | |
| res.status(500).json({ | |
| message: error.message, | |
| status: false | |
| }) | |
| } | |
| }) | |
| exports.hintProvider = asyncHandler(async (req, res) => { | |
| try { | |
| //In data from frontend we will recieve hintAddress of that question | |
| let data = req.body; | |
| let round = data.Round; | |
| let hintAddress = data.HintAddress; | |
| let question = await UserQuestionModel.findOne({ TeamID: req.team._id, HintAddress: hintAddress, Round: round }) | |
| if (question !== null) { | |
| res.json({ | |
| Hint: question.Hint, | |
| status: true | |
| }) | |
| } | |
| else{ | |
| res.json({ | |
| Hint:hintAddress+":Some Error Occured Contact ccs for more help", | |
| status:true | |
| }) | |
| } | |
| } | |
| catch (error) { | |
| res.status(500).json({ | |
| message: error.message, | |
| status: false | |
| }) | |
| } | |
| }) | |