From cf187db768d7fde723978fe468e984549cf04272 Mon Sep 17 00:00:00 2001
From: Fredrik Fonn Hansen <fredrfha@stud.ntnu.no>
Date: Sat, 15 Apr 2023 13:13:58 +0200
Subject: [PATCH] Backend highscore fix

---
 .../src/controllers/highscoreController.ts    | 20 +++----------------
 backend/src/functions/firebaseCache.ts        |  8 ++------
 backend/src/routes/gameRoutes.ts              |  4 ++--
 backend/src/routes/lobbyRoutes.ts             |  4 ++--
 4 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/backend/src/controllers/highscoreController.ts b/backend/src/controllers/highscoreController.ts
index dd508aa..cf570fa 100644
--- a/backend/src/controllers/highscoreController.ts
+++ b/backend/src/controllers/highscoreController.ts
@@ -1,27 +1,13 @@
 import { Request, Response } from 'express';
-import { Lobby } from '../models/Lobby';
-import { GameHandler } from '../gameHandler';
-import { getUserById } from '../functions/getUserById';
 import { User } from '../../types/User';
-import admin from '../functions/firebaseAdmin';
-import { IGame } from '../interfaces/IGame';
 import { getTopUsers } from '../functions/firebaseCache';
 
-const gameHandler = GameHandler.getInstance();
-
 export const top10 = async (req: Request, res: Response): Promise<void> => {
   const topUsers = await getTopUsers();
 
-  if (topUsers === null) {
-    res.status(204).send('No highscores found');
+  if (topUsers) {
+    res.status(200).send(topUsers);
   } else {
-    const users = topUsers.docs.map(
-      (doc: any) =>
-        ({
-          id: doc.id,
-          ...doc.data(),
-        } as User)
-    );
-    res.status(200).send(users);
+    res.status(204).send('No highscores found');
   }
 };
diff --git a/backend/src/functions/firebaseCache.ts b/backend/src/functions/firebaseCache.ts
index 18376f4..97ae006 100644
--- a/backend/src/functions/firebaseCache.ts
+++ b/backend/src/functions/firebaseCache.ts
@@ -58,17 +58,13 @@ export async function getUsersIds(): Promise<any> {
  *
  * @returns a list of users
  */
-export async function getTopUsers(): Promise<any> {
+export async function getTopUsers(): Promise<User[] | null> {
   const usersRef: CollectionReference = admin.firestore().collection('users');
   const querySnapshot: Query = usersRef.orderBy('highscore', 'desc').limit(10);
   const cacheKey = 'topUsers';
   const responseMapper = (firestoreResponse: QueryDocumentSnapshot[]) => {
     return firestoreResponse.map((doc) => {
-      return {
-        id: doc.id,
-        username: doc.data().username,
-        highscore: doc.data().highscore,
-      };
+      return { ...doc.data } as User;
     });
   };
   return retrieveFromCache(querySnapshot, queryCache, cacheKey, responseMapper);
diff --git a/backend/src/routes/gameRoutes.ts b/backend/src/routes/gameRoutes.ts
index e192fb1..d3afad7 100644
--- a/backend/src/routes/gameRoutes.ts
+++ b/backend/src/routes/gameRoutes.ts
@@ -107,7 +107,7 @@ router.post('/:gameid/move', gameController.move);
 
 /**
  * @swagger
- * /game/{id}/currentTurn:
+ * /game/{gameId}/gameState:
  *   post:
  *     tags: [Game]
  *     summary: Check current turn.
@@ -171,7 +171,7 @@ router.post('/:gameid/move', gameController.move);
  *                             tankType:
  *                               type: string
  */
-router.get('/:gameid/currentTurn', gameController.currentTurn);
+router.get('/:gameid/gameState', gameController.currentTurn);
 
 /**
  * @swagger
diff --git a/backend/src/routes/lobbyRoutes.ts b/backend/src/routes/lobbyRoutes.ts
index 5318f5e..12b8157 100644
--- a/backend/src/routes/lobbyRoutes.ts
+++ b/backend/src/routes/lobbyRoutes.ts
@@ -5,7 +5,7 @@ const router = express.Router();
 
 /**
  * @swagger
- * /lobby/{id}/leave:
+ * /lobby/{lobbyId}/leave:
  *   post:
  *     tags: [Lobby]
  *     summary: Leaves a lobby.
@@ -30,7 +30,7 @@ router.post('/:id/leave', lobbyController.leaveLobby);
 
 /**
  * @swagger
- * /lobby/{id}/join:
+ * /lobby/{lobbyId}/join:
  *   post:
  *     tags: [Lobby]
  *     summary: Joins a lobby.
-- 
GitLab