import { Injectable } from '@angular/core' ; import { Effect , Actions , ofType } from '@ngrx/effects' ; import { switchMap , map , filter , mergeMap , catchError } from 'rxjs/operators' ; import { Store } from '@ngrx/store' ; import { GameService , Utils } from 'shared-library/core/services' ; import { Game , Question , RouterStateUrl } from 'shared-library/shared/model' ; import { GamePlayActionTypes } from '../actions' ; import * as gameplayactions from '../actions/game-play.actions' ; import { GameActions } from 'shared-library/core/store/actions/game.actions' ; import { GamePlayState } from '../reducers' ; import { ROUTER_NAVIGATION } from '@ngrx/router-store' ; import { of } from 'rxjs' ; @ Injectable ( ) export class GamePlayEffects { @ Effect ( ) startNewGame$ = this . actions$ . pipe ( ofType ( GamePlayActionTypes . CREATE_NEW ) ) . pipe ( switchMap ( ( action ) => this . svc . createNewGame ( action . payload . gameOptions , action . payload . user ) . pipe ( mergeMap ( ( gameId ) => this . utils . setNewGameFirebaseAnalyticsParameter ( action . payload . gameOptions , action . payload . user . userId , gameId ) ) , map ( ( gameId ) => this . gameActions . createNewGameSuccess ( gameId ) ) , catchError ( ( error ) => { return of ( this . gameActions . createNewGameError ( error . error ) ) ; } ) ) ) ) ; @ Effect ( ) loadGame$ = this . actions$ . pipe ( ofType ( GamePlayActionTypes . LOAD_GAME ) ) . pipe ( switchMap ( ( action ) => this . svc . getGame ( action . payload ) . pipe ( map ( ( game ) => new gameplayactions . LoadGameSuccess ( game ) ) ) ) ) ; @ Effect ( ) loadGame2$ = this . actions$ . pipe ( ofType ( ROUTER_NAVIGATION ) ) . pipe ( map ( ( action ) : RouterStateUrl => action . payload . routerState ) , filter ( ( routerState ) => { if ( routerState . url . toLowerCase ( ) . startsWith ( '/game-play/' ) && ! routerState . url . toLowerCase ( ) . startsWith ( '/game-play/challenge' ) && ! routerState . url . toLowerCase ( ) . startsWith ( '/game-play/play-game-with-random-user' ) && ! routerState . url . toLowerCase ( ) . startsWith ( '/game-play/play-game-with-friend' ) && ! routerState [ 'url' ] . toLowerCase ( ) . startsWith ( '/game-play/game-option' ) && routerState . params ) { return true ; } else if ( ( routerState . url . toLowerCase ( ) . startsWith ( '/game-play/' ) || routerState . url . toLowerCase ( ) . startsWith ( '/game-play/challenge' ) || routerState . url . toLowerCase ( ) . startsWith ( '/game-play/play-game-with-random-user' ) || routerState . url . toLowerCase ( ) . startsWith ( '/game-play/play-game-with-friend' ) ) && ( routerState [ 'root' ] && ! routerState . url . toLowerCase ( ) . startsWith ( '/game-play/challenge' ) && ! routerState . url . toLowerCase ( ) . startsWith ( '/game-play/play-game-with-random-user' ) && ! routerState . url . toLowerCase ( ) . startsWith ( '/game-play/play-game-with-friend' ) ) && ( routerState [ 'root' ] && ! routerState [ 'url' ] . toLowerCase ( ) . startsWith ( '/game-play/game-option' ) ) ) { return true ; } else { return false ; } } ) ) . pipe ( switchMap ( ( routerState ) => { let gameid = '' ; if ( routerState . params ) { gameid = routerState . params . gameid ; } else if ( routerState [ 'root' ] . firstChild . firstChild . params . gameid ) { gameid = routerState [ 'root' ] . firstChild . firstChild . params . gameid ; } this . store . dispatch ( new gameplayactions . ResetCurrentGame ( ) ) ; return this . svc . getGame ( gameid ) . pipe ( map ( ( game ) => new gameplayactions . LoadGameSuccess ( game ) ) ) ; } ) ) ; @ Effect ( ) loadNextQuestion$ = this . actions$ . pipe ( ofType ( GamePlayActionTypes . GET_NEXT_QUESTION ) ) . pipe ( switchMap ( ( action ) => this . svc . getNextQuestion ( action . payload ) . pipe ( map ( ( question ) => new gameplayactions . GetNextQuestionSuccess ( question ) ) ) ) ) ; @ Effect ( ) addPlayerQnA$ = this . actions$ . pipe ( ofType ( GamePlayActionTypes . ADD_PLAYER_QNA ) ) . pipe ( switchMap ( ( action ) => this . svc . addPlayerQnAToGame ( action . payload . gameId , action . payload . playerQnA ) . pipe ( map ( ( msg ) => new gameplayactions . UpdateGameSuccess ( ) ) ) ) ) ; @ Effect ( ) setGameOver$ = this . actions$ . pipe ( ofType ( GamePlayActionTypes . SET_GAME_OVER ) ) . pipe ( switchMap ( ( action ) => this . svc . setGameOver ( action . payload . playedGame . gameId ) . pipe ( mergeMap ( ( msg ) => this . utils . setEndGameFirebaseAnalyticsParameter ( action . payload . playedGame , action . payload . userId , action . payload . otherUserId ) ) , map ( ( msg ) => new gameplayactions . UpdateGameSuccess ( ) ) ) ) ) ; @ Effect ( ) getUserAnsweredQuestions$ = this . actions$ . pipe ( ofType ( GamePlayActionTypes . GET_USERS_ANSWERED_QUESTION ) ) . pipe ( switchMap ( ( action ) => this . svc . getUsersAnsweredQuestion ( action . payload . userId , action . payload . game ) . pipe ( map ( ( questionArray ) => new gameplayactions . GetUsersAnsweredQuestionSuccess ( questionArray ) ) ) ) ) ; @ Effect ( ) reportQuestion$ = this . actions$ . pipe ( ofType ( GamePlayActionTypes . SAVE_REPORT_QUESTION ) ) . pipe ( switchMap ( ( action ) => this . svc . saveReportQuestion ( action . payload . reportQuestion , action . payload . game ) . pipe ( mergeMap ( ( status ) => this . svc . updateGame ( action . payload . reportQuestion , action . payload . game ) ) ) . pipe ( map ( ( report ) => new gameplayactions . SaveReportQuestionSuccess ( ) ) ) ) ) ; @ Effect ( ) UpdateGameRound$ = this . actions$ . pipe ( ofType ( GamePlayActionTypes . UPDATE_GAME_ROUND ) ) . pipe ( switchMap ( ( action ) => this . svc . updateGameRound ( action . payload ) . pipe ( map ( ( msg ) => new gameplayactions . UpdateGameSuccess ( ) ) ) ) ) ; constructor ( private actions$ , public store , public gameActions , private svc , private utils ) { } }