Skip to content

Commit

Permalink
websocket for Raid (update quest/task participant)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalinow committed Apr 3, 2020
1 parent 4b83103 commit ede02f1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ActivatedRoute, Router, ParamMap } from '@angular/router';
import { switchMap } from 'rxjs/operators';
import { Subscription } from 'rxjs';
import { Task } from 'app/Interfaces/Task';
import { EventService } from 'app/services/event.service';

@Component({
selector: 'app-raid-page',
Expand All @@ -22,13 +23,19 @@ export class RaidPageComponent implements OnInit, OnDestroy {
public raidLeaderBoardList: RaidLeaderboard[] = [];
private raidSubscription: Subscription;

constructor(private route: ActivatedRoute, private router: Router, private raidService: RaidService) { }
constructor(private route: ActivatedRoute, private router: Router, private raidService: RaidService,
private eventService: EventService) { }

ngOnDestroy(): void {
this.raidSubscription.unsubscribe();
}

ngOnInit() {
this.getRaid();
this.subscribeTaskEvent();
}

getRaid() {
const id = this.route.snapshot.paramMap.get('id');
this.raidSubscription = this.raidService.findRaidById(id).subscribe(resp => {
this.monster = new BaseMonster(resp.monsterName, resp.monsterImage,
Expand All @@ -38,4 +45,10 @@ export class RaidPageComponent implements OnInit, OnDestroy {
this.raidLeaderBoardList = resp.raidLeaderboardList;
});
}

private subscribeTaskEvent() {
this.eventService.taskEvents$.subscribe(updated => {
this.getRaid();
});
}
}
13 changes: 13 additions & 0 deletions sonarQuest-frontend/src/app/services/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class EventService implements OnChanges {
public userDtos$ = this.userDtosSubject.asObservable();
private unseenEventsSubject: Subject<boolean> = new ReplaySubject(1);
public unseenEvents$ = this.unseenEventsSubject.asObservable();

private taskEventsSubject: Subject<boolean> = new ReplaySubject(1);
public taskEvents$ = this.taskEventsSubject.asObservable();

eventDtos: EventDto[] = [];
userDtos: UserDto[] = [];

Expand Down Expand Up @@ -176,6 +180,15 @@ export class EventService implements OnChanges {
}
}

//TODO: Refactoring this! Issue: https://github.com/viadee/sonarQuest/issues/266
public eventUpdateTask(): Observable<boolean> {
const currentUrl = this.router.url.substring(1);
if (currentUrl !== RoutingUrls.events) {
this.taskEventsSubject.next(true);
return this.taskEventsSubject.asObservable();
}
}

private handleError(error: HttpErrorResponse | any) {
let errMsg: string;
if (error instanceof HttpErrorResponse) {
Expand Down
6 changes: 6 additions & 0 deletions sonarQuest-frontend/src/app/services/websocket.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RaidService } from 'app/services/raid.service';
import { EventUserDto } from './../Interfaces/EventUserDto';
import { World } from '../Interfaces/World';
import { WorldService } from './world.service';
Expand Down Expand Up @@ -41,6 +42,11 @@ export class WebsocketService {
that.eventService.addEvent(eventUserDto);
});
});
this.stompClient.connect({}, function(frame) {
this.chatStomp = that.stompClient.subscribe('/raid', raid => {
that.eventService.eventUpdateTask();
});
});
}

sendMessage(message) {
Expand Down

0 comments on commit ede02f1

Please sign in to comment.