SES-116 Get lsit of logged characters

This commit is contained in:
Łukasz Góreczny 2020-12-27 21:17:19 +01:00
parent fe5921fa57
commit 2a5e04bbd1
5 changed files with 84 additions and 6 deletions

View File

@ -26,6 +26,7 @@ import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { reducers} from './reducers';
import {AppReducer} from './store/reducers/app.reducer';
import {environment} from '../environments/environment';
import {CharacterService} from '../services/character.service';
@NgModule({
declarations: [
@ -60,7 +61,8 @@ BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
})
],
providers: [
UserService
UserService,
CharacterService
],
bootstrap: [AppComponent]
})

View File

@ -1,6 +1,10 @@
import {Component, OnInit} from '@angular/core';
import {Component, OnDestroy, OnInit} from '@angular/core';
import {animateText, onSideNavChange} from '../../shared/animations/sidenav-animations';
import {GMSignalRService} from '../../shared/signalR-service/gm-signalR.service';
import {CharacterService} from '../../../services/character.service';
import {Subscription} from 'rxjs';
import {ErrorResponse} from '../../../types/ErrorResponse';
import {HttpErrorResponse} from '@angular/common/http';
@Component({
selector: 'app-game-master-dashboard',
@ -8,13 +12,16 @@ import {GMSignalRService} from '../../shared/signalR-service/gm-signalR.service'
styleUrls: ['./game-master-dashboard.component.css'],
animations: [onSideNavChange, animateText]
})
export class GameMasterDashboardComponent implements OnInit {
export class GameMasterDashboardComponent implements OnInit, OnDestroy {
allSubscriptions = new Subscription();
leftSidenavExpanded = false;
leftSidenavTextExpanded = false;
rightSidenavExpanded = false;
rightSidenavTextExpanded = false;
constructor(private signalRService: GMSignalRService) {}
constructor(private signalRService: GMSignalRService, private characterService: CharacterService) {
this.SubscribeToEvents();
}
ngOnInit() {
this.signalRService.Login();
@ -36,4 +43,30 @@ export class GameMasterDashboardComponent implements OnInit {
break;
}
}
UpdateCharactersList(): void {
this.allSubscriptions.add(
this.characterService.getLoggedCharacters().subscribe((success) => {
console.log(success);
},
(error: ErrorResponse | HttpErrorResponse) => {
if (error instanceof HttpErrorResponse) {
error = error.error as ErrorResponse;
}
console.error(error.message);
}));
}
private SubscribeToEvents(): void {
debugger
this.signalRService.message.subscribe((message: string) => {
if (message === 'New player connected' || message === 'Player disconnected') {
this.UpdateCharactersList();
}
});
}
ngOnDestroy() {
this.allSubscriptions.unsubscribe();
}
}

View File

@ -1,12 +1,15 @@
import { Inject, Injectable } from '@angular/core';
import { SignalRService } from './base/signalR.service';
import {Subject} from 'rxjs';
@Injectable({ providedIn: 'root' })
export class GMSignalRService {
signalR: SignalRService;
message: Subject<string>;
constructor(@Inject('BASE_URL') baseUrl: string) {
this.signalR = new SignalRService(baseUrl);
this.message = new Subject<string>();
this.registerOnServerEvents();
}
@ -17,10 +20,15 @@ export class GMSignalRService {
if (this.signalR.connectionEstablished$.getValue() === true) {
this.signalR.hubConnection.send('GameMasterLogin');
}
});
}).unsubscribe();
}
private registerOnServerEvents(): void {
this.signalR.hubConnection.on('Welcome', (message: string) => {
this.message.next('New player connected');
});
this.signalR.hubConnection.on('GoodBye', (message: string) => {
this.message.next('Player disconnected');
});
}
}

View File

@ -0,0 +1,29 @@
import {Inject, Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable, of, throwError} from 'rxjs';
import {ErrorResponse} from '../types/ErrorResponse';
import {Either} from '../types/Either';
import {switchMap} from 'rxjs/operators';
import {LoggedCharactersViewModel} from '../types/viewmodels/character-viewmodels/LoggedCharactersViewModel';
Injectable({
providedIn: 'root'
})
export class CharacterService {
private baseUrl = 'api/character/';
constructor(private http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
this.baseUrl = baseUrl + this.baseUrl;
}
getLoggedCharacters(): Observable<LoggedCharactersViewModel[]> {
return this.http.get<Either<LoggedCharactersViewModel[], ErrorResponse>>(this.baseUrl + 'loggedCharacters').pipe(
switchMap(response => {
if (response.isLeft) {
return of(response.left);
} else {
return throwError(response.right);
}
})
);
}
}

View File

@ -0,0 +1,6 @@
export interface LoggedCharactersViewModel {
id: number;
name: string;
level: number;
currentHealthPoints: number;
}