namespace Models { export const enum GameResultType { Unknown , Scored , Timeout , Resignation , Anulled , Forfeit } export class GameResult { private _score : string | number ; public resultType : Models . GameResultType ; public victor : Models . GameStone ; public points : number ; constructor ( ) { this . resultType = GameResultType . Unknown ; } public mergeScore ( score ) { if ( this . _score == score ) return false ; if ( GameResult . isKnownResult ( score ) ) { this . _score = score ; if ( Utils . isNumber ( score ) ) { if ( score > 0 ) { this . resultType = GameResultType . Scored ; this . victor = Models . GameStone . Black ; this . points = + score ; } else if ( score < 0 ) { this . resultType = GameResultType . Scored ; this . victor = Models . GameStone . White ; this . points = - ( + score ) ; } else { this . resultType = GameResultType . Scored ; this . victor = null ; this . points = 0 ; } } else if ( score == "NO_RESULT" ) { this . resultType = GameResultType . Anulled ; this . victor = null ; this . points = null ; } else { let v = ( < string > score ) . substr ( 0 , 1 ) ; this . victor = ( v == "B" ) ? Models . GameStone . Black : ( v == "W" ) ? Models . GameStone . White : null ; this . points = null ; let w = ( < string > score ) . substr ( 2 ) ; switch ( w ) { case "TIME" : this . resultType = GameResultType . Timeout ; break ; case "RESIGN" : this . resultType = GameResultType . Resignation ; break ; case "FORFEIT" : this . resultType = GameResultType . Forfeit ; break ; } } return true ; } else { if ( this . resultType == GameResultType . Unknown ) return false ; this . _score = null ; this . resultType = GameResultType . Unknown ; this . victor = null ; this . points = null ; return true ; } } public static isKnownResult ( score ) { return ( ( score != null ) && ( score != "UNKNOWN" ) && ( score != "UNFINISHED" ) ) ; } public getHeadline ( userColour ? , whiteName ? , blackName ? ) { if ( this . resultType == GameResultType . Anulled ) { return "no result" ; } else if ( ( this . resultType == GameResultType . Scored ) && ( this . points == 0 ) ) { return "jigo" ; } let byLine ; switch ( this . resultType ) { case GameResultType . Scored : byLine = " by " + this . points . toString ( ) + " points" ; break ; case GameResultType . Timeout : byLine = " by time" ; break ; case GameResultType . Resignation : byLine = " by resignation" ; break ; case GameResultType . Forfeit : byLine = " by forfeiture" ; break ; } if ( byLine ) { if ( userColour != null ) return ( ( this . victor == userColour ) ? "victory" : "defeat" ) + byLine ; else return ( ( this . victor == Models . GameStone . White ) ? ( whiteName || "White" ) : ( blackName || "Black" ) ) + " won" + byLine ; } return "unknown result" ; } public getShortFormat ( ) { if ( this . resultType == GameResultType . Anulled ) { return "" ; } else if ( ( this . resultType == GameResultType . Scored ) && ( this . points == 0 ) ) { return "JIGO" ; } let byLine ; switch ( this . resultType ) { case GameResultType . Scored : byLine = this . points . toString ( ) ; break ; case GameResultType . Timeout : byLine = "TIME" ; break ; case GameResultType . Resignation : byLine = "RESIGN" ; break ; case GameResultType . Forfeit : byLine = "FORFEIT" ; break ; } if ( byLine ) { return ( ( this . victor == Models . GameStone . White ) ? "W+" : "B+" ) + byLine ; } return "??" ; } } }