@ -1,22 +1,27 @@
const { Model } = require ( 'flitter-orm' )
const ActiveScope = require ( './scopes/Active.scope' )
/ *
/ * *
* Player Model
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
* A model representing a single player in the game .
*
* @ extends Model
* /
class Player extends Model {
static get services ( ) {
return [ ... super . services , 'output' , 'models' , 'sports_data' ]
}
// Enable soft-deletes using the active scope
static scopes = [ new ActiveScope ( ) ]
/ *
/ * *
* Define the flitter - orm schema of the model .
* /
static get schema ( ) {
return {
// Data used by the patches internally, but not exposed to the API
patch _data : {
patch _team _id : Number ,
patch _team _name : String ,
@ -24,6 +29,7 @@ class Player extends Model {
player _id : Number ,
draft _position : Number ,
} ,
player _number : Number ,
first _name : String ,
last _name : String ,
@ -38,6 +44,7 @@ class Player extends Model {
age : Number ,
photo _url : String ,
// Statistics pre-generated for the player to optimize performance
seed _stats : Object ,
// False if the player doesn't have any week-1 stats.
@ -74,7 +81,8 @@ class Player extends Model {
}
/ * *
* returns the id ' s of the unobligated players
* returns all of the unobligated players across all teams
* @ return Promise < Array < Player >>
* /
static async get _unobligated _players ( ) {
const Team = this . prototype . models . get ( 'Team' )
@ -93,9 +101,9 @@ class Player extends Model {
}
/ * *
*
* @ param week _num
* @ returns the points scored of that week
* Returns the stats for the player for the given week .
* @ param { number } week _num
* @ returns Promise < WeeklyPlayerStat >
* /
async points _for _week ( week _num ) {
const WeeklyPlayerStat = this . models . get ( 'WeeklyPlayerStat' )
@ -103,8 +111,8 @@ class Player extends Model {
}
/ * *
* @ returns true if the player is obligated
* @ returns false if the player is not obligates
* Determine whether the player belongs to a team or not .
* @ returns { Promise < boolean > } - true if the player is obligated
* /
async is _obligated ( ) {
const Team = this . models . get ( 'Team' )
@ -117,12 +125,13 @@ class Player extends Model {
}
/ * *
*
* @ param with _stats
* @ returns updates the API ' s data
* Cast the player to a format compatible with the API .
* @ param { boolean } [ with _stats = false ] - if true , look up the player ' s weekly stats
* @ returns Promise < object >
* /
async to _api ( with _stats = false ) {
const stat = with _stats ? await this . points _for _week ( ) : undefined
const current _week = await this . sports _data . current _play _week ( )
const stat = with _stats ? await this . points _for _week ( current _week ) : undefined
return {
id : this . id ,