From 1f085ce1fbc1e6f58ad50beed8a0fc92d91710cd Mon Sep 17 00:00:00 2001 From: Tzvetan Trave Date: Fri, 17 Oct 2025 14:39:46 +0200 Subject: [PATCH] added date to match objects --- doc/user/user.md | 12 +++++++----- src/api/user/default.js | 10 ++++++---- src/api/user/gMatchHistory.js | 9 +++++---- src/api/user/pMatchHistory.js | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/doc/user/user.md b/doc/user/user.md index da1a991..d982e68 100644 --- a/doc/user/user.md +++ b/doc/user/user.md @@ -97,7 +97,8 @@ Input needed : "game": "" "opponent": "", "myScore": , - "opponentScore": + "opponentScore": , + "date": } ``` @@ -174,7 +175,7 @@ Always returns: - 200 with response ```json { - "n_": + "n_users": } ``` @@ -243,7 +244,7 @@ Can return: - 200 with response ```json { - "n_": + "n_friends": } ``` - 404 with response (if user does not exist) @@ -269,7 +270,8 @@ Can return: "p1": "", "p2": "", "p1Score": "", - "p2Score": "" + "p2Score": "", + "date": }, "tx": "" }, @@ -298,7 +300,7 @@ Can return: - 200 with response ```json { - "n_": + "n_matches": } ``` - 400 with response (if game does not exist) diff --git a/src/api/user/default.js b/src/api/user/default.js index 22ca384..a2cc806 100644 --- a/src/api/user/default.js +++ b/src/api/user/default.js @@ -59,11 +59,13 @@ function prepareDB() { CREATE TABLE IF NOT EXISTS matchHistory ( id INTEGER PRIMARY KEY AUTOINCREMENT, game TEXT, + date INTEGER, player1 TEXT, player2 TEXT, matchId INTEGER, CHECK(player1 != player2), - CHECK(game = 'pong' OR game = 'tetris') + CHECK(game = 'pong' OR game = 'tetris'), + CHECK(date >= 0) ) STRICT `); } @@ -73,7 +75,7 @@ prepareDB(); // POST const createUser = database.prepare('INSERT INTO userData (username, displayName, pongWins, pongLosses, tetrisWins, tetrisLosses) VALUES (?, ?, 0, 0, 0, 0);'); const addFriend = database.prepare('INSERT INTO friends (username, friendName) VALUES (?, ?);'); -const addMatch = database.prepare('INSERT INTO matchHistory (game, player1, player2, matchId) VALUES (?, ?, ?, ?);'); +const addMatch = database.prepare('INSERT INTO matchHistory (game, date, player1, player2, matchId) VALUES (?, ?, ?, ?, ?);'); const incWinsPong = database.prepare('UPDATE userData SET pongWins = pongWins + 1 WHERE username = ?;'); const incLossesPong = database.prepare('UPDATE userData SET pongLosses = pongLosses + 1 WHERE username = ?'); const incWinsTetris = database.prepare('UPDATE userData SET tetrisWins = tetrisWins + 1 WHERE username = ?;'); @@ -87,7 +89,7 @@ const getUserData = database.prepare('SELECT username, displayName, pongWins, po const getUserInfo = database.prepare('SELECT username, displayName, pongWins, pongLosses, tetrisWins, tetrisLosses FROM userData WHERE username = ?;'); const getFriends = database.prepare('SELECT friendName FROM friends WHERE username = ? LIMIT ? OFFSET ?;'); const getFriend = database.prepare('SELECT friendName FROM friends WHERE username = ? AND friendName = ?;'); -const getMatchHistory = database.prepare('SELECT matchId FROM matchHistory WHERE game = ? AND ? IN (player1, player2) LIMIT ? OFFSET ?;'); +const getMatchHistory = database.prepare('SELECT matchId, date FROM matchHistory WHERE game = ? AND ? IN (player1, player2) LIMIT ? OFFSET ?;'); const getNumberUsers = database.prepare('SELECT COUNT (DISTINCT username) AS n_users FROM userData;'); const getNumberFriends = database.prepare('SELECT COUNT (DISTINCT friendName) AS n_friends FROM friends WHERE username = ?;'); const getNumberMatches = database.prepare('SELECT COUNT (DISTINCT id) AS n_matches FROM matchHistory WHERE game = ? AND ? IN (player1, player2);') @@ -103,7 +105,7 @@ const deleteStatsTetris = database.prepare('UPDATE userData SET tetrisWins = 0, const querySchema = { type: 'object', required: ['iStart', 'iEnd'], properties: { iStart: { type: 'integer', minimum: 0 }, iEnd: { type: 'integer', minimum: 0 } } } const bodySchema = { type: 'object', required: ['opponent', 'myScore', 'opponentScore'], properties: { opponent: { type: 'string' }, myScore: { type: 'integer', minimum: 0 }, opponentScore: { type: 'integer', minimum: 0 } } } const querySchemaMatchHistory = { type: 'object', required: ['game', 'iStart', 'iEnd'], properties: { game: { type: 'string' }, iStart: { type: 'integer', minimum: 0 }, iEnd: { type: 'integer', minimum: 0 } } } -const bodySchemaMatchHistory = { type: 'object', required: ['game', 'opponent', 'myScore', 'opponentScore'], properties: { game: { type: 'string' }, opponent: { type: 'string' }, myScore: { type: 'integer', minimum: 0 }, opponentScore: { type: 'integer', minimum: 0 } } } +const bodySchemaMatchHistory = { type: 'object', required: ['game', 'date', 'opponent', 'myScore', 'opponentScore'], properties: { game: { type: 'string' }, date: { type: 'integer', minimum: 0 }, opponent: { type: 'string' }, myScore: { type: 'integer', minimum: 0 }, opponentScore: { type: 'integer', minimum: 0 } } } const querySchemaMatchHistoryGame = { type: 'object', required: ['game'], properties: { game: { type: 'string' } } } export default async function(fastify, options) { diff --git a/src/api/user/gMatchHistory.js b/src/api/user/gMatchHistory.js index 6fde63e..7531ca0 100644 --- a/src/api/user/gMatchHistory.js +++ b/src/api/user/gMatchHistory.js @@ -15,13 +15,14 @@ export async function gMatchHistory(request, reply, fastify, getUserInfo, getMat if (!matchHistoryId.length) { return reply.code(404).send({ error: "No matches exist in the selected range" }); } - const ids = matchHistoryId.map(obj => Object.values(obj)[0]); - const promises = ids.map(async (id) => { - const res = await fetch(`http://localhost:3003/${id}`, { method: "GET" }); + const promises = matchHistoryId.map(async (match) => { + const res = await fetch(`http://localhost:3003/${match.matchId}`, { method: "GET" }); if (!res.ok) { throw new Error('Failed to fetch item from blockchain API'); } - return await res.json(); + const resJson = await res.json(); + resJson.score.date = match.date; + return resJson; }); const matchHistory = await Promise.all(promises); return reply.code(200).send({ matchHistory }); diff --git a/src/api/user/pMatchHistory.js b/src/api/user/pMatchHistory.js index fc346d1..f22ef38 100644 --- a/src/api/user/pMatchHistory.js +++ b/src/api/user/pMatchHistory.js @@ -4,7 +4,7 @@ async function fetchSave(request, reply, userId, addMatch) { throw new Error('Internal server error'); } const data = await res.json(); - addMatch.run(request.body.game, userId, request.body.opponent, data.id); + addMatch.run(request.body.game, request.body.date, userId, request.body.opponent, data.id); } export async function pMatchHistory(request, reply, fastify, getUserInfo, addMatch, incWinsPong, incLossesPong, incWinsTetris, incLossesTetris) {