2020-11-07 19:22:15 +00:00
|
|
|
const { Injectable } = require('flitter-di')
|
|
|
|
|
2020-11-08 01:49:32 +00:00
|
|
|
/**
|
|
|
|
* GenerateWeeklyResultsPatch
|
|
|
|
* ----------------------------------------------------------------------------
|
2020-11-08 18:34:50 +00:00
|
|
|
* A patch which generates the weekly team results using the player stat data
|
|
|
|
* for teh currently configured play week.
|
|
|
|
*
|
|
|
|
* @extends Injectable
|
2020-11-08 01:49:32 +00:00
|
|
|
*/
|
2020-11-07 19:22:15 +00:00
|
|
|
class GenerateWeeklyResultsPatch extends Injectable {
|
|
|
|
static get services() {
|
2020-11-09 02:10:41 +00:00
|
|
|
return [...super.services, 'models', 'sports_data', 'output']
|
2020-11-07 19:22:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 01:49:32 +00:00
|
|
|
/**
|
|
|
|
* Runs the patch
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2020-11-07 19:22:15 +00:00
|
|
|
async run() {
|
|
|
|
const Matchup = this.models.get('Matchup')
|
|
|
|
const current_week = await this.sports_data.current_play_week()
|
|
|
|
|
|
|
|
const week_matchups = await Matchup.find({ week_num: current_week })
|
|
|
|
this.output.info(`Processing ${week_matchups.length} matchups...`)
|
|
|
|
|
|
|
|
for ( const matchup of week_matchups ) {
|
|
|
|
const visitor_team = await matchup.visitor_team()
|
|
|
|
const home_team = await matchup.home_team()
|
|
|
|
|
|
|
|
const visitor_lineup = await visitor_team.lineup()
|
|
|
|
const home_lineup = await home_team.lineup()
|
|
|
|
|
|
|
|
matchup.visitor_team_score = await visitor_lineup.calculate_fantasy_points()
|
|
|
|
matchup.home_team_score = await home_lineup.calculate_fantasy_points()
|
|
|
|
matchup.complete = true
|
|
|
|
|
|
|
|
this.output.success(`Scored matchup ${matchup.id}`)
|
|
|
|
this.output.info(` Team ${home_team.id}: ${matchup.home_team_score}`)
|
|
|
|
this.output.info(` Team ${visitor_team.id}: ${matchup.visitor_team_score}`)
|
|
|
|
await matchup.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.output.info('Finished scoring match-ups.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = GenerateWeeklyResultsPatch
|