added bullpen seeder

This commit is contained in:
Sascha Kühl 2025-06-10 07:06:30 +02:00
parent 1719473dcc
commit 2e1370392e
1 changed files with 74 additions and 44 deletions

View File

@ -1,48 +1,69 @@
const process = require('process'); const process = require('process');
const {createPlayer, createUsers} = require("../helper/seeder.helper"); const {createPlayer, createUsers} = require("../helper/seeder.helper");
const createBullpen = async (queryInterface, demoPlayer) => {
const pickRandomValueFromArray = (values) => {
return values[Math.floor(Math.random() * values.length)];
}
const shouldMatch = (percentage) => {
return Math.random() * 100 < percentage;
}
const createBullpens = async (queryInterface, demoPlayer) => {
const startDate = new Date(); const startDate = new Date();
const endDate = new Date(new Date().setTime(startDate.getTime() + 10 * 60000)); // const endDate = new Date(new Date().setTime(startDate.getTime() + (10 * 60000)));
// create 20 bullpens for demo player
const bullpens = [...Array(20).keys()].map(key => { const bullpens = [...Array(20).keys()].map(key => {
const days = 5 + key;
const startedAt = new Date(new Date().setDate(startDate.getDate() - days));
return { return {
pitcherId: demoPlayer.user.id, pitcherId: demoPlayer.userId,
startedAt: new Date(new Date().setDate(startDate.getDate()-5)), startedAt: startedAt,
finishedAt: new Date(new Date().setDate(startDate.getDate()-5)), finishedAt: new Date(new Date().setTime(startedAt.getTime() + (10 * 60000))),
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date() updatedAt: new Date()
} }
}) })
await queryInterface.bulkInsert('BullpenSessions', bullpens); await queryInterface.bulkInsert('BullpenSessions', bullpens);
const pitchTypeIds = await queryInterface const pitchTypeIds = (await queryInterface
.select(null, 'PitchTypes') .select(null, 'PitchTypes'))
.map(pitchType => pitchType.id); .map(pitchType => pitchType.id);
// red: 21-28 // red: 21-28
// orange: 11-18: // orange: 11-18:
// green: 1-9 // green: 1-9
const bullpenChart = [ const bullpenChart = [
[21,22,23,24,25,26,27,28], 1,2,3,4,5,6,7,8,9,
[11,12,13,14,15,16,17,18], 11,12,13,14,15,16,17,18,
[1,2,3,4,5,6,7,8,9] 21,22,23,24,25,26,27,28
] ];
const dbBullpens = await queryInterface.select(null, 'BullpenSessions', { const dbBullpens = await queryInterface.select(null, 'BullpenSessions', {
where: { where: {
pitcherId: demoPlayer.user.id pitcherId: demoPlayer.userId
} }
}); });
[...Array(20).keys()].map(key => { // fill pitches for each bullpen
return { const pitches = dbBullpens.map(bullpen => {
pitchTypeId: pitchTypeIds[Math.floor(Math.random() * pitchTypeIds.length)], return [...Array(20).keys()].map(key => {
pitchTime: new Date(2025, 3, 22, 16, 7, 21), const aimedArea = pickRandomValueFromArray(bullpenChart);
aimedArea: 11, const hitArea = shouldMatch(30) ? aimedArea : pickRandomValueFromArray(bullpenChart);
hitArea: 12 return {
} pitchTypeId: pickRandomValueFromArray(pitchTypeIds),
bullpenSessionId: bullpen.id,
pitchTime: new Date(new Date().setTime(bullpen.startedAt.getTime() + key * 60000)),
aimedArea: aimedArea,
hitArea: hitArea,
createdAt: new Date(),
updatedAt: new Date()
};
});
}); });
await queryInterface.bulkInsert('Pitches', pitches.flat());
} }
/** @type {import('sequelize-cli').Migration} */ /** @type {import('sequelize-cli').Migration} */
@ -50,34 +71,43 @@ module.exports = {
async up (queryInterface, /*Sequelize*/) { async up (queryInterface, /*Sequelize*/) {
if (process.env.NODE_ENV !== 'development') return; if (process.env.NODE_ENV !== 'development') return;
const roles = await queryInterface.select(null, 'Roles'); let demoPlayer = (await queryInterface.select(null, 'Players', {
const players = [{ where: {
email: 'demo@bullpen.com', height: 187,
password: 'demo$123', weight: 85,
firstName: 'Demo', }
lastName: 'Player', })).shift();
dateOfBirth: new Date(1975, 6, 19),
jerseyNumber: 33,
height: 187,
weight: 85,
gender: 'male',
bats: 'right',
throws: 'right',
state: 'active'
}];
const dbPlayers = await createPlayer(queryInterface, roles, players); if (demoPlayer === undefined) {
const demoPlayer = dbPlayers const roles = await queryInterface.select(null, 'Roles');
.filter(player => player.user.auth.email === 'demo@bullpen.com').shift(); const players = [{
email: 'demo@bullpen.com',
password: 'demo$123',
firstName: 'Demo',
lastName: 'Player',
dateOfBirth: new Date(1975, 6, 19),
jerseyNumber: 33,
height: 187,
weight: 85,
gender: 'male',
bats: 'right',
throws: 'right',
state: 'active'
}];
for (let i = 0; i < 10; ++i) { await createPlayer(queryInterface, roles, players);
createBullpen(queryInterface, demoPlayer); demoPlayer = (await queryInterface.select(null, 'Players', {
where: {
height: 187,
weight: 85,
}
})).shift();
} }
await createBullpens(queryInterface, demoPlayer);
}, },
async down (queryInterface, /*Sequelize*/) { async down (/*queryInterface,*/ /*Sequelize*/) {
if (process.env.NODE_ENV !== 'development') return; // if (process.env.NODE_ENV !== 'development') return;
return;
} }
}; };