195 lines
6.7 KiB
HTML
195 lines
6.7 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html lang="en">
|
||
|
<head>
|
||
|
<meta charset="utf-8">
|
||
|
<title>JSDoc: Source: app/models/Team.model.js</title>
|
||
|
|
||
|
<script src="scripts/prettify/prettify.js"> </script>
|
||
|
<script src="scripts/prettify/lang-css.js"> </script>
|
||
|
<!--[if lt IE 9]>
|
||
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
||
|
<![endif]-->
|
||
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
||
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
|
||
|
<div id="main">
|
||
|
|
||
|
<h1 class="page-title">Source: app/models/Team.model.js</h1>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
<section>
|
||
|
<article>
|
||
|
<pre class="prettyprint source linenums"><code>const { Model } = require('flitter-orm')
|
||
|
|
||
|
/**
|
||
|
* Team Model
|
||
|
* -------------------------------------------------------------
|
||
|
* A model representing a single team in the game.
|
||
|
*
|
||
|
* @extends Model
|
||
|
*/
|
||
|
class Team extends Model {
|
||
|
static get services() {
|
||
|
return [...super.services, 'output', 'models']
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Define the flitter-orm schema of the model.
|
||
|
*/
|
||
|
static get schema() {
|
||
|
return {
|
||
|
user_id: String,
|
||
|
team_name: String,
|
||
|
team_num: Number,
|
||
|
player_ids: [String],
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Look up or create a team for the given user.
|
||
|
* @param {User} user
|
||
|
* @return {Promise<Team>}
|
||
|
*/
|
||
|
static async getForUser(user) {
|
||
|
// try to find an existing team first
|
||
|
const existing_team = await this.findOne({ user_id: user.id })
|
||
|
if ( existing_team ) return existing_team
|
||
|
|
||
|
// otherwise create a team for the user
|
||
|
const new_team = new this({
|
||
|
user_id: user.id,
|
||
|
team_name: `${user.uid}'s team`,
|
||
|
player_ids: [],
|
||
|
})
|
||
|
|
||
|
// Generate the next team number
|
||
|
const highest_num_team = await this.sort('-team_num').findOne()
|
||
|
if ( highest_num_team ) {
|
||
|
new_team.team_num = highest_num_team.team_num + 1
|
||
|
} else {
|
||
|
new_team.team_num = 1
|
||
|
}
|
||
|
|
||
|
await new_team.save()
|
||
|
return new_team
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* returns the lineup
|
||
|
* @return Promise<Lineup>
|
||
|
*/
|
||
|
async lineup() {
|
||
|
const Lineup = this.models.get('Lineup')
|
||
|
return Lineup.get_and_update_for_team(this)
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the players associated with the team.
|
||
|
* @return Promise<Array<Player>>
|
||
|
*/
|
||
|
async players() {
|
||
|
const Player = this.models.get('Player')
|
||
|
return Player.find({
|
||
|
_id: {
|
||
|
$in: this.player_ids.map(x => Player.to_object_id(x))
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the cumulative data for the team (total wins, losses, points scored & allowed)
|
||
|
* @return object
|
||
|
*/
|
||
|
async cumulative_data() {
|
||
|
const Matchup = this.models.get('Matchup')
|
||
|
const home_matchups = await Matchup.find({ home_team_id: this.id })
|
||
|
const visitor_matchups = await Matchup.find({ visitor_team_id: this.id })
|
||
|
|
||
|
const data = {
|
||
|
wins: 0,
|
||
|
losses: 0,
|
||
|
points_scored: 0,
|
||
|
points_allowed: 0,
|
||
|
}
|
||
|
|
||
|
for ( const matchup of home_matchups ) {
|
||
|
if ( !matchup.complete ) continue
|
||
|
|
||
|
data.points_scored += matchup.home_team_score
|
||
|
data.points_allowed += matchup.visitor_team_score
|
||
|
|
||
|
if ( matchup.home_team_score > matchup.visitor_team_score ) {
|
||
|
data.wins += 1
|
||
|
} else {
|
||
|
data.losses += 0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for ( const matchup of visitor_matchups ) {
|
||
|
if ( !matchup.complete ) continue
|
||
|
|
||
|
data.points_scored += matchup.visitor_team_score
|
||
|
data.points_allowed += matchup.home_team_score
|
||
|
|
||
|
if ( matchup.visitor_team_score > matchup.home_team_score ) {
|
||
|
data.wins += 1
|
||
|
} else {
|
||
|
data.losses += 0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return data
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Cast the team to the format expected for the API.
|
||
|
* @return Promise<object>
|
||
|
*/
|
||
|
async to_api() {
|
||
|
let user
|
||
|
try {
|
||
|
const User = this.models.get('auth:User')
|
||
|
user = await User.findById(this.user_id)
|
||
|
} catch(e) {}
|
||
|
|
||
|
return {
|
||
|
user_id: this.user_id,
|
||
|
user_display: user?.uid || 'Unknown User',
|
||
|
team_name: this.team_name,
|
||
|
team_num: this.team_num,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = exports = Team
|
||
|
</code></pre>
|
||
|
</article>
|
||
|
</section>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
<nav>
|
||
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fake_data.html">fake_data</a></li><li><a href="module-routing.html">routing</a></li><li><a href="module-util.html">util</a></li></ul><h3>Classes</h3><ul><li><a href="ActiveScope.html">ActiveScope</a></li><li><a href="AddPlayersComponent.html">AddPlayersComponent</a></li><li><a href="DraftBoard.html">DraftBoard</a></li><li><a href="DraftBoardComponent.html">DraftBoardComponent</a></li><li><a href="FrontendUnit.html">FrontendUnit</a></li><li><a href="GenerateMatchupsForWeekPatch.html">GenerateMatchupsForWeekPatch</a></li><li><a href="GenerateWeeklyResultsPatch.html">GenerateWeeklyResultsPatch</a></li><li><a href="GridActionButtonComponent.html">GridActionButtonComponent</a></li><li><a href="GridComponent.html">GridComponent</a></li><li><a href="Home.html">Home</a></li><li><a href="InjectUserTeam.html">InjectUserTeam</a></li><li><a href="LeagueComponent.html">LeagueComponent</a></li><li><a href="Lineup.html">Lineup</a></li><li><a href="LinkComponent.html">LinkComponent</a></li><li><a href="Matchup.html">Matchup</a></li><li><a href="module-routing-Router.html">Router</a></li><li><a href="MyTeamComponent.html">MyTeamComponent</a></li><li><a href="Player.html">Player</a></li><li><a href="ScoresComponent.html">ScoresComponent</a></li><li><a href="ScoresController.html">ScoresController</a></li><li><a href="SeedAPIDataPatch.html">SeedAPIDataPatch</a></li><li><a href="SeedWeeklyPlayerDataPatch.html">SeedWeeklyPlayerDataPatch</a></li><li><a href="SportsDataService.html">SportsDataService</a></li><li><a href="Team.html">Team</a></li><li><a href="Teams.html">Teams</a></li><li><a href="TopLevelComponent.html">TopLevelComponent</a></li><li><a href="User.html">User</a></li><li><a href="WeeklyPlayerStat.html">WeeklyPlayerStat</a></li><li><a href="WeeklyTeamStat.html">WeeklyTeamStat</a></li></ul><h3>Global</h3><ul><li><a href="global.html#GridCellRenderType">GridCellRenderType</a></li></ul>
|
||
|
</nav>
|
||
|
|
||
|
<br class="clear">
|
||
|
|
||
|
<footer>
|
||
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Sun Nov 08 2020 14:34:32 GMT-0600 (Central Standard Time)
|
||
|
</footer>
|
||
|
|
||
|
<script> prettyPrint(); </script>
|
||
|
<script src="scripts/linenumber.js"> </script>
|
||
|
</body>
|
||
|
</html>
|