Skip to content

Commit

Permalink
show record at the pick level (winsleague#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsw authored Aug 8, 2016
1 parent 7c49864 commit 50527ef
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 7 deletions.
17 changes: 17 additions & 0 deletions app/imports/api/pool_team_picks/pool_team_picks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Factory } from 'meteor/dburles:factory';
import log from '../../utils/log';

import { LeagueTeams } from '../league_teams/league_teams';
import { SeasonLeagueTeams } from '../season_league_teams/season_league_teams';
import { PoolTeams } from '../pool_teams/pool_teams';
import { Pools } from '../pools/pools';
import { Seasons } from '../seasons/seasons';
Expand Down Expand Up @@ -100,6 +101,14 @@ PoolTeamPicks.schema = new SimpleSchema({
decimal: true,
defaultValue: 0,
},
actualLosses: {
type: Number,
defaultValue: 0,
},
actualTies: {
type: Number,
defaultValue: 0,
},
createdAt: {
// Force value to be current date (on server) upon insert
// and prevent updates thereafter.
Expand Down Expand Up @@ -129,6 +138,14 @@ PoolTeamPicks.schema = new SimpleSchema({

PoolTeamPicks.attachSchema(PoolTeamPicks.schema);

PoolTeamPicks.helpers({
record() {
if (this.actualTies > 0) {
return `${this.actualWins}-${this.actualLosses}-${this.actualTies}`;
}
return `${this.actualWins}-${this.actualLosses}`;
},
});

/* Access control */
function isPoolTeamOwner(userId, poolTeamId) {
Expand Down
2 changes: 1 addition & 1 deletion app/imports/api/pool_team_picks/server/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PoolTeamPicks.hookOptions.after.update = { fetchPrevious: false };

function updatePoolTeam(doc) {
PoolTeamUpdater.updateTeamSummary(doc.poolTeamId);
PoolTeamUpdater.updatePoolTeamWins(doc.poolTeamId);
PoolTeamUpdater.updatePoolTeamRecord(doc.poolTeamId);
PoolTeamUpdater.updatePoolTeamPickQuality(doc.poolTeamId);
}

Expand Down
15 changes: 11 additions & 4 deletions app/imports/api/pool_teams/server/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export default {

const poolTeamPicks = PoolTeamPicks.find({ leagueTeamId });
poolTeamPicks.forEach(poolTeamPick => {
this.updatePoolTeamWins(poolTeamPick.poolTeamId);
this.updatePoolTeamRecord(poolTeamPick.poolTeamId);
this.updatePoolTeamPickQuality(poolTeamPick.poolTeamId);
});

log.debug('Done finding PoolTeams who picked leagueTeamId:', leagueTeamId);
},

updatePoolTeamWins(poolTeamId) {
log.info('Updating PoolTeam wins', poolTeamId);
updatePoolTeamRecord(poolTeamId) {
log.info('Updating PoolTeam record', poolTeamId);

let totalWins = 0;
let totalLosses = 0;
Expand All @@ -34,8 +34,15 @@ export default {
const seasonId = poolTeamPick.seasonId;
const leagueTeamId = poolTeamPick.leagueTeamId;
const seasonLeagueTeam = SeasonLeagueTeams.findOne({ seasonId, leagueTeamId });
log.debug('Found seasonLeagueTeam', seasonLeagueTeam);
if (seasonLeagueTeam) {
PoolTeamPicks.direct.update(poolTeamPick._id, {
$set: {
actualWins: seasonLeagueTeam.wins,
actualLosses: seasonLeagueTeam.losses,
actualTies: seasonLeagueTeam.ties,
},
});

totalWins += seasonLeagueTeam.wins;
totalLosses += seasonLeagueTeam.losses;
totalGames += seasonLeagueTeam.totalGames();
Expand Down
2 changes: 1 addition & 1 deletion app/imports/api/pool_teams/server/updater.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Pool Teams Updater', () => {
period: 'final',
});

PoolTeamsUpdater.updatePoolTeamWins(poolTeam._id);
PoolTeamsUpdater.updatePoolTeamRecord(poolTeam._id);

poolTeam = PoolTeams.findOne(poolTeam._id);
log.debug('poolTeam:', poolTeam);
Expand Down
7 changes: 7 additions & 0 deletions app/imports/api/season_league_teams/season_league_teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ SeasonLeagueTeams.helpers({
totalGames() {
return this.wins + this.losses + this.ties;
},

record() {
if (this.ties > 0) {
return `${this.wins}-${this.losses}-${this.ties}`;
}
return `${this.wins}-${this.losses}`;
}
});

SeasonLeagueTeams.deny({
Expand Down
2 changes: 2 additions & 0 deletions app/imports/ui/pages/pool-teams-show-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h3 id="PoolTeams_show_title">{{poolTeamName}}</h3>
<tr>
<th>Pick</th>
<th>Team</th>
<th>Record</th>
<th>Pick Quality</th>
<th>&nbsp;</th>
</tr>
Expand All @@ -19,6 +20,7 @@ <h3 id="PoolTeams_show_title">{{poolTeamName}}</h3>
<tr id="{{poolTeamPick._id}}">
<td class="PoolTeamPick">{{poolTeamPick.pickNumber}}</td>
<td>{{leagueTeamName poolTeamPick.leagueTeamId}}</td>
<td>{{poolTeamPick.record}}</td>
<td>{{roundedPickQuality poolTeamPick.pickQuality}}</td>
<td>
{{#if isCommissioner}}
Expand Down
2 changes: 1 addition & 1 deletion app/server/migrations/v1_add_total_losses_to_pool_teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Migrations.add({
name: 'Adds totalLosses field to PoolTeams and populates it',
up: () => {
PoolTeams.find().forEach(poolTeam => {
PoolTeamsUpdater.updatePoolTeamWins(poolTeam);
PoolTeamsUpdater.updatePoolTeamRecord(poolTeam);
});
},
down: () => {
Expand Down

0 comments on commit 50527ef

Please sign in to comment.