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 {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 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 days = 5 + key;
const startedAt = new Date(new Date().setDate(startDate.getDate() - days));
return {
pitcherId: demoPlayer.user.id,
startedAt: new Date(new Date().setDate(startDate.getDate()-5)),
finishedAt: new Date(new Date().setDate(startDate.getDate()-5)),
pitcherId: demoPlayer.userId,
startedAt: startedAt,
finishedAt: new Date(new Date().setTime(startedAt.getTime() + (10 * 60000))),
createdAt: new Date(),
updatedAt: new Date()
}
})
await queryInterface.bulkInsert('BullpenSessions', bullpens);
const pitchTypeIds = await queryInterface
.select(null, 'PitchTypes')
const pitchTypeIds = (await queryInterface
.select(null, 'PitchTypes'))
.map(pitchType => pitchType.id);
// red: 21-28
// orange: 11-18:
// green: 1-9
const bullpenChart = [
[21,22,23,24,25,26,27,28],
[11,12,13,14,15,16,17,18],
[1,2,3,4,5,6,7,8,9]
]
1,2,3,4,5,6,7,8,9,
11,12,13,14,15,16,17,18,
21,22,23,24,25,26,27,28
];
const dbBullpens = await queryInterface.select(null, 'BullpenSessions', {
where: {
pitcherId: demoPlayer.user.id
pitcherId: demoPlayer.userId
}
});
[...Array(20).keys()].map(key => {
// fill pitches for each bullpen
const pitches = dbBullpens.map(bullpen => {
return [...Array(20).keys()].map(key => {
const aimedArea = pickRandomValueFromArray(bullpenChart);
const hitArea = shouldMatch(30) ? aimedArea : pickRandomValueFromArray(bullpenChart);
return {
pitchTypeId: pitchTypeIds[Math.floor(Math.random() * pitchTypeIds.length)],
pitchTime: new Date(2025, 3, 22, 16, 7, 21),
aimedArea: 11,
hitArea: 12
}
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} */
@ -50,6 +71,14 @@ module.exports = {
async up (queryInterface, /*Sequelize*/) {
if (process.env.NODE_ENV !== 'development') return;
let demoPlayer = (await queryInterface.select(null, 'Players', {
where: {
height: 187,
weight: 85,
}
})).shift();
if (demoPlayer === undefined) {
const roles = await queryInterface.select(null, 'Roles');
const players = [{
email: 'demo@bullpen.com',
@ -66,18 +95,19 @@ module.exports = {
state: 'active'
}];
const dbPlayers = await createPlayer(queryInterface, roles, players);
const demoPlayer = dbPlayers
.filter(player => player.user.auth.email === 'demo@bullpen.com').shift();
for (let i = 0; i < 10; ++i) {
createBullpen(queryInterface, demoPlayer);
await createPlayer(queryInterface, roles, players);
demoPlayer = (await queryInterface.select(null, 'Players', {
where: {
height: 187,
weight: 85,
}
})).shift();
}
await createBullpens(queryInterface, demoPlayer);
},
async down (queryInterface, /*Sequelize*/) {
if (process.env.NODE_ENV !== 'development') return;
return;
async down (/*queryInterface,*/ /*Sequelize*/) {
// if (process.env.NODE_ENV !== 'development') return;
}
};