Skip to content

Commit 510789e

Browse files
committed
Calculate average more often and cleanup votes better when observing
Renamed votes to participants. Make setStoryAverage return the modified story. Set average after a client is (re)joined. Set average after removing a client (disconnect or observing).
1 parent 15d72ba commit 510789e

File tree

3 files changed

+61
-53
lines changed

3 files changed

+61
-53
lines changed

backend/src/pokers/poker-room.ts

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface Vote {
2727
}
2828

2929
export 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
/**

backend/src/pokers/pokers.gateway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class PokersGateway implements OnGatewayInit {
9090
/* eslint-disable require-jsdoc */
9191
@SubscribeMessage( "identify" )
9292
identify( client: Socket, message: { id: string } ): void {
93-
this.pokersService.greet( client, message.id );
93+
this.pokersService.identify( client, message.id );
9494
client.emit( "welcome" );
9595
}
9696

@@ -357,7 +357,7 @@ export class PokersGateway implements OnGatewayInit {
357357
const mapCallback = this.formatClientResponse;
358358

359359
return {
360-
voters: Object.values( memberList.voters ).map( mapCallback ),
360+
voters: Object.values( memberList.participants ).map( mapCallback ),
361361
observers: Object.values( memberList.observers ).map( mapCallback ),
362362
disconnected: Object.values( memberList.disconnected ).map( mapCallback ),
363363
};

backend/src/pokers/pokers.service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class PokersService {
4242
*
4343
* @returns {void}
4444
*/
45-
public greet( client: Socket, userId: string ): void {
45+
public identify( client: Socket, userId: string ): void {
4646
this.users[ client.id ] = userId;
4747
}
4848

@@ -66,7 +66,7 @@ export class PokersService {
6666
* @returns {void}
6767
*/
6868
public exit( client: Socket ): void {
69-
const userId = this.users[ client.id ];
69+
const userId = this.getUserId( client );
7070

7171
this.removeUserFromRooms( userId );
7272

@@ -101,7 +101,7 @@ export class PokersService {
101101
private removeUserFromRooms( userId: string ): void {
102102
for ( const room of Object.keys( this.rooms ) ) {
103103
this.rooms[ room ].removeClient( userId );
104-
this.cleanRoom( room );
104+
this.cleanupRoom( room );
105105
}
106106
}
107107

@@ -114,7 +114,7 @@ export class PokersService {
114114
*/
115115
public getClientSockets( client: Socket ): string[] {
116116
const sockets = [];
117-
const userId = this.users[ client.id ];
117+
const userId = this.getUserId( client );
118118

119119
if ( ! userId ) {
120120
return sockets;
@@ -189,7 +189,7 @@ export class PokersService {
189189
public leave( client: Socket, poker: string ): void {
190190
this.getRoom( poker ).removeClient( this.getUserId( client ) );
191191

192-
this.cleanRoom( poker );
192+
this.cleanupRoom( poker );
193193

194194
client.leave( poker );
195195
}
@@ -203,8 +203,8 @@ export class PokersService {
203203
*
204204
* @private
205205
*/
206-
private cleanRoom( room: string ): void {
207-
if ( this.getRoom( room ).getClientCount() === 0 ) {
206+
private cleanupRoom( room: string ): void {
207+
if ( this.getRoom( room ).getClientCount( false ) === 0 ) {
208208
delete this.rooms[ room ];
209209
}
210210
}
@@ -218,7 +218,7 @@ export class PokersService {
218218
* @returns {void}
219219
*/
220220
public observe( client: Socket, poker: string ): void {
221-
this.getRoom( poker ).setObserver( this.getUserId( client ) );
221+
this.getRoom( poker ).makeObserver( this.getUserId( client ) );
222222
}
223223

224224
/**

0 commit comments

Comments
 (0)