2020-11-05 03:18:47 +00:00
|
|
|
const { Model } = require('flitter-orm')
|
|
|
|
|
2020-11-05 04:57:22 +00:00
|
|
|
|
2020-11-05 03:18:47 +00:00
|
|
|
/*
|
|
|
|
* Player Model
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class Player extends Model {
|
|
|
|
static get services() {
|
2020-11-07 22:14:00 +00:00
|
|
|
return [...super.services, 'output', 'models', 'sports_data']
|
2020-11-05 03:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define the flitter-orm schema of the model.
|
|
|
|
*/
|
|
|
|
static get schema() {
|
|
|
|
return {
|
2020-11-05 04:57:22 +00:00
|
|
|
patch_data: {
|
|
|
|
patch_team_id: Number,
|
|
|
|
patch_team_name: String,
|
|
|
|
patch_team_key: String,
|
|
|
|
player_id: Number,
|
|
|
|
draft_position: Number,
|
|
|
|
},
|
|
|
|
player_number: Number,
|
|
|
|
first_name: String,
|
|
|
|
last_name: String,
|
|
|
|
full_name: String,
|
|
|
|
position: String,
|
|
|
|
fantasy_position: String,
|
|
|
|
height: String,
|
|
|
|
weight: Number,
|
|
|
|
birthday: String,
|
|
|
|
experience: String,
|
|
|
|
experience_string: String,
|
|
|
|
age: Number,
|
|
|
|
photo_url: String,
|
2020-11-07 22:14:00 +00:00
|
|
|
|
|
|
|
seed_stats: Object,
|
2020-11-05 03:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 04:57:22 +00:00
|
|
|
static from_patch_data(data) {
|
|
|
|
const model_data = {
|
|
|
|
patch_data: {
|
|
|
|
patch_team_id: data.TeamID,
|
|
|
|
// patch_team_name,
|
|
|
|
// patch_team_key,
|
|
|
|
player_id: data.PlayerID,
|
|
|
|
draft_position: data.AverageDraftPosition,
|
|
|
|
},
|
|
|
|
player_number: data.Number,
|
|
|
|
first_name: data.FirstName,
|
|
|
|
last_name: data.LastName,
|
|
|
|
full_name: data.Name,
|
|
|
|
position: data.Position,
|
|
|
|
fantasy_position: data.FantasyPosition,
|
|
|
|
height: data.Height,
|
|
|
|
weight: data.Weight,
|
|
|
|
birthday: data.BirthDateString,
|
|
|
|
experience: data.Experience,
|
|
|
|
experience_string: data.ExperienceString,
|
|
|
|
age: data.Age,
|
|
|
|
photo_url: data.PhotoUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
return new this(model_data)
|
|
|
|
}
|
|
|
|
|
2020-11-08 00:05:26 +00:00
|
|
|
/**
|
|
|
|
* returns the id's of the unobligated players
|
|
|
|
*/
|
2020-11-06 03:53:35 +00:00
|
|
|
static async get_unobligated_players() {
|
|
|
|
const Team = this.prototype.models.get('Team')
|
|
|
|
let obligated_player_ids = []
|
|
|
|
|
|
|
|
const teams = await Team.find()
|
|
|
|
for ( const team of teams ) {
|
|
|
|
obligated_player_ids = obligated_player_ids.concat(team.player_ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.find({
|
|
|
|
_id: {
|
|
|
|
$nin: obligated_player_ids.map(x => this.to_object_id(x))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-08 00:05:26 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param week_num
|
|
|
|
* @returns the points scored of that week
|
|
|
|
*/
|
2020-11-07 19:22:15 +00:00
|
|
|
async points_for_week(week_num) {
|
|
|
|
const WeeklyPlayerStat = this.models.get('WeeklyPlayerStat')
|
|
|
|
return WeeklyPlayerStat.findOne({ week_num, player_id: this.id })
|
|
|
|
}
|
|
|
|
|
2020-11-08 00:05:26 +00:00
|
|
|
/**
|
|
|
|
* @returns true if the player is obligated
|
|
|
|
* @returns false if the player is not obligates
|
|
|
|
*/
|
2020-11-06 03:53:35 +00:00
|
|
|
async is_obligated() {
|
|
|
|
const Team = this.models.get('Team')
|
|
|
|
const teams = await Team.find()
|
|
|
|
for ( const team of teams ) {
|
|
|
|
if ( team.player_ids.includes(this.id) ) return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-08 00:05:26 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param with_stats
|
|
|
|
* @returns updates the API's data
|
|
|
|
*/
|
2020-11-07 22:14:00 +00:00
|
|
|
async to_api(with_stats = false) {
|
|
|
|
const stat = with_stats ? await this.points_for_week() : undefined
|
|
|
|
|
2020-11-05 03:18:47 +00:00
|
|
|
return {
|
2020-11-06 02:57:08 +00:00
|
|
|
id: this.id,
|
2020-11-05 05:09:36 +00:00
|
|
|
number: this.player_number,
|
|
|
|
name: this.full_name,
|
|
|
|
position: this.fantasy_position,
|
|
|
|
team_name: this.patch_data.patch_team_name,
|
|
|
|
image: this.photo_url,
|
2020-11-07 22:14:00 +00:00
|
|
|
stats: (await stat?.to_api()) || this.seed_stats || {},
|
2020-11-05 03:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Player
|