2020-11-05 04:57:22 +00:00
|
|
|
const { Service } = require('flitter-di')
|
|
|
|
const axios = require('axios').default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A service class for interacting with data from the SportsDataIO API.
|
2020-11-08 18:34:50 +00:00
|
|
|
* @extends Service
|
2020-11-05 04:57:22 +00:00
|
|
|
*/
|
|
|
|
class SportsDataService extends Service {
|
|
|
|
static get services() {
|
2020-11-07 18:10:19 +00:00
|
|
|
return [...super.services, 'configs', 'models', 'utility']
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
/**
|
|
|
|
* Resolves true if the game is currently in the draft stage.
|
|
|
|
* @return {Promise<boolean>}
|
|
|
|
*/
|
2020-11-07 18:10:19 +00:00
|
|
|
async is_draft_stage() {
|
|
|
|
const Setting = this.models.get('models::setting')
|
2020-11-07 19:02:37 +00:00
|
|
|
return this.utility.infer(await Setting.get('in_draft_stage'))
|
2020-11-07 18:10:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
/**
|
|
|
|
* Resolves to the current week number of gameplay.
|
|
|
|
* @return {Promise<number>}
|
|
|
|
*/
|
2020-11-07 18:10:19 +00:00
|
|
|
async current_play_week() {
|
|
|
|
const Setting = this.models.get('models::setting')
|
2020-11-07 19:02:37 +00:00
|
|
|
return this.utility.infer(await Setting.get('current_week'))
|
2020-11-05 04:57:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
/**
|
|
|
|
* Fetches a list of all players on the given team from the sports data API.
|
|
|
|
* @param {string} team_key
|
|
|
|
* @return {Promise<Array<any>>}
|
|
|
|
*/
|
2020-11-05 04:57:22 +00:00
|
|
|
async get_team_players(team_key) {
|
|
|
|
return this.get_request(`Players/${team_key}`)
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
/**
|
|
|
|
* Fetches a list of all active teams from the sports data API.
|
|
|
|
* @return {Promise<Array<any>>}
|
|
|
|
*/
|
2020-11-05 04:57:22 +00:00
|
|
|
async get_active_teams() {
|
|
|
|
return this.get_request('Teams')
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
/**
|
|
|
|
* Make a get request to the sports data API.
|
|
|
|
* @param {string} path
|
|
|
|
* @param {string} [base = 'scores'] - the API domain (scores, projections, &c.)
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
2020-11-07 18:10:19 +00:00
|
|
|
async get_request(path, base = 'scores') {
|
|
|
|
const response = await axios.get(this.url(path, base))
|
2020-11-05 04:57:22 +00:00
|
|
|
return response.data
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
/**
|
|
|
|
* Fetches a list of player stats for all players in the league for the given week.
|
|
|
|
* @param {number} week_num
|
2020-11-08 20:35:27 +00:00
|
|
|
* @return {Promise<Array<any>>}
|
2020-11-08 18:34:50 +00:00
|
|
|
*/
|
2020-11-07 18:10:19 +00:00
|
|
|
async get_week_player_stats(week_num) {
|
|
|
|
return this.get_request(`PlayerGameProjectionStatsByWeek/${this.configs.get('server.sports_data.season')}/${week_num}`, 'projections')
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:34:50 +00:00
|
|
|
/**
|
|
|
|
* Resolve an endpoint and an API domain to a fully-qualified URL to the sports data API.
|
|
|
|
* @param {string} path
|
|
|
|
* @param {string} [base = 'scores'] - the API domain (scores, projections, &c.)
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2020-11-07 18:10:19 +00:00
|
|
|
url(path, base = 'scores') {
|
2020-11-05 04:57:22 +00:00
|
|
|
if ( path.startsWith('/') ) path = path.slice(1)
|
2020-11-07 18:10:19 +00:00
|
|
|
return `https://api.sportsdata.io/v3/nfl/${base}/json/${path}?key=${this.configs.get('server.sports_data.api_key')}`
|
2020-11-05 04:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = SportsDataService
|