33namespace Controllers {
44 export class GameChannel extends ChannelBase {
55 private _board : Views . GoBoard ;
6+ private _userColour : Models . GameStone ;
67
78 constructor ( parent : ChannelController , channelId : number ) {
89 super ( parent , channelId ) ;
@@ -37,14 +38,15 @@ namespace Controllers {
3738 }
3839
3940 let gameChannel = this . channel as Models . GameChannel ;
40- let userColour : Models . GameStone ;
4141 if ( gameChannel . playerWhite == this . database . username )
42- userColour = Models . GameStone . White ;
42+ this . _userColour = Models . GameStone . White ;
4343 else if ( gameChannel . playerBlack == this . database . username )
44- userColour = Models . GameStone . Black ;
44+ this . _userColour = Models . GameStone . Black ;
45+ else
46+ this . _userColour = null ;
4547
4648 if ( updateOverlay ) {
47- this . _board . updateOverlay ( gameChannel , userColour ) ;
49+ this . _board . updateOverlay ( gameChannel , this . _userColour ) ;
4850 }
4951
5052 let splitKomi = gameState . rules . splitKomi ( ) ;
@@ -69,38 +71,68 @@ namespace Controllers {
6971 home = temp ;
7072 }
7173
72- this . _board . playerHome . update ( home . colour , home . clock , home . prisoners , home . komi , home . user ) ;
74+ if ( this . _userColour != null ) {
75+ let passCallback : Function = ( ( gameChannel . phase == Models . GamePhase . Active ) && ( gameChannel . hasAction ( Models . GameActions . Move ) ) ) ? this . _passCallback : null ;
76+ let resignCallback : Function = ( gameChannel . phase != Models . GamePhase . Concluded ) ? this . _resignCallback : null ;
77+ this . _board . playerHome . update ( home . colour , home . clock , home . prisoners , home . komi , home . user , true , passCallback , resignCallback ) ;
78+ }
79+ else {
80+ this . _board . playerHome . update ( home . colour , home . clock , home . prisoners , home . komi , home . user ) ;
81+ }
82+
7383 this . _board . playerAway . update ( away . colour , away . clock , away . prisoners , away . komi , away . user ) ;
7484 }
7585 }
7686
7787 private tryPlay ( x : number , y : number ) : boolean {
7888 let gameChannel = this . channel as Models . GameChannel ;
79- if ( ! gameChannel . hasAction ( Models . GameActions . Move ) ) {
80- console . log ( "move action not available" ) ;
81- return false ;
82- }
89+ if ( ! gameChannel . hasAction ( Models . GameActions . Move ) ) return false ;
8390
8491 let gameState = this . database . games [ this . channelId ] ;
85- if ( ( ! gameState ) || ( ! gameState . tree ) ) return ;
86-
87- let r = gameState . tree . tryPlay ( x , y ) ;
88- switch ( r as Models . GameMoveError ) {
89- case Models . GameMoveError . Success :
90- gameChannel . disableAction ( Models . GameActions . Move ) ;
91- this . client . post ( < KGS . Upstream . GAME_MOVE > {
92- type : KGS . Upstream . _GAME_MOVE ,
93- channelId : this . channelId ,
94- x : x ,
95- y : y
96- } ) ;
97- return true ;
98-
99- case Models . GameMoveError . InvalidLocation : console . log ( "given coordinates are not on board" ) ; return false ;
100- case Models . GameMoveError . StonePresent : console . log ( "on given coordinates already is a stone" ) ; return false ;
101- case Models . GameMoveError . Suicide : console . log ( "suicide (currently they are forbbiden)" ) ; return false ;
102- case Models . GameMoveError . Ko : console . log ( "ko ko ko" ) ; return false ;
103- default : console . log ( "unknown outcome" ) ; return false ;
92+ if ( ( ! gameState ) || ( ! gameState . tree ) ) return false ;
93+
94+ if ( ( x != null ) && ( y != null ) ) {
95+ let r = gameState . tree . tryPlay ( x , y ) ;
96+ switch ( r as Models . GameMoveError ) {
97+ case Models . GameMoveError . Success :
98+ gameChannel . disableAction ( Models . GameActions . Move ) ;
99+ this . client . post ( < KGS . Upstream . GAME_MOVE > {
100+ type : KGS . Upstream . _GAME_MOVE ,
101+ channelId : this . channelId ,
102+ x : x ,
103+ y : y
104+ } ) ;
105+ return true ;
106+
107+ case Models . GameMoveError . InvalidLocation : console . log ( "given coordinates are not on board" ) ; return false ;
108+ case Models . GameMoveError . StonePresent : console . log ( "on given coordinates already is a stone" ) ; return false ;
109+ case Models . GameMoveError . Suicide : console . log ( "suicide (currently they are forbbiden)" ) ; return false ;
110+ case Models . GameMoveError . Ko : console . log ( "ko ko ko" ) ; return false ;
111+ default : console . log ( "unknown outcome" ) ; return false ;
112+ }
113+ }
114+ else {
115+ gameChannel . disableAction ( Models . GameActions . Move ) ;
116+ this . client . post ( < KGS . Upstream . GAME_MOVE > {
117+ type : KGS . Upstream . _GAME_MOVE ,
118+ channelId : this . channelId ,
119+ } ) ;
120+ return true ;
121+ }
122+ }
123+
124+ private _passCallback = ( ) => {
125+ this . tryPlay ( undefined , undefined ) ;
126+ }
127+ private _resignCallback = ( ) => {
128+ if ( this . _userColour != null ) {
129+ let gameChannel = this . channel as Models . GameChannel ;
130+ gameChannel . disableAction ( Models . GameActions . Move ) ;
131+
132+ this . client . post ( < KGS . Upstream . GAME_RESIGN > {
133+ type : KGS . Upstream . _GAME_RESIGN ,
134+ channelId : this . channelId ,
135+ } ) ;
104136 }
105137 }
106138 }
0 commit comments