Skip to content

Commit

Permalink
* add callback to pollservice so it can send updates to the client i…
Browse files Browse the repository at this point in the history
…f a vote changes due to disconnect
  • Loading branch information
ConorMurphy21 committed Feb 19, 2024
1 parent f4cc222 commit 772e1e7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
'vue/no-unused-properties': 2,
'vue/no-unused-refs': 2,
'@intlify/vue-i18n/valid-message-syntax': 2,
'@intlify/vue-i18n/key-format-style': [2, 'camelCase', {allowArray: true}],
'@intlify/vue-i18n/key-format-style': [2, 'camelCase', { allowArray: true }],
'@intlify/vue-i18n/no-duplicate-keys-in-locale': 2,
'@intlify/vue-i18n/no-missing-keys-in-other-locales': 2,
'prettier/prettier': 2
Expand Down
6 changes: 3 additions & 3 deletions client/src/stores/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { acceptHMRUpdate, defineStore } from 'pinia';
import type {
Player,
Match as ServerMatch,
PollName,
VoteCount,
Score as ServerScore,
Responses,
MidgameConnectData
MidgameConnectData,
PollVoteCount
} from ':common/stateTypes';
import type { Options, VisibleOptions } from ':common/options';
import { defaultOptions } from ':common/options';
Expand Down Expand Up @@ -244,7 +244,7 @@ export const useGameStore = defineStore('game', {
this.selectionType = selectionType;
});

socket.on('setVoteCount', (data: { pollName: PollName } & VoteCount) => {
socket.on('setVoteCount', (data: PollVoteCount) => {
this.voteCounts[data.pollName] = { count: data.count, next: data.next };
});
socket.on('beginPrompt', (prompt: string) => {
Expand Down
13 changes: 11 additions & 2 deletions common/src/socketioTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { Server, Socket } from 'socket.io';
import { Socket as ClientSocket } from 'socket.io-client';
import { SettableOptions, VisibleOptions } from './options';
import { Player, PollName, Match, MidgameConnectData, Responses, SelectionType, VoteCount, Score } from './stateTypes';
import {
Player,
PollName,
Match,
MidgameConnectData,
Responses,
SelectionType,
Score,
PollVoteCount
} from './stateTypes';
import { Result } from './result';

interface ServerToClientRoomEvents {
Expand All @@ -27,7 +36,7 @@ interface ServerToClientGameEvents {

nextSelection(args: { selector: string; selectionType: SelectionType }): void;

setVoteCount(args: { pollName: PollName } & VoteCount): void;
setVoteCount(args: PollVoteCount): void;

selectionTypeChosen(selectionType: SelectionType): void;

Expand Down
2 changes: 2 additions & 0 deletions common/src/stateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export type VoteCount = {
next: boolean;
};

export type PollVoteCount = { pollName: PollName } & VoteCount;

export const zPollName = z.enum(['skipPrompt', 'startNextRound', 'sikeDispute']);
export type PollName = z.infer<typeof zPollName>;

Expand Down
10 changes: 7 additions & 3 deletions server/src/routes/gameHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Result } from ':common/result';
import { isErr, isOk, isSuccess } from ':common/result';
import type { SettableOptions } from ':common/options';
import { getSettableOptionsSchema, getVisibleOptionsSchema } from ':common/options';
import type { PollName } from ':common/stateTypes';
import type { PollName, PollVoteCount } from ':common/stateTypes';
import { zPollName } from ':common/stateTypes';
import type { TypedServer, TypedSocket } from ':common/socketioTypes';
import type { Responses } from ':common/stateTypes';
Expand Down Expand Up @@ -216,18 +216,22 @@ function registerCallbacks(io: TypedServer, room: Room) {
//continueSelection(io, room);
//});

state.registerDisputeCompleteCb((action) => {
state.registerDisputeCompleteCb((action: string) => {
applyDisputeAction(io, room, action);
});

state.registerMatchingCompleteCb((selectorActive) => {
state.registerMatchingCompleteCb((selectorActive: boolean) => {
// give a little time to show score before moving on to next selection
if (!selectorActive) {
state.promptTimeout = setTimeout(() => {
continueSelection(io, room);
}, 5000);
}
});

state.registerPollVoteUpdateCb((pollVoteCount: PollVoteCount) => {
io.to(room.name).emit('setVoteCount', pollVoteCount);
});
}

function beginPrompt(io: TypedServer, room: Room) {
Expand Down
7 changes: 6 additions & 1 deletion server/src/state/gameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import type {
SelectionType,
Stage,
Player as RoomPlayer,
Score
Score,
PollVoteCount
} from ':common/stateTypes';

type Player = {
Expand Down Expand Up @@ -123,6 +124,10 @@ export class GameState {
this._matchingCompleteCb = cb;
}

registerPollVoteUpdateCb(cb: (pollVoteCounts: PollVoteCount) => void): void {
this.pollService.registerPollVoteUpdateCb(cb);
}

/*** PROMPT RESPONSE state changes ***/
hasNewPrompt(): boolean {
// return false if no rounds left
Expand Down
13 changes: 10 additions & 3 deletions server/src/state/pollService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GameState } from './gameState';
import type { Result } from ':common/result';
import { Err, Ok, Warn } from ':common/result';
import type { PollName, Stage } from ':common/stateTypes';
import type { PollName, PollVoteCount, Stage } from ':common/stateTypes';
import logger from '../logger/logger';

type Poll = {
Expand All @@ -15,12 +15,16 @@ type Poll = {
export class PollService {
private gameState: GameState;
private readonly polls: Map<PollName, Poll>;

private _pollVoteUpdateCb: null | ((pollVoteCounts: PollVoteCount) => void) = null;
constructor(gameState: GameState) {
this.gameState = gameState;
this.polls = new Map<PollName, Poll>();
}

registerPollVoteUpdateCb(cb: (pollVoteCounts: PollVoteCount) => void): void {
this._pollVoteUpdateCb = cb;
}

registerPoll(
pollName: PollName,
completeCb: () => void,
Expand Down Expand Up @@ -115,10 +119,13 @@ export class PollService {
}

disconnect(id: string): void {
for (const poll of this.polls.values()) {
for (const [pollName, poll] of this.polls.entries()) {
const index = poll.inFavor.indexOf(id);
if (index >= 0) {
poll.inFavor.splice(index, 1);
if (this._pollVoteUpdateCb) {
this._pollVoteUpdateCb({ pollName, count: this._countVotes(poll), next: this._nextComplete(poll) });
}
}
}
}
Expand Down

0 comments on commit 772e1e7

Please sign in to comment.