mirror of
https://github.com/KeyZox71/knl_meowscendence.git
synced 2026-01-01 06:06:41 +01:00
added tetris game management
This commit is contained in:
@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user