-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathseed.js
429 lines (404 loc) · 11.9 KB
/
seed.js
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
require("dotenv").config();
// Import Mongoose models
const GamesModel = require("../model/Games").default;
const UserModel = require("../model/User");
const OrganizerModel = require("../model/Organizer");
const TournamentModel = require("../model/Tournament");
const BracketModel = require("../model/Bracket");
const { TeamModel } = require("../model/Team");
async function connectToMongoDB() {
try {
await mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected to MongoDB");
} catch (error) {
console.error("Error connecting to MongoDB:", error);
process.exit(1);
}
}
async function seedGames() {
const gameData = [
{
name: "BGMI",
category: "Battle Royale",
profile: "Battlegrounds Mobile India",
gameBannerPhoto:
"https://media.battlexo.com/tournament/668292838ab430dcee21f257/banner/icon/29fd717a-2be9-4c19-8394-865ff112a15a.webp",
},
{
name: "Call of Duty Mobile",
category: "FPS",
profile: "Mobile version of the popular Call of Duty franchise",
gameBannerPhoto:
"https://media.battlexo.com/tournament/668292838ab430dcee21f257/banner/icon/29fd717a-2be9-4c19-8394-865ff112a15a.webp",
},
];
for (const game of gameData) {
try {
const existingGame = await GamesModel.findOne({ name: game.name });
if (!existingGame) {
await GamesModel.create(game);
console.log(`Game "${game.name}" inserted successfully`);
} else {
console.log(`Game "${game.name}" already exists`);
}
} catch (error) {
console.error(`Error inserting game "${game.name}":`, error);
}
}
}
async function seedUsers() {
const userData = [
{
username: "player1",
name: "John Doe",
email: "[email protected]",
bio: "Passionate gamer",
password: await bcrypt.hash("password123", 10),
verifyCode: "123456",
verifyCodeExpiry: new Date(Date.now() + 24 * 60 * 60 * 1000),
},
{
username: "player2",
name: "Jane Smith",
email: "[email protected]",
bio: "Pro gamer",
password: await bcrypt.hash("password456", 10),
verifyCode: "654321",
verifyCodeExpiry: new Date(Date.now() + 24 * 60 * 60 * 1000),
},
];
for (const user of userData) {
try {
const existingUser = await UserModel.findOne({ email: user.email });
if (!existingUser) {
await UserModel.create(user);
console.log(`User "${user.username}" inserted successfully`);
} else {
console.log(`User "${user.username}" already exists`);
}
} catch (error) {
console.error(`Error inserting user "${user.username}":`, error);
}
}
}
async function seedOrganizers() {
const organizerData = [
{
orgName: "Epic Gaming",
orgEmail: "[email protected]",
description: "Professional esports organization",
bannerPhoto:
"https://media.battlexo.com/tournament/668292838ab430dcee21f257/banner/icon/29fd717a-2be9-4c19-8394-865ff112a15a.webp",
},
{
orgName: "Pro Tournaments",
orgEmail: "[email protected]",
description: "Leading tournament organizer",
bannerPhoto:
"https://media.battlexo.com/tournament/668292838ab430dcee21f257/banner/icon/29fd717a-2be9-4c19-8394-865ff112a15a.webp",
},
];
for (const organizer of organizerData) {
try {
const existingOrganizer = await OrganizerModel.findOne({
orgEmail: organizer.orgEmail,
});
if (!existingOrganizer) {
await OrganizerModel.create(organizer);
console.log(`Organizer "${organizer.orgName}" inserted successfully`);
} else {
console.log(`Organizer "${organizer.orgName}" already exists`);
}
} catch (error) {
console.error(`Error inserting organizer "${organizer.orgName}":`, error);
}
}
}
async function seedTournaments() {
const tournamentData = [
{
tournamentName: "BGMI Pro League",
tournamentDates: {
started: new Date("2024-08-01"),
ended: new Date("2024-08-15"),
},
gameType: "SQUAD",
teamSize: 4,
slots: 16,
prize: [
{ rank: 1, amount: 10000 },
{ rank: 2, amount: 5000 },
{ rank: 3, amount: 2500 },
],
rules: "Standard BGMI rules apply",
email: "[email protected]", // Matches Organizer seed
participantCount: 16,
},
{
tournamentName: "CoD Mobile Championship",
tournamentDates: {
started: new Date("2024-09-01"),
ended: new Date("2024-09-10"),
},
gameType: "SOLO",
teamSize: 1,
slots: 32,
prize: [
{ rank: 1, amount: 5000 },
{ rank: 2, amount: 2500 },
{ rank: 3, amount: 1000 },
],
rules: "Standard CoD Mobile rules apply",
email: "[email protected]", // Matches Organizer seed
participantCount: 32,
},
];
for (const tournament of tournamentData) {
try {
const existingTournament = await TournamentModel.findOne({
tournamentName: tournament.tournamentName,
});
if (!existingTournament) {
const game = await GamesModel.findOne({
name: tournament.tournamentName.includes("BGMI")
? "BGMI"
: "Call of Duty Mobile",
});
const organizer = await OrganizerModel.findOne({
orgEmail: tournament.email,
});
if (game && organizer) {
await TournamentModel.create({
...tournament,
gameId: game._id,
organizerId: organizer._id,
});
console.log(
`Tournament "${tournament.tournamentName}" inserted successfully`,
);
} else {
console.log(
`Failed to create tournament "${tournament.tournamentName}": Game or Organizer not found`,
);
}
} else {
console.log(`Tournament "${tournament.tournamentName}" already exists`);
}
} catch (error) {
console.error(
`Error inserting tournament "${tournament.tournamentName}":`,
error,
);
}
}
}
async function seedTeams() {
// Update your seed data for teams to use valid references to `UserModel` by `_id`.
const teamsData = [
{
image: "https://example.com/team1-image.jpg",
teamname: "Squad Warriors",
game: "BGMI",
role: "SQUAD",
rank: "Diamond",
server: "Asia",
language: "English",
players: [],
requests: [],
},
{
image: "https://example.com/team2-image.jpg",
teamname: "Sharp Shooters",
game: "Call of Duty Mobile",
role: "SOLO",
rank: "Platinum",
server: "North America",
language: "English",
players: [],
requests: [],
},
];
for (const team of teamsData) {
try {
const existingTeam = await TeamModel.findOne({ teamname: team.teamname });
if (!existingTeam) {
// Retrieve user IDs to populate players and requests
const users = await UserModel.find().limit(4); // Assuming you want to use the first 4 users as players
if (users.length === 0) {
console.error("No users found to populate players and requests");
continue;
}
team.players = users.map((user) => user._id);
team.requests = users.slice(0, 2).map((user) => user._id); // Example: first 2 users as requests
await TeamModel.create(team);
console.log(`Team "${team.teamname}" inserted successfully`);
} else {
console.log(`Team "${team.teamname}" already exists`);
}
} catch (error) {
console.error(`Error inserting team "${team.teamname}":`, error);
}
}
}
async function seedBracket() {
const bracketData = [
{
tournamentName: "BGMI Pro League",
format: "single_elimination", // or "double_elimination"
group: [{ id: 1, number: 1, stage_id: 1 }],
match: [
{
child_count: 2,
group_id: 1,
id: 1,
number: 1,
opponent1: { id: 1, position: 1 },
opponent2: { id: 2, position: 2 },
round_id: 1,
stage_id: 1,
status: 1,
},
],
participant: [
{
name: "Squad Warriors",
},
],
round: [
{
group_id: 1,
id: 1,
number: 1,
stage_id: 1,
},
],
stage: [
{
name: "Stage 1",
number: 1,
settings: {
consolationFinal: true,
grandFinalType: "Best of 3",
matchesChildCount: 4,
seedOrdering: ["A", "B", "C"],
size: 8,
},
type: "knockout",
},
],
match_game: ["Game 1", "Game 2"],
},
{
tournamentName: "CoD Mobile Championship",
format: "double_elimination", // or "single_elimination"
group: [{ id: 2, number: 2, stage_id: 2 }],
match: [
{
child_count: 2,
group_id: 2,
id: 2,
number: 2,
opponent1: { id: 3, position: 3 },
opponent2: { id: 4, position: 4 },
round_id: 2,
stage_id: 2,
status: 1,
},
],
participant: [
{
name: "Sharp Shooters",
},
],
round: [
{
group_id: 2,
id: 2,
number: 2,
stage_id: 2,
},
],
stage: [
{
name: "Stage 2",
number: 2,
settings: {
consolationFinal: false,
grandFinalType: "Best of 5",
matchesChildCount: 8,
seedOrdering: ["D", "E", "F"],
size: 16,
},
type: "knockout",
},
],
match_game: ["Game 1", "Game 2", "Game 3"],
},
];
for (const bracket of bracketData) {
try {
const existingTournament = await TournamentModel.findOne({
tournamentName: bracket.tournamentName,
});
if (!existingTournament) {
console.error(`Tournament "${bracket.tournamentName}" not found`);
continue;
}
// Assign the tournament's ObjectId
const tournamentId = existingTournament._id;
// Fetch the teams for the participants
const teams = await TeamModel.find().limit(bracket.participant.length); // Adjust as needed
if (teams.length < bracket.participant.length) {
console.error("Not enough teams found to seed participants");
continue;
}
// Replace participant names with valid ObjectIds
const participants = bracket.participant.map((participant, index) => ({
...participant,
id: teams[index]._id, // Use actual team ObjectIds
tournament_id: tournamentId, // Use valid tournamentId
}));
// Replace the "tournamentId" string with the actual ObjectId in stage
const stages = bracket.stage.map((stage) => ({
...stage,
id: tournamentId, // Replace with actual tournament ObjectId
tournament_id: tournamentId, // Same for tournament_id
}));
// Create the Bracket document
await BracketModel.create({
...bracket,
tournament: tournamentId, // Ensure the `tournament` field is populated correctly
participant: participants,
stage: stages,
});
console.log(
`Bracket for tournament "${bracket.tournamentName}" inserted successfully`,
);
} catch (error) {
console.error(
`Error inserting bracket for tournament "${bracket.tournamentName}":`,
error,
);
}
}
}
async function main() {
await connectToMongoDB();
await seedGames();
await seedUsers();
await seedOrganizers();
await seedTournaments();
await seedTeams();
await seedBracket();
mongoose.disconnect();
console.log("Seeding completed");
}
main().catch((error) => {
console.error("Error during seeding:", error);
process.exit(1);
});