diff --git a/backend/controllers/gameController.js b/backend/controllers/gameController.js index e8ae76c..b5241a3 100644 --- a/backend/controllers/gameController.js +++ b/backend/controllers/gameController.js @@ -2,12 +2,19 @@ import Game from '../models/Game.js'; import GameLevel from '../models/GameLevel.js'; import GameAttempt from '../models/GameAttempt.js'; import UserGameProgress from '../models/UserGameProgress.js'; +import { seedGames } from '../config/seedGames.js'; // @desc Get all active games with company details and user progress // @route GET /api/games // @access Public (with optional user context) export const getGames = async (req, res) => { try { + let count = await Game.countDocuments({ isActive: true }); + if (count === 0) { + console.log('No active games in DB. Auto-seeding 10 cognitive games on the fly...'); + await seedGames(); + } + const games = await Game.find({ isActive: true }) .populate('companies', 'name slug logo') .sort({ order: 1 }) @@ -54,10 +61,21 @@ export const getGames = async (req, res) => { // @access Public (with optional user context) export const getGameBySlug = async (req, res) => { try { - const game = await Game.findOne({ slug: req.params.slug, isActive: true }) + let game = await Game.findOne({ slug: req.params.slug, isActive: true }) .populate('companies', 'name slug logo website') .lean(); + if (!game) { + const count = await Game.countDocuments({ isActive: true }); + if (count === 0) { + console.log('Database empty during game lookup. Auto-seeding 10 games...'); + await seedGames(); + game = await Game.findOne({ slug: req.params.slug, isActive: true }) + .populate('companies', 'name slug logo website') + .lean(); + } + } + if (!game) { return res.status(404).json({ message: 'Game not found' }); } diff --git a/backend/server.js b/backend/server.js index ea78d2e..2aff056 100644 --- a/backend/server.js +++ b/backend/server.js @@ -19,8 +19,20 @@ import adminCompanyGuideRoutes from './routes/adminCompanyGuideRoutes.js'; import gameRoutes from './routes/gameRoutes.js'; import { notFound, errorHandler } from './middleware/errorMiddleware.js'; -// Connect to database -connectDB(); +// Connect to database & auto-seed cognitive games if empty +connectDB().then(async () => { + try { + const Game = (await import('./models/Game.js')).default; + const count = await Game.countDocuments({ isActive: true }); + if (count === 0) { + console.log('Production startup check: 0 games found in database. Auto-seeding 10 cognitive games & 50 levels...'); + const { seedGames } = await import('./config/seedGames.js'); + await seedGames(); + } + } catch (err) { + console.error('Error during initial game seed check:', err.message); + } +}); const app = express(); diff --git a/frontend/src/services/gameService.js b/frontend/src/services/gameService.js index d963782..4afa97b 100644 --- a/frontend/src/services/gameService.js +++ b/frontend/src/services/gameService.js @@ -4,47 +4,230 @@ import api from './api'; const getGuestProgressKey = (slug) => `nqtcoder_guest_game_${slug}`; const getGuestStatsKey = () => `nqtcoder_guest_game_stats`; +export const FALLBACK_GAMES = [ + { + _id: 'geo-sudo-fallback', + name: 'Geo-Sudo', + slug: 'geo-sudo', + gameType: 'geo-sudo', + shortDescription: 'Visual logical reasoning game based on completing geometric Latin-square grids using unique shapes.', + description: 'Geo-Sudo is a visual logical reasoning game based on completing a grid using different shapes. Instead of numbers, Geo-Sudo uses geometric symbols. Find the correct shape that should be placed in the missing position following Latin-square rules.', + category: 'deductive', + skills: ['Identify visual patterns', 'Think logically without guessing', 'Eliminate incorrect possibilities', 'Concentrate under time pressure'], + difficulty: 'Medium to Hard', + companyNames: ['Cognizant', 'Capgemini'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'switch-challenge-fallback', + name: 'Switch Challenge', + slug: 'switch-challenge', + gameType: 'switch-challenge', + shortDescription: 'Decode 4-digit sequence permutation operators transforming geometric shapes under strict countdown timers.', + description: 'Switch Challenge tests cognitive speed and deductive problem-solving. Given an initial sequence of 4 lime-green geometric shapes and their transformed arrangement, deduce the active permutation operator (e.g. 2413).', + category: 'deductive', + skills: ['Rapid deductive reasoning', 'Sequence decoding', 'Working memory under strict time pressure'], + difficulty: 'Medium to Hard', + companyNames: ['Cognizant'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'motion-challenge-fallback', + name: 'Motion Challenge', + slug: 'motion-challenge', + gameType: 'motion-challenge', + shortDescription: 'Sliding-block Klotski puzzle. Slide colored barrier blocks to clear a path for the Red Ball to reach the goal.', + description: 'Motion Challenge is a spatial navigation puzzle inspired by classic sliding block and Klotski mechanics. Slide rectangular barrier blocks horizontally and vertically to maneuver the red ball to the black hole in minimum moves.', + category: 'spatial', + skills: ['Spatial planning', 'Short-term visual simulation', 'Obstacle clearance strategy', 'Move optimization'], + difficulty: 'Medium to Hard', + companyNames: ['Capgemini'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'inductive-challenge-fallback', + name: 'Inductive Challenge', + slug: 'inductive-challenge', + gameType: 'inductive-challenge', + shortDescription: 'Discover the governing geometric transformation rule between example grids and select matching candidate grids.', + description: 'Inductive Challenge evaluates your inductive reasoning ability. Study two example 3×3 transformation grids to identify the governing spatial rule (rotations, flips, reflections) and identify which pair follows the exact same rule.', + category: 'inductive', + skills: ['Pattern induction', 'Abstract spatial rotation', 'Dual-grid relationship matching'], + difficulty: 'Hard', + companyNames: ['Capgemini', 'Cognizant'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'grid-challenge-fallback', + name: 'Grid Challenge', + slug: 'grid-challenge', + gameType: 'grid-challenge', + shortDescription: '3-phase working memory capacity task: memorizing flashing dot coordinates amidst symmetry distractions.', + description: 'Grid Challenge assesses multi-step working memory capacity. Memorize flashing dots in sequential order, solve an intermediate symmetry task, and recall all coordinates in the exact original sequence.', + category: 'memory', + skills: ['Working memory capacity', 'Distraction filtering', 'Sequential coordinate recall'], + difficulty: 'Medium to Hard', + companyNames: ['Cognizant'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'digit-challenge-fallback', + name: 'Digit Challenge', + slug: 'digit-challenge', + gameType: 'digit-challenge', + shortDescription: 'Mental arithmetic under pressure: combine single-use digits and arithmetic operators to hit target values.', + description: 'Digit Challenge tests quantitative agility and fast mental arithmetic. Use given single-use digits and mathematical operators (+, -, *, /) to construct an equation matching the target value before time expires.', + category: 'numerical', + skills: ['Rapid mental calculation', 'Arithmetic decomposition', 'High-pressure speed accuracy'], + difficulty: 'Medium to Hard', + companyNames: ['Capgemini'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'colour-the-grid-fallback', + name: 'Colour the Grid', + slug: 'colour-the-grid', + gameType: 'colour-the-grid', + shortDescription: 'Binary classification game: deduce the underlying feature rule determining whether a grid is Orange or Blue.', + description: 'Colour the Grid tests feature rule abstraction. Analyze example grids categorized into Orange and Blue classes based on topological or numeric features, then classify new candidate grids accurately.', + category: 'inductive', + skills: ['Rule induction', 'Multi-attribute classification', 'Feature abstraction'], + difficulty: 'Medium', + companyNames: ['Capgemini'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'the-same-rule-fallback', + name: 'The Same Rule', + slug: 'the-same-rule', + gameType: 'the-same-rule', + shortDescription: 'Identify which candidate sequence obeys the governing structural relationship shown in example patterns.', + description: 'The Same Rule measures abstract relationship transfer. Discover the hidden structural property shared by example sequences (such as alternation, symmetry, or parity) and pick the matching candidate sequence.', + category: 'inductive', + skills: ['Rule transfer', 'Relational abstraction', 'Structural mapping'], + difficulty: 'Medium to Hard', + companyNames: ['Capgemini'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'oddo-similarity-grid-fallback', + name: 'Oddo / Similarity Grid', + slug: 'oddo-similarity-grid', + gameType: 'oddo-similarity-grid', + shortDescription: 'Matrix topology and symmetry matching: identify which two candidate grids share the same symmetry.', + description: 'Oddo evaluates visual-spatial matrix topology. Given 4 candidate matrices (A, B, C, D), identify the two grids that share identical underlying structural symmetry (such as vertical reflection, diagonal invariance, or center point symmetry).', + category: 'spatial', + skills: ['Matrix symmetry matching', 'Topology extraction', 'Perceptual invariance'], + difficulty: 'Medium to Hard', + companyNames: ['Capgemini'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + }, + { + _id: 'doesnt-fit-the-rule-fallback', + name: 'Doesn\'t Fit the Rule', + slug: 'doesnt-fit-the-rule', + gameType: 'doesnt-fit-the-rule', + shortDescription: 'Outlier detection: identify the geometric figure that violates the common rule connecting all other options.', + description: 'Doesn\'t Fit the Rule tests anomaly detection and systematic feature evaluation. Given multiple geometric figures, discover the common governing rule and select the single outlier that violates it.', + category: 'deductive', + skills: ['Anomaly detection', 'Feature comparison', 'Systematic elimination'], + difficulty: 'Medium', + companyNames: ['Cognizant', 'Capgemini'], + totalLevels: 5, + estimatedTime: '10-15 mins', + progress: { highestUnlockedLevel: 1, completedLevelsCount: 0, bestScore: 0, bestAccuracy: 0, bestStreak: 0 } + } +]; + export const getGames = async () => { try { const { data } = await api.get('/api/games'); - return data; + if (Array.isArray(data) && data.length > 0) { + return data; + } + return FALLBACK_GAMES; } catch (error) { console.warn('API /api/games failed, using fallback games catalog', error); - // If backend is unreachable, fallback to minimal seed list - return []; + return FALLBACK_GAMES; } }; export const getGameBySlug = async (slug) => { + let gameData = null; try { const { data } = await api.get(`/api/games/${slug}`); - - // Check if guest has local progress stored - const localProgressStr = localStorage.getItem(getGuestProgressKey(slug)); - if (localProgressStr) { - try { - const localProgress = JSON.parse(localProgressStr); - if (!data.userProgress || (data.userProgress.highestUnlockedLevel || 1) < (localProgress.highestUnlockedLevel || 1)) { - data.userProgress = { ...data.userProgress, ...localProgress }; - // update levels unlocked status - if (data.levels) { - data.levels = data.levels.map((lvl) => ({ - ...lvl, - isUnlocked: lvl.levelNumber <= (localProgress.highestUnlockedLevel || 1), - isCompleted: (localProgress.completedLevels || []).includes(lvl.levelNumber), - })); - } + gameData = data; + } catch (error) { + console.warn(`API /api/games/${slug} failed, building local fallback game payload`, error); + const fallbackGame = FALLBACK_GAMES.find((g) => g.slug === slug || g.gameType === slug) || FALLBACK_GAMES[0]; + gameData = { + game: fallbackGame, + levels: [1, 2, 3, 4, 5].map((lvlNum) => ({ + levelNumber: lvlNum, + name: `Level ${lvlNum}`, + description: `Progressive cognitive challenge tier ${lvlNum}`, + timeLimit: 50, + scoreMultiplier: 1.0 + (lvlNum - 1) * 0.2, + passingCriteria: { minAccuracy: 70 }, + totalChallenges: 5, + difficultyConfig: { gridSize: lvlNum >= 4 ? 5 : 4, missingCells: lvlNum * 2 }, + isUnlocked: lvlNum === 1, + isCompleted: false, + stars: 0, + bestScore: 0, + bestAccuracy: 0 + })), + userProgress: { + highestUnlockedLevel: 1, + completedLevels: [], + bestScore: 0, + bestAccuracy: 0, + bestTime: 0, + bestStreak: 0, + levelStats: [] + } + }; + } + + // Check if guest has local progress stored + const localProgressStr = localStorage.getItem(getGuestProgressKey(slug)); + if (localProgressStr && gameData) { + try { + const localProgress = JSON.parse(localProgressStr); + if (!gameData.userProgress || (gameData.userProgress.highestUnlockedLevel || 1) < (localProgress.highestUnlockedLevel || 1)) { + gameData.userProgress = { ...gameData.userProgress, ...localProgress }; + if (gameData.levels) { + gameData.levels = gameData.levels.map((lvl) => ({ + ...lvl, + isUnlocked: lvl.levelNumber <= (localProgress.highestUnlockedLevel || 1), + isCompleted: (localProgress.completedLevels || []).includes(lvl.levelNumber), + })); } - } catch (e) { - console.error('Error merging local progress:', e); } + } catch (e) { + console.error('Error merging local progress:', e); } - - return data; - } catch (error) { - console.error('Error fetching game details:', error); - throw error; } + + return gameData; }; export const submitLevelAttempt = async (slug, levelNumber, attemptData) => {