Add ability to edit team name & starting lineup & save to server

This commit is contained in:
2020-11-05 20:57:08 -06:00
parent 47f3c0e6b0
commit fc5b90a938
7 changed files with 180 additions and 12 deletions

View File

@@ -138,6 +138,24 @@ class Lineup extends Model {
}
}
/**
* Given the player_id/position record, add it to the starting lineup.
* @param {object} player_position_record
*/
start_player(player_position_record) {
this.benched_player_ids = this.benched_player_ids.filter(x => x !== player_position_record.player_id)
this.starting_players = this.starting_players.filter(x => x.player_id !== player_position_record.player_id)
this.starting_players.push(player_position_record)
}
/**
* Remove all players from the bench and the starting lineup.
*/
clear_lineup() {
this.starting_players = []
this.benched_player_ids = []
}
/**
* Cast the lineup to an object which can be returned via the API.
* @return {Promise<object>}
@@ -159,12 +177,20 @@ class Lineup extends Model {
// Find the player instance and cast it to an API object
const player_inst = starting_players.find(x => x.id === player.player_id)
build_starting_players.push({
position: player.position,
...(await player_inst.to_api())
...(await player_inst.to_api()),
position: player.position
})
// remove the position from the array of positions to back-fill
lineup_positions = lineup_positions.filter(x => x !== player.position)
let found_one = false
lineup_positions = lineup_positions.filter(x => {
if ( !found_one && x === player.position ) {
found_one = true
return false
}
return true
})
}
// Fill in any missing positions into the data
@@ -182,6 +208,11 @@ class Lineup extends Model {
build_benched_players.push(obj)
}
// If there are no players on the bench, add a placeholder slot.
if ( build_benched_players.length < 1 ) {
build_benched_players.push({ position: 'B' })
}
data.starting_players = build_starting_players
data.benched_players = build_benched_players
return data

View File

@@ -67,6 +67,7 @@ class Player extends Model {
async to_api() {
return {
id: this.id,
number: this.player_number,
name: this.full_name,
position: this.fantasy_position,

View File

@@ -50,6 +50,11 @@ class Team extends Model {
return new_team
}
async lineup() {
const Lineup = this.models.get('Lineup')
return Lineup.get_and_update_for_team(this)
}
async players() {
const Player = this.models.get('Player')
return Player.find({