added tetris game management

This commit is contained in:
Tzvetan Trave
2025-10-15 18:19:51 +02:00
parent 005e9ebbbb
commit e4e53e06f6
9 changed files with 130 additions and 54 deletions

View File

@ -4,10 +4,10 @@ async function fetchSave(request, reply, userId, addMatch) {
throw new Error('Internal server error');
}
const data = await res.json();
addMatch.run(userId, data.id);
addMatch.run(request.body.game, userId, request.body.opponent, data.id);
}
export async function pMatchHistory(request, reply, fastify, getUserInfo, addMatch, incWins, incLosses) {
export async function pMatchHistory(request, reply, fastify, getUserInfo, addMatch, incWinsPong, incLossesPong, incWinsTetris, incLossesTetris) {
try {
const userId = request.params.userId;
if (!request.user) {
@ -16,6 +16,9 @@ export async function pMatchHistory(request, reply, fastify, getUserInfo, addMat
if (request.user !== 'admin' && request.user !== userId) {
return reply.code(401).send({ error: "Unauthorized" });
}
if (request.body.game !== 'pong' && request.body.game !== 'tetris') {
return reply.code(400).send({ error: "Specified game does not exist" });
}
if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" });
}
@ -26,12 +29,23 @@ export async function pMatchHistory(request, reply, fastify, getUserInfo, addMat
return reply.code(400).send({ error: "Do you have dementia ? You cannot have played a match against yourself gramps" });
}
await fetchSave(request, reply, userId, addMatch);
if (request.body.myScore > request.body.opponentScore) {
incWins.run(userId);
incLosses.run(request.body.opponent);
} else if (request.body.myScore < request.body.opponentScore) {
incWins.run(request.body.opponent);
incLosses.run(userId);
if (request.body.game === 'pong') {
if (request.body.myScore > request.body.opponentScore) {
incWinsPong.run(userId);
incLossesPong.run(request.body.opponent);
} else if (request.body.myScore < request.body.opponentScore) {
incWinsPong.run(request.body.opponent);
incLossesPong.run(userId);
}
}
else if (request.body.game === 'tetris') {
if (request.body.myScore > request.body.opponentScore) {
incWinsTetris.run(userId);
incLossesTetris.run(request.body.opponent);
} else if (request.body.myScore < request.body.opponentScore) {
incWinsTetris.run(request.body.opponent);
incLossesTetris.run(userId);
}
}
return reply.code(200).send({ msg: "Match successfully saved to the blockchain" });
} catch (err) {