286 lines
7.1 KiB
TypeScript
286 lines
7.1 KiB
TypeScript
import {Component, OnInit} from '@angular/core';
|
|
import {Player} from '../../models/doppelkopf/player';
|
|
import * as Announcement from '../../models/doppelkopf/enums/announcement';
|
|
import {Team} from '../../models/doppelkopf/enums/team';
|
|
|
|
@Component({
|
|
selector: 'app-add-game',
|
|
templateUrl: './add-game.component.html',
|
|
styleUrls: ['./add-game.component.scss']
|
|
})
|
|
export class AddGameComponent implements OnInit {
|
|
potentialPlayers: Player[] = [];
|
|
actualPlayers: Player[] = [];
|
|
|
|
selectedAnnouncements: Announcement.Announcement[] = [];
|
|
|
|
currentPage: number = 0;
|
|
|
|
constructor() {
|
|
this.generateDemoData();
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
}
|
|
|
|
/**
|
|
* Generates demo data to test the UI
|
|
*/
|
|
generateDemoData() {
|
|
this.potentialPlayers.push({
|
|
firebonkId: 1,
|
|
uuid: 'abc-def-ghi-j',
|
|
firstName: 'Patrick',
|
|
team: Team.CONTRA
|
|
});
|
|
this.potentialPlayers.push({
|
|
firebonkId: 2,
|
|
uuid: 'abc-def-ghi-k',
|
|
firstName: 'Julian',
|
|
team: Team.CONTRA
|
|
});
|
|
this.potentialPlayers.push({
|
|
firebonkId: 3,
|
|
uuid: 'abc-def-ghi-l',
|
|
firstName: 'Yannick',
|
|
team: Team.CONTRA
|
|
});
|
|
this.potentialPlayers.push({
|
|
firebonkId: 4,
|
|
uuid: 'abc-def-ghi-m',
|
|
firstName: 'Janina',
|
|
team: Team.CONTRA
|
|
});
|
|
this.potentialPlayers.push({
|
|
firebonkId: 5,
|
|
uuid: 'abc-def-ghi-n',
|
|
firstName: 'Moritz',
|
|
team: Team.CONTRA
|
|
});
|
|
|
|
this.actualPlayers.push(...this.potentialPlayers.slice(0, 4));
|
|
}
|
|
|
|
/**
|
|
* Switches the entry mask UI to the next page
|
|
*/
|
|
switchToNextPage(): void {
|
|
this.currentPage++;
|
|
}
|
|
|
|
/**
|
|
* ____ __
|
|
* / __ \/ /___ ___ _____ __________
|
|
* / /_/ / / __ `/ / / / _ \/ ___/ ___/
|
|
* / ____/ / /_/ / /_/ / __/ / (__ )
|
|
* /_/ /_/\__,_/\__, /\___/_/ /____/
|
|
* /____/
|
|
*/
|
|
|
|
/**
|
|
* Toggles if the given player is should be an active player for the current game
|
|
* @param player The player to toggle the activity for
|
|
*/
|
|
togglePlayer(player: Player): void {
|
|
let index = this.actualPlayers.indexOf(player);
|
|
if (index !== -1) {
|
|
this.actualPlayers.splice(index, 1);
|
|
} else {
|
|
this.actualPlayers.push(player);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks, if the given player is currently marked as active for this game
|
|
* @param player The player to check the activity status for
|
|
* @returns boolean If the player is active
|
|
*/
|
|
isPlayerActive(player: Player): boolean {
|
|
return this.actualPlayers.indexOf(player) !== -1;
|
|
}
|
|
|
|
/**
|
|
* Returns, if the amount of currently active players is greater or less than 4
|
|
*/
|
|
not4Players() {
|
|
return this.actualPlayers.length !== 4;
|
|
}
|
|
|
|
/**
|
|
* Returns the names of the active players as a comma-separated string
|
|
*/
|
|
getPlayerNamesAsString(): string {
|
|
let playerNames = '';
|
|
for (let player of this.actualPlayers) {
|
|
playerNames += player.firstName + ', ';
|
|
}
|
|
// Remove last ", "
|
|
return playerNames.substring(0, playerNames.length - 2);
|
|
}
|
|
|
|
/**
|
|
* ___ __
|
|
* / | ____ ____ ____ __ ______ ________ ____ ___ ___ ____ / /______
|
|
* / /| | / __ \/ __ \/ __ \/ / / / __ \/ ___/ _ \/ __ `__ \/ _ \/ __ \/ __/ ___/
|
|
* / ___ |/ / / / / / / /_/ / /_/ / / / / /__/ __/ / / / / / __/ / / / /_(__ )
|
|
* /_/ |_/_/ /_/_/ /_/\____/\__,_/_/ /_/\___/\___/_/ /_/ /_/\___/_/ /_/\__/____/
|
|
*/
|
|
|
|
/**
|
|
* Toggles the activity status for the given announcement
|
|
* @param announcement The announcement to toggle activity for
|
|
*/
|
|
toggleAnnouncement(announcement: Announcement.Announcement): void {
|
|
let index = this.selectedAnnouncements.indexOf(announcement);
|
|
if (index !== -1) {
|
|
this.selectedAnnouncements.splice(index, 1);
|
|
} else {
|
|
this.selectedAnnouncements.push(announcement);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks if the given announcement is already marked as selected
|
|
* @param announcement The announcement to check the status for
|
|
*/
|
|
isAnnouncementActive(announcement: Announcement.Announcement): boolean {
|
|
return this.selectedAnnouncements.indexOf(announcement) !== -1;
|
|
}
|
|
|
|
/**
|
|
* Returns a list of all possible announcements
|
|
*/
|
|
getAllPossibleAnnouncements(): Announcement.Announcement[] {
|
|
return Announcement.getAllAnnouncementValues();
|
|
}
|
|
|
|
/**
|
|
* Checks, if the currently selected announcements are a valid set of announcements
|
|
*/
|
|
checkAnnouncementsValid(): boolean {
|
|
return Announcement.checkValidity(this.selectedAnnouncements);
|
|
}
|
|
|
|
getHighestAnnouncements(): string {
|
|
return Announcement.returnTwoHighestAnnouncements(this.selectedAnnouncements);
|
|
}
|
|
|
|
/**
|
|
* ______
|
|
* /_ __/__ ____ _____ ___ _____
|
|
* / / / _ \/ __ `/ __ `__ \/ ___/
|
|
* / / / __/ /_/ / / / / / (__ )
|
|
* /_/ \___/\__,_/_/ /_/ /_/____/
|
|
*/
|
|
|
|
/**
|
|
* Toggles the players team
|
|
* @param player The player to toggle the team for
|
|
*/
|
|
toggleElderPlayer(player: Player): void {
|
|
if (player.team === Team.RE) {
|
|
player.team = Team.CONTRA;
|
|
} else {
|
|
player.team = Team.RE;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks if the player is an elder
|
|
* @param player The player to check
|
|
*/
|
|
isPlayerElder(player: Player): boolean {
|
|
return player.team === Team.RE;
|
|
}
|
|
|
|
/**
|
|
* Checks if the current team assignment is valid
|
|
*/
|
|
checkValidTeamAssignment(): boolean {
|
|
let numberOfElderPlayers: number = 0;
|
|
for (let player of this.actualPlayers) {
|
|
if (player.team === Team.RE) {
|
|
numberOfElderPlayers++;
|
|
}
|
|
}
|
|
|
|
return !(numberOfElderPlayers !== 1 && numberOfElderPlayers !== 2);
|
|
}
|
|
|
|
/**
|
|
* _____
|
|
* / ___/_________ ________ _____
|
|
* \__ \/ ___/ __ \/ ___/ _ \/ ___/
|
|
* ___/ / /__/ /_/ / / / __(__ )
|
|
* /____/\___/\____/_/ \___/____/
|
|
*/
|
|
|
|
/**
|
|
* Checks if the sum of all points is exactly 240
|
|
*/
|
|
totalScoreValid(): boolean {
|
|
let totalScore: number = 0;
|
|
for (let player of this.actualPlayers) {
|
|
totalScore += player.finalCardScore ?? 0;
|
|
}
|
|
|
|
return totalScore === 240;
|
|
}
|
|
|
|
/**
|
|
* ______ _____
|
|
* / ____/___ _____ ___ ___ / ___/_________ ________
|
|
* / / __/ __ `/ __ `__ \/ _ \ \__ \/ ___/ __ \/ ___/ _ \
|
|
* / /_/ / /_/ / / / / / / __/ ___/ / /__/ /_/ / / / __/
|
|
* \____/\__,_/_/ /_/ /_/\___/ /____/\___/\____/_/ \___/
|
|
*/
|
|
|
|
/**
|
|
* Calculates the game points for the given player
|
|
* @param player The player to calculate points for
|
|
*/
|
|
calculateCurrentScore(player: Player): number {
|
|
let gameScore: number = 0;
|
|
let teamScore = this.getTeamScore(player);
|
|
|
|
// Won? If so, by how much?
|
|
if (teamScore > 210) {
|
|
gameScore += 4;
|
|
} else if (teamScore > 180) {
|
|
gameScore += 3;
|
|
} else if (teamScore > 150) {
|
|
gameScore += 2;
|
|
} else if (teamScore > 120) {
|
|
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
|
|
|
|
return gameScore;
|
|
}
|
|
|
|
/**
|
|
* Calculates the team card score of the given players team
|
|
* @param player The player to calculate the teams points for
|
|
*/
|
|
getTeamScore(player: Player): number {
|
|
let totalTeamScore: number = player.finalCardScore ?? 0;
|
|
|
|
for (let otherPlayer of this.actualPlayers) {
|
|
if (otherPlayer !== player && otherPlayer.team === player.team) {
|
|
totalTeamScore += otherPlayer.finalCardScore ?? 0;
|
|
}
|
|
}
|
|
|
|
return totalTeamScore;
|
|
}
|
|
}
|