bullpen/backend/seeders/05-bullpens.js

114 lines
3.6 KiB
JavaScript

const process = require('process');
const {createPlayer, createUsers} = require("../helper/seeder.helper");
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)));
// 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.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'))
.map(pitchType => pitchType.id);
// red: 21-28
// orange: 11-18:
// green: 1-9
const bullpenChart = [
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.userId
}
});
// 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: 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} */
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',
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'
}];
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;
}
};