diff --git a/src/app/common/models/server.ts b/src/app/common/models/server.ts index 161ef42..740062d 100644 --- a/src/app/common/models/server.ts +++ b/src/app/common/models/server.ts @@ -3,4 +3,6 @@ export interface Server { name: string; owner: string; riders?: number; + ridden?: boolean; + public?: boolean; } diff --git a/src/app/common/services/server.service.ts b/src/app/common/services/server.service.ts index 1f2847c..e21b38a 100644 --- a/src/app/common/services/server.service.ts +++ b/src/app/common/services/server.service.ts @@ -22,9 +22,8 @@ export class ServerService { } public getUserServers$(): Observable { - return combineLatest([this.getOwnedServers$(), this.getRiddenServers$()]).pipe( - map(([owned, ridden]) => [...owned, ...ridden]), - map(servers => servers.filter((server, index, self) => index === self.findIndex(t => t.id === server.id))), + return combineLatest([this.getPublicServers$(), this.getOwnedServers$(), this.getRiddenServers$()]).pipe( + map(([publics, owned, ridden]) => this.combineServers([...publics, ...owned, ...ridden])), mergeAll(), concatMap(server => { return this.getRiders$(server.id).pipe( @@ -38,12 +37,6 @@ export class ServerService { ); } - public getPublicServers$(): Observable { - return from(environment.pb.collection('public_servers').getFullList()).pipe( - map(servers => servers.map(server => ({ id: server['id'], name: server['name'], owner: server['owner'] }))) - ); - } - public getEvents$(serverId: string): Observable { return from( environment.pb @@ -119,7 +112,8 @@ export class ServerService { return { id: participation['server'] as string, name: participation['name'] as string, - owner: participation['owner'] as string + owner: participation['owner'] as string, + ridden: true }; }) ) @@ -128,7 +122,41 @@ export class ServerService { private getOwnedServers$(): Observable { return from(environment.pb.collection('servers').getFullList({ sort: '-updated' })).pipe( - map(servers => servers.map(server => ({ id: server['id'], name: server['name'], owner: server['owner'] }))) + map(servers => + servers.map(server => ({ + id: server['id'], + name: server['name'], + owner: server['owner'] + })) + ) + ); + } + + private getPublicServers$(): Observable { + return from(environment.pb.collection('public_servers').getFullList()).pipe( + map(servers => + servers.map(server => ({ + id: server['id'], + name: server['name'], + owner: server['owner'], + public: true + })) + ) ); } + + private combineServers(servers: Server[]): Server[] { + const combinedServers: Map = new Map(); + for (const server of servers) { + const existing = combinedServers.get(server.id); + if (existing) { + existing.ridden = existing.ridden || server.ridden; + existing.public = existing.public || server.public; + combinedServers.set(existing.id, existing); + } else { + combinedServers.set(server.id, server); + } + } + return Array.from(combinedServers.values()); + } } diff --git a/src/app/pages/ride-online/ride-online.component.ts b/src/app/pages/ride-online/ride-online.component.ts index 72f0f29..27f69fe 100644 --- a/src/app/pages/ride-online/ride-online.component.ts +++ b/src/app/pages/ride-online/ride-online.component.ts @@ -1,4 +1,4 @@ -import { ChangeDetectionStrategy, Component, inject, signal, type Signal } from '@angular/core'; +import { ChangeDetectionStrategy, Component, computed, inject, signal, type Signal } from '@angular/core'; import { ButtonIconComponent } from '../../common/components/button-icon/button-icon.component'; import { ToolbarComponent } from '../../common/components/toolbar/toolbar.component'; import { Router, RouterLink } from '@angular/router'; @@ -24,26 +24,21 @@ export class RideOnlineComponent extends Destroyable { private router = inject(Router); private serverService = inject(ServerService); - protected user: User | null; + protected user = this.authService.getUser(); protected serverCode = new FormControl('', [Validators.required]); protected serverName = new FormControl('', [ Validators.required, Validators.minLength(3), Validators.maxLength(16) ]); - protected userServers: Signal; - protected publicServers = toSignal(this.serverService.getPublicServers$()); + + private servers = toSignal(this.serverService.getUserServers$()); + protected userServers = computed(() => this.servers()?.filter(s => s.owner === this.user?.id || s.ridden)); + protected publicServers = computed(() => this.servers()?.filter(s => s.public)); protected connectionError = signal(null); protected creationError = signal(null); - constructor() { - super(); - - this.user = this.authService.getUser(); - this.userServers = toSignal(this.serverService.getUserServers$()); - } - protected createServer(): void { if (this.serverName.valid && this.user) { this.serverService