This commit is contained in:
2025-09-14 19:16:16 +02:00
parent 1dfe3b3c41
commit 5ad8402cfb

View File

@@ -33,12 +33,9 @@ const lockGameSchema = Joi.object({
const turnSchema = Joi.object({
user: Joi.number().integer().positive().required(),
round_number: Joi.number().integer().min(1).required(),
start_points: Joi.number().integer().min(0).required(),
first_throw: Joi.number().integer().min(0).required(),
second_throw: Joi.number().integer().min(0).required(),
third_throw: Joi.number().integer().min(0).required(),
end_points: Joi.number().integer().min(0).required()
});
const endGameSchema = Joi.object({
@@ -220,6 +217,8 @@ router.get("/games/:id", asyncHandler(async (req, res) => {
const [games] = await pool.query("SELECT * FROM games WHERE id = ?", [gameId]);
if (!games.length) throw new ApiError(404, "Game not found");
const game = games[0];
const [creatorRows] = await pool.query(
`SELECT u.id
FROM game_players gp
@@ -228,10 +227,33 @@ router.get("/games/:id", asyncHandler(async (req, res) => {
LIMIT 1`,
[gameId]
);
const creator = creatorRows.length ? creatorRows[0].id : null;
res.json({ ...games[0], creator});
const [players] = await pool.query(
`SELECT u.id, u.username
FROM game_players gp
JOIN users u ON gp.user = u.id
WHERE gp.game = ?`,
[gameId]
);
const pointsPromises = players.map(async p => {
const [lastTurn] = await pool.query(
"SELECT end_points FROM turns WHERE game = ? AND user = ? ORDER BY id DESC LIMIT 1",
[gameId, p.id]
);
return {
...p,
points: lastTurn.length ? lastTurn[0].end_points : 501
};
});
const playersWithPoints = await Promise.all(pointsPromises);
res.json({
...game,
creator,
players: playersWithPoints
});
}));
/**
@@ -568,7 +590,7 @@ router.post("/games/:id/turns", asyncHandler(async (req, res) => {
const { error, value } = turnSchema.validate(req.body);
if (error) throw new ApiError(400, error.details[0].message);
const { user, round_number, start_points, first_throw, second_throw, third_throw, end_points } = value;
const { user, first_throw, second_throw, third_throw } = value;
const conn = await pool.getConnection();
try {
@@ -578,14 +600,25 @@ router.post("/games/:id/turns", asyncHandler(async (req, res) => {
"SELECT current_playing_user, turn_order FROM games WHERE id = ? FOR UPDATE",
[gameId]
);
if (!gameRows.length) throw new ApiError(404, "Game not found");
const game = gameRows[0];
const turnOrder = JSON.parse(game.turn_order || '[]');
if (game.current_playing_user !== user) throw new ApiError(403, "Not your turn");
const [lastTurn] = await conn.query(
"SELECT end_points FROM turns WHERE game = ? AND user = ? ORDER BY id DESC LIMIT 1",
[gameId, user]
);
const start_points = lastTurn.length ? lastTurn[0].end_points : 501;
const totalScore = first_throw + second_throw + third_throw;
let end_points = start_points - totalScore;
if (end_points < 0) {
end_points = start_points;
}
const round_number = lastTurn.length ? lastTurn[0].round_number + 1 : 1;
const [result] = await conn.query(
`INSERT INTO turns
(game, user, round_number, start_points, first_throw, second_throw, third_throw, end_points)
@@ -593,6 +626,7 @@ router.post("/games/:id/turns", asyncHandler(async (req, res) => {
[gameId, user, round_number, start_points, first_throw, second_throw, third_throw, end_points]
);
const turnOrder = JSON.parse(game.turn_order || '[]');
const currentIndex = turnOrder.indexOf(user);
const nextIndex = (currentIndex + 1) % turnOrder.length;
const nextPlayer = turnOrder[nextIndex];
@@ -603,7 +637,7 @@ router.post("/games/:id/turns", asyncHandler(async (req, res) => {
);
await conn.commit();
res.status(201).json({ turnId: result.insertId, nextPlayer });
res.status(201).json({ turnId: result.insertId, start_points, end_points, nextPlayer });
} catch (err) {
await conn.rollback();
throw new ApiError(500, "Failed to record turn", err);