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
const pitches = dbBullpens.map(bullpen => {
return [...Array(20).keys()].map(key => {
const aimedArea = pickRandomValueFromArray(bullpenChart);
const hitArea = shouldMatch(30) ? aimedArea : pickRandomValueFromArray(bullpenChart);
return { return {
pitchTypeId: pitchTypeIds[Math.floor(Math.random() * pitchTypeIds.length)], pitchTypeId: pickRandomValueFromArray(pitchTypeIds),
pitchTime: new Date(2025, 3, 22, 16, 7, 21), bullpenSessionId: bullpen.id,
aimedArea: 11, pitchTime: new Date(new Date().setTime(bullpen.startedAt.getTime() + key * 60000)),
hitArea: 12 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,6 +71,14 @@ module.exports = {
async up (queryInterface, /*Sequelize*/) { async up (queryInterface, /*Sequelize*/) {
if (process.env.NODE_ENV !== 'development') return; 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 roles = await queryInterface.select(null, 'Roles');
const players = [{ const players = [{
email: 'demo@bullpen.com', email: 'demo@bullpen.com',
@ -66,18 +95,19 @@ module.exports = {
state: 'active' state: 'active'
}]; }];
const dbPlayers = await createPlayer(queryInterface, roles, players); await createPlayer(queryInterface, roles, players);
const demoPlayer = dbPlayers demoPlayer = (await queryInterface.select(null, 'Players', {
.filter(player => player.user.auth.email === 'demo@bullpen.com').shift(); where: {
height: 187,
for (let i = 0; i < 10; ++i) { weight: 85,
createBullpen(queryInterface, demoPlayer);
} }
})).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;
} }
}; };