Skip to content

Commit

Permalink
Añadido historial de partidas recientes en el perfil del usuario
Browse files Browse the repository at this point in the history
  • Loading branch information
iyanfdezz committed Mar 28, 2024
1 parent 8988b80 commit abb7c8a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
15 changes: 15 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ app.get("/userInfo", async (req, res) => {
}
});

app.post("/saveGameList", async (req, res) => {
try {
// Forward the save game request to the stats service
const gameResponse = await axios.post(
userServiceUrl + "/saveGameList",
req.body
);
res.json(gameResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
}
});

app.get("/questions", async (req, res) => {
try {
// Forward the question request to the question service
Expand Down
1 change: 1 addition & 0 deletions users/userservice/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const userSchema = new mongoose.Schema({
default: Date.now,
},
games: [{
gamemode: String,
correctAnswers: Number,
incorrectAnswers: Number,
points: Number,
Expand Down
22 changes: 22 additions & 0 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ app.get('/userInfo', async (req, res) => {
res.status(400).json({ error: error.message });
}});

app.post("/saveGameList", async (req, res) => {
try {
const username = req.body.username;
const gamemode = req.body.gameMode;
const gameData = req.body.gameData;

let user = await User.findOne({ username: username });

if (!user) {
return res.status(404).json({ error: "Usuario no encontrado" });
}
const gameDataWithGamemode = { ...gameData, gamemode };
user.games.push(gameDataWithGamemode);

await user.save();

res.json({ message: "Partida guardada exitosamente" });
} catch (error) {
res.status(400).json({ error: "Error al guardar partida en la lista: " + error.message });
}
});

const server = app.listen(port, () => {
console.log(`User Service listening at http://localhost:${port}`);
});
Expand Down
6 changes: 6 additions & 0 deletions webapp/src/pages/Bateria/Bateria.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ const JuegoPreguntas = () => {
} catch (error) {
console.error('Error al guardar el juego:', error);
}
try {
const response = await axios.post(URL + "/saveGameList", newGame);
console.log("Solicitud exitosa:", response.data);
} catch (error) {
console.error("Error al guardar el juego:", error);
}
}

useEffect(() => {
Expand Down
6 changes: 6 additions & 0 deletions webapp/src/pages/Clasico/Clasico.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ const JuegoPreguntas = () => {
},
};

try {
const response = await axios.post(URL + "/saveGameList", newGame);
console.log("Solicitud exitosa:", response.data);
} catch (error) {
console.error("Error al guardar el juego en la lista de partidas:", error);
}
try {
const response = await axios.post(URL + "/saveGame", newGame);
console.log("Solicitud exitosa:", response.data);
Expand Down
38 changes: 37 additions & 1 deletion webapp/src/pages/Perfil/Perfil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Box, VStack, Heading, Text, Center, Spinner } from "@chakra-ui/react";
import { Box, VStack, Heading, Text, Center, Spinner, Table, Thead, Tbody, Tr, Th, Td } from "@chakra-ui/react";
import React, { useEffect, useState } from "react";
import Nav from "../../components/Nav/Nav.js";
import Footer from "../../components/Footer/Footer.js";
import axios from "axios";

const Perfil = () => {
Expand All @@ -24,6 +26,8 @@ const Perfil = () => {
}, []);

return (
<>
<Nav/>
<Center py={8}>
<Box w="xl" borderWidth="1px" borderRadius="lg" overflow="hidden" boxShadow="lg">
<VStack p={8} align="start" spacing={6}>
Expand All @@ -43,12 +47,44 @@ const Perfil = () => {
<strong>Fecha de creación de la cuenta:</strong>{" "}
{new Date(userData.createdAt).toLocaleString()}
</Text>
<Heading as="h2" size="md">
Partidas Recientes
</Heading>
{userData.games.length > 0 ? (
<Table variant="simple">
<Thead>
<Tr>
<Th>Modo de Juego</Th>
<Th>Respuestas Correctas</Th>
<Th>Respuestas Incorrectas</Th>
<Th>Puntos</Th>
<Th>Tiempo Promedio</Th>
</Tr>
</Thead>
<Tbody>
{userData.games.slice(0, 10).map((game, index) => (
<Tr key={index}>
<Td>{game.gamemode}</Td>
<Td>{game.correctAnswers}</Td>
<Td>{game.incorrectAnswers}</Td>
<Td>{game.points}</Td>
<Td>{game.avgTime} segundos</Td>
</Tr>
))}
</Tbody>
</Table>
) : (
<Text>No hay partidas recientes.</Text>
)}
</>
)}
</VStack>
</Box>
</Center>
<Footer/>
</>
);
};

export default Perfil;

1 change: 0 additions & 1 deletion webapp/src/pages/Stats/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ const Stats = () => {
<>
<Nav />
<div>
<Heading as="h2"><em>Estadísticas de Usuario - Modo {getModeName()}</em></Heading>
<label htmlFor="usernameInput"> <strong>Nombre de Usuario: </strong></label>
<Input
type="text"
Expand Down

0 comments on commit abb7c8a

Please sign in to comment.