Skip to content

Commit

Permalink
feat(ranking): add goal diff
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsch committed Aug 3, 2020
1 parent 147138e commit 3436415
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/api/ranking/ranking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions src/api/ranking/ranking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class Ranking {
lost: number
goals: number
goalsAgainst: number
goalsDifference: number
points: number

constructor(public team: Team) {
Expand All @@ -18,6 +19,7 @@ export class Ranking {
this.lost = 0
this.goals = 0
this.goalsAgainst = 0
this.goalsDifference = 0
this.points = 0
}

Expand Down

0 comments on commit 3436415

Please sign in to comment.