Add logic to calculate league standings and hook up to league standings page

master
Garrett Mills 4 years ago
parent 12ff8c680f
commit 2241779f5d
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

@ -30,6 +30,47 @@ class ScoresController extends Controller {
return res.api(weekly_data)
}
/**
* 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 = []
for ( const team of all_teams ) {
const rec = await team.cumulative_data()
rec.team_name = team.team_name
stat_records.push(rec)
}
stat_records.sort((a, b) => {
if ( a.wins === b.wins ) {
return a.points_scored - b.points_scored
}
return a.wins > b.wins ? 1 : -1
})
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 },
],
}
}))
}
}
module.exports = exports = ScoresController

@ -59,7 +59,7 @@ class Team extends Model {
}
/**
*
* Returns the players associated with the team.
*/
async players() {
const Player = this.models.get('Player')
@ -70,6 +70,51 @@ class Team extends Model {
})
}
/**
* Get the cumulative data for the team (total wins, losses, points scored & allowed)
* @return object
*/
async cumulative_data() {
const Matchup = this.models.get('Matchup')
const home_matchups = await Matchup.find({ home_team_id: this.id })
const visitor_matchups = await Matchup.find({ visitor_team_id: this.id })
const data = {
wins: 0,
losses: 0,
points_scored: 0,
points_allowed: 0,
}
for ( const matchup of home_matchups ) {
if ( !matchup.complete ) continue
data.points_scored += matchup.home_team_score
data.points_allowed += matchup.visitor_team_score
if ( matchup.home_team_score > matchup.visitor_team_score ) {
data.wins += 1
} else {
data.losses += 0
}
}
for ( const matchup of visitor_matchups ) {
if ( !matchup.complete ) continue
data.points_scored += matchup.visitor_team_score
data.points_allowed += matchup.home_team_score
if ( matchup.visitor_team_score > matchup.home_team_score ) {
data.wins += 1
} else {
data.losses += 0
}
}
return data
}
/**
* updates the API's data
*/

@ -54,6 +54,7 @@ const index = {
'/draft-board/available': ['controller::DraftBoard.get_available_players'],
'/matchups': ['controller::Scores.get_weekly_scores'],
'/league-standings': ['controller::Scores.get_league_standings'],
},
/*

File diff suppressed because one or more lines are too long

@ -3,6 +3,10 @@ class API {
this.base_url = APP_BASE_PATH.replace('/app/', '/api/v1/')
}
async get_standings() {
return this.get_request('league-standings')
}
async get_matchups() {
return this.get_request('matchups')
}

Loading…
Cancel
Save