diff --git a/src/app/components/add-game/add-game.component.html b/src/app/components/add-game/add-game.component.html index 7c07c7e..d3779c2 100644 --- a/src/app/components/add-game/add-game.component.html +++ b/src/app/components/add-game/add-game.component.html @@ -28,19 +28,19 @@ - + - + - + - +

{{this.actualPlayers[0].firstName}}

{{this.actualPlayers[1].firstName}}

{{this.actualPlayers[2].firstName}}

{{this.actualPlayers[3].firstName}}

Total score doesn't add up ({{getScoreDifferenceText()}})

@@ -55,7 +55,7 @@

- {{player.firstName}}: {{calculateCurrentScore(player)}} + {{player.firstName}}: {{player.gamePoints}}

diff --git a/src/app/components/add-game/add-game.component.ts b/src/app/components/add-game/add-game.component.ts index bd12a19..1f52fc0 100644 --- a/src/app/components/add-game/add-game.component.ts +++ b/src/app/components/add-game/add-game.component.ts @@ -31,31 +31,41 @@ export class AddGameComponent implements OnInit { firebonkId: 1, uuid: 'abc-def-ghi-j', firstName: 'Patrick', - team: Team.CONTRA + team: Team.CONTRA, + gamePoints: 0, + finalCardScore: 0 }); this.potentialPlayers.push({ firebonkId: 2, uuid: 'abc-def-ghi-k', firstName: 'Julian', - team: Team.CONTRA + team: Team.CONTRA, + gamePoints: 0, + finalCardScore: 0 }); this.potentialPlayers.push({ firebonkId: 3, uuid: 'abc-def-ghi-l', firstName: 'Yannick', - team: Team.CONTRA + team: Team.CONTRA, + gamePoints: 0, + finalCardScore: 0 }); this.potentialPlayers.push({ firebonkId: 4, uuid: 'abc-def-ghi-m', firstName: 'Janina', - team: Team.CONTRA + team: Team.CONTRA, + gamePoints: 0, + finalCardScore: 0 }); this.potentialPlayers.push({ firebonkId: 5, uuid: 'abc-def-ghi-n', firstName: 'Moritz', - team: Team.CONTRA + team: Team.CONTRA, + gamePoints: 0, + finalCardScore: 0 }); this.actualPlayers.push(...this.potentialPlayers.slice(0, 4)); @@ -65,6 +75,10 @@ export class AddGameComponent implements OnInit { * Switches the entry mask UI to the next page */ switchToNextPage(): void { + if (this.currentPage >= 4) { + this.calculateCurrentScores(); + } + this.currentPage++; } @@ -148,7 +162,7 @@ export class AddGameComponent implements OnInit { */ deactivateLowerAnnouncements(announcement: announcements.Announcement) { // First for RE - switch(announcement) { + switch (announcement) { case announcements.Announcement.RE: this.deactivateAnnouncement(announcements.Announcement.RE_NO_NINETY); this.deactivateAnnouncement(announcements.Announcement.RE_NO_SIXTY); @@ -164,7 +178,7 @@ export class AddGameComponent implements OnInit { } // Now for CONTRA - switch(announcement) { + switch (announcement) { case announcements.Announcement.CONTRA: this.deactivateAnnouncement(announcements.Announcement.CONTRA_NO_NINETY); this.deactivateAnnouncement(announcements.Announcement.CONTRA_NO_SIXTY); @@ -197,7 +211,7 @@ export class AddGameComponent implements OnInit { */ activateHigherAnnouncements(announcement: announcements.Announcement) { // First for RE - switch(announcement) { + switch (announcement) { case announcements.Announcement.RE_NO_THIRTY: this.activateAnnouncement(announcements.Announcement.RE_NO_SIXTY); this.activateAnnouncement(announcements.Announcement.RE_NO_NINETY); @@ -213,7 +227,7 @@ export class AddGameComponent implements OnInit { } // Now for CONTRA - switch(announcement) { + switch (announcement) { case announcements.Announcement.CONTRA_NO_THIRTY: this.activateAnnouncement(announcements.Announcement.CONTRA_NO_SIXTY); this.activateAnnouncement(announcements.Announcement.CONTRA_NO_NINETY); @@ -262,10 +276,22 @@ export class AddGameComponent implements OnInit { return announcements.checkValidity(this.selectedAnnouncements); } + /** + * Returns the two highest announcements for this game (one for RE, one for CONTRA, if applicable) + */ getHighestAnnouncements(): string { return announcements.returnTwoHighestAnnouncements(this.selectedAnnouncements); } + /** + * Checks if the given announcement has been selected + * @param announcement The announcement to check + */ + checkAnnouncementActive(announcement: announcements.Announcement): boolean { + let index = this.selectedAnnouncements.indexOf(announcement); + return index !== -1; + } + /** * ______ * /_ __/__ ____ _____ ___ _____ @@ -334,7 +360,7 @@ export class AddGameComponent implements OnInit { getScoreDifferenceText(): string { let difference = this.calculatePointSum() - 240; - if(difference > 0) { + if (difference > 0) { return difference + ' more than expected'; } else { difference *= -1; @@ -351,35 +377,63 @@ export class AddGameComponent implements OnInit { */ /** - * Calculates the game points for the given player - * @param player The player to calculate points for + * Calculates the game points for all players */ - calculateCurrentScore(player: Player): number { + calculateCurrentScores(): void { let gameScore: number = 0; - let teamScore = this.getTeamScore(player); + let winningTeamScore = this.getWinningTeamAndScore().score; + let winningTeam = this.getWinningTeamAndScore().team; + console.log(winningTeamScore); + console.log(winningTeam); - // Won? If so, by how much? - if (teamScore > 210) { - gameScore += 4; - } else if (teamScore > 180) { + // We are going to calculate the points for the winning team and then set all players points accordingly + + gameScore = 1; // 1 Point for Winning + + // Won by how much? + if (winningTeamScore > 210) { gameScore += 3; - } else if (teamScore > 150) { + } else if (winningTeamScore > 180) { gameScore += 2; - } else if (teamScore > 120) { + } else if (winningTeamScore > 150) { gameScore += 1; - } else if (teamScore < 30) { - gameScore -= 4; - } else if (teamScore < 60) { - gameScore -= 3; - } else if (teamScore < 90) { - gameScore -= 2; - } else { - gameScore -= 1; } - // TODO Announcements etc + // Announcements + if (winningTeam === Team.RE) { + if (this.checkAnnouncementActive(announcements.Announcement.RE_NO_NINETY) && winningTeamScore > 150) { + gameScore += 1; + } + if (this.checkAnnouncementActive(announcements.Announcement.RE_NO_SIXTY) && winningTeamScore > 180) { + gameScore += 1; + } + if (this.checkAnnouncementActive(announcements.Announcement.RE_NO_THIRTY) && winningTeamScore > 210) { + gameScore += 1; + } + } + if (winningTeam === Team.CONTRA) { + if (this.checkAnnouncementActive(announcements.Announcement.CONTRA_NO_NINETY) && winningTeamScore > 150) { + gameScore += 1; + } + if (this.checkAnnouncementActive(announcements.Announcement.CONTRA_NO_SIXTY) && winningTeamScore > 180) { + gameScore += 1; + } + if (this.checkAnnouncementActive(announcements.Announcement.CONTRA_NO_THIRTY) && winningTeamScore > 210) { + gameScore += 1; + } + } - return gameScore; + // Double Score in case of announcement + if (winningTeam === Team.RE && this.checkAnnouncementActive(announcements.Announcement.RE)) { + gameScore *= 2; + } + if (winningTeam === Team.CONTRA && this.checkAnnouncementActive(announcements.Announcement.CONTRA)) { + gameScore *= 2; + } + + // TODO: Check for announcements that have not been fulfilled! + + this.setGameScores(gameScore, winningTeam); } /** @@ -397,4 +451,46 @@ export class AddGameComponent implements OnInit { return totalTeamScore; } + + /** + * Calculates which team won and returns the Team Name and the achieved score + */ + getWinningTeamAndScore(): { team: Team, score: number } { + let elderPoints: number = 0; + let otherPoints: number = 0; + + for (let player of this.actualPlayers) { + if (player.team === Team.RE) { + elderPoints += player.finalCardScore ?? 0; + } else { + otherPoints += player.finalCardScore ?? 0; + } + } + + if (elderPoints > otherPoints) { + return { + team: Team.RE, + score: elderPoints + }; + } + return { + team: Team.CONTRA, + score: otherPoints + }; + } + + /** + * Sets the game scores for all players + * @param score The score to set + * @param winningTeam The team that won + */ + setGameScores(score: number, winningTeam: Team) { + for (let player of this.actualPlayers) { + if (player.team === winningTeam) { + player.gamePoints = score; + } else { + player.gamePoints = -score; + } + } + } } diff --git a/src/app/models/doppelkopf/enums/announcement.ts b/src/app/models/doppelkopf/enums/announcement.ts index 95d010d..bfada55 100644 --- a/src/app/models/doppelkopf/enums/announcement.ts +++ b/src/app/models/doppelkopf/enums/announcement.ts @@ -28,8 +28,7 @@ export function getAllAnnouncementValues(): Announcement[] { /** * Checks if the selected announcements are a valid set of announcements. * E.g.: If RE_NO_NINETY is newly selected, RE also has to be selected - * @param alreadySelected - * @param newSelected + * @param selectedAnnouncements The list of selected announcements */ export function checkValidity(selectedAnnouncements: Announcement[]): boolean { // First check all "RE" Announcements @@ -70,6 +69,10 @@ export function checkValidity(selectedAnnouncements: Announcement[]): boolean { return true } +/** + * Returns the names of the two highest announcements from the given list, one for RE and one for CONTRA if applicable + * @param selectedAnnouncements The list of announcements to check + */ export function returnTwoHighestAnnouncements(selectedAnnouncements: Announcement[]): string { let finalString: string = ''; diff --git a/src/app/models/doppelkopf/game.ts b/src/app/models/doppelkopf/game.ts index 4f9ce1e..c2fe79e 100644 --- a/src/app/models/doppelkopf/game.ts +++ b/src/app/models/doppelkopf/game.ts @@ -5,8 +5,6 @@ import {Solo} from './enums/solo'; export interface Game { gameId: number; players: Player[]; - foxesCaught: number; - announcements: Announcement[]; solo?: Solo; againstTheElders: boolean; } diff --git a/src/app/models/doppelkopf/player.ts b/src/app/models/doppelkopf/player.ts index b53cbb5..bb02f49 100644 --- a/src/app/models/doppelkopf/player.ts +++ b/src/app/models/doppelkopf/player.ts @@ -4,9 +4,10 @@ export interface Player { firebonkId: number; uuid: string; firstName: string; - finalCardScore?: number; - gamePoints?: number; + finalCardScore: number; + gamePoints: number; hadMarriage?: boolean; hadTrumpHandoff?: boolean; team?: Team; + foxesCaught?: number; }