Skip to content

Commit

Permalink
Fix a bug where refreshing the page would zero the score
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfisz committed May 20, 2020
1 parent 95bea6b commit 662d3cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
31 changes: 16 additions & 15 deletions server/Players.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Session } from 'Session';
import { getOrThrow } from 'shared/getOrThrow';

export class Players {
private activeSessions = new Set<Session>();
private scores: Map<Session, number>;
private sessions = new Set<Session>();

constructor();
constructor(initialData: { scores: Map<Session, number> });
Expand All @@ -27,11 +27,11 @@ export class Players {
}

getScores() {
return [...this.sessions]
.map((session) => ({
return [...this.scores]
.map(([session, score]) => ({
sessionId: session.id,
name: session.name,
score: this.scores.get(session) as number,
score,
}))
.sort(
(playerA, playerB) =>
Expand All @@ -40,32 +40,33 @@ export class Players {
);
}

has(session: Session) {
return this.sessions.has(session);
hasAnActivePlayer(session: Session) {
return this.activeSessions.has(session);
}

getCount() {
return this.sessions.size;
getActivePlayerCount() {
return this.activeSessions.size;
}

add(session: Session) {
if (!this.has(session)) {
this.sessions.add(session);
if (!this.activeSessions.has(session)) {
this.activeSessions.add(session);
}
if (!this.scores.has(session)) {
this.scores.set(session, 0);
}
}

delete(session: Session) {
this.sessions.delete(session);
this.scores.delete(session);
this.activeSessions.delete(session);
}

forEach(callback: (session: Session) => void) {
this.sessions.forEach(callback);
forEachActivePlayer(callback: (session: Session) => void) {
this.activeSessions.forEach(callback);
}

increaseScore(session: Session) {
if (!this.has(session)) {
if (!this.scores.has(session)) {
return;
}
this.scores.set(session, (this.scores.get(session) as number) + 1);
Expand Down
6 changes: 3 additions & 3 deletions server/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Room {
}

forEachParticipant(callback: (participantSession: Session) => void) {
this.players.forEach(callback);
this.players.forEachActivePlayer(callback);
}

trySelectSet(session: Session, cards: number[]) {
Expand All @@ -104,10 +104,10 @@ export class Room {
return;
}

if (this.players.has(session)) {
if (this.players.hasAnActivePlayer(session)) {
this.nextCardRequests.add(session);

if (this.nextCardRequests.size === this.players.getCount()) {
if (this.nextCardRequests.size === this.players.getActivePlayerCount()) {
this.table.tryAddNextCard();
}
}
Expand Down

0 comments on commit 662d3cd

Please sign in to comment.