Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed algorithm for questions without multiple choice & MyTrivia now … #34

Merged
merged 1 commit into from
May 30, 2024
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
2 changes: 2 additions & 0 deletions trivia-forge/backend/endpoints/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def get_patch_delete_game(game_id):
def get_games_with_details():
user_id = request.args.get('user_id')
try:
print(f"Fetching games with details for user_id: {user_id}")
games_query = supabase.table("Games").select("*").eq("user_id", user_id).execute()
games = games_query.data

Expand All @@ -122,6 +123,7 @@ def get_games_with_details():
game['categories'] = categories
game_details.append(game_detail)
#print(game_details)
print(f"Returning game details:")
return jsonify(game_details)
except Exception as e:
print(f"Error fetching games with details: {str(e)}")
Expand Down
4 changes: 2 additions & 2 deletions trivia-forge/frontend/src/Components/Categories.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import Questions from "../Components/Questions";

function Categories({ category, index, changeValue}) {
function Categories({ category, index, changeValue, isMultipleChoice }) {
let questions = category.questions;
const path = ['categories', index];

Expand All @@ -10,7 +10,7 @@ function Categories({ category, index, changeValue}) {
<h2>{category.title || category.name}</h2>
{questions.map((question, index) => {
return (
<Questions key={index} data={question} path={path} index={index} changeValue={changeValue}/>
<Questions key={index} data={question} path={path} index={index} changeValue={changeValue} isMultipleChoice={isMultipleChoice}/>
);
})}
</div>
Expand Down
19 changes: 11 additions & 8 deletions trivia-forge/frontend/src/Components/Questions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Question } from "../Models/Question";



function Questions({ data, path, index, changeValue }) {
function Questions({ data, path, index, changeValue, isMultipleChoice }) {
let choices = data.choices;
let newPath = structuredClone(path)
newPath.push('questions', index)
Expand All @@ -24,13 +24,16 @@ function Questions({ data, path, index, changeValue }) {
<div className="card-body">
<textarea className="form-control" defaultValue={data.problem || data.question} onChange={(e) => {changeValue(newPath, "problem", e.target.value)}}></textarea>
</div>
<h2>Choices</h2>
{choices.map((choice, index) => {
return (
<Choices key={index} data={choice} path={newPath} index={index} changeValue={changeValue}/>
);
})}

{isMultipleChoice && (
<>
<h2>Choices</h2>
{choices.map((choice, index) => {
return (
<Choices key={index} data={choice} path={newPath} index={index} changeValue={changeValue}/>
);
})}
</>
)}
<h2>Answer</h2>
<div className="card-body">
<textarea className="form-control" defaultValue={data.answer} onChange={(e) => {changeValue(newPath, "answer", e.target.value)}}></textarea>
Expand Down
8 changes: 4 additions & 4 deletions trivia-forge/frontend/src/Pages/MyTrivia.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ function MyTrivia() {
}

// Cleanup function to reset loaded when component unmounts
return () => {
setLoaded(false);
};
}, [currentUser, userGames, setUserGames]);
// return () => {
// setLoaded(false);
// };
}, [loaded]);

useEffect(() => {
if (currentGame && showWarning === false) {
Expand Down
53 changes: 33 additions & 20 deletions trivia-forge/frontend/src/Pages/TriviaGenPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function TriviaGenPage() {
if (isMultipleChoice) {
prompt += "Each question should be in the format Question:...\nChoice:...\nChoice:...\nChoice:...\nChoice:...\nAnswer:...\nHint:...\n---\nQuestion:... ect. Do not include A), B), C), or D) in the choices.";
} else {
prompt += "Each question should be in the format \nQuestion:...\nAnswer:...\n---\nQuestion:... ect.";
prompt += "Each question should be in the format \nQuestion:...\nAnswer:...\nHint:...\n---\nQuestion:... ect.";
}

// api call
Expand Down Expand Up @@ -101,36 +101,49 @@ function TriviaGenPage() {
for (let i = 0; i < sections.length; i++) {
if (sections[i] === '') { sections.splice(i, 1); }
}
//loop through sections and create question and choice objects
for (let i = 0; i < sections.length; i += 7) {
let question = sections[i].slice(10);
let choices = [];
for (let k = 0; k < 4; k++) {
let choice = sections[i + k + 1];
let newChoice = new Choice(choice.slice(8));
choices.push(newChoice);
}
let answer = sections[i + 5].slice(8);
let hint = sections[i + 6].slice(6);

//create question object and add it to category
let newQuestion = new Question(question, answer, hint, isMultipleChoice);
newCategory.addQuestion(newQuestion);

//add choices to question object
for (let i = 0; i < choices.length; i++) {
newQuestion.addChoice(choices[i]);
//loop through sections and create question and choice objects
if (isMultipleChoice) {
for (let i = 0; i < sections.length; i += 7) {
let question = sections[i].slice(10);
let choices = [];
for (let k = 0; k < 4; k++) {
let choice = sections[i + k + 1];
let newChoice = new Choice(choice.slice(8));
choices.push(newChoice);
}
let answer = sections[i + 5].slice(8);
let hint = sections[i + 6].slice(6);

//create question object and add it to category
let newQuestion = new Question(question, answer, hint, isMultipleChoice);
newCategory.addQuestion(newQuestion);

//add choices to question object
for (let i = 0; i < choices.length; i++) {
newQuestion.addChoice(choices[i]);
}
}
} else {
for (let j = 0; j < sections.length; j += 3) {
let question = sections[j].slice(10);
let answer = sections[j + 1].slice(8);
let hint = sections[j + 2].slice(6);

//create question object and add it to category
let newQuestion = new Question(question, answer, hint, isMultipleChoice);
newCategory.addQuestion(newQuestion);
}
}

console.log("Category Questions:", newCategory.questions);
}
console.log("Game Categories", game.categories);

// Save game to global state and local storage
addGame(game);
// state property to pass data as object to new route
navigate('/review', { state: { game: game, page: 'review' } });
navigate('/review', { state: { game: game, page: 'review', isMultipleChoice: isMultipleChoice } });
//console.log(completion.choices[0].message);


Expand Down
4 changes: 2 additions & 2 deletions trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function TriviaReviewPage() {
// Reference: https://reactrouter.com/en/main/hooks/use-location
// pulls object from state property in TriviaGenPage
const location = useLocation();
const { game, page } = location.state;
const { game, page, isMultipleChoice } = location.state;
let categories = game.categories;
const navigate = useNavigate();
const updateGame = useStore(state => state.updateGame);
Expand Down Expand Up @@ -53,7 +53,7 @@ function TriviaReviewPage() {
<div key={index} className="category-container">
<label className="input-label">Category Name:</label>
<input type="text" className="input-field" value={cat.title || cat.name} readOnly />
<Categories category={cat} index={index} changeValue={changeValue} />
<Categories category={cat} index={index} changeValue={changeValue} isMultipleChoice={isMultipleChoice}/>
</div>
))}
</div>
Expand Down