diff --git a/frontend/src/app/feature/game/blackjack/blackjack.component.ts b/frontend/src/app/feature/game/blackjack/blackjack.component.ts index d6bfec4..e5dc78f 100644 --- a/frontend/src/app/feature/game/blackjack/blackjack.component.ts +++ b/frontend/src/app/feature/game/blackjack/blackjack.component.ts @@ -82,7 +82,8 @@ export default class BlackjackComponent { if (isGameOver) { console.log('Game is over, state:', game.state); - this.userService.refreshCurrentUser(); + this.refreshUserBalance(); + this.showGameResult.set(true); console.log('Game result dialog should be shown now'); } @@ -95,7 +96,7 @@ export default class BlackjackComponent { this.blackjackService.startGame(bet).subscribe({ next: (game) => { this.updateGameState(game); - this.userService.refreshCurrentUser(); + this.refreshUserBalance(); this.isActionInProgress.set(false); }, error: (error) => { @@ -114,9 +115,6 @@ export default class BlackjackComponent { this.blackjackService.hit(this.currentGameId()!).subscribe({ next: (game) => { this.updateGameState(game); - if (game.state !== 'IN_PROGRESS') { - this.userService.refreshCurrentUser(); - } this.isActionInProgress.set(false); }, error: (error) => { @@ -141,7 +139,6 @@ export default class BlackjackComponent { this.blackjackService.stand(this.currentGameId()!).subscribe({ next: (game) => { this.updateGameState(game); - this.userService.refreshCurrentUser(); this.isActionInProgress.set(false); }, error: (error) => { @@ -166,7 +163,6 @@ export default class BlackjackComponent { this.blackjackService.doubleDown(this.currentGameId()!).subscribe({ next: (game) => { this.updateGameState(game); - this.userService.refreshCurrentUser(); this.isActionInProgress.set(false); }, error: (error) => { diff --git a/frontend/src/app/feature/game/blackjack/components/game-info/game-info.component.ts b/frontend/src/app/feature/game/blackjack/components/game-info/game-info.component.ts index fea5453..6219b98 100644 --- a/frontend/src/app/feature/game/blackjack/components/game-info/game-info.component.ts +++ b/frontend/src/app/feature/game/blackjack/components/game-info/game-info.component.ts @@ -18,6 +18,10 @@ import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angula

Spiel Informationen

+
+ Guthaben: + {{ balance | currency: 'EUR' }} +
Aktuelle Wette: diff --git a/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.ts b/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.ts index b76baa5..0044e0d 100644 --- a/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.ts +++ b/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.ts @@ -86,7 +86,7 @@ export class GameResultComponent { visible = false; get isWin(): boolean { - return this.gameState === GameState.PLAYER_WON || this.gameState === GameState.PLAYER_BLACKJACK; + return this.gameState === GameState.PLAYER_WON; } get isLoss(): boolean { @@ -98,7 +98,6 @@ export class GameResultComponent { } getResultTitle(): string { - if (this.gameState === GameState.PLAYER_BLACKJACK) return 'Blackjack!'; if (this.isWin) return 'Gewonnen!'; if (this.isLoss) return 'Verloren!'; if (this.isDraw) return 'Unentschieden!'; @@ -106,8 +105,6 @@ export class GameResultComponent { } getResultMessage(): string { - if (this.gameState === GameState.PLAYER_BLACKJACK) - return 'Glückwunsch! Du hast mit einem Blackjack gewonnen!'; if (this.isWin) return 'Glückwunsch! Du hast diese Runde gewonnen.'; if (this.isLoss) return 'Schade! Du hast diese Runde verloren.'; if (this.isDraw) return 'Diese Runde endet unentschieden. Dein Einsatz wurde zurückgegeben.'; @@ -115,7 +112,6 @@ export class GameResultComponent { } getResultClass(): string { - if (this.gameState === GameState.PLAYER_BLACKJACK) return 'text-emerald font-bold'; if (this.isWin) return 'text-emerald'; if (this.isLoss) return 'text-accent-red'; if (this.isDraw) return 'text-yellow-400'; diff --git a/frontend/src/app/feature/game/blackjack/enum/gameState.ts b/frontend/src/app/feature/game/blackjack/enum/gameState.ts index 977e16d..69e3ddf 100644 --- a/frontend/src/app/feature/game/blackjack/enum/gameState.ts +++ b/frontend/src/app/feature/game/blackjack/enum/gameState.ts @@ -3,5 +3,4 @@ export enum GameState { IN_PROGRESS = 'IN_PROGRESS', PLAYER_LOST = 'PLAYER_LOST', DRAW = 'DRAW', - PLAYER_BLACKJACK = 'PLAYER_BLACKJACK', } diff --git a/frontend/src/app/service/user.service.ts b/frontend/src/app/service/user.service.ts index 2ad53ce..20573ff 100644 --- a/frontend/src/app/service/user.service.ts +++ b/frontend/src/app/service/user.service.ts @@ -1,7 +1,7 @@ import { inject, Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { KeycloakProfile } from 'keycloak-js'; -import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs'; +import { catchError, EMPTY, Observable } from 'rxjs'; import { User } from '../model/User'; @Injectable({ @@ -9,39 +9,20 @@ import { User } from '../model/User'; }) export class UserService { private http: HttpClient = inject(HttpClient); - private currentUserSubject = new BehaviorSubject(null); - public currentUser$ = this.currentUserSubject.asObservable(); - - constructor() { - // Initialize with current user data - this.getCurrentUser().subscribe(); - } public getUser(id: string): Observable { - return this.http.get(`/backend/user/${id}`).pipe( - catchError(() => EMPTY), - tap((user) => this.currentUserSubject.next(user)) - ); + return this.http.get(`/backend/user/${id}`).pipe(catchError(() => EMPTY)); } public getCurrentUser(): Observable { - return this.http.get('/backend/user').pipe( - catchError(() => EMPTY), - tap((user) => this.currentUserSubject.next(user)) - ); - } - - public refreshCurrentUser(): void { - this.getCurrentUser().subscribe(); + return this.http.get('/backend/user').pipe(catchError(() => EMPTY)); } public createUser(id: string, username: string): Observable { - return this.http - .post('/backend/user', { - keycloakId: id, - username: username, - }) - .pipe(tap((user) => this.currentUserSubject.next(user))); + return this.http.post('/backend/user', { + keycloakId: id, + username: username, + }); } public async getOrCreateUser(userProfile: KeycloakProfile) { diff --git a/frontend/src/app/shared/components/navbar/navbar.component.html b/frontend/src/app/shared/components/navbar/navbar.component.html index 040e7cf..ee95fa2 100644 --- a/frontend/src/app/shared/components/navbar/navbar.component.html +++ b/frontend/src/app/shared/components/navbar/navbar.component.html @@ -18,7 +18,7 @@
- {{ balance() | currency: 'EUR' : 'symbol' : '1.2-2' }} + Guthaben: {{ balance() | currency: 'EUR' : 'symbol' : '1.2-2' }}
} diff --git a/frontend/src/app/shared/components/navbar/navbar.component.ts b/frontend/src/app/shared/components/navbar/navbar.component.ts index 6b972ac..adf7b78 100644 --- a/frontend/src/app/shared/components/navbar/navbar.component.ts +++ b/frontend/src/app/shared/components/navbar/navbar.component.ts @@ -1,17 +1,9 @@ -import { - ChangeDetectionStrategy, - Component, - inject, - OnInit, - OnDestroy, - signal, -} from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject, OnInit, signal } from '@angular/core'; import { RouterModule } from '@angular/router'; import { KeycloakService } from 'keycloak-angular'; + import { CurrencyPipe } from '@angular/common'; import { UserService } from '@service/user.service'; -import { Subscription } from 'rxjs'; - @Component({ selector: 'app-navbar', templateUrl: './navbar.component.html', @@ -19,27 +11,22 @@ import { Subscription } from 'rxjs'; imports: [RouterModule, CurrencyPipe], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class NavbarComponent implements OnInit, OnDestroy { +export class NavbarComponent implements OnInit { isMenuOpen = false; private keycloakService: KeycloakService = inject(KeycloakService); isLoggedIn = this.keycloakService.isLoggedIn(); private userService = inject(UserService); - private userSubscription: Subscription | undefined; + private user = this.userService.getCurrentUser(); + public balance = signal(0); ngOnInit() { - this.userSubscription = this.userService.currentUser$.subscribe((user) => { + this.user.subscribe((user) => { this.balance.set(user?.balance ?? 0); }); } - ngOnDestroy() { - if (this.userSubscription) { - this.userSubscription.unsubscribe(); - } - } - login() { try { const baseUrl = window.location.origin;