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

Added changes in statistics and user registration #162

Merged
merged 8 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
63 changes: 57 additions & 6 deletions users/models/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,78 @@ const Statistics = sequelize.define('Statistics', {
primaryKey: true,
allowNull: false
},
earned_money: {
the_callenge_earned_money: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
classic_correctly_answered_questions: {
the_callenge_correctly_answered_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
classic_incorrectly_answered_questions: {
the_callenge_incorrectly_answered_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
classic_total_time_played: {
the_callenge_total_time_played: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
classic_games_played: {
the_callenge_games_played: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
wise_men_stack_earned_money: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
wise_men_stack_correctly_answered_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
wise_men_stack_incorrectly_answered_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
wise_men_stack_games_played: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
warm_question_earned_money: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
warm_question_correctly_answered_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
warm_question_incorrectly_answered_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
warm_question_passed_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
warm_question_games_played: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
discovering_cities_earned_money: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
discovering_cities_correctly_answered_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
discovering_cities_incorrectly_answered_questions: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
discovering_cities_games_played: {
type: DataTypes.INTEGER,
defaultValue: 0,
}
//we have to add more statics for more games
})

// Define the relationship between User and Statics
Expand Down
10 changes: 5 additions & 5 deletions users/routes/statistics-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ router.post('/edit', async (req, res) => {
}

// Update the user's fields with the provided values
statisticsUserToUpdate.earned_money = statisticsUserToUpdate.earned_money + earned_money;
statisticsUserToUpdate.classic_correctly_answered_questions = statisticsUserToUpdate.classic_correctly_answered_questions
statisticsUserToUpdate.the_callenge_earned_money = statisticsUserToUpdate.the_callenge_earned_money + earned_money;
statisticsUserToUpdate.the_callenge_correctly_answered_questions = statisticsUserToUpdate.the_callenge_correctly_answered_questions
+ classic_correctly_answered_questions;
statisticsUserToUpdate.classic_incorrectly_answered_questions = statisticsUserToUpdate.classic_incorrectly_answered_questions
statisticsUserToUpdate.the_callenge_incorrectly_answered_questions = statisticsUserToUpdate.the_callenge_incorrectly_answered_questions
+ classic_incorrectly_answered_questions;
statisticsUserToUpdate.classic_total_time_played = statisticsUserToUpdate.classic_total_time_played + classic_total_time_played;
statisticsUserToUpdate.classic_games_played = statisticsUserToUpdate.classic_games_played + classic_games_played;
statisticsUserToUpdate.the_callenge_total_time_played = statisticsUserToUpdate.the_callenge_total_time_played + classic_total_time_played;
statisticsUserToUpdate.the_callenge_games_played = statisticsUserToUpdate.the_callenge_games_played + classic_games_played;

// Save the changes to the database
await statisticsUserToUpdate.save();
Expand Down
15 changes: 13 additions & 2 deletions webapp/src/pages/AddUser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useState } from 'react';
import React, { useState,useContext } from 'react';
import axios from 'axios';
import { Container, Typography, TextField, Button, Snackbar, Box, Divider } from '@mui/material';
import { Link } from 'react-router-dom';
import { Link,useNavigate } from 'react-router-dom';
import { SessionContext } from '../SessionContext';

const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';

Expand All @@ -13,6 +14,10 @@ const AddUser = () => {
const [error, setError] = useState('');
const [openSnackbar, setOpenSnackbar] = useState(false);

const navigate = useNavigate();

const { createSession } = useContext(SessionContext);

const addUser = async () => {
try {
await axios.post(`${apiEndpoint}/user/add`, {
Expand All @@ -21,7 +26,13 @@ const AddUser = () => {
name,
surname
});


await axios.post(`${apiEndpoint}/login`, { username, password });

setOpenSnackbar(true);
createSession(username);
navigate('/homepage');
} catch (error) {
setError(error.response.data.error);
}
Expand Down
36 changes: 18 additions & 18 deletions webapp/src/pages/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const Game = () => {
setTimerRunning(false);
setShouldRedirect(true);
setQuestionCountdownRunning(false);
updateStatistics();
}
}, [round]);

Expand Down Expand Up @@ -92,6 +93,21 @@ const Game = () => {

};

const updateStatistics = async() => {
try {
await axios.post(`${apiEndpoint}/statistics/edit`, {
username:username,
earned_money:totalScore,
classic_correctly_answered_questions:correctlyAnsweredQuestions,
classic_incorrectly_answered_questions:incorrectlyAnsweredQuestions,
classic_total_time_played:totalTimePlayed,
classic_games_played:1
});
} catch (error) {
console.error("Error:", error);
};
}

// this function is called when a user selects a response.
const selectResponse = async (index, response) => {
setAnswered(true);
Expand Down Expand Up @@ -130,26 +146,10 @@ const Game = () => {

setButtonStates(newButtonStates);

if (round >= 3) {
// Update user data before redirecting
try {
await axios.post(`${apiEndpoint}/statistics/edit`, {
username:username,
earned_money:totalScore,
classic_correctly_answered_questions:correctlyAnsweredQuestions,
classic_incorrectly_answered_questions:incorrectlyAnsweredQuestions,
classic_total_time_played:totalTimePlayed,
classic_games_played:1
});
} catch (error) {
console.error("Error:", error);
}
}

setTimeout(() => {
setTimeout(async() => {
setRound(round + 1);
setButtonStates([]);
}, 2000);
}, 4000);
};

const questionHistorialBar = () => {
Expand Down
70 changes: 63 additions & 7 deletions webapp/src/pages/Statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,83 @@ const Statistics = () => {
Statistics
</Typography>
<Box>
<Typography variant="h5" align="center" gutterBottom>
Classic Game
<Typography variant="h5" align="center" gutterBottom style={{ marginTop: '20px'}}>
The Challenge
</Typography>
<List sx={{ width: '100%', maxWidth: 360, margin: 'auto' }}>
<Divider style={{ marginTop:'3%'}}/>
<ListItem>
<ListItemText primary={`Earned Money: ${userStatics.earned_money}€`} />
<ListItemText primary={`Earned Money: ${userStatics.the_callenge_earned_money}€`} />
</ListItem>
<ListItem>
<ListItemText primary={`Correctly Answered Questions: ${userStatics.classic_correctly_answered_questions}`} />
<ListItemText primary={`Correctly Answered Questions: ${userStatics.the_callenge_correctly_answered_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Incorrectly Answered Questions: ${userStatics.classic_incorrectly_answered_questions}`} />
<ListItemText primary={`Incorrectly Answered Questions: ${userStatics.the_callenge_incorrectly_answered_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Total Time Played: ${userStatics.classic_total_time_played}''`} />
<ListItemText primary={`Total Time Played: ${userStatics.the_callenge_total_time_played}''`} />
</ListItem>
<ListItem>
<ListItemText primary={`Games Played: ${userStatics.classic_games_played}`} />
<ListItemText primary={`Games Played: ${userStatics.the_callenge_games_played}`} />
</ListItem>
</List>
<Typography variant="h5" align="center" gutterBottom style={{ marginTop: '20px'}}>
Wise Men Stack
</Typography>
<List sx={{ width: '100%', maxWidth: 360, margin: 'auto' }}>
<Divider style={{ marginTop:'3%'}}/>
<ListItem>
<ListItemText primary={`Earned Money: ${userStatics.wise_men_stack_earned_money}€`} />
</ListItem>
<ListItem>
<ListItemText primary={`Correctly Answered Questions: ${userStatics.wise_men_stack_correctly_answered_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Incorrectly Answered Questions: ${userStatics.wise_men_stack_incorrectly_answered_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Games Played: ${userStatics.wise_men_stack_games_played}`} />
</ListItem>
</List>
<Typography variant="h5" align="center" gutterBottom style={{ marginTop: '20px'}}>
Warm Question
</Typography>
<List sx={{ width: '100%', maxWidth: 360, margin: 'auto' }}>
<Divider style={{ marginTop:'3%'}}/>
<ListItem>
<ListItemText primary={`Earned Money: ${userStatics.warm_question_earned_money}€`} />
</ListItem>
<ListItem>
<ListItemText primary={`Correctly Answered Questions: ${userStatics.warm_question_correctly_answered_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Incorrectly Answered Questions: ${userStatics.warm_question_incorrectly_answered_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Passed Questions: ${userStatics.warm_question_passed_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Games Played: ${userStatics.warm_question_games_played}`} />
</ListItem>
</List>
<Typography variant="h5" align="center" gutterBottom style={{ marginTop: '20px'}}>
Discovering Cities
</Typography>
<List sx={{ width: '100%', maxWidth: 360, margin: 'auto' }}>
<Divider style={{ marginTop:'3%'}}/>
<ListItem>
<ListItemText primary={`Earned Money: ${userStatics.discovering_cities_earned_money}€`} />
</ListItem>
<ListItem>
<ListItemText primary={`Correctly Answered Cities: ${userStatics.discovering_cities_correctly_answered_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Incorrectly Answered Cities: ${userStatics.discovering_cities_incorrectly_answered_questions}`} />
</ListItem>
<ListItem>
<ListItemText primary={`Games Played: ${userStatics.discovering_cities_games_played}`} />
</ListItem>
</List>
</Box>
</Container>
Expand Down