diff --git a/app/controllers/Scores.controller.js b/app/controllers/Scores.controller.js new file mode 100644 index 0000000..a9933c2 --- /dev/null +++ b/app/controllers/Scores.controller.js @@ -0,0 +1,24 @@ +const { Controller } = require('libflitter') + +class ScoresController extends Controller { + static get services() { + return [...super.services, 'models', 'sports_data'] + } + + 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 = [] + + 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) + } +} + +module.exports = exports = ScoresController diff --git a/app/models/Matchup.model.js b/app/models/Matchup.model.js index 1c0acb4..551edff 100644 --- a/app/models/Matchup.model.js +++ b/app/models/Matchup.model.js @@ -25,6 +25,30 @@ class Matchup extends Model { const Team = this.models.get('Team') return Team.findById(this.visitor_team_id) } + + async to_api() { + const home_team = await this.home_team() + const visitor_team = await this.visitor_team() + + const data = { + date: '2020-11-11', // TODO generate this in the matches patch + team_1: home_team.team_name, + team_1_projection: 0, + + team_2: visitor_team.team_name, + team_2_projection: 0, + } + + if ( this.complete ) { + const winner = this.home_team_score > this.visitor_team_score ? home_team : visitor_team + + data.winner = winner.team_name + data.winner_score = Math.max(this.home_team_score, this.visitor_team_score) + data.loser_score = Math.min(this.home_team_score, this.visitor_team_score) + } + + return data + } } module.exports = exports = Matchup diff --git a/app/routing/routers/api.routes.js b/app/routing/routers/api.routes.js index 9e8069f..5668d9e 100644 --- a/app/routing/routers/api.routes.js +++ b/app/routing/routers/api.routes.js @@ -52,6 +52,8 @@ const index = { '/my-team/lineup': ['controller::Teams.get_my_team_current_lineup'], '/draft-board/available': ['controller::DraftBoard.get_available_players'], + + '/matchups': ['controller::Scores.get_weekly_scores'], }, /* diff --git a/frontend/src/components/pages/Scores.component.js b/frontend/src/components/pages/Scores.component.js index 9d67ad0..58037bd 100644 --- a/frontend/src/components/pages/Scores.component.js +++ b/frontend/src/components/pages/Scores.component.js @@ -1,5 +1,6 @@ import {Component} from '../../../lib/vues6.js' import {GridCellRenderType} from '../Grid.component.js' +import {api} from '../../module/api.js' const template = `
@@ -7,7 +8,7 @@ const template = `

Matchups & Scores - Week {{ current_week }}

-
+
@@ -32,13 +33,13 @@ class ScoresComponent extends Component { * The number of the current week shown in the interface * @type {number} */ - current_week = 6 + current_week = 1 /** * Most recent week number. * @type {number} */ - max_week = 6 + max_week = 1 /** * Least recent week number. @@ -50,206 +51,7 @@ class ScoresComponent extends Component { * Array of arrays of data for each week with first item being week 1, second being week 2, &c. * @type {object[][]} */ - week_x_data = [ - // Week 1 Data - [ - { - "date": "11/2/2020", - "team_1": "Team 1", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 50, - "team_2": "Team 6", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 73 - }, - { - "date": "10/23/2020", - "team_1": "Team 2", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 66, - "team_2": "Team 5", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 71, - "winner": "Team 5", - "winner_score": "84", - "loser_score": "41", - }, - { - "date": "10/31/2020", - "team_1": "Team 3", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 85, - "team_2": "Team 4", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 67 - }, - ], - // Week 2 Data - [ - { - "date": "11/2/2020", - "team_1": "Team 1", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 58, - "team_2": "Team 6", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 34 - }, - { - "date": "10/23/2020", - "team_1": "Team 2", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 57, - "team_2": "Team 5", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 27, - "winner": "Team 5", - "winner_score": "84", - "loser_score": "41", - }, - { - "date": "10/31/2020", - "team_1": "Team 3", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 48, - "team_2": "Team 4", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 49 - }, - ], - // Week 3 Data - [ - { - "date": "11/2/2020", - "team_1": "Team 1", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 67, - "team_2": "Team 6", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 47 - }, - { - "date": "10/23/2020", - "team_1": "Team 2", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 83, - "team_2": "Team 5", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 62, - "winner": "Team 5", - "winner_score": "84", - "loser_score": "41", - }, - { - "date": "10/31/2020", - "team_1": "Team 3", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 48, - "team_2": "Team 4", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 17 - }, - ], - // Week 4 Data - [ - { - "date": "11/2/2020", - "team_1": "Team 1", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 30, - "team_2": "Team 6", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 41 - }, - { - "date": "10/23/2020", - "team_1": "Team 2", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 65, - "team_2": "Team 5", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 27, - "winner": "Team 5", - "winner_score": "84", - "loser_score": "41", - }, - { - "date": "10/31/2020", - "team_1": "Team 3", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 48, - "team_2": "Team 4", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 24 - }, - ], - // Week 5 Data - [ - { - "date": "11/2/2020", - "team_1": "Team 1", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 43, - "team_2": "Team 6", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 48 - }, - { - "date": "10/23/2020", - "team_1": "Team 2", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 57, - "team_2": "Team 5", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 61, - "winner": "Team 5", - "winner_score": "84", - "loser_score": "41", - }, - { - "date": "10/31/2020", - "team_1": "Team 3", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 48, - "team_2": "Team 4", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 91 - }, - ], - // Week 6 Data - [ - { - "date": "11/2/2020", - "team_1": "Team 1", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 50, - "team_2": "Team 6", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 37 - }, - { - "date": "10/23/2020", - "team_1": "Team 2", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 36, - "team_2": "Team 5", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 71, - "winner": "Team 5", - "winner_score": "84", - "loser_score": "41", - }, - { - "date": "10/31/2020", - "team_1": "Team 3", - "team_1_logo": "https://via.placeholder.com/150x100", - "team_1_projection": 48, - "team_2": "Team 4", - "team_2_logo": "https://via.placeholder.com/150x100", - "team_2_projection": 1 - }, - ] - ] + week_x_data = [] /** * Column definitions for the matchups grid. @@ -270,7 +72,7 @@ class ScoresComponent extends Component { key: 'team_1', renderer: (_, data) => `
- ${data.team_1} +
${data.team_1}

Projection: ${data.team_1_projection}

@@ -284,7 +86,7 @@ class ScoresComponent extends Component { key: 'team_2', renderer: (_, data) => `
- ${data.team_2} +
${data.team_2}

Projection: ${data.team_2_projection}

@@ -320,6 +122,9 @@ class ScoresComponent extends Component { * @return {Promise} */ async vue_on_create() { + this.week_x_data = await api.get_matchups() + this.max_week = this.current_week = this.week_x_data.length + this.data = this.week_x_data[this.max_week - 1]; } diff --git a/frontend/src/module/api.js b/frontend/src/module/api.js index 744f80e..b950241 100644 --- a/frontend/src/module/api.js +++ b/frontend/src/module/api.js @@ -3,6 +3,10 @@ class API { this.base_url = APP_BASE_PATH.replace('/app/', '/api/v1/') } + async get_matchups() { + return this.get_request('matchups') + } + async get_available_draft_players() { return this.get_request('draft-board/available') }