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 calculateRates = (pitches) => { const bullpenPitches = pitches.reduce((acc, pitch) => { const { bullpenSessionId } = pitch; if (!acc[bullpenSessionId]) { acc[bullpenSessionId] = []; } acc[bullpenSessionId].push(pitch); return acc; }, {}); const totalPitches = bullpenPitches.length; if (totalPitches === 0) return { precisionRate: 0, strikeRate: 0, ballRate: 0, fastBallRate: 0, offSpeedRate: 0 }; const counts = bullpenPitches.reduce((acc, pitch) => { // Check if it's a strike (hitArea 1-9) const isStrike = pitch.hitArea >= 1 && pitch.hitArea <= 9; // Update counts based on conditions acc.precision += pitch.aimedArea === pitch.hitArea ? 1 : 0; acc.strikes += isStrike ? 1 : 0; if (isStrike) { if (pitch.pitchTypeId === 1) { acc.fastballs += 1; } else { acc.offspeed += 1; } } return acc; }, { precision: 0, strikes: 0, fastballs: 0, offspeed: 0 }); return { precisionRate: (counts.precision / totalPitches) * 100, strikeRate: (counts.strikes / totalPitches) * 100, ballRate: ((totalPitches - counts.strikes) / totalPitches) * 100, fastBallRate: (counts.fastballs / totalPitches) * 100, offSpeedRate: (counts.offspeed / totalPitches) * 100 }; } 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(), precisionRate: 0, strikeRate: 0, ballRate: 0, fastBallRate: 0, offSpeedRate: 0 } }) 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()); const rates = calculateRates(pitches); await queryInterface.update(null, 'BullpenSessions', bullpens.map(bullpen => {})) } /** @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; } };