Skip to content
Snippets Groups Projects
Select Git revision
  • c9e4da9c71bd393f8ae3a20675b1769045f2e2ff
  • main default protected
  • documentation
  • admin_rights_refactor
4 results

gameroom.component.ts

Blame
  • 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);
            }
        }