Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions client/src/components/AnswerResultModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";

interface AnswerResultModalProps {
isOpen: boolean;
onClose: () => void;
isCorrect: boolean;
drDanQuote: string;
}

const AnswerResultModal = ({ isOpen, onClose, isCorrect, drDanQuote }: AnswerResultModalProps) => {
return (
<Dialog open={isOpen} onOpenChange={(open) => { if (!open) onClose(); }}>
<DialogContent>
<DialogHeader>
<DialogTitle className={isCorrect ? 'text-green-500' : 'text-red-500'}>
{isCorrect ? "Correct!" : "Wrong!"}
</DialogTitle>
</DialogHeader>
<p>{drDanQuote}</p>
</DialogContent>
</Dialog>
);
};

export default AnswerResultModal;
23 changes: 23 additions & 0 deletions client/src/components/MinionModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";

interface MinionModalProps {
isOpen: boolean;
onClose: () => void;
minionName: string;
minionQuote: string;
}

const MinionModal = ({ isOpen, onClose, minionName, minionQuote }: MinionModalProps) => {
return (
<Dialog open={isOpen} onOpenChange={(open) => { if (!open) onClose(); }}>
<DialogContent>
<DialogHeader>
<DialogTitle>{minionName} Appears!</DialogTitle>
</DialogHeader>
<p>{minionQuote}</p>
</DialogContent>
</Dialog>
);
};

export default MinionModal;
12 changes: 6 additions & 6 deletions client/src/graphql/queries.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { gql } from '@apollo/client';

export const GET_QUESTIONS = gql`
query GetQuestions {
export const GENERATE_QUESTIONS = gql`
query GenerateQuestion($track: String!, level: $level, minion: $minion) {
questions {
_id
questionText
options
correctAnswer
question
choices
answer
}
}
`;


38 changes: 38 additions & 0 deletions server/src/schemas/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import User from '../models/User';
import Character from '../models/Characters';
import { signToken } from '../utils/auth';
import { AuthenticationError } from 'apollo-server-express';
import { OpenAI } from 'openai';
import { PromptBuilder } from '../utils/PromptBuilder';
import dotenv from 'dotenv';

dotenv.config();

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

interface AddUserArgs {
input: {
Expand All @@ -24,6 +31,35 @@ const resolvers = {
}
throw new AuthenticationError('You need to be logged in!');
},
<<<<<<< HEAD
generateQuestion: async (_parent: any, args: { track: string; level: string; minion: string }) => {
const { track, level, minion } = args;
const prompt = PromptBuilder.getPrompt(track, level);

try {
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
max_tokens: 250,
});

const raw = completion.choices[0].message.content ?? '';
const [question, ...choicesAndAnswer] = raw.split('\n').filter(Boolean);
const choices = choicesAndAnswer.slice(0, -1);
const answer = choicesAndAnswer[choicesAndAnswer.length - 1];

return { question, choices, answer };
} catch (error) {
console.error('OpenAI failed, falling back to hardcoded question:', error);
const fallback = PromptBuilder.getFallbackQuestion(minion);

return {
question: fallback.question,
choices: fallback.choices,
answer: fallback.choices[fallback.correctIndex],
};
}
=======
users: async () => {
return await User.find();
},
Expand All @@ -35,8 +71,10 @@ const resolvers = {
},
character: async (_: any, { id }: { id: string }) => {
return await Character.findById(id);
>>>>>>> d7567a5c20d729e2d5c004a2d70be9176db8ea33
},
},

Mutation: {
addUser: async (_parent: any, { input }: AddUserArgs) => {
const user = await User.create(input);
Expand Down
13 changes: 13 additions & 0 deletions server/src/schemas/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,38 @@ const typeDefs = gql`
password: String!
}

<<<<<<< HEAD
=======
type Character {
_id: ID!
name: String!
picture: String!
voice: String!
}

>>>>>>> d7567a5c20d729e2d5c004a2d70be9176db8ea33
type Auth {
token: ID!
user: User
}

type Question {
question: String!
choices: [String!]!
answer: String!
}

type Query {
users: [User]
user(username: String!): User
me: User
<<<<<<< HEAD
generateQuestion(track: String!, level: String!, minion: String!): Question
=======
updateStats(isCorrect: Boolean!): User
characters: [Character]
character(id: ID!): Character
>>>>>>> d7567a5c20d729e2d5c004a2d70be9176db8ea33
}

type Mutation {
Expand Down
4 changes: 4 additions & 0 deletions server/src/utils/PromptBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<<<<<<< HEAD
import { FallbackQuestion, fallbackQuestion } from "../utils/fallbackQuestions";
=======

import { FallbackQuestion, fallbackQuestion } from "../utils/fallbackQuestions";

>>>>>>> d7567a5c20d729e2d5c004a2d70be9176db8ea33

export class PromptBuilder {
/**
Expand Down