Skip to content

Commit

Permalink
* add info log when poll completes
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorMurphy21 committed Dec 28, 2023
1 parent 26d6ee7 commit 71492a7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
6 changes: 3 additions & 3 deletions server/src/state/gameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getCorrections, stringMatch } from './matchUtils';
import { PollName, PollService } from './pollService';
import logger from '../logger/logger';
import { Player as RoomPlayer, Room } from './rooms';
import { Err, Info, Ok, Result, Success, VoidResult, Warning } from '../types/result';
import { Err, Info, Ok, Result, Success, VoidResult, Warn } from '../types/result';
import { ConfigurableOptions, defaultOptions, getConfigurableOptionsSchema, Options } from './options';
import { Match, MidgameConnectData, Responses, SelectionType, Stage } from '../types/stateTypes';

Expand Down Expand Up @@ -165,11 +165,11 @@ export class GameState {

acceptPromptResponse(id: string, response: string): Result<{ response: string }> {
if (!response) {
return Warning('emptyResponse');
return Warn('emptyResponse');
}
response = response.trim().normalize().trim();
if (!response) {
return Warning('emptyResponse');
return Warn('emptyResponse');
}
if (this.stage === Stage.Response) {
const playerState = this.players.find((player) => player.id === id);
Expand Down
15 changes: 10 additions & 5 deletions server/src/state/pollService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GameState } from './gameState';
import { Err, Ok, Result } from '../types/result';
import { Err, Ok, Result, Warn } from '../types/result';
import { Stage } from '../types/stateTypes';
import logger from '../logger/logger';

export enum PollName {
SkipPrompt = 'skipPrompt',
Expand Down Expand Up @@ -48,7 +49,7 @@ export class PollService {
acceptVote(pollName: PollName, id: string, stage: Stage): Result<{ count: number; next: boolean }> {
const poll = this.polls.get(pollName);
if (!poll) {
return Err(`noPoll${pollName}`);
return Warn(`noPoll${pollName}`);
}
if (poll.stage && stage !== poll.stage) {
return Err('invalidStage');
Expand Down Expand Up @@ -86,17 +87,21 @@ export class PollService {
}

_cbIfComplete(poll: Poll, pollName: PollName): boolean {
if (this._complete(poll)) {
if (this._complete(poll, pollName)) {
this.clearPoll(pollName);
poll!.completeCb();
return true;
}
return false;
}

_complete(poll: Poll) {
_complete(poll: Poll, pollName: PollName) {
const threshold = Math.ceil(this.gameState.numVoters(poll.exclude) * poll.majorityPercent);
return this._countVotes(poll) >= threshold;
if (this._countVotes(poll) >= threshold) {
logger.info(`(pollService) ${pollName} completed`);
return true;
}
return false;
}

_nextComplete(poll: Poll) {
Expand Down
10 changes: 5 additions & 5 deletions server/src/state/rooms.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GameState } from './gameState';
import parameterize from 'parameterize';
import locale from 'locale';
import { Info, isErr, Ok, Result, Success, VoidResult, Warning } from '../types/result';
import { Info, isErr, Ok, Result, Success, VoidResult, Warn } from '../types/result';

export type Player = {
id: string;
Expand All @@ -27,14 +27,14 @@ const playerRoom: Record<string, Room> = {};
const rooms: { [key: string]: Room } = {};

function isValidName(name: string): VoidResult {
if (name.length < 2) return Warning('shortName');
if (name.length > 20) return Warning('longName');
if (name.length < 2) return Warn('shortName');
if (name.length > 20) return Warn('longName');
return Success();
}

function isValidRoomName(name: string): VoidResult {
if (name.length < 2) return Warning('shortRoomName');
if (name.length > 15) return Warning('longRoomName');
if (name.length < 2) return Warn('shortRoomName');
if (name.length > 15) return Warn('longRoomName');
if (rooms[name]) return Info('roomTaken');
return Success();
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/types/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ResultErr {
}

// Construct ResultErr with given log level
export function Warning(message: string): ResultErr {
export function Warn(message: string): ResultErr {
return new ResultErr(message, 'warning');
}

Expand Down

0 comments on commit 71492a7

Please sign in to comment.