namespace Models { export class GameTreeNode { public tree : GameTree ; public nodeId : number ; public parentId : number ; public children : number [ ] ; public position : Models . GamePosition ; private _properties : KGS . SGF . Property [ ] ; constructor ( tree , nodeId ) { this . tree = tree ; this . nodeId = nodeId ; } public addChild ( childNodeId ) { let child = this . tree . create ( childNodeId ) ; child . parentId = this . nodeId ; if ( this . children == null ) this . children = [ childNodeId ] ; else this . children . push ( childNodeId ) ; return child ; } public get parent ( ) : GameTreeNode { if ( null == this . parentId ) return null ; else if ( null != this . tree ) return this . tree . get ( this . parentId ) ; else throw "Game Tree Node [" + this . nodeId . toString ( ) + "] is an orphan" ; } public get properties ( ) : KGS . SGF . Property [ ] { return this . _properties ; } public addProperty ( property ) { if ( this . _properties == null ) this . _properties = [ property ] ; else this . _properties . push ( property ) ; return true ; } private locationsEqual ( left , right ) { if ( left . loc == right . loc ) return true ; if ( ( left . loc != null ) && ( Utils . isObject ( left . loc ) ) && ( right . loc != null ) && ( Utils . isObject ( right . loc ) ) ) { let l = left . loc as KGS . Coordinates ; let r = right . loc as KGS . Coordinates ; return ( ( l . x == r . x ) && ( l . y == r . y ) ) ; } return false ; } private findProperty ( property ) { if ( property != null ) { const notFound = - 1 ; if ( this . _properties == null ) { return notFound ; } else if ( Utils . isString ( property ) ) { for ( let i = 0 ; i < this . _properties . length ; ++ i ) { if ( this . _properties [ i ] . name == property ) return i ; } return notFound ; } else if ( ( < KGS . SGF . Property > property ) . name ) { let propertyName = ( < KGS . SGF . Property > property ) . name ; if ( ( < KGS . SGF . LocationProperty > property ) . loc == null ) return this . findProperty ( propertyName ) ; else { let location = < KGS . SGF . LocationProperty > property ; let firstProperty = notFound ; for ( let i = 0 ; i < this . _properties . length ; ++ i ) { if ( this . _properties [ i ] . name == propertyName ) { if ( this . locationsEqual ( ( < KGS . SGF . LocationProperty > this . _properties [ i ] ) , location ) ) return i ; else if ( firstProperty == notFound ) firstProperty = i ; } } return firstProperty ; } } } throw 'Argument was not a valid SGF Property' ; } public setProperty ( property ) { let i = this . findProperty ( property ) ; if ( i >= 0 ) { this . _properties [ i ] = property ; return true ; } else { return this . addProperty ( property ) ; } } public removeProperty ( property ) { let i = this . findProperty ( property ) ; if ( i >= 0 ) { this . _properties . splice ( i , 1 ) ; return true ; } else return false ; } public getProperty ( name ) { let i = this . findProperty ( name ) ; if ( i >= 0 ) { return this . _properties [ i ] ; } else return undefined ; } } }