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

Registro histórico de jugadas y borrado enlace innecesario #104

Merged
merged 4 commits into from
Mar 8, 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
9 changes: 9 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const port = 8000;
const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002';
const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001';
const questionServiceUrl = process.env.QUES_SERVICE_URL || 'http://localhost:8005';
const recordServiceUrl = process.env.REC_SERVICE_URL || 'http://localhost:8006';

app.use(cors());
app.use(express.json());
Expand Down Expand Up @@ -42,6 +43,14 @@ app.post('/adduser', async (req, res) => {
}
});

app.post('/addRecord', async(req, res) => {
try{
const recordResponse = await axios.post(recordServiceUrl+'/addRecord', req.body);
}catch (error){
res.status(error.response.status).json({ error: error.response.data.error });
}
});

app.post('/getQuestionBody', async (req, res) => {
try {
// Forward the add user request to the user service
Expand Down
2 changes: 1 addition & 1 deletion questions/recordservice/record-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ app.post('/addRecord', async (req, res) => {
correctQuestions: req.body.correctQuestions,
failedQuestions: req.body.failedQuestions
});
newRecord.save();
await newRecord.save();
res.json(newRecord);

} catch (error) {
Expand Down
31 changes: 20 additions & 11 deletions webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import Link from '@mui/material/Link';

function App() {
const [showLogin, setShowLogin] = useState(true);
const [isLogged, setLogged] = useState(false);

const handleIsLogged = () => {
setLogged(true);
};

const handleToggleView = () => {
setShowLogin(!showLogin);
Expand All @@ -19,18 +24,22 @@ function App() {
<Typography component="h1" variant="h5" align="center" sx={{ marginTop: 2 }}>
Welcome to wiq_6B
</Typography>
{showLogin ? <Login /> : <AddUser />}
<Typography component="div" align="center" sx={{ marginTop: 2 }}>
{showLogin ? (
<Link name="gotoregister" component="button" variant="body2" onClick={handleToggleView}>
Don't have an account? Register here.
</Link>
{showLogin ? <Login setLogged={handleIsLogged}/> : <AddUser />}
{!isLogged ? (<Typography component="div" align="center" sx={{ marginTop: 2 }}>
{showLogin ? (
<Link name="gotoregister" component="button" variant="body2" onClick={handleToggleView}>
Don't have an account? Register here.
</Link>
) : (
<Link component="button" variant="body2" onClick={handleToggleView}>
Already have an account? Login here.
</Link>
)}
</Typography>
) : (
<Link component="button" variant="body2" onClick={handleToggleView}>
Already have an account? Login here.
</Link>
)}
</Typography>
<></>
) }

</Container>
);
}
Expand Down
36 changes: 33 additions & 3 deletions webapp/src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { Container, Typography, TextField, Button, Snackbar } from '@mui/materia

import Link from '@mui/material/Link';

const Game = () => {
const Game = ({username}) => {
const [questionBody, setQuestionBody] = useState('');
const [informacionWikidata, setInformacionWikidata] = useState('');
const [respuestaCorrecta, setRespuestaCorrecta] = useState('');
const [respuestasFalsas, setRespuestasFalsas] = useState([]);
const [numberClics, setNumberClics] = useState(1);
const [timer, setTimer] = useState(0);
const [correctQuestions, setCorrectQuestions] = useState(0);
const [error, setError] = useState('');
const [finish, setFinish] = useState(false);

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

Expand Down Expand Up @@ -96,6 +99,11 @@ const Game = () => {
fetchData();
}, [obtenerPreguntaAleatoria]);

const handleButtonClickCorrecta = () => {
setCorrectQuestions(correctQuestions+1);
handleButtonClick();
};

const handleButtonClick = () => {
setNumberClics(numberClics + 1);
obtenerPreguntaAleatoria();
Expand All @@ -109,10 +117,32 @@ const Game = () => {
return `${minsRStr}:${secsRStr}`;
};

useEffect(() => {
const addRecord = async () => {
try {
const response = await axios.post(`${apiEndpoint}/addRecord`, {
userId: username,
date: new Date(),
time: timer,
money: (25*correctQuestions),
correctQuestions: correctQuestions,
failedQuestions: (10-correctQuestions)
});
} catch (error) {
setError(error.response.data.error);
}
};

if((numberClics>10 || timer>180) && !finish){
addRecord();
setFinish(true);
}
}, [numberClics, timer]);

return (
<div>
{numberClics > 10 || timer > 180 ? (
<p>Fin</p>
<p>Fin de la partida</p>
) : (
<>
<Typography component="h1" variant='h5' sx={{ textAlign: 'center' }}>
Expand All @@ -125,7 +155,7 @@ const Game = () => {
<Typography component="h1" variant="h5" sx={{ textAlign: 'center' }}>
{questionBody} {informacionWikidata}
</Typography>
<Button variant="contained" color="primary" onClick={handleButtonClick}>
<Button variant="contained" color="primary" onClick={handleButtonClickCorrecta}>
{respuestaCorrecta}
</Button>

Expand Down
7 changes: 4 additions & 3 deletions webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UsersList from './UsersList';

//import Link from '@mui/material/Link';

const Login = () => {
const Login = ({setLogged}) => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
Expand Down Expand Up @@ -38,6 +38,7 @@ const Login = () => {
};

const handleShowGame = () => {
setLogged();
setShowGame(true);
};

Expand All @@ -56,8 +57,8 @@ const Login = () => {


showGame ? (
< Game/>
) :
< Game username={username}> </Game>
) :

showUsersList?(
< UsersList/>
Expand Down