Fusion : séances accessibles et règles sous la main en séance
# Conflicts: # web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.ts # web/src/assets/i18n/en.json # web/src/assets/i18n/fr.json
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { NavigationEnd, Router } from '@angular/router';
|
||||
import { BehaviorSubject, Observable, forkJoin, merge, of } from 'rxjs';
|
||||
import { catchError, filter, map, startWith, switchMap } from 'rxjs/operators';
|
||||
import { SessionService } from './session.service';
|
||||
import { PlaythroughService } from './playthrough.service';
|
||||
import { CampaignService } from './campaign.service';
|
||||
import { Session } from './session.model';
|
||||
|
||||
/** Séance en cours enrichie de quoi l'identifier (Partie + Campagne). */
|
||||
export interface ActiveSessionEntry {
|
||||
session: Session;
|
||||
playthroughName: string | null;
|
||||
campaignId: string | null;
|
||||
campaignName: string | null;
|
||||
}
|
||||
|
||||
interface PlaythroughLabel {
|
||||
playthroughName: string | null;
|
||||
campaignId: string | null;
|
||||
campaignName: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* État partagé des séances en cours (toutes Parties confondues), la plus récente d'abord.
|
||||
*
|
||||
* <p>Alimente le raccourci « Séance en cours » du menu principal et le bloc « Jouer »
|
||||
* du hub campagne : une séance ne doit plus se perdre au fond d'une page. Rafraîchi à
|
||||
* chaque navigation et après chaque démarrage/fin/suppression ({@link SessionService.changed$}).
|
||||
* Les libellés Partie/Campagne sont mis en cache (une séance active change rarement de Partie).</p>
|
||||
*/
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ActiveSessionsService {
|
||||
private readonly subject = new BehaviorSubject<ActiveSessionEntry[]>([]);
|
||||
readonly active$: Observable<ActiveSessionEntry[]> = this.subject.asObservable();
|
||||
|
||||
private readonly labels = new Map<string, PlaythroughLabel>();
|
||||
|
||||
constructor(
|
||||
router: Router,
|
||||
private sessionService: SessionService,
|
||||
private playthroughService: PlaythroughService,
|
||||
private campaignService: CampaignService
|
||||
) {
|
||||
merge(
|
||||
router.events.pipe(filter(e => e instanceof NavigationEnd)),
|
||||
this.sessionService.changed$
|
||||
).pipe(
|
||||
startWith(null),
|
||||
// switchMap : une navigation rapide annule le fetch précédent (pas de réponse périmée).
|
||||
switchMap(() => this.fetch())
|
||||
).subscribe(list => this.subject.next(list));
|
||||
}
|
||||
|
||||
/** Force un rafraîchissement (ex. après une action hors SessionService). */
|
||||
refresh(): void {
|
||||
this.fetch().subscribe(list => this.subject.next(list));
|
||||
}
|
||||
|
||||
private fetch(): Observable<ActiveSessionEntry[]> {
|
||||
return this.sessionService.getActiveSessions().pipe(
|
||||
// Back indisponible / ancienne version : on garde l'état connu plutôt que de faire clignoter.
|
||||
catchError(() => of(null)),
|
||||
switchMap(sessions => {
|
||||
if (!sessions) return of(this.subject.value);
|
||||
if (sessions.length === 0) return of([] as ActiveSessionEntry[]);
|
||||
return forkJoin(sessions.map(s => this.labelOf(s.playthroughId).pipe(
|
||||
map(label => ({ session: s, ...label }))
|
||||
)));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private labelOf(playthroughId: string): Observable<PlaythroughLabel> {
|
||||
const cached = this.labels.get(playthroughId);
|
||||
if (cached) return of(cached);
|
||||
const unknown: PlaythroughLabel = { playthroughName: null, campaignId: null, campaignName: null };
|
||||
return this.playthroughService.getById(playthroughId).pipe(
|
||||
switchMap(p => this.campaignService.getCampaignById(p.campaignId).pipe(
|
||||
map(c => ({ playthroughName: p.name, campaignId: p.campaignId, campaignName: c.name })),
|
||||
catchError(() => of({ ...unknown, playthroughName: p.name, campaignId: p.campaignId }))
|
||||
)),
|
||||
map(label => { this.labels.set(playthroughId, label); return label; }),
|
||||
catchError(() => of(unknown))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Campaign, CampaignCreate, Arc, ArcCreate, Chapter, ChapterCreate, Scene, SceneCreate, Quest } from './campaign.model';
|
||||
import { Campaign, CampaignCreate, Arc, ArcCreate, Chapter, ChapterCreate, Scene, SceneCreate, Quest, Playthrough } from './campaign.model';
|
||||
import { CampaignReadinessAssessment } from './readiness.model';
|
||||
import { Npc } from './npc.model';
|
||||
import { RandomTable } from './random-table.model';
|
||||
@@ -19,6 +19,8 @@ export interface CampaignTreeResponse {
|
||||
randomTables: RandomTable[];
|
||||
enemies: Enemy[];
|
||||
quests: Quest[];
|
||||
/** Parties (Playthroughs) de la campagne — nœud « Parties » de l'arbre. */
|
||||
playthroughs?: Playthrough[];
|
||||
readiness: CampaignReadinessAssessment;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Observable, Subject, tap } from 'rxjs';
|
||||
import { Session } from './session.model';
|
||||
|
||||
/**
|
||||
@@ -13,11 +13,23 @@ import { Session } from './session.model';
|
||||
export class SessionService {
|
||||
private apiUrl = '/api/sessions';
|
||||
|
||||
/**
|
||||
* Émet après chaque mutation qui change l'ensemble des séances en cours
|
||||
* (démarrage, fin, renommage, suppression) — écouté par {@link ActiveSessionsService}
|
||||
* pour rafraîchir le raccourci « Séance en cours » sans attendre une navigation.
|
||||
*/
|
||||
readonly changed$ = new Subject<void>();
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
/** Lance une nouvelle session sur la Partie (Playthrough) donnée. */
|
||||
startSession(playthroughId: string): Observable<Session> {
|
||||
return this.http.post<Session>(this.apiUrl, { playthroughId });
|
||||
return this.http.post<Session>(this.apiUrl, { playthroughId }).pipe(tap(() => this.changed$.next()));
|
||||
}
|
||||
|
||||
/** Toutes les séances en cours (toutes Parties confondues), la plus récente d'abord. */
|
||||
getActiveSessions(): Observable<Session[]> {
|
||||
return this.http.get<Session[]>(this.apiUrl, { params: new HttpParams().set('active', 'true') });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,15 +61,15 @@ export class SessionService {
|
||||
}
|
||||
|
||||
endSession(id: string): Observable<Session> {
|
||||
return this.http.post<Session>(`${this.apiUrl}/${id}/end`, {});
|
||||
return this.http.post<Session>(`${this.apiUrl}/${id}/end`, {}).pipe(tap(() => this.changed$.next()));
|
||||
}
|
||||
|
||||
renameSession(id: string, name: string): Observable<Session> {
|
||||
return this.http.patch<Session>(`${this.apiUrl}/${id}`, { name });
|
||||
return this.http.patch<Session>(`${this.apiUrl}/${id}`, { name }).pipe(tap(() => this.changed$.next()));
|
||||
}
|
||||
|
||||
deleteSession(id: string): Observable<void> {
|
||||
return this.http.delete<void>(`${this.apiUrl}/${id}`);
|
||||
return this.http.delete<void>(`${this.apiUrl}/${id}`).pipe(tap(() => this.changed$.next()));
|
||||
}
|
||||
|
||||
/** Épingle (sceneId) ou dés-épingle (null) la scène courante — mode cockpit. */
|
||||
|
||||
Reference in New Issue
Block a user