Skip to content
Snippets Groups Projects
Commit 25da6521 authored by Fredrik Fonn Hansen's avatar Fredrik Fonn Hansen :8ball:
Browse files

Resolve "backend highscore and stats"

parent 370f2ab3
No related branches found
No related tags found
1 merge request!62Resolve "backend highscore and stats"
......@@ -90,7 +90,7 @@ Gradle-build:
- ./gradlew build --refresh-dependencies
- gradle build
Backend (NTNU-VM):
VM Deploy:
image: node:16.10.0
stage: deploy
needs: [Typescript-compile, API-test]
......@@ -114,14 +114,14 @@ Backend (NTNU-VM):
VM Heartbeat:
stage: deploy
needs: [Backend (NTNU-VM)]
needs: [VM Deploy]
image: alpine
script:
- apk add --no-cache curl
- |
for i in {1..18}; do
sleep 10
response=$(curl --silent --write-out "%{http_code}" --connect-timeout 5 --max-time 10 http://10.212.26.72/server/heartbeat)
response=$(curl --silent --write-out "%{http_code}\n" --output /dev/null --connect-timeout 5 --max-time 10 http://10.212.26.72/server/heartbeat)
echo "Response: $response"
if [ "$response" -eq 200 ]; then
echo "Server is alive"
......@@ -132,4 +132,4 @@ VM Heartbeat:
exit 1
only:
refs:
- main
- main
\ No newline at end of file
......@@ -84,3 +84,43 @@ export const deleteUser = async (req: Request, res: Response): Promise<void> =>
res.status(404).send('User not found');
}
};
// update highscore
export const updateHighscore = async (req: Request, res: Response): Promise<void> => {
const usersRef = admin.firestore().collection('users');
const username = req.params.username;
const requestBody = req.body;
// check if request body is a valid User object
if (
requestBody.username === undefined ||
requestBody.highscore === undefined ||
requestBody.games === undefined ||
requestBody.wins === undefined ||
requestBody.losses === undefined
) {
res.status(400).send('Invalid request body');
}
if (username === undefined || username === '') {
res.status(400).send('No username provided');
}
const user = await getUserById(username);
if (user) {
// update highscore
const newUserData: Partial<User> = {
highscore: requestBody.highscore,
games: requestBody.games,
wins: requestBody.wins,
losses: requestBody.losses,
};
log('firestore: sending request to update score of ' + user.username);
await usersRef.doc(user.id).update(newUserData);
res.status(200).send('Highscore updated');
} else {
res.status(404).send('User not found');
}
};
......@@ -113,4 +113,45 @@ router.post('/:username', userController.createUser);
*/
router.delete('/:username', userController.deleteUser);
/**
* @swagger
* /user/{username}/highscore:
* post:
* tags: [User]
* summary: Update the users stats.
* description: Update the users stats.
* parameters:
* - name: username
* in: query
* required: true
* description: Username of the user to update.
* schema:
* type: string
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* username:
* type: string
* wins:
* type: integer
* losses:
* type: integer
* highscore:
* type: integer
* games:
* type: integer
* responses:
* 200:
* description: Score updated.
* 400:
* description: Invalid request.
* 404:
* description: User not found.
*/
router.post('/:username/highscore', userController.updateHighscore);
export default router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment