Comment all the things!

This commit is contained in:
2020-11-08 12:34:50 -06:00
parent 72f3923866
commit 06515b0559
73 changed files with 269 additions and 162 deletions

View File

@@ -1,8 +1,12 @@
const { Controller } = require('libflitter')
/**
* DraftBoard controller
* @extends Controller
* ------------------------------------------------------------------------
* This controller contains logic for handling API requests related to fetching
* and drafting available players. Its methods should handle Express requests &
* responses.
*
* @extends Controller
*/
class DraftBoard extends Controller {
static get services() {
@@ -42,6 +46,7 @@ class DraftBoard extends Controller {
.api()
}
// look up the player specified in the request
const Player = this.models.get('Player')
const player = await Player.findById(req.body.player_id)
if ( !player ) {
@@ -50,6 +55,7 @@ class DraftBoard extends Controller {
.api()
}
// Don't allow drafting already-drafted players
if ( await player.is_obligated() ) {
return res.status(400)
.message('This player has already been drafted.')

View File

@@ -1,18 +1,22 @@
const { Controller } = require('libflitter')
/*
/**
* Home Controller
* -------------------------------------------------------------
* Controller for the main homepage of this Flitter app. Methods here
* are used as handlers for routes specified in the route files.
*
* @extends Controller
*/
class Home extends Controller {
static get services() {
return [...super.services, 'sports_data']
}
/*
/**
* Serve the main welcome page.
* @param req
* @param res
*/
welcome(req, res){
if ( req.user ) {
@@ -22,6 +26,14 @@ class Home extends Controller {
}
}
/**
* Return the current session's status (including team information and
* information about the current stage of gameplay).
* @param req
* @param res
* @param next
* @return {Promise<*>}
*/
async get_status(req, res, next) {
return res.api({
team_id: req.user_team.id,

View File

@@ -1,8 +1,12 @@
const { Controller } = require('libflitter')
/**
* ScoresController
* @extends Controller
* ----------------------------------------------------------------------
* This controller contains logic for handling API requests related to the
* weekly scores and matchups endpoints.
*
* @extends Controller
*/
class ScoresController extends Controller {
static get services() {
@@ -22,6 +26,7 @@ class ScoresController extends Controller {
const current_week = await this.sports_data.current_play_week()
const weekly_data = []
// Convert all of the matchup instances to API format for each week
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()))
@@ -43,12 +48,14 @@ class ScoresController extends Controller {
const all_teams = await Team.find()
const stat_records = []
// Generate the cumulative team data for all teams
for ( const team of all_teams ) {
const rec = await team.cumulative_data()
rec.team_name = team.team_name
stat_records.push(rec)
}
// Sort the teams by number of wins, then number of points scored
stat_records.sort((a, b) => {
if ( a.wins === b.wins ) {
return a.points_scored - b.points_scored
@@ -57,6 +64,7 @@ class ScoresController extends Controller {
return a.wins > b.wins ? 1 : -1
})
// Return the records in a format compatible with the front-end
return res.api(stat_records.map((x, i) => {
return {
standing: {

View File

@@ -1,8 +1,12 @@
const { Controller } = require('libflitter')
/*
/**
* Teams Controller
* -------------------------------------------------------------
* This controller contains logic related to viewing and managing
* the user's team, team lineups, and team players.
*
* @extends Controller
*/
class Teams extends Controller {
static get services() {
@@ -78,10 +82,12 @@ class Teams extends Controller {
.api()
}
// fetch the team players & the current lineup
const player_ids = (await req.user_team.players()).map(x => x.id)
const lineup = await req.user_team.lineup()
lineup.clear_lineup()
// Add all the starting players to the lineup
for ( const player of req.body.starting_players ) {
if ( !player.id || !player.position ) continue;
@@ -90,6 +96,7 @@ class Teams extends Controller {
position: player.position,
}
// Don't allow adding other teams' players to the lineup
if ( !player_ids.includes(lineup_record.player_id) ) {
return res.status(400)
.message(`Sorry, the player ${lineup_record.player_id} is not on your team.`)
@@ -99,6 +106,7 @@ class Teams extends Controller {
lineup.start_player(lineup_record)
}
// Bench all the other players
for ( const player of req.body.benched_players ) {
if ( !player.id ) continue;
@@ -111,18 +119,12 @@ class Teams extends Controller {
lineup.bench_player(player)
}
console.log('pre save', lineup)
// Save the partial lineup
await lineup.save()
console.log('post save', lineup)
// Fetch a fresh version to fill in any missing players
const corrected_lineup = await req.user_team.lineup()
console.log('corrected', corrected_lineup)
return res.api(await corrected_lineup.to_api())
}

View File

@@ -4,6 +4,8 @@ const FormController = require('flitter-auth/controllers/Forms')
* Handles views and processing for auth registration/login/logout/etc.
* Most handlers are inherited from the default flitter-auth/controllers/Forms
* controller, however you can override them here as you need.
*
* This file was auto-generated by the Flitter framework.
*/
class Forms extends FormController {

View File

@@ -8,6 +8,8 @@ const Controller = require('flitter-auth/controllers/KeyAction')
* one-time links that call methods on controllers and (optionally)
* can even automatically sign in a user for the request, then log
* them out. e.g. a password reset link could use a key action.
*
* This file was auto-generated by the Flitter framework.
*/
class KeyAction extends Controller {

View File

@@ -5,6 +5,8 @@ const Oauth2Controller = require('flitter-auth/controllers/Oauth2')
* built-in OAuth2 server, if it is enabled. Most handlers are inherited
* from flitter-auth/controllers/Oauth2, but you can override them here
* as you need.
*
* This file was auto-generated by the Flitter framework.
*/
class Oauth2 extends Oauth2Controller {