File size: 8,741 Bytes
df72131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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 } = require("../helpers/LeaderBoardPreFetch");
const Team = require("../models/TeamModel");
const User = require("../models/UserModel");
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.roundTwoAssign = asyncHandler(async (req, res) => {
    try {
        let date = new Date();
        let roundNumber = 2;
        let round = await Round.findOne({ Round: 2 });
        if (!round.Enabled) {
            res.send("Round is disabled");
            return;
        }
        if (date >= round.StartTime && date <= round.EndTime) {
            //Router Should be open
            let leaderboardR1 = await getRoomLeaderBoardR1R2(req.team,  1)
            //here 1 represents round is 1
          //  console.log(leaderboardR1)
            if (checkTeamIdInTop5(leaderboardR1, req.team._id)) {
                //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 board1 = 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,
                        Questions: {
                            Solved: [],
                            Unsolved: []
                        },
                        Round: 2
                    });
                }
                else if (!board.Enabled) {
                    res.send("This round is no longer available");
                    return;
                }
                //ASSIGNING ROOMID
                let rooms = await Room.find({ Round: roundNumber });

                if (board.RoomID === undefined) {
                    board.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: roundNumber
                    });
                    await all_member_room_assign(req.team, rooms[count]._id);
                    await PreviousRoom.save();
                    await team.save();
                    count++;
                }
                if (count === rooms.length) {
                    count = 0;
                }

                //ASSIGNING QUESTIONS 
                //ASSIGNING QUESTIONS 

                let temp = await Round.findOne({ Round: roundNumber });
                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);
                    
                    //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() * 8) + 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();

                }







                //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,
                    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 checkTeamIdInTop5(leaderboard, teamId) {
    // Get the top 5 array elements
   // console.log(leaderboard);
    const top5 = leaderboard.slice(0, 8);
    // Check if the teamId exists in the top 5
    for (let i = 0; i < top5.length; i++) {
        if(top5[i].TeamID===undefined)
        {
            return false;

        }
        if (top5[i].TeamID.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] });
        if (member) {
            member.RoomID = RoomID;
            await member.save();
        }
    }
}