Select Git revision
gameroom.component.ts
Forked from an inaccessible project.
gameroom.component.ts 1.95 KiB
import { Component, OnDestroy, OnInit, Pipe, PipeTransform } from '@angular/core';
import { SocketService } from '../services/socket.service';
import { Answer } from '../Types/types';
@Component({
selector: 'app-gameroom',
templateUrl: './gameroom.component.html'
})
export class GameroomComponent implements OnInit, OnDestroy {
playerNumber!: number;
gameStart: boolean = false;
question: string = "";
answers!: Answer[];
inRoom: boolean = false;
endGame: boolean = false;
scoreboard: { [username: string] : number; } = {};
points!: number;
constructor(
private socket: SocketService) {}
ngOnInit(): void {
this.socket.connectSocket();
this.gameStart = false;
this.inRoom = false;
this.endGame = false;
this.socket.playerNumber.subscribe(number => {
this.playerNumber = number;
})
this.socket.gameStart.subscribe(status => {
this.gameStart = status;
})
this.socket.questionID.subscribe(id => {
this.question = id;
})
this.socket.answers.subscribe(answers => {
this.answers = answers;
})
this.socket.points.subscribe(points => {
this.points = points;
})
this.socket.scoreboard.subscribe(scoreboard => {
this.scoreboard = scoreboard;
if (this.gameStart)
this.endGame = true;
})
}
ngOnDestroy(): void {
this.socket.disconnectSocket();
}
joinRoom(): void {
this.socket.joinRoom();
this.inRoom = true;
}
leaveRoom(): void {
this.socket.leaveRoom();
this.inRoom = false;
}
sendAnswer(answer: Answer) {
this.socket.sendAnswer(answer);
}
exitGame(): void {
this.endGame = false;
this.gameStart = false;
this.inRoom = false;
this.points = 0;
this.socket.leaveRoom();
}
}
@Pipe({ name: 'keys', pure: false })
export class KeysPipe implements PipeTransform {
transform(value: any, args: any[] = null): any {
return Object.keys(value);
}
}