import { Injectable } from '@angular/core' ; import { GameEngine } from '@runox-game/game-engine' ; import { GameModes } from '@runox-game/game-engine/lib/models/game-modes' ; import { environment } from '../environments/environment' ; import { IPlayer , Player , } from '@runox-game/game-engine/lib/models/player.model' ; import { ICard } from '@runox-game/game-engine/lib/models/card.model' ; import { ILog } from '@runox-game/game-engine/lib/log/log.factory' ; import { IGameState } from '@runox-game/game-engine/lib/models/game-state.model' ; import { Observable } from 'rxjs' ; import { tap } from 'rxjs/internal/operators/tap' ; import { filter } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' , } ) export class GameEngineService { game = new GameEngine ( ) ; public playerId : string ; logs$ : Observable < ILog > ; constructor ( ) { } get players ( ) : IPlayer [ ] { return this . game . players ; } create ( ) { this . game . reset ( ) ; } startGame ( randomTakeDeckCard = environment . gameMode . randomTakeDeckCard ) { const gameModes = { randomTakeDeckCard : randomTakeDeckCard , } ; this . logs$ = this . game . logs ( ) ; return this . game . start ( gameModes ) ; } joinUser ( user ) { this . playerId = user . id ; if ( ! this . game . players . find ( x => x . id === this . playerId ) ) { this . game . join ( [ user ] ) . subscribe ( ) ; } } removeUser ( user ) { this . game . remove ( user ) . subscribe ( ) ; } playCard ( card ) { this . game . playCard ( this . game . playerTurn ?. id , card ) . subscribe ( ) ; } takeCard ( ) { this . game . takeCard ( ) ; } poneleeeeUno ( userId ) { this . game . uno ( userId ) . subscribe ( ) ; } overrideInternalState ( data ) { this . game . overrideInternalState ( data ) ; } gameStateAsJSON ( ) { return this . game . gameStateAsJSON ; } onStateChanged ( ) { return this . game . onStateChanged ( ) ; } userMessages ( ) { return this . logs$ ; } onCardPlayed ( ) { return this . game . onCardPlayed ( ) . pipe ( filter ( ( card ) => ! ! card ) ) ; } onSpecialCardPlayed ( ) { return this . game . onSpecialCardPlayed ( ) . pipe ( filter ( ( card ) => ! ! card ) ) ; } loggedUser ( ) { return this . game . players . find ( ( player ) => player . id === this . playerId ) ; } }