import { CumulatedStatistic } from '../../../protocol/statistic/cumulatedstatistic' ; import { ServerWrapper } from '../server/serverwrapper' ; import { StatisticsModel } from './statisticsmodel' ; export class ServerStatisticsModel implements StatisticsModel { private repertoireFinishLineCount_ : Promise < number > ; private repertoireRightMoveCount_ : Promise < number > ; private repertoireWrongMoveCount_ : Promise < number > ; private rightMoveCounts_ : Promise < Map < string , number > > ; private wrongMoveCounts_ : Promise < Map < string , number > > ; constructor ( server , repertoireId ) { const cumulatedStatistics = server . loadCumulatedStatistics ( repertoireId ) ; const finishLineCounts = this . mapFromCumulatedStatistics_ ( cumulatedStatistics , cs => cs . finishLineCount ) ; this . rightMoveCounts_ = this . mapFromCumulatedStatistics_ ( cumulatedStatistics , cs => cs . rightMoveCount ) ; this . wrongMoveCounts_ = this . mapFromCumulatedStatistics_ ( cumulatedStatistics , cs => cs . wrongMoveCount ) ; this . repertoireFinishLineCount_ = this . sumValues_ ( finishLineCounts ) ; this . repertoireRightMoveCount_ = this . sumValues_ ( this . rightMoveCounts_ ) ; this . repertoireWrongMoveCount_ = this . sumValues_ ( this . wrongMoveCounts_ ) ; } private mapFromCumulatedStatistics_ < T > ( cumulatedStatistics , mapFn ) { return cumulatedStatistics . then ( csList => { const map = new Map ( ) ; csList . forEach ( cs => map . set ( cs . pgn , mapFn ( cs ) ) ) ; return map ; } ) ; } private sumValues_ ( m ) { return m . then ( map => Array . from ( map . values ( ) ) . reduce ( ( a , b ) => a + b , 0 ) ) ; } getRepertoireFinishLineCount ( ) { return this . repertoireFinishLineCount_ ; } getRepertoireRightMoveCount ( ) { return this . repertoireRightMoveCount_ ; } getRepertoireWrongMoveCount ( ) { return this . repertoireWrongMoveCount_ ; } getRightMoveCount ( pgn ) { return this . rightMoveCounts_ . then ( map => map . get ( pgn ) || 0 ) ; } getWrongMoveCount ( pgn ) { return this . wrongMoveCounts_ . then ( map => map . get ( pgn ) || 0 ) ; } }