import {Component, inject, Inject} from '@angular/core'; import { MAT_DIALOG_DATA, MatDialogActions, MatDialogClose, MatDialogContent, MatDialogRef, MatDialogTitle } from "@angular/material/dialog"; import {MatButton} from "@angular/material/button"; import {NgClass} from "@angular/common"; import {TeamPipe} from "../../pipes/team-pipe"; import {FullNamePipe} from "../../pipes/fullname-pipe"; import {Match} from "../../model/match"; import {MatFormField, MatInput} from "@angular/material/input"; import {FormsModule, ReactiveFormsModule} from "@angular/forms"; import {Result} from "../../model/result"; import {MatGridList, MatGridTile} from "@angular/material/grid-list"; import {Round} from "../../model/round"; import {Group} from "../../model/group"; import {Game} from "../../model/game"; import {TeamDisplayComponent} from "../team-display/team-display.component"; import {Event} from "../../model/event"; import {Tournament} from "../../model/tournament"; @Component({ selector: 'app-match-result', imports: [ MatDialogContent, MatDialogActions, MatButton, MatDialogClose, MatDialogTitle, MatInput, ReactiveFormsModule, FormsModule, MatFormField, MatGridList, MatGridTile, NgClass, TeamDisplayComponent ], providers: [ FullNamePipe, TeamPipe ], templateUrl: './match-result.component.html', standalone: true, styleUrl: './match-result.component.scss' }) export class MatchResultComponent { result: Result = new Result(); isValidResult: number = 0; constructor(@Inject(MAT_DIALOG_DATA) public data: {match: Match, tournament: Tournament, event: Event, group: Group, round: Round}) { this.result.matchId = this.data.match.id; if (data.match.games.length == 0) { this.result.games.push(new Game()); this.result.games.push(new Game()); this.result.games.push(new Game()); } else { for (let game of data.match.games) { this.result.games.push(game); } if (data.match.games.length == 2) { this.result.games.push(new Game()); } } } readonly dialogRef = inject(MatDialogRef); set21(team: number, game: number) { if (team == 1) { this.result.games[game - 1].score1 = 21; } if (team == 2) { this.result.games[game - 1].score2 = 21; } this.validateResult(); } onAnnulerenClick() { this.dialogRef.close(); } complementScores() { // console.log("in complementScores"); } validateResult(): void { let valid : boolean = true; valid &&= this.gameValid(this.result.games[0].score1, this.result.games[0].score2); valid &&= this.gameValid(this.result.games[1].score1, this.result.games[1].score2); if (this.result.games[2].score1 != undefined && this.result.games[2].score2 != undefined) { valid &&= this.gameValid(this.result.games[2].score1, this.result.games[2].score2); } this.isValidResult = valid ? this.matchResult(this.result) : 0; } gameValid(score1: number, score2: number): boolean { if (score1 == undefined) return false; if (score2 == undefined) return false; if (score1 < 0 || score1 > 30) return false; if (score2 < 0 || score2 > 30) return false; if (score1 == 21 && score2 <= 19) return true; if (score1 <= 19 && score2 == 21) return true; if (score1 == 30 && score2 == 29) return true; if (score1 == 29 && score2 == 30) return true; if (score1 >= 22 && score1 <= 30 && (score1 - score2) == 2) return true; if (score2 >= 22 && score2 <= 30 && (score2 - score1) == 2) return true; return false; } matchResult(result: Result): number { let team1Wins = 0; let team2Wins = 0; // Count wins for games 1 and 2 if (result.games[0].score1 > result.games[0].score2) team1Wins++; else team2Wins++; if (result.games[1].score1 > result.games[1].score2) team1Wins++; else team2Wins++; // If match is already decided (2-0), game 3 shouldn't have scores if (Math.max(team1Wins, team2Wins) == 2) { if (result.games[2].score1 != undefined || result.games[2].score2 != undefined) { return 0; // Invalid } return team1Wins > team2Wins ? 1 : -1; } // Match is 1-1, check game 3 if (result.games[2].score1 != undefined && result.games[2].score2 != undefined) { if (result.games[2].score1 > result.games[2].score2) team1Wins++; else team2Wins++; return team1Wins > team2Wins ? 1 : -1; } return 0; // Incomplete } }