Skip to content

Commit

Permalink
feat(public-server): add logic for public server
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuher committed Jan 29, 2025
1 parent cee53c6 commit 11c4ab2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/app/common/models/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export interface Server {
name: string;
owner: string;
riders?: number;
ridden?: boolean;
public?: boolean;
}
50 changes: 39 additions & 11 deletions src/app/common/services/server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ export class ServerService {
}

public getUserServers$(): Observable<Server[]> {
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(
Expand All @@ -38,12 +37,6 @@ export class ServerService {
);
}

public getPublicServers$(): Observable<Server[]> {
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<ServerEvent[]> {
return from(
environment.pb
Expand Down Expand Up @@ -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
};
})
)
Expand All @@ -128,7 +122,41 @@ export class ServerService {

private getOwnedServers$(): Observable<Server[]> {
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<Server[]> {
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<string, Server> = 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());
}
}
17 changes: 6 additions & 11 deletions src/app/pages/ride-online/ride-online.component.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<string>('', [Validators.required]);
protected serverName = new FormControl<string>('', [
Validators.required,
Validators.minLength(3),
Validators.maxLength(16)
]);
protected userServers: Signal<Server[] | undefined>;
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<string | null>(null);
protected creationError = signal<string | null>(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
Expand Down

0 comments on commit 11c4ab2

Please sign in to comment.