32273/backend/src/db/seeders/20231127130745-sample-data.js
2025-06-16 12:11:34 +00:00

291 lines
6.3 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Beatmaps = db.beatmaps;
const Games = db.games;
const Leaderboards = db.leaderboards;
const BeatmapsData = [
{
name: 'Fast Beats',
// type code here for "relation_one" field
// type code here for "files" field
},
{
name: 'Chill Vibes',
// type code here for "relation_one" field
// type code here for "files" field
},
{
name: 'Intense Rhythm',
// type code here for "relation_one" field
// type code here for "files" field
},
{
name: 'Smooth Jazz',
// type code here for "relation_one" field
// type code here for "files" field
},
];
const GamesData = [
{
title: 'Epic Beat Battle',
start_time: new Date('2023-11-01T14:00:00Z'),
end_time: new Date('2023-11-01T15:00:00Z'),
// type code here for "relation_many" field
},
{
title: 'Rhythm Challenge',
start_time: new Date('2023-11-02T16:00:00Z'),
end_time: new Date('2023-11-02T17:00:00Z'),
// type code here for "relation_many" field
},
{
title: 'Speed Run',
start_time: new Date('2023-11-03T18:00:00Z'),
end_time: new Date('2023-11-03T19:00:00Z'),
// type code here for "relation_many" field
},
{
title: 'Night Owl Session',
start_time: new Date('2023-11-04T20:00:00Z'),
end_time: new Date('2023-11-04T21:00:00Z'),
// type code here for "relation_many" field
},
];
const LeaderboardsData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
score: 9500,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
score: 8700,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
score: 9200,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
score: 8800,
},
];
// Similar logic for "relation_many"
async function associateBeatmapWithCreator() {
const relatedCreator0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Beatmap0 = await Beatmaps.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Beatmap0?.setCreator) {
await Beatmap0.setCreator(relatedCreator0);
}
const relatedCreator1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Beatmap1 = await Beatmaps.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Beatmap1?.setCreator) {
await Beatmap1.setCreator(relatedCreator1);
}
const relatedCreator2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Beatmap2 = await Beatmaps.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Beatmap2?.setCreator) {
await Beatmap2.setCreator(relatedCreator2);
}
const relatedCreator3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Beatmap3 = await Beatmaps.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Beatmap3?.setCreator) {
await Beatmap3.setCreator(relatedCreator3);
}
}
// Similar logic for "relation_many"
async function associateLeaderboardWithGame() {
const relatedGame0 = await Games.findOne({
offset: Math.floor(Math.random() * (await Games.count())),
});
const Leaderboard0 = await Leaderboards.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Leaderboard0?.setGame) {
await Leaderboard0.setGame(relatedGame0);
}
const relatedGame1 = await Games.findOne({
offset: Math.floor(Math.random() * (await Games.count())),
});
const Leaderboard1 = await Leaderboards.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Leaderboard1?.setGame) {
await Leaderboard1.setGame(relatedGame1);
}
const relatedGame2 = await Games.findOne({
offset: Math.floor(Math.random() * (await Games.count())),
});
const Leaderboard2 = await Leaderboards.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Leaderboard2?.setGame) {
await Leaderboard2.setGame(relatedGame2);
}
const relatedGame3 = await Games.findOne({
offset: Math.floor(Math.random() * (await Games.count())),
});
const Leaderboard3 = await Leaderboards.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Leaderboard3?.setGame) {
await Leaderboard3.setGame(relatedGame3);
}
}
async function associateLeaderboardWithPlayer() {
const relatedPlayer0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Leaderboard0 = await Leaderboards.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Leaderboard0?.setPlayer) {
await Leaderboard0.setPlayer(relatedPlayer0);
}
const relatedPlayer1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Leaderboard1 = await Leaderboards.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Leaderboard1?.setPlayer) {
await Leaderboard1.setPlayer(relatedPlayer1);
}
const relatedPlayer2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Leaderboard2 = await Leaderboards.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Leaderboard2?.setPlayer) {
await Leaderboard2.setPlayer(relatedPlayer2);
}
const relatedPlayer3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Leaderboard3 = await Leaderboards.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Leaderboard3?.setPlayer) {
await Leaderboard3.setPlayer(relatedPlayer3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Beatmaps.bulkCreate(BeatmapsData);
await Games.bulkCreate(GamesData);
await Leaderboards.bulkCreate(LeaderboardsData);
await Promise.all([
// Similar logic for "relation_many"
await associateBeatmapWithCreator(),
// Similar logic for "relation_many"
await associateLeaderboardWithGame(),
await associateLeaderboardWithPlayer(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('beatmaps', null, {});
await queryInterface.bulkDelete('games', null, {});
await queryInterface.bulkDelete('leaderboards', null, {});
},
};