From eb9d912f60a4a278713bccb3c0dd3c073bf23c9e Mon Sep 17 00:00:00 2001 From: Sagnik Mandal Date: Tue, 28 May 2024 13:53:29 +0530 Subject: [PATCH] gameStore: Added functions to add and retrieve games Implemented the createGame function which creates a new game, generates it's id and adds it the the games map. Also added the retrieveGame function which retrieves a game, given it's id. Also added the uuid package to generate unique ids for games. Fixes #5 Signed-off-by: Sagnik Mandal --- backend/gameStore.js | 21 ++++++++++++----- backend/package.json | 55 ++++++++++++++++++++++---------------------- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/backend/gameStore.js b/backend/gameStore.js index 0fe61c5..53251fb 100644 --- a/backend/gameStore.js +++ b/backend/gameStore.js @@ -1,16 +1,25 @@ // This module is responsible for setting and retrieving the game state. +import { v4 as uuid } from 'uuid'; + import { GameEngine } from './uno-game-engine/engine'; -const games = map(); +const games = new Map(); +/** + * Create a new game and store it in the games map + * @returns {string} gameId +*/ export function createGame() { - //todo: generate a unique game id and store + const gameId = uuid(); + const game = new GameEngine(); + games.set(gameId, game); + return gameId; } /** - * - * @param {string} id Game id + * Retrieve a game from the games map + * @param {string} id gameId * @returns {GameEngine|null} GameEngine instance */ export function retrieveGame(id) { - //todo: Retrieve the game from the store -} + return games.get(id) || null; +} \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index 6d51df2..849790f 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,29 +1,30 @@ { - "name": "backend", - "version": "1.0.0", - "main": "index.js", - "type": "module", - "scripts": { - "lint": "eslint .", - "format": "prettier --check .", - "fix-format": "prettier --write .", - "test": "jest", - "start": "node src/index.js" - }, - "keywords": [], - "author": "", - "license": "ISC", - "description": "", - "dependencies": { - "cors": "^2.8.5", - "dotenv": "^16.4.5", - "express": "^4.19.2", - "mongoose": "^8.4.0" - }, - "devDependencies": { - "eslint": "^8.57.0", - "jest": "^29.7.0", - "prettier": "^3.2.5", - "supertest": "^7.0.0" - } + "name": "backend", + "version": "1.0.0", + "main": "index.js", + "type": "module", + "scripts": { + "lint": "eslint .", + "format": "prettier --check .", + "fix-format": "prettier --write .", + "test": "jest", + "start": "node src/index.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "cors": "^2.8.5", + "dotenv": "^16.4.5", + "express": "^4.19.2", + "mongoose": "^8.4.0", + "uuid": "^9.0.1" + }, + "devDependencies": { + "eslint": "^8.57.0", + "jest": "^29.7.0", + "prettier": "^3.2.5", + "supertest": "^7.0.0" + } }