@@ -2,10 +2,17 @@ namespace Views {
22 export class GoClock implements Views . View < HTMLDivElement > {
33 private _div : HTMLDivElement ;
44 private _clock : Views . LCDClock ;
5- private _periodNumber : Views . LCDDisplay ;
6- private _periodCounter : Views . LCDCounter ;
5+ private _overtimeNumber : Views . LCDDisplay ;
6+ private _overtimeCounter : Views . LCDCounter ;
77
8- private _maxPeriods : number ;
8+ private _data : Models . GameClock ;
9+ private _rules : KGS . SGF . RULES ;
10+ private _expired : boolean ;
11+ private _hidden : boolean ;
12+
13+ private _resolution : number = 500 ;
14+ private _timeoutHandler : Function ;
15+ private _timeoutHandle : number ;
916
1017 constructor ( ) {
1118 this . _div = document . createElement ( 'div' ) ;
@@ -14,11 +21,11 @@ namespace Views {
1421 this . _clock = new Views . LCDClock ( ) ;
1522 this . _clock . attach ( this . _div ) ;
1623
17- this . _periodNumber = new Views . LCDDisplay ( 2 , 0 , false , true ) ;
18- this . _periodNumber . attach ( this . _div ) ;
24+ this . _overtimeNumber = new Views . LCDDisplay ( 2 , 0 , false , true ) ;
25+ this . _overtimeNumber . attach ( this . _div ) ;
1926
20- this . _periodCounter = new Views . LCDCounter ( 25 ) ;
21- this . _periodCounter . attach ( this . _div ) ;
27+ this . _overtimeCounter = new Views . LCDCounter ( 25 ) ;
28+ this . _overtimeCounter . attach ( this . _div ) ;
2229 }
2330
2431 public attach ( parent : HTMLElement ) : void {
@@ -27,55 +34,107 @@ namespace Views {
2734
2835 public activate ( ) : void {
2936 this . _clock . activate ( ) ;
30- this . _periodNumber . activate ( ) ;
31- this . _periodCounter . activate ( ) ;
37+ this . _overtimeNumber . activate ( ) ;
38+ this . _overtimeCounter . activate ( ) ;
39+
40+ if ( this . _timeoutHandle == null ) {
41+ this . restoreTimeout ( ) ;
42+ }
3243 }
3344 public deactivate ( ) : void {
34- this . _periodCounter . deactivate ( ) ;
35- this . _periodNumber . deactivate ( ) ;
45+ this . clearTimeout ( ) ;
46+
47+ this . _overtimeCounter . deactivate ( ) ;
48+ this . _overtimeNumber . deactivate ( ) ;
3649 this . _clock . deactivate ( ) ;
3750 }
3851
39- public hide ( ) {
40- this . _div . classList . add ( 'hidden' ) ;
52+ private restoreTimeout ( ) {
53+ if ( this . _timeoutHandler != null ) {
54+ this . _timeoutHandle = window . setTimeout ( this . _timeoutHandler , this . _resolution ) ;
55+ }
4156 }
42- public show ( ) {
43- this . _div . classList . remove ( 'hidden' ) ;
57+
58+ private clearTimeout ( ) {
59+ if ( this . _timeoutHandle != null ) {
60+ window . clearTimeout ( this . _timeoutHandle ) ;
61+ this . _timeoutHandle = null ;
62+ }
4463 }
4564
46- public update ( clock : Models . GameClock ) {
47- if ( ( clock ) && ( clock . timeSystem ) && ( clock . timeSystem != KGS . Constants . TimeSystems . None ) ) {
48- if ( this . _maxPeriods != clock . maxPeriods ) {
49- this . _maxPeriods = clock . maxPeriods ;
65+ public update ( data : Models . GameClock ) {
66+ if ( ( data ) && ( data . rules ) && ( data . rules . timeSystem ) && ( data . rules . timeSystem != KGS . Constants . TimeSystems . None ) ) {
67+ // Register a Timeout of the clock is running
68+ let clockState = data . now ( ) ;
69+ if ( clockState . running ) {
70+ if ( this . _data != data ) {
71+ this . _data = data ;
72+ this . _timeoutHandler = this . update . bind ( this , data ) ;
73+ }
74+
75+ this . restoreTimeout ( ) ;
76+ }
77+
78+ if ( this . _rules != data . rules ) {
79+ // Show or hide the Overtime Counters
80+ let maxOvertimes : number ;
81+ switch ( data . rules . timeSystem ) {
82+ case KGS . Constants . TimeSystems . Japanese : maxOvertimes = data . rules . byoYomiPeriods ; break ;
83+ case KGS . Constants . TimeSystems . Canadian : maxOvertimes = data . rules . byoYomiStones ; break ;
84+ default : maxOvertimes = 0 ;
85+ }
5086
51- if ( ( this . _maxPeriods ) && ( this . _maxPeriods > 0 ) && ( this . _maxPeriods <= 30 ) ) {
52- this . _periodCounter . setMaximum ( this . _maxPeriods , ( this . _maxPeriods <= 15 ) ? 1 : 2 ) ;
53- this . _periodCounter . show ( ) ;
87+ if ( ( maxOvertimes ) && ( maxOvertimes > 0 ) && ( maxOvertimes <= 30 ) ) {
88+ this . _overtimeCounter . setMaximum ( maxOvertimes , ( maxOvertimes <= 15 ) ? 1 : 2 ) ;
89+ this . _overtimeCounter . show ( ) ;
5490 }
5591 else {
56- this . _periodCounter . hide ( ) ;
92+ this . _overtimeCounter . hide ( ) ;
5793 }
5894
59- if ( ( this . _maxPeriods ) && ( this . _maxPeriods > 0 ) ) {
60- this . _periodNumber . show ( ) ;
95+ if ( ( maxOvertimes ) && ( maxOvertimes ) ) {
96+ this . _overtimeNumber . show ( ) ;
6197 }
6298 else {
63- this . _periodNumber . hide ( ) ;
99+ this . _overtimeNumber . hide ( ) ;
64100 }
65101 }
66102
67- let periods = ( clock . overtime ) ? clock . periods : null ;
68- this . _periodCounter . value = periods ;
69- this . _periodNumber . value = periods ;
103+ if ( ! clockState . expired ) {
104+ let overtimeValue : number = ( clockState . overtime ) ? ( clockState . periods || clockState . stones ) : null ;
105+ this . _overtimeCounter . value = overtimeValue ;
106+ this . _overtimeNumber . value = overtimeValue ;
70107
71- if ( clock . running ) this . _clock . start ( clock . time , clock . updated ) ;
72- else this . _clock . stop ( clock . time ) ;
108+ this . _clock . update ( clockState . time , clockState . running ) ;
73109
74- this . show ( ) ;
110+ if ( this . _expired ) {
111+ this . _div . classList . remove ( 'expired' ) ;
112+ this . _expired = false ;
113+ }
114+ }
115+ else {
116+ let overtimeValue : number = ( clockState . stones ) ? clockState . stones : 0 ;
117+ this . _overtimeCounter . value = overtimeValue ;
118+ this . _overtimeNumber . value = overtimeValue ;
119+ this . _clock . update ( 0 , false ) ;
120+ if ( ! this . _expired ) {
121+ this . _div . classList . add ( 'expired' ) ;
122+ this . _expired = true ;
123+ }
124+ }
125+
126+ // Show the clock if it was previously hidden
127+ if ( this . _hidden ) {
128+ this . _div . classList . remove ( 'hidden' ) ;
129+ this . _hidden = false ;
130+ }
75131 }
76132 else {
77- this . _clock . stop ( ) ;
78- this . hide ( ) ;
133+ // Clear Timeout and Hide the clock
134+ this . clearTimeout ( ) ;
135+ if ( ! this . _hidden ) this . _div . classList . add ( 'hidden' ) ;
136+ this . _data = null ;
137+ this . _hidden = true ;
79138 }
80139 }
81140 }
0 commit comments