added date to match objects

This commit is contained in:
Tzvetan Trave
2025-10-17 14:39:46 +02:00
parent e4e53e06f6
commit 1f085ce1fb
4 changed files with 19 additions and 14 deletions

View File

@ -97,7 +97,8 @@ Input needed :
"game": "<pong/tetris>"
"opponent": "<the opponent's username>",
"myScore": <my score>,
"opponentScore": <the opponent's score>
"opponentScore": <the opponent's score>,
"date": <seconds since Epoch (Date.now() return)>
}
```
@ -174,7 +175,7 @@ Always returns:
- 200 with response
```json
{
"n_<name of the counted objects>": <number of users>
"n_users": <number of users>
}
```
@ -243,7 +244,7 @@ Can return:
- 200 with response
```json
{
"n_<name of the counted objects>": <number of users>
"n_friends": <number of friends>
}
```
- 404 with response (if user does not exist)
@ -269,7 +270,8 @@ Can return:
"p1": "<the name of the p1>",
"p2": "<the name of the p2>",
"p1Score": "<the score of the p1>",
"p2Score": "<the score of the p2>"
"p2Score": "<the score of the p2>",
"date": <seconds since Epoch (Date.now() return)>
},
"tx": "<the transcaction hash>"
},
@ -298,7 +300,7 @@ Can return:
- 200 with response
```json
{
"n_<name of the counted objects>": <number of users>
"n_matches": <number of matches played by the user>
}
```
- 400 with response (if game does not exist)

View File

@ -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) {

View File

@ -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 });

View File

@ -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) {