Populate player stats when weekly player data patch is run

This commit is contained in:
Garrett Mills 2020-11-07 16:14:00 -06:00
parent 27f8f5e4dc
commit d85570600a
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
5 changed files with 26 additions and 7 deletions

View File

@ -60,6 +60,10 @@ class SeedWeeklyPlayerDataPatch extends Injectable {
})
await weekly_stat.save()
if ( week === 1 || !player.seed_stats || Object.values(player.seed_stats).length < 1 ) {
player.seed_stats = await weekly_stat.to_api()
}
} else {
this.output.warn(` - Player ID ${stat.PlayerID} does not exist.`)
}

View File

@ -43,7 +43,7 @@ class Teams extends Controller {
*/
async get_my_team_players(req, res, next) {
const players = await req.user_team.players()
return res.api(await Promise.all(players.map(x => x.to_api())))
return res.api(await Promise.all(players.map(x => x.to_api(true))))
}
/**

View File

@ -37,7 +37,7 @@ class Lineup extends Model {
let points = 0
for ( const player of starting_players ) {
points += await player.points_for_week(this.week_num)
points += (await player.points_for_week(this.week_num)).fantasy_points
}
return points
@ -188,7 +188,7 @@ class Lineup extends Model {
// Find the player instance and cast it to an API object
const player_inst = starting_players.find(x => x.id === player.player_id)
build_starting_players.push({
...(await player_inst.to_api()),
...(await player_inst.to_api(true)),
position: player.position
})
@ -214,7 +214,7 @@ class Lineup extends Model {
const build_benched_players = []
for ( const player of bench_players ) {
// Cast the starting player to an API object
const obj = await player.to_api()
const obj = await player.to_api(true)
obj.position = 'B'
build_benched_players.push(obj)
}

View File

@ -7,7 +7,7 @@ const { Model } = require('flitter-orm')
*/
class Player extends Model {
static get services() {
return [...super.services, 'output', 'models']
return [...super.services, 'output', 'models', 'sports_data']
}
/*
@ -35,6 +35,8 @@ class Player extends Model {
experience_string: String,
age: Number,
photo_url: String,
seed_stats: Object,
}
}
@ -96,7 +98,9 @@ class Player extends Model {
return false
}
async to_api() {
async to_api(with_stats = false) {
const stat = with_stats ? await this.points_for_week() : undefined
return {
id: this.id,
number: this.player_number,
@ -104,7 +108,7 @@ class Player extends Model {
position: this.fantasy_position,
team_name: this.patch_data.patch_team_name,
image: this.photo_url,
stats: {}, // TODO - populate some stats!
stats: (await stat?.to_api()) || this.seed_stats || {},
}
}
}

View File

@ -20,6 +20,17 @@ class WeeklyPlayerStat extends Model {
sacks: Number,
}
}
async to_api() {
return {
'Passing Attempts': this.passing_attempts,
'Passing Completions': this.passing_completions,
'Passing Yards': this.passing_yards,
'Fumbles': this.fumbles,
'Kick Returns': this.kick_returns,
'Sacks': this.sacks,
}
}
}
module.exports = exports = WeeklyPlayerStat