Skip to content

Commit

Permalink
Merge pull request #125 from agrattan0820/feat/ai-players
Browse files Browse the repository at this point in the history
Fix: Working Player Count for State Machine
  • Loading branch information
agrattan0820 authored Jul 26, 2024
2 parents 88cbf3d + 2cad0fb commit 356641c
Show file tree
Hide file tree
Showing 12 changed files with 1,109 additions and 1,417 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: 🏗 Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: 20.15.0
node-version: 20.16.0
cache: "pnpm"

- name: 👷 Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 20.15.0
nodejs 20.16.0
28 changes: 14 additions & 14 deletions apps/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,45 @@
},
"dependencies": {
"@auth/drizzle-adapter": "^1.4.1",
"@sentry/nextjs": "^8.13.0",
"@tanstack/react-query": "^5.48.0",
"@upstash/ratelimit": "^1.2.1",
"@upstash/redis": "^1.31.6",
"@sentry/nextjs": "^8.20.0",
"@tanstack/react-query": "^5.51.15",
"@upstash/ratelimit": "^2.0.1",
"@upstash/redis": "^1.33.0",
"@vercel/analytics": "^1.3.1",
"@xstate/react": "^4.1.1",
"autoprefixer": "10.4.19",
"clsx": "^2.1.1",
"database": "workspace:*",
"drizzle-orm": "^0.31.2",
"framer-motion": "^11.2.12",
"next": "^14.2.4",
"drizzle-orm": "^0.32.1",
"framer-motion": "^11.3.17",
"next": "^14.2.5",
"next-auth": "^4.24.7",
"postcss": "8.4.38",
"postcss": "8.4.40",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hot-toast": "^2.4.1",
"react-icons": "^5.2.1",
"server-only": "^0.0.1",
"sharp": "^0.33.4",
"socket.io-client": "^4.7.5",
"tailwind-merge": "^2.3.0",
"tailwindcss": "^3.4.4",
"tailwind-merge": "^2.4.0",
"tailwindcss": "^3.4.7",
"typescript": "5.4.4",
"use-sound": "^4.0.3",
"xstate": "^5.14.0"
"xstate": "^5.16.0"
},
"devDependencies": {
"@playwright/test": "^1.45.0",
"@playwright/test": "^1.45.3",
"@tailwindcss/typography": "^0.5.13",
"@types/node": "^20.14.9",
"@types/node": "^20.14.12",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"eslint-config-custom": "workspace:*",
"prettier-plugin-tailwindcss": "^0.6.5",
"tsconfig": "workspace:*"
},
"engines": {
"node": "20.15.0"
"node": "20.16.0"
},
"optionalDependencies": {
"bufferutil": "^4.0.8",
Expand Down
3 changes: 3 additions & 0 deletions apps/client/src/app/room/[code]/game/[gameId]/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export default function Game({ gameInfo, session }: GameProps) {
// State machine
const [state, send, actor] = useMachine(gameMachine, {
snapshot: serverState,
input: {
playerCount: gameInfo.players.length,
},
});

const handleRoomState = (roomInfo: RoomInfo) => {
Expand Down
10 changes: 7 additions & 3 deletions apps/client/src/components/game/game-machine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type MachineContext = {
questionIdx: number;
};
type MachineEvent = { type: "NEXT" } | { type: "SUBMIT" } | { type: "MORE" };
type MachineInput = {
playerCount: number;
};

// MACHINE
export const gameMachine = createMachine(
Expand All @@ -58,14 +61,15 @@ export const gameMachine = createMachine(
// typegen: {} as import("./game-machine.typegen").Typegen0,
context: {} as MachineContext,
events: {} as MachineEvent,
input: {} as MachineInput,
},
// tsTypes: {} as import("./game-machine.typegen").Typegen0,
initial: "connectingToMainframe",
context: {
context: ({ input }) => ({
round: 1,
playerCount: 0,
playerCount: input.playerCount,
questionIdx: 0,
},
}),
states: {
connectingToMainframe: {
after: {
Expand Down
110 changes: 62 additions & 48 deletions apps/client/src/components/game/prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import toast from "react-hot-toast";
import { FiHelpCircle, FiX } from "react-icons/fi";
import useSound from "use-sound";
import type { Session } from "next-auth";
import { captureException } from "@sentry/nextjs";

import Button, { SecondaryButton } from "@ai/components/button";
import Ellipsis from "@ai/components/ellipsis";
Expand Down Expand Up @@ -109,45 +110,54 @@ const Prompt = ({
const outOfRegenerations = remainingImageGenerations === 0;

const onPromptSubmit = async (e: FormEvent<PromptFormType>) => {
e.preventDefault();
setLoading(true);
try {
e.preventDefault();
setLoading(true);

const formPrompt = e.currentTarget.elements.prompt.value;
setImagePrompt(formPrompt);
const formPrompt = e.currentTarget.elements.prompt.value;
setImagePrompt(formPrompt);

console.time("Execution Time");
console.time("Execution Time");

const images = await generateSDXLImages(formPrompt);
const images = await generateSDXLImages(formPrompt);

console.timeEnd("Execution Time");
console.timeEnd("Execution Time");

if (images && Array.isArray(images) && images.length === 2) {
if (userId) {
const generations = await createGenerations({
userId,
gameId,
questionId: currQuestion.id,
images: images?.map((image) => {
return {
text: formPrompt,
imageUrl: image,
};
}),
});

setImageOption1(generations[0]);
setImageOption2(generations[1]);

setCurrQuestionNumGenerations(
currQuestionNumGenerations + generations.length,
if (!images || !Array.isArray(images) || images.length !== 2) {
throw new Error(
"Images wasn't defined or the incorrect amount of images were returned",
);
}
} else {

if (!userId) {
throw new Error("User id not defined when creating generations");
}

const generations = await createGenerations({
userId,
gameId,
questionId: currQuestion.id,
images: images?.map((image) => {
return {
text: formPrompt,
imageUrl: image,
};
}),
});

setImageOption1(generations[0]);
setImageOption2(generations[1]);

setCurrQuestionNumGenerations(
currQuestionNumGenerations + generations.length,
);
} catch (error) {
console.error("Images were unable to be generated");
toast.error("I'm afraid I don't know how to process such a request.");
captureException(error);
} finally {
setLoading(false);
}

setLoading(false);
};

const resetImage = () => {
Expand All @@ -161,34 +171,38 @@ const Prompt = ({
};

const onImageSubmit = async () => {
setLoading(true);
try {
setLoading(true);

if (!session || !imageOption1 || !imageOption2) {
throw new Error(
"User was not defined when trying to submit a generation",
);
}

if (session && imageOption1 && imageOption2) {
socket.emit("generationSubmitted", {
generationId: selectedImage === 1 ? imageOption1?.id : imageOption2?.id,
gameId: gameId,
round: currRound,
});
} else {
console.error("User was not defined when trying to submit a generation");
toast.error("Oops, we can't submit your generation.");
setLoading(false);
return;
}

if (stage === "FIRST") {
setCurrQuestionNumGenerations(0);
setStage("SECOND");
setImagePrompt("");
resetImage();
} else {
if (soundEnabled) {
play();
if (stage === "FIRST") {
setCurrQuestionNumGenerations(0);
setStage("SECOND");
setImagePrompt("");
resetImage();
} else {
if (soundEnabled) {
play();
}
send({ type: "SUBMIT" });
}
send({ type: "SUBMIT" });
} catch (error) {
toast.error("Oops, we can't submit your generation.");
captureException(error);
} finally {
setLoading(false);
}

setLoading(false);
};

useEffect(() => {
Expand Down
16 changes: 8 additions & 8 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,39 @@
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org alexander-grattan --project artificial-unintelligence-server ./dist && sentry-cli sourcemaps upload --org alexander-grattan --project artificial-unintelligence-server ./dist"
},
"dependencies": {
"@sentry/node": "^8.13.0",
"@sentry/profiling-node": "^8.13.0",
"@sentry/node": "^8.20.0",
"@sentry/profiling-node": "^8.20.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"database": "workspace:*",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.31.2",
"drizzle-orm": "^0.32.1",
"express": "^4.19.2",
"helmet": "^7.1.0",
"ioredis": "^5.4.1",
"morgan": "^1.10.0",
"replicate": "^0.30.2",
"replicate": "^0.32.0",
"socket.io": "^4.7.5"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@sentry/cli": "^2.32.1",
"@sentry/cli": "^2.33.0",
"@types/cookie-parser": "^1.4.7",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/morgan": "^1.9.9",
"@types/node": "^20.14.9",
"@types/node": "^20.14.12",
"cross-env": "^7.0.3",
"eslint-config-custom": "workspace:*",
"jest": "^29.7.0",
"nodemon": "^3.1.4",
"ts-jest": "^29.1.5",
"ts-jest": "^29.2.3",
"ts-node": "^10.9.2",
"tsconfig": "workspace:*",
"typescript": "5.4.4"
},
"engines": {
"node": "20.15.0"
"node": "20.16.0"
},
"optionalDependencies": {
"bufferutil": "^4.0.8",
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { buildServer } from "./server";

const server = buildServer();

const port = process.env.PORT;
const port = Number.parseInt(process.env.PORT ?? "8080");

server.listen(port, async () => {
console.log(`listening on *:${port}`);
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
},
"devDependencies": {
"eslint": "8.57.0",
"prettier": "^3.3.2",
"prettier": "^3.3.3",
"tsconfig": "workspace:*",
"turbo": "^2.0.6"
"turbo": "^2.0.9"
},
"engines": {
"node": "20.15.0"
"node": "20.16.0"
},
"packageManager": "pnpm@9.4.0",
"name": "beeeeeeeep"
"packageManager": "pnpm@9.6.0",
"name": "artificial-unintelligence"
}
4 changes: 2 additions & 2 deletions packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
},
"dependencies": {
"dotenv": "^16.4.5",
"drizzle-orm": "^0.31.2",
"drizzle-orm": "^0.32.1",
"postgres": "^3.4.4"
},
"devDependencies": {
"@auth/core": "^0.34.1",
"drizzle-kit": "^0.22.7",
"drizzle-kit": "^0.23.0",
"eslint-config-custom": "workspace:*",
"ts-node": "^10.9.2",
"tsconfig": "workspace:*",
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-config-custom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"version": "0.0.0",
"private": true,
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^7.14.1",
"@typescript-eslint/parser": "^7.14.1",
"eslint-config-next": "14.2.4",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"eslint-config-next": "14.2.5",
"eslint-config-prettier": "^9.1.0",
"eslint-config-turbo": "^2.0.6"
"eslint-config-turbo": "^2.0.9"
}
}
Loading

0 comments on commit 356641c

Please sign in to comment.