@@ -27,7 +27,7 @@ export interface Vote {
2727}
2828
2929export interface MemberList {
30- voters : {
30+ participants : {
3131 [ clientId : string ] : Client ;
3232 } ;
3333 observers : {
@@ -57,7 +57,7 @@ export class PokerRoom {
5757 * Creates the empty objects.
5858 */
5959 constructor ( ) {
60- this . clients = { disconnected : { } , observers : { } , voters : { } } ;
60+ this . clients = { disconnected : { } , observers : { } , participants : { } } ;
6161 this . history = [ ] ;
6262 this . currentStory = { name : "" , votes : [ ] , votesRevealed : false } ;
6363 }
@@ -81,19 +81,21 @@ export class PokerRoom {
8181 * @private
8282 */
8383 public getVotersCount ( ) : number {
84- return Object . values ( this . clients . voters ) . length ;
84+ return Object . values ( this . clients . participants ) . length ;
8585 }
8686
8787 /**
8888 * Retrieves the total client count.
8989 *
90+ * @param {boolean } includeDisconnected Take disconnected clients into account.
91+ *
9092 * @returns {number } The total number of clients connected.
9193 */
92- public getClientCount ( ) : number {
94+ public getClientCount ( includeDisconnected : boolean = true ) : number {
9395 return (
94- Object . values ( this . clients . voters ) . length +
96+ Object . values ( this . clients . participants ) . length +
9597 Object . values ( this . clients . observers ) . length +
96- Object . values ( this . clients . disconnected ) . length
98+ Object . values ( this . clients . disconnected ) . length * ( includeDisconnected ? 1 : 0 )
9799 ) ;
98100 }
99101
@@ -113,17 +115,17 @@ export class PokerRoom {
113115 }
114116
115117 if ( this . clients . disconnected [ userId ] ) {
116- this . clients . voters [ userId ] = this . clients . disconnected [ userId ] ;
117118 delete this . clients . disconnected [ userId ] ;
118119 }
119120
120- this . currentStory . votesRevealed = false ;
121-
122- this . clients . voters [ userId ] = {
121+ this . clients . participants [ userId ] = {
123122 id : userId ,
124123 name,
125124 votes : [ ] ,
126125 } ;
126+
127+ this . currentStory = this . setStoryAverage ( ) ;
128+ this . currentStory . votesRevealed = false ;
127129 }
128130
129131 /**
@@ -137,8 +139,8 @@ export class PokerRoom {
137139 * @private
138140 */
139141 public setClientName ( userId : string , name : string ) : void {
140- if ( this . clients . voters [ userId ] ) {
141- this . clients . voters [ userId ] . name = name ;
142+ if ( this . clients . participants [ userId ] ) {
143+ this . clients . participants [ userId ] . name = name ;
142144 }
143145 if ( this . clients . observers [ userId ] ) {
144146 this . clients . observers [ userId ] . name = name ;
@@ -155,15 +157,20 @@ export class PokerRoom {
155157 * @private
156158 */
157159 public removeClient ( userId : string ) : void {
158- delete this . clients . voters [ userId ] ;
159- delete this . clients . disconnected [ userId ] ;
160+ delete this . clients . participants [ userId ] ;
160161 delete this . clients . observers [ userId ] ;
162+ delete this . clients . disconnected [ userId ] ;
163+
164+ this . removeVote ( userId ) ;
165+ }
161166
167+ public removeVote ( userId : string ) : void {
162168 // Remove client's current votes.
163169 this . currentStory . votes = this . currentStory . votes . filter (
164170 ( vote : Vote ) => vote . voter . id !== userId ,
165171 ) ;
166- this . setStoryAverage ( this . currentStory ) ;
172+
173+ this . currentStory = this . setStoryAverage ( ) ;
167174 this . currentStory . votesRevealed = false ;
168175 }
169176
@@ -174,19 +181,15 @@ export class PokerRoom {
174181 *
175182 * @returns {void }
176183 */
177- public setObserver ( userId : string ) : void {
178- if ( this . clients . voters [ userId ] ) {
179- // Remove the users vote from the vote-list.
180- const userVote = this . getCurrentVote ( userId ) ;
181- this . currentStory . votes = this . currentStory . votes . filter ( ( vote ) => userVote !== vote ) ;
182- this . setStoryAverage ( this . currentStory ) ;
183-
184- this . clients . observers [ userId ] = this . clients . voters [ userId ] ;
185- delete this . clients . voters [ userId ] ;
184+ public makeObserver ( userId : string ) : void {
185+ this . removeVote ( userId ) ;
186+
187+ if ( this . clients . participants [ userId ] ) {
188+ this . clients . observers [ userId ] = this . clients . participants [ userId ] ;
189+ delete this . clients . participants [ userId ] ;
186190 }
187191
188192 delete this . clients . disconnected [ userId ] ;
189- this . currentStory . votesRevealed = false ;
190193 }
191194
192195 /**
@@ -197,9 +200,9 @@ export class PokerRoom {
197200 * @returns {void }
198201 */
199202 public setDisconnected ( userId : string ) : void {
200- if ( this . clients . voters [ userId ] ) {
201- this . clients . disconnected [ userId ] = this . clients . voters [ userId ] ;
202- delete this . clients . voters [ userId ] ;
203+ if ( this . clients . participants [ userId ] ) {
204+ this . clients . disconnected [ userId ] = this . clients . participants [ userId ] ;
205+ delete this . clients . participants [ userId ] ;
203206 }
204207
205208 if ( this . clients . observers [ userId ] ) {
@@ -214,7 +217,7 @@ export class PokerRoom {
214217 * @returns {Client[] } List of voted voters.
215218 */
216219 public getVotedClients ( ) : Client [ ] {
217- return Object . values ( this . clients . voters ) . filter ( ( client : Client ) =>
220+ return Object . values ( this . clients . participants ) . filter ( ( client : Client ) =>
218221 client . votes . some ( ( vote : Vote ) => vote . story === this . currentStory ) ,
219222 ) ;
220223 }
@@ -225,7 +228,7 @@ export class PokerRoom {
225228 * @returns {Client[] } List of voters that haven't voted yet.
226229 */
227230 public getVotePendingClients ( ) : Client [ ] {
228- return Object . values ( this . clients . voters ) . filter ( ( client : Client ) =>
231+ return Object . values ( this . clients . participants ) . filter ( ( client : Client ) =>
229232 ! client . votes . some ( ( vote : Vote ) => vote . story === this . currentStory ) ,
230233 ) ;
231234 }
@@ -258,14 +261,15 @@ export class PokerRoom {
258261 */
259262 private addVote ( userId : string , voteValue : VoteValue ) : void {
260263 const vote : Vote = {
261- story : this . currentStory ,
262- voter : this . clients . voters [ userId ] ,
264+ story : this . getCurrentStory ( ) ,
265+ voter : this . clients . participants [ userId ] ,
263266 initialValue : voteValue ,
264267 currentValue : voteValue ,
265268 } ;
266- this . clients . voters [ userId ] . votes . push ( vote ) ;
267- this . currentStory . votes . push ( vote ) ;
268- this . setStoryAverage ( this . currentStory ) ;
269+ this . clients . participants [ userId ] . votes . push ( vote ) ;
270+ this . getCurrentStory ( ) . votes . push ( vote ) ;
271+
272+ this . currentStory = this . setStoryAverage ( ) ;
269273 }
270274
271275 /**
@@ -284,7 +288,7 @@ export class PokerRoom {
284288 this . getCurrentVote ( userId ) . initialValue = vote ;
285289 }
286290 this . getCurrentVote ( userId ) . currentValue = vote ;
287- this . setStoryAverage ( this . currentStory ) ;
291+ this . currentStory = this . setStoryAverage ( ) ;
288292 }
289293
290294 /**
@@ -306,7 +310,7 @@ export class PokerRoom {
306310 * @returns {Vote } The vote of the user.
307311 */
308312 public getCurrentVote ( userId : string ) : Vote | undefined {
309- return this . clients . voters [ userId ] . votes . find (
313+ return this . clients . participants [ userId ] . votes . find (
310314 ( vote : Vote ) => vote . story === this . currentStory ,
311315 ) ;
312316 }
@@ -350,8 +354,8 @@ export class PokerRoom {
350354 * @returns {ObscuredVote[] } List of obscured votes.
351355 */
352356 public getObscuredVotes ( story : Story ) : ObscuredVote [ ] {
353- return Object . values ( this . clients . voters ) . map (
354- ( client : Client ) : ObscuredVote => this . getObscuredVote ( story , client ) ,
357+ return Object . values ( this . clients . participants ) . map (
358+ ( client : Client ) : ObscuredVote => this . getObscuredVote ( story , client ) ,
355359 ) . sort ( ( a , b ) => {
356360 if ( a . currentValue === b . currentValue ) {
357361 return 0 ;
@@ -375,7 +379,7 @@ export class PokerRoom {
375379 initialValue : voteValue ,
376380 currentValue : voteValue ,
377381 voter : client ,
378- story : this . currentStory ,
382+ story : this . getCurrentStory ( ) ,
379383 } ;
380384 }
381385
@@ -384,12 +388,14 @@ export class PokerRoom {
384388 *
385389 * @param {Story } story The story.
386390 *
387- * @returns {void }
391+ * @returns {Story } The adjusted story.
388392 */
389- public setStoryAverage ( story : Story ) : void {
393+ public setStoryAverage ( ) : Story {
394+ const story = this . getCurrentStory ( ) ;
395+
390396 if ( story . votes . length === 0 ) {
391397 delete story . voteAverage ;
392- return ;
398+ return story ;
393399 }
394400
395401 if ( story . votes . some ( ( vote : Vote ) => vote . currentValue === "coffee" ) ) {
@@ -428,6 +434,8 @@ export class PokerRoom {
428434 break ;
429435 }
430436 }
437+
438+ return story ;
431439 }
432440
433441 /**
0 commit comments