Skip to content

Commit

Permalink
refactor gamestate, fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Dec 31, 2024
1 parent 389d3ce commit 8ac85b2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 71 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"buildReact": "vite build && npm run typecheck",
"buildServer": "tsc --project server/tsconfig.json --outDir buildServer",
"typecheckServer": "tsc --project server/tsconfig.json --noEmit",
"typecheck": "tsc --project tsconfig.json --noEmit",
"typecheck": "tsc --project src/tsconfig.json --noEmit",
"dev": "ts-node-dev --respawn --transpile-only --project server/tsconfig.json server/server.ts",
"prettier": "prettier --write .",
"updateEps": "curl -sSL -O https://github.com/howardchung/j-archive-parser/raw/release/jeopardy.json.gz"
Expand Down
66 changes: 66 additions & 0 deletions server/gamestate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export const getPerQuestionState = () => {
return {
currentQ: '',
currentAnswer: undefined as string | undefined,
currentValue: 0,
playClueEndTS: 0,
questionEndTS: 0,
wagerEndTS: 0,
buzzUnlockTS: 0,
currentDailyDouble: false,
canBuzz: false,
canNextQ: false,
currentJudgeAnswerIndex: undefined as number | undefined,
currentJudgeAnswer: undefined as string | undefined, //socket.id
dailyDoublePlayer: undefined as string | undefined, //socket.id
answers: {} as Record<string, string>,
submitted: {} as Record<string, boolean>,
judges: {} as Record<string, boolean | null>,
buzzes: {} as Record<string, number>,
wagers: {} as Record<string, number>,
// We track this separately from wagers because the list of people to wait for is different depending on context
// e.g. for Double we only need to wait for 1 player, for final we have to wait for everyone
waitingForWager: undefined as Record<string, boolean> | undefined,
};
};

export const getGameState = (
options: {
epNum?: string;
airDate?: string;
info?: string;
answerTimeout?: number;
finalTimeout?: number;
allowMultipleCorrect?: boolean;
host?: string;
enableAIJudge?: boolean;
},
jeopardy?: RawQuestion[],
double?: RawQuestion[],
final?: RawQuestion[],
) => {
return {
jeopardy,
double,
final,
answers: {} as Record<string, string>,
wagers: {} as Record<string, number>,
board: {} as { [key: string]: RawQuestion },
public: {
serverTime: Date.now(),
epNum: options.epNum,
airDate: options.airDate,
info: options.info,
board: {} as { [key: string]: Question },
scores: {} as Record<string, number>, // player scores
round: '', // jeopardy or double or final
picker: undefined as string | undefined, // If null let anyone pick, otherwise last correct answer
// below is populated in emitstate from settings
host: undefined as string | undefined,
enableAIJudge: false,
enableAIVoices: undefined as string | undefined,
...getPerQuestionState(),
},
};
};
export type PublicGameState = ReturnType<typeof getGameState>['public'];
68 changes: 1 addition & 67 deletions server/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import nodeCrypto from 'node:crypto';
import { genAITextToSpeech } from './aivoice';
import { getOpenAIDecision, openai } from './openai';
import config from './config';
import { getGameState, getPerQuestionState } from './gamestate';

// On boot, start with the initial data included in repo
console.time('load');
Expand Down Expand Up @@ -46,73 +47,6 @@ async function refreshEpisodes() {
setInterval(refreshEpisodes, 24 * 60 * 60 * 1000);
refreshEpisodes();

const getPerQuestionState = () => {
return {
currentQ: '',
currentAnswer: undefined as string | undefined,
currentValue: 0,
playClueEndTS: 0,
questionEndTS: 0,
wagerEndTS: 0,
buzzUnlockTS: 0,
currentDailyDouble: false,
canBuzz: false,
canNextQ: false,
currentJudgeAnswerIndex: undefined as number | undefined,
currentJudgeAnswer: undefined as string | undefined, //socket.id
dailyDoublePlayer: undefined as string | undefined, //socket.id
answers: {} as Record<string, string>,
submitted: {} as Record<string, boolean>,
judges: {} as Record<string, boolean | null>,
buzzes: {} as Record<string, number>,
wagers: {} as Record<string, number>,
// We track this separately from wagers because the list of people to wait for is different depending on context
// e.g. for Double we only need to wait for 1 player, for final we have to wait for everyone
waitingForWager: undefined as Record<string, boolean> | undefined,
};
};

const getGameState = (
options: {
epNum?: string;
airDate?: string;
info?: string;
answerTimeout?: number;
finalTimeout?: number;
allowMultipleCorrect?: boolean;
host?: string;
enableAIJudge?: boolean;
},
jeopardy?: RawQuestion[],
double?: RawQuestion[],
final?: RawQuestion[],
) => {
return {
jeopardy,
double,
final,
answers: {} as Record<string, string>,
wagers: {} as Record<string, number>,
board: {} as { [key: string]: RawQuestion },
public: {
serverTime: Date.now(),
epNum: options.epNum,
airDate: options.airDate,
info: options.info,
board: {} as { [key: string]: Question },
scores: {} as Record<string, number>, // player scores
round: '', // jeopardy or double or final
picker: undefined as string | undefined, // If null let anyone pick, otherwise last correct answer
// below is populated in emitstate from settings
host: undefined as string | undefined,
enableAIJudge: false,
enableAIVoices: undefined as string | undefined,
...getPerQuestionState(),
},
};
};
export type PublicGameState = ReturnType<typeof getGameState>['public'];

export class Room {
// Serialized state
public roster: User[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/components/Jeopardy/Jeopardy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import { io, type Socket } from 'socket.io-client';
import { type AppState } from '../App/App';
import ReactMarkdown from 'react-markdown';
import { type PublicGameState } from '../../../server/room';
import { type PublicGameState } from '../../../server/gamestate';
import { MD5 } from '../../md5';

const dailyDouble = new Audio('/jeopardy/jeopardy-daily-double.mp3');
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json → src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true,
"types": ["vite/client"]
"types": ["vite/client", "node"]
},
"include": ["src", "global.d.ts"]
"include": ["./**/*", "../global.d.ts"]
}

0 comments on commit 8ac85b2

Please sign in to comment.