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 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: 1 addition & 1 deletion questions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const express = require('express');
const cors = require('cors');
// Routes:
const questionRoutes = require('./routes/question-routes.js');
const questionRoutes = require('./routes/question-routes-api.js');

// App definition and
const app = express();
Expand Down
2 changes: 1 addition & 1 deletion users/__tests/routes/statistics-routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const request = require('supertest');
const express = require('express');
const bodyParser = require('body-parser');
const statisticsRoutes = require('../../routes/statistics-routes.js');
const { User } = require('../../models/user-model.js');
const { User } = require('../../services/user-model.js');

const app = express();
app.use(bodyParser.json());
Expand Down
2 changes: 1 addition & 1 deletion users/__tests/routes/user-routes.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { User, Statistics, sequelize } = require('../../models/user-model.js');
const { User, Statistics, sequelize } = require('../../services/user-model.js');
const bcrypt = require('bcrypt');
const request = require('supertest');
const express = require('express');
Expand Down
2 changes: 1 addition & 1 deletion users/__tests/services/statistics-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const request = require('supertest');
const express = require('express');
const bodyParser = require('body-parser');
const statisticsRoutes = require('../../routes/statistics-routes.js');
const { User } = require('../../models/user-model.js');
const { User } = require('../../services/user-model.js');

const app = express();
app.use(bodyParser.json());
Expand Down
2 changes: 1 addition & 1 deletion users/routes/auth-routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require('express');
const router = express.Router();
const bcrypt = require('bcrypt');
const { User } = require('../models/user-model');
const { User } = require('../services/user-model');


require('dotenv').config();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const router = express.Router();
const { Group,User,UserGroup } = require('../models/user-model');
const { Group,User,UserGroup } = require('../services/user-model');


// Getting the list of groups in the database
Expand Down
4 changes: 2 additions & 2 deletions users/routes/group-routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const express = require('express');
const router = express.Router();
const { Group,UserGroup } = require('../models/user-model');
const { Group,UserGroup } = require('../services/user-model');

//Group internal routes
const apiRoutes = require('../services/group-api');
const apiRoutes = require('../routes/group-routes-api');

// Adding a group to the database and creating the relationship with the creator
router.post('/add', async (req, res) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const router = express.Router();
const { Statistics } = require('../models/user-model');
const { Statistics } = require('../services/user-model');

//Get user statics by username
router.get('/:username', async (req,res) => {
Expand Down
14 changes: 7 additions & 7 deletions users/routes/statistics-routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const express = require('express');
const router = express.Router();
const { Statistics } = require('../models/user-model');
const { Statistics } = require('../services/user-model');

//Group internal routes
const apiRoutes = require('../services/statistics-api');
const apiRoutes = require('../routes/statistics-routes-api');

// Route for edit the statics of a user
router.post('/edit', async (req, res) => {
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const router = express.Router();
const { User } = require('../models/user-model');
const { User } = require('../services/user-model');

//Get all users
router.get('/allUsers', async (req,res) => {
Expand Down
4 changes: 2 additions & 2 deletions users/routes/user-routes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const express = require('express');
const router = express.Router();
const bcrypt = require('bcrypt');
const { User, Statistics } = require('../models/user-model');
const { User, Statistics } = require('../services/user-model');

//User internal routes
const apiRoutes = require('../services/user-api');
const apiRoutes = require('../routes/user-routes-api');

// Route for add a user
router.post('/add', async (req, res) => {
Expand Down
63 changes: 57 additions & 6 deletions users/models/user-model.js → users/services/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
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