Write patch for generating weekly matchup results
This commit is contained in:
parent
feadc6201d
commit
eee8d9a6a3
36
app/GenerateWeeklyResults.patch.js
Normal file
36
app/GenerateWeeklyResults.patch.js
Normal file
@ -0,0 +1,36 @@
|
||||
const { Injectable } = require('flitter-di')
|
||||
|
||||
class GenerateWeeklyResultsPatch extends Injectable {
|
||||
static get services() {
|
||||
return [...super.services, 'models', 'sports_data']
|
||||
}
|
||||
|
||||
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
|
@ -32,6 +32,17 @@ class Lineup extends Model {
|
||||
}
|
||||
}
|
||||
|
||||
async calculate_fantasy_points() {
|
||||
const starting_players = await this.players_in_starting()
|
||||
let points = 0
|
||||
|
||||
for ( const player of starting_players ) {
|
||||
points += await player.points_for_week(this.week_num)
|
||||
}
|
||||
|
||||
return points
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a team, fetch the latest draft lineup for that team.
|
||||
* This will also update the lineup record so that it includes all players
|
||||
|
@ -15,6 +15,16 @@ class Matchup extends Model {
|
||||
visitor_team_score: Number,
|
||||
}
|
||||
}
|
||||
|
||||
async home_team() {
|
||||
const Team = this.models.get('Team')
|
||||
return Team.findById(this.home_team_id)
|
||||
}
|
||||
|
||||
async visitor_team() {
|
||||
const Team = this.models.get('Team')
|
||||
return Team.findById(this.visitor_team_id)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = Matchup
|
||||
|
@ -81,6 +81,11 @@ class Player extends Model {
|
||||
})
|
||||
}
|
||||
|
||||
async points_for_week(week_num) {
|
||||
const WeeklyPlayerStat = this.models.get('WeeklyPlayerStat')
|
||||
return WeeklyPlayerStat.findOne({ week_num, player_id: this.id })
|
||||
}
|
||||
|
||||
async is_obligated() {
|
||||
const Team = this.models.get('Team')
|
||||
const teams = await Team.find()
|
||||
|
Loading…
Reference in New Issue
Block a user