Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Fix stats if 0 games have been played.
Browse files Browse the repository at this point in the history
  • Loading branch information
w-biggs committed Dec 1, 2020
1 parent 60a7462 commit 1c76206
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,35 +225,45 @@ const statsRoute = async function statsRoute(req, res) {
const games = await getPopulatedSeasonGames(seasonNo, 13);
// Iterate thru games and get stats
const teamsStats = [];
for (let j = 0; j < games.length; j += 1) {
const game = games[j];
if (!game.live) {
const homeTeamIndex = teamsStats.findIndex((team) => team.name === game.homeTeam.team.name);
if (homeTeamIndex < 0) {
const teamStats = new TeamStats(game.homeTeam.team.name);
teamStats.addGame(game.homeTeam.stats, game.awayTeam.stats, game.awayTeam.team.name);
teamsStats.push(teamStats);
} else {
teamsStats[homeTeamIndex].addGame(
game.homeTeam.stats, game.awayTeam.stats, game.awayTeam.team.name,
);
}
const awayTeamIndex = teamsStats.findIndex((team) => team.name === game.awayTeam.team.name);
if (awayTeamIndex < 0) {
const teamStats = new TeamStats(game.awayTeam.team.name);
teamStats.addGame(game.awayTeam.stats, game.homeTeam.stats, game.homeTeam.team.name);
teamsStats.push(teamStats);
} else {
teamsStats[awayTeamIndex].addGame(
game.awayTeam.stats, game.homeTeam.stats, game.homeTeam.team.name,
);
if (!games.length) {
// Add empty teams
const teams = await Team.find().lean().select('name');
for (let i = 0; i < teams.length; i += 1) {
const team = teams[i];
const teamStats = new TeamStats(team.name);
teamsStats.push(teamStats);
}
} else {
for (let j = 0; j < games.length; j += 1) {
const game = games[j];
if (!game.live) {
const homeTeamIndex = teamsStats.findIndex((team) => team.name === game.homeTeam.team.name);
if (homeTeamIndex < 0) {
const teamStats = new TeamStats(game.homeTeam.team.name);
teamStats.addGame(game.homeTeam.stats, game.awayTeam.stats, game.awayTeam.team.name);
teamsStats.push(teamStats);
} else {
teamsStats[homeTeamIndex].addGame(
game.homeTeam.stats, game.awayTeam.stats, game.awayTeam.team.name,
);
}
const awayTeamIndex = teamsStats.findIndex((team) => team.name === game.awayTeam.team.name);
if (awayTeamIndex < 0) {
const teamStats = new TeamStats(game.awayTeam.team.name);
teamStats.addGame(game.awayTeam.stats, game.homeTeam.stats, game.homeTeam.team.name);
teamsStats.push(teamStats);
} else {
teamsStats[awayTeamIndex].addGame(
game.awayTeam.stats, game.homeTeam.stats, game.homeTeam.team.name,
);
}
}
}
}
// SoS
for (let i = 0; i < teamsStats.length; i += 1) {
const teamStats = teamsStats[i];
teamStats.calcSOS(teamsStats);
// SoS
for (let i = 0; i < teamsStats.length; i += 1) {
const teamStats = teamsStats[i];
teamStats.calcSOS(teamsStats);
}
}
teamsStats.sort((a, b) => a.name.localeCompare(b.name));
res.send(teamsStats);
Expand Down

0 comments on commit 1c76206

Please sign in to comment.