From 343641525e201e14a53d15a1bb2d18f30c9e8319 Mon Sep 17 00:00:00 2001 From: hirsch Date: Mon, 3 Aug 2020 20:51:31 +0200 Subject: [PATCH] feat(ranking): add goal diff --- src/api/ranking/ranking.service.ts | 27 +++++++++++++++------------ src/api/ranking/ranking.ts | 2 ++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/api/ranking/ranking.service.ts b/src/api/ranking/ranking.service.ts index 22e220b..56d011a 100644 --- a/src/api/ranking/ranking.service.ts +++ b/src/api/ranking/ranking.service.ts @@ -19,39 +19,42 @@ export class RankingService { const teams = await this.teamService.findAll(tournament) const games = await this.gameService.findAll(tournament) return teams - .map(team => new Ranking(team)) - .map(ranking => { - const hostGames = games.filter(game => game.host.id === ranking.team.id) + .map((team) => new Ranking(team)) + .map((ranking) => { + const hostGames = games.filter( + (game) => game.host.id === ranking.team.id && game.wasPlayed(), + ) const guestGames = games.filter( - game => game.guest.id === ranking.team.id, + (game) => game.guest.id === ranking.team.id && game.wasPlayed(), ) ranking.played = hostGames.length + guestGames.length - const hostWins = hostGames.filter(g => g.didHostWin()).length - const guestWins = guestGames.filter(g => g.didGuestWin()).length + const hostWins = hostGames.filter((g) => g.didHostWin()).length + const guestWins = guestGames.filter((g) => g.didGuestWin()).length const draws = - hostGames.filter(g => g.isADraw()).length + - guestGames.filter(g => g.isADraw()).length + hostGames.filter((g) => g.isADraw()).length + + guestGames.filter((g) => g.isADraw()).length ranking.won = hostWins + guestWins ranking.drawn = draws ranking.lost = ranking.played - ranking.won - ranking.drawn ranking.points = ranking.won * 3 + ranking.drawn const hostGoals = hostGames - .map(g => g.hostScore) + .map((g) => g.hostScore) .reduce((acc, score) => acc + score, 0) const guestGoals = guestGames - .map(g => g.guestScore) + .map((g) => g.guestScore) .reduce((acc, score) => acc + score, 0) ranking.goals = hostGoals + guestGoals const hostGoalsAgainst = hostGames - .map(g => g.guestScore) + .map((g) => g.guestScore) .reduce((acc, score) => acc + score, 0) const guestGoalsAgainst = guestGames - .map(g => g.hostScore) + .map((g) => g.hostScore) .reduce((acc, score) => acc + score, 0) ranking.goalsAgainst = hostGoalsAgainst + guestGoalsAgainst + ranking.goalsDifference = ranking.goals - ranking.goalsAgainst return ranking }) .sort((a, b) => a.compare(b)) diff --git a/src/api/ranking/ranking.ts b/src/api/ranking/ranking.ts index e32cba8..bc763ec 100644 --- a/src/api/ranking/ranking.ts +++ b/src/api/ranking/ranking.ts @@ -8,6 +8,7 @@ export class Ranking { lost: number goals: number goalsAgainst: number + goalsDifference: number points: number constructor(public team: Team) { @@ -18,6 +19,7 @@ export class Ranking { this.lost = 0 this.goals = 0 this.goalsAgainst = 0 + this.goalsDifference = 0 this.points = 0 }