2020-11-07 22:36:43 +00:00
|
|
|
const { Controller } = require('libflitter')
|
2020-11-08 18:34:50 +00:00
|
|
|
|
2020-11-08 00:05:26 +00:00
|
|
|
/**
|
|
|
|
* ScoresController
|
|
|
|
* ----------------------------------------------------------------------
|
2020-11-08 18:34:50 +00:00
|
|
|
* This controller contains logic for handling API requests related to the
|
|
|
|
* weekly scores and matchups endpoints.
|
|
|
|
*
|
|
|
|
* @extends Controller
|
2020-11-08 00:05:26 +00:00
|
|
|
*/
|
2020-11-07 22:36:43 +00:00
|
|
|
class ScoresController extends Controller {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models', 'sports_data']
|
|
|
|
}
|
|
|
|
|
2020-11-08 00:05:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the weekly scores
|
|
|
|
* @param req
|
|
|
|
* @param res
|
2020-11-08 20:35:27 +00:00
|
|
|
* @param next
|
|
|
|
* @returns Promise<void>
|
2020-11-08 00:05:26 +00:00
|
|
|
*/
|
2020-11-07 22:36:43 +00:00
|
|
|
async get_weekly_scores(req, res, next) {
|
|
|
|
const Matchup = this.models.get('Matchup')
|
|
|
|
|
|
|
|
const current_week = await this.sports_data.current_play_week()
|
|
|
|
const weekly_data = []
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
// Convert all of the matchup instances to API format for each week
|
2020-11-07 22:36:43 +00:00
|
|
|
for ( let i = 1; i <= current_week; i += 1 ) {
|
|
|
|
const matchups = await Matchup.find({ week_num: i })
|
|
|
|
const api_data = await Promise.all(matchups.map(x => x.to_api()))
|
|
|
|
weekly_data.push(api_data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.api(weekly_data)
|
|
|
|
}
|
2020-11-08 01:08:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the league standings with calculated stats
|
|
|
|
* @param req
|
|
|
|
* @param res
|
|
|
|
* @param next
|
|
|
|
* @return {Promise<Array<object>>}
|
|
|
|
*/
|
|
|
|
async get_league_standings(req, res, next) {
|
|
|
|
const Team = this.models.get('Team')
|
|
|
|
const all_teams = await Team.find()
|
|
|
|
const stat_records = []
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
// Generate the cumulative team data for all teams
|
2020-11-08 01:08:46 +00:00
|
|
|
for ( const team of all_teams ) {
|
|
|
|
const rec = await team.cumulative_data()
|
|
|
|
rec.team_name = team.team_name
|
|
|
|
stat_records.push(rec)
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
// Sort the teams by number of wins, then number of points scored
|
2020-11-08 01:08:46 +00:00
|
|
|
stat_records.sort((a, b) => {
|
|
|
|
if ( a.wins === b.wins ) {
|
|
|
|
return a.points_scored - b.points_scored
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.wins > b.wins ? 1 : -1
|
|
|
|
})
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
// Return the records in a format compatible with the front-end
|
2020-11-08 01:08:46 +00:00
|
|
|
return res.api(stat_records.map((x, i) => {
|
|
|
|
return {
|
|
|
|
standing: {
|
|
|
|
rank: i + 1,
|
|
|
|
win_loss: `${x.wins}/${x.losses}`,
|
|
|
|
},
|
|
|
|
team_name: x.team_name,
|
|
|
|
stats: [
|
|
|
|
{ name: 'Total Points Scored', value: x.points_scored },
|
|
|
|
{ name: 'Total Points Allowed', value: x.points_allowed },
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
2020-11-07 22:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = ScoresController
|