import { Injectable } from '@angular/core' ; import { Effect , Actions , ofType } from '@ngrx/effects' ; import { Game , PlayerQnA , GameOptions , User , Question } from '../../../shared/model' ; import { ActionWithPayload , GameActions } from '../actions' ; import { GameService } from '../../services' ; import { map , switchMap } from 'rxjs/operators' ; @ Injectable ( ) export class GameEffects { @ Effect ( ) UserReaction$ = this . actions$ . pipe ( ofType ( GameActions . USER_REACTION ) ) . pipe ( switchMap ( ( action ) => this . svc . userReaction ( action . payload . questionId , action . payload . userId , action . payload . status ) . pipe ( map ( ( ) => this . gameActions . UpdateUserReactionSuccess ( ) ) ) ) ) ; @ Effect ( ) GetQuestion$ = this . actions$ . pipe ( ofType ( GameActions . GET_QUESTION ) ) . pipe ( switchMap ( ( action ) => this . svc . getQuestion ( action . payload ) . pipe ( map ( ( question ) => this . gameActions . GetQuestionSuccess ( question ) ) ) ) ) ; @ Effect ( ) getUserReaction$ = this . actions$ . pipe ( ofType ( GameActions . GET_USER_REACTION ) ) . pipe ( switchMap ( ( action ) => this . svc . getUserReaction ( action . payload . questionId , action . payload . userId ) . pipe ( map ( ( reaction ) => this . gameActions . GetUserReactionSuccess ( reaction ) ) ) ) ) ; @ Effect ( ) getActiveGames$ = this . actions$ . pipe ( ofType ( GameActions . GET_ACTIVE_GAMES ) ) . pipe ( map ( ( action ) => action . payload ) , switchMap ( ( payload ) => this . svc . getActiveGames ( payload ) ) , map ( ( games ) => this . gameActions . getActiveGamesSuccess ( games ) ) ) ; @ Effect ( ) UpdateQuestionStat$ = this . actions$ . pipe ( ofType ( GameActions . UPDATE_QUESTION_STAT ) ) . pipe ( switchMap ( ( action ) => this . svc . updateQuestionStat ( action . payload . questionId , action . payload . type ) . pipe ( map ( ( ) => this . gameActions . UpdateQuestionStatSuccess ( ) ) ) ) ) ; constructor ( private actions$ , private gameActions , private svc ) { } }