{"size": 1505, "ext": "js", "max_stars_repo_path": "src/models/Team.js", "max_stars_repo_name": "hathix/baseball-sim", "max_stars_repo_stars_event_min_datetime": "2018-08-16T06:44:02.000Z", "max_stars_repo_stars_event_max_datetime": "2018-08-16T06:44:02.000Z", "max_issues_repo_path": "src/models/Team.js", "max_issues_repo_name": "hathix/baseball-sim", "max_issues_repo_issues_event_min_datetime": "2018-08-14T05:27:48.000Z", "max_issues_repo_issues_event_max_datetime": "2018-08-16T06:43:37.000Z", "max_forks_repo_path": "src/models/Team.js", "max_forks_repo_name": "hathix/baseball-sim", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 22.803030303, "max_line_length": 82, "alphanum_fraction": 0.6757475083}
import Vue from "vue";
import Event from "@/models/Event";
import Pitch from "@/models/Pitch";
import PitchTypes from "@/models/PitchTypes"
import Player from "@/models/Player"

/**
  A team composed of multiple players.
*/
export default class Team {

  /**
    Parameters:
      players {Player[]} -- the players on the team
      battingOrder {int[]} -- the IDs of the players who are batting, in order 0-9
  */
  constructor(name, players, battingOrder) {
    this.name = name
    
    this.players = players

    this.battingOrder = battingOrder
    this.currentBatterIndex = 0 // 0 through 8, inclusive

    this.currentPitcherId = this.players[0].id
  }

  get currentBatter() {
    return this.getBatterFromOrder(this.currentBatterIndex)
  }

  get atBat() {
    // synonym for currentBatter
    return this.currentBatter
  }

  get currentPitcher() {
    return this.getPlayerById(this.currentPitcherId)
  }

  get onDeck() {
    let nextBatterIndex = (this.currentBatterIndex + 1) % 9
    return this.getBatterFromOrder(nextBatterIndex)
  }

  getPlayerById(id) {
    return _.find(this.players, (p) => p.id === id)
  }

  /**
    Gets the batter at the specified batting order index.
  */
  getBatterFromOrder(index) {
    return this.getPlayerById(this.battingOrder[index])
  }

  /**
    Advance the batting order and return the next batter
  */
  nextBatter() {
    // wrap around if needed
    this.currentBatterIndex = (this.currentBatterIndex + 1) % 9
    return this.currentBatter
  }
}
