import { Injectable, inject } from '@angular/core'; import { BehaviorSubject, Observable, Subject } from 'rxjs'; import { Challenge } from '../models/challenge.models'; /** * Service to manage challenge events via WebSocket * Listens for incoming challenges and emits them to subscribers */ @Injectable({ providedIn: 'root' }) export class ChallengeEventService { private readonly incomingChallenges$ = new BehaviorSubject([]); private readonly challengeReceived$ = new Subject(); private readonly challengeAccepted$ = new Subject(); private readonly challengeDeclined$ = new Subject(); getIncomingChallenges$(): Observable { return this.incomingChallenges$.asObservable(); } getChallengeReceived$(): Observable { return this.challengeReceived$.asObservable(); } getChallengeAccepted$(): Observable { return this.challengeAccepted$.asObservable(); } getChallengeDeclined$(): Observable { return this.challengeDeclined$.asObservable(); } /** * Called when a new challenge is received via WebSocket */ onChallengeReceived(challenge: Challenge): void { const current = this.incomingChallenges$.value; this.incomingChallenges$.next([...current, challenge]); this.challengeReceived$.next(challenge); } /** * Called when a challenge is accepted */ onChallengeAccepted(challenge: Challenge): void { const current = this.incomingChallenges$.value; this.incomingChallenges$.next(current.filter(c => c.id !== challenge.id)); this.challengeAccepted$.next(challenge); } /** * Called when a challenge is declined or expires */ onChallengeRemoved(challengeId: string): void { const current = this.incomingChallenges$.value; this.incomingChallenges$.next(current.filter(c => c.id !== challengeId)); } /** * Remove a challenge from the incoming list */ removeChallenge(challengeId: string): void { this.onChallengeRemoved(challengeId); } }