From e41a07411ecce6e48901c61edb1562f6e2feeb4c Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Wed, 14 Sep 2022 19:00:23 +0200 Subject: [PATCH] Adding advanced announcement selection logic --- .../components/add-game/add-game.component.ts | 119 ++++++++++++++++-- 1 file changed, 110 insertions(+), 9 deletions(-) diff --git a/src/app/components/add-game/add-game.component.ts b/src/app/components/add-game/add-game.component.ts index fe528e0..bd12a19 100644 --- a/src/app/components/add-game/add-game.component.ts +++ b/src/app/components/add-game/add-game.component.ts @@ -1,6 +1,6 @@ import {Component, OnInit} from '@angular/core'; import {Player} from '../../models/doppelkopf/player'; -import * as Announcement from '../../models/doppelkopf/enums/announcement'; +import * as announcements from '../../models/doppelkopf/enums/announcement'; import {Team} from '../../models/doppelkopf/enums/team'; @Component({ @@ -12,7 +12,7 @@ export class AddGameComponent implements OnInit { potentialPlayers: Player[] = []; actualPlayers: Player[] = []; - selectedAnnouncements: Announcement.Announcement[] = []; + selectedAnnouncements: announcements.Announcement[] = []; currentPage: number = 0; @@ -127,15 +127,116 @@ export class AddGameComponent implements OnInit { */ /** - * Toggles the activity status for the given announcement + * Toggles the activity status for the given announcement and also activates / deactivates other announcements + * to conform to the rules. * @param announcement The announcement to toggle activity for */ - toggleAnnouncement(announcement: Announcement.Announcement): void { + toggleAnnouncement(announcement: announcements.Announcement): void { let index = this.selectedAnnouncements.indexOf(announcement); if (index !== -1) { this.selectedAnnouncements.splice(index, 1); + this.deactivateLowerAnnouncements(announcement); } else { this.selectedAnnouncements.push(announcement); + this.activateHigherAnnouncements(announcement); + } + } + + /** + * Deactivates all lower announcements. E.g.: When RE is deactivated, RE_NO_NINETY and all lower also get deactivated + * @param announcement The announcement that has been deactivated + */ + deactivateLowerAnnouncements(announcement: announcements.Announcement) { + // First for RE + switch(announcement) { + case announcements.Announcement.RE: + this.deactivateAnnouncement(announcements.Announcement.RE_NO_NINETY); + this.deactivateAnnouncement(announcements.Announcement.RE_NO_SIXTY); + this.deactivateAnnouncement(announcements.Announcement.RE_NO_THIRTY); + break; + case announcements.Announcement.RE_NO_NINETY: + this.deactivateAnnouncement(announcements.Announcement.RE_NO_SIXTY); + this.deactivateAnnouncement(announcements.Announcement.RE_NO_THIRTY); + break; + case announcements.Announcement.RE_NO_SIXTY: + this.deactivateAnnouncement(announcements.Announcement.RE_NO_THIRTY); + break; + } + + // Now for CONTRA + switch(announcement) { + case announcements.Announcement.CONTRA: + this.deactivateAnnouncement(announcements.Announcement.CONTRA_NO_NINETY); + this.deactivateAnnouncement(announcements.Announcement.CONTRA_NO_SIXTY); + this.deactivateAnnouncement(announcements.Announcement.CONTRA_NO_THIRTY); + break; + case announcements.Announcement.CONTRA_NO_NINETY: + this.deactivateAnnouncement(announcements.Announcement.CONTRA_NO_SIXTY); + this.deactivateAnnouncement(announcements.Announcement.CONTRA_NO_THIRTY); + break; + case announcements.Announcement.CONTRA_NO_SIXTY: + this.deactivateAnnouncement(announcements.Announcement.CONTRA_NO_THIRTY); + break; + } + } + + /** + * Deactivates the given announcement if it is active + * @param announcement The announcement to deactivate + */ + deactivateAnnouncement(announcement: announcements.Announcement) { + let index = this.selectedAnnouncements.indexOf(announcement); + if (index !== -1) { + this.selectedAnnouncements.splice(index, 1); + } + } + + /** + * Activates all higher announcements. E.g.: When RE_NO_NINETY is activated, RE also gets activated + * @param announcement The announcement that has been activated + */ + activateHigherAnnouncements(announcement: announcements.Announcement) { + // First for RE + switch(announcement) { + case announcements.Announcement.RE_NO_THIRTY: + this.activateAnnouncement(announcements.Announcement.RE_NO_SIXTY); + this.activateAnnouncement(announcements.Announcement.RE_NO_NINETY); + this.activateAnnouncement(announcements.Announcement.RE); + break; + case announcements.Announcement.RE_NO_SIXTY: + this.activateAnnouncement(announcements.Announcement.RE_NO_NINETY); + this.activateAnnouncement(announcements.Announcement.RE); + break; + case announcements.Announcement.RE_NO_NINETY: + this.activateAnnouncement(announcements.Announcement.RE); + break; + } + + // Now for CONTRA + switch(announcement) { + case announcements.Announcement.CONTRA_NO_THIRTY: + this.activateAnnouncement(announcements.Announcement.CONTRA_NO_SIXTY); + this.activateAnnouncement(announcements.Announcement.CONTRA_NO_NINETY); + this.activateAnnouncement(announcements.Announcement.CONTRA); + break; + case announcements.Announcement.CONTRA_NO_SIXTY: + this.activateAnnouncement(announcements.Announcement.CONTRA_NO_NINETY); + this.activateAnnouncement(announcements.Announcement.CONTRA); + break; + case announcements.Announcement.CONTRA_NO_NINETY: + this.activateAnnouncement(announcements.Announcement.CONTRA); + break; + } + } + + /** + * Activates the given announcement if it is inactive + * @param announcement The announcement to activate + */ + activateAnnouncement(announcement: announcements.Announcement) { + let index = this.selectedAnnouncements.indexOf(announcement); + if (index === -1) { + this.selectedAnnouncements.push(announcement); } } @@ -143,26 +244,26 @@ export class AddGameComponent implements OnInit { * Checks if the given announcement is already marked as selected * @param announcement The announcement to check the status for */ - isAnnouncementActive(announcement: Announcement.Announcement): boolean { + isAnnouncementActive(announcement: announcements.Announcement): boolean { return this.selectedAnnouncements.indexOf(announcement) !== -1; } /** * Returns a list of all possible announcements */ - getAllPossibleAnnouncements(): Announcement.Announcement[] { - return Announcement.getAllAnnouncementValues(); + getAllPossibleAnnouncements(): announcements.Announcement[] { + return announcements.getAllAnnouncementValues(); } /** * Checks, if the currently selected announcements are a valid set of announcements */ checkAnnouncementsValid(): boolean { - return Announcement.checkValidity(this.selectedAnnouncements); + return announcements.checkValidity(this.selectedAnnouncements); } getHighestAnnouncements(): string { - return Announcement.returnTwoHighestAnnouncements(this.selectedAnnouncements); + return announcements.returnTwoHighestAnnouncements(this.selectedAnnouncements); } /**