# krishbansal10/wordsense architecture

> Architecture diagram of the GitHub repository krishbansal10/wordsense, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/krishbansal10/wordsense
- Repository: https://github.com/krishbansal10/wordsense

## Overview

#### Purpose and Top-Level Shape
WordSense is a semantic word-guessing game. A React/Vite frontend drives authenticated gameplay against a Node.js backend. The backend uses local GloVe 100-dimensional embeddings and a vocabulary dataset to rank guesses by semantic proximity rather than spelling.

Primary runtime entry points:
- Backend HTTP server: `backend/src/server.js`
- Frontend bootstrap and app routing: `frontend/src/main.jsx`, `frontend/src/App.jsx`

#### Backend API and Request Boundary
The backend is organized as route → middleware → controller → service/model. Route modules expose authentication, game, and user-facing endpoints:
- `backend/src/routes/authRoutes.js`
- `backend/src/routes/gameRoutes.js`
- `backend/src/routes/userRoutes.js`

Cross-cutting request controls protect gameplay and normalize failures:
- Authentication enforcement: `backend/src/middleware/authMiddleware.js`
- Guess and hint throttling: `backend/src/middleware/guessRateLimiter.js`, `backend/src/middleware/hintLimiter.js`
- Central error handling: `backend/src/middleware/errorMiddleware.js`

#### Identity and Player State
Authentication and player-account operations are handled separately from game logic:
- `backend/src/controllers/authController.js`
- `backend/src/controllers/userController.js`
- `backend/src/models/User.js`

The frontend maintains the authenticated session in React context and uses route protection before rendering game/account pages:
- `frontend/src/context/AuthContext.jsx`
- `frontend/src/components/ProtectedRoute.jsx`
- `frontend/src/services/authService.js`

#### Game Lifecycle and Persistence
Game requests are orchestrated in the game controller/service layer. This is the main control boundary between HTTP concerns, semantic ranking logic, and persisted game state:
- `backend/src/controllers/gameController.js`
- `backend/src/services/gameService.js`
- `backend/src/models/Game.js`

Individual guesses are persisted independently, enabling game history, latest-guess display, rankings, and statistics:
- `backend/src/models/Guess.js`
- `backend/src/controllers/gameController.js`

Database connectivity is initialized through:
- `backend/src/config/db.js`

#### Semantic Ranking Engine
The core domain mechanism is offline/precomputed semantic ranking. Embeddings are loaded from the bundled GloVe data; similarity logic computes cosine closeness and game logic caches or retrieves target-specific ranks:
- `backend/src/services/embeddingService.js`
- `backend/src/services/similarityService.js`
- `backend/src/data/embeddings/glove.6B.100d.wordsense.txt`

The valid vocabulary/word source is local rather than an external AI inference service:
- `backend/src/data/words.json`
- `backend/src/services/gameService.js`

Gameplay flow is: select hidden target → derive/cache vocabulary similarity rankings → validate guess → look up rank → persist guess/update game → return state, hints, and progress.

#### Frontend Game Experience
The client separates page-level flows from reusable game presentation components:
- Main gameplay page: `frontend/src/pages/GamePage.jsx`
- Guess submission UI: `frontend/src/components/GuessInput.jsx`
- Guess and ranking presentation: `frontend/src/components/GuessItem.jsx`, `frontend/src/components/TopWords.jsx`

Frontend service modules form the client/API boundary, with API base configuration centralized in:
- `frontend/src/config/api.js`
- `frontend/src/services/gameService.js`
- `frontend/src/services/userService.js`

Additional routed views cover landing, authentication, leaderboard/closest-word discovery, and player stats:
- `frontend/src/pages/HomePage.jsx`
- `frontend/src/pages/ClosestWordsPage.jsx`
- `frontend/src/pages/StatsPage.jsx`

#### Material Runtimes and Infrastructure
The system runs a Vite-built React frontend and a Node.js backend, each with its own package manifest:
- `frontend/package.json`
- `backend/package.json`

Persistence is database-backed through the backend model layer and connection configuration. Semantic intelligence is provided by checked-in pretrained embedding assets, so gameplay ranking does not require a live external ML or embedding API.

Main entry points: [`backend/src/services/gameService.js`](https://github.com/krishbansal10/wordsense/blob/master/backend/src/services/gameService.js) (Game lifecycle service), [`frontend/src/config/api.js`](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/config/api.js) (API client services), [`frontend/src/main.jsx`](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/main.jsx) (App bootstrap & routing), [`backend/src/config/db.js`](https://github.com/krishbansal10/wordsense/blob/master/backend/src/config/db.js) (Database connection)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React frontend"]
  node_frontend_bootstrap["App bootstrap &amp; routing<br/>React/Vite entry<br/>[main.jsx]"]
  node_auth_context["Auth session context<br/>React context<br/>[AuthContext.jsx]"]
  node_protected_route["Protected routes<br/>route guard<br/>[ProtectedRoute.jsx]"]
  node_game_page["Gameplay page<br/>[GamePage.jsx]"]
  node_game_ui["Guess &amp; rank UI<br/>components<br/>[GuessInput.jsx]"]
  node_other_pages["Discovery &amp; stats pages"]
  node_client_services["API client services<br/>client API boundary<br/>[api.js]"]
end

subgraph group_backend["Node backend"]
  node_http_server["HTTP server<br/>Node entry<br/>[server.js]"]
  node_routes["API routes<br/>route modules<br/>[gameRoutes.js]"]
  node_request_controls["Auth, limits &amp; errors<br/>middleware<br/>[authMiddleware.js]"]
  node_auth_user_controllers["Auth &amp; user controllers<br/>[authController.js]"]
  node_game_controller["Game controller<br/>[gameController.js]"]
  node_game_service["Game lifecycle service<br/>domain service<br/>[gameService.js]"]
end

subgraph group_semantic["Semantic ranking"]
  node_embedding_service["Embedding loader<br/>semantic service"]
  node_similarity_service["Cosine similarity<br/>ranking service"]
  node_embedding_data["GloVe 100d embeddings<br/>pretrained local data"]
  node_vocabulary["Valid vocabulary<br/>local word data<br/>[words.json]"]
end

subgraph group_persistence["Persistence"]
  node_db_connection[("Database connection<br/>database setup<br/>[db.js]")]
  node_user_model[("Player accounts<br/>User model<br/>[User.js]")]
  node_game_model[("Game state<br/>Game model<br/>[Game.js]")]
  node_guess_model[("Guess history<br/>Guess model<br/>[Guess.js]")]
end

node_frontend_bootstrap -->|"provides session"| node_auth_context
node_frontend_bootstrap -->|"guards routes"| node_protected_route
node_protected_route -->|"renders"| node_game_page
node_frontend_bootstrap -->|"routes to"| node_other_pages
node_game_page -->|"composes"| node_game_ui
node_auth_context -->|"auth requests"| node_client_services
node_game_ui -->|"game requests"| node_client_services
node_other_pages -->|"user/game requests"| node_client_services
node_client_services -->|"HTTP API"| node_http_server
node_http_server -->|"mounts"| node_routes
node_routes -->|"applies"| node_request_controls
node_request_controls -->|"auth/user endpoints"| node_auth_user_controllers
node_request_controls -->|"protected game endpoints"| node_game_controller
node_auth_user_controllers -->|"accounts"| node_user_model
node_game_controller -->|"orchestrates"| node_game_service
node_game_controller -->|"persists guesses"| node_guess_model
node_game_service -->|"creates and updates"| node_game_model
node_game_service -->|"selects and validates words"| node_vocabulary
node_game_service -->|"gets target ranks"| node_similarity_service
node_similarity_service -->|"uses vectors"| node_embedding_service
node_embedding_service -->|"loads"| node_embedding_data
node_similarity_service -->|"ranks vocabulary"| node_vocabulary
node_db_connection -->|"connects"| node_user_model
node_db_connection -->|"connects"| node_game_model
node_db_connection -->|"connects"| node_guess_model

click node_frontend_bootstrap "https://github.com/krishbansal10/wordsense/blob/master/frontend/src/main.jsx"
click node_auth_context "https://github.com/krishbansal10/wordsense/blob/master/frontend/src/context/AuthContext.jsx"
click node_protected_route "https://github.com/krishbansal10/wordsense/blob/master/frontend/src/components/ProtectedRoute.jsx"
click node_game_page "https://github.com/krishbansal10/wordsense/blob/master/frontend/src/pages/GamePage.jsx"
click node_game_ui "https://github.com/krishbansal10/wordsense/blob/master/frontend/src/components/GuessInput.jsx"
click node_other_pages "https://github.com/krishbansal10/wordsense/blob/master/frontend/src/pages/ClosestWordsPage.jsx"
click node_client_services "https://github.com/krishbansal10/wordsense/blob/master/frontend/src/config/api.js"
click node_http_server "https://github.com/krishbansal10/wordsense/blob/master/backend/src/server.js"
click node_routes "https://github.com/krishbansal10/wordsense/blob/master/backend/src/routes/gameRoutes.js"
click node_request_controls "https://github.com/krishbansal10/wordsense/blob/master/backend/src/middleware/authMiddleware.js"
click node_auth_user_controllers "https://github.com/krishbansal10/wordsense/blob/master/backend/src/controllers/authController.js"
click node_game_controller "https://github.com/krishbansal10/wordsense/blob/master/backend/src/controllers/gameController.js"
click node_game_service "https://github.com/krishbansal10/wordsense/blob/master/backend/src/services/gameService.js"
click node_embedding_service "https://github.com/krishbansal10/wordsense/blob/master/backend/src/services/embeddingService.js"
click node_similarity_service "https://github.com/krishbansal10/wordsense/blob/master/backend/src/services/similarityService.js"
click node_embedding_data "https://github.com/krishbansal10/wordsense/blob/master/backend/src/data/embeddings/glove.6B.100d.wordsense.txt"
click node_vocabulary "https://github.com/krishbansal10/wordsense/blob/master/backend/src/data/words.json"
click node_db_connection "https://github.com/krishbansal10/wordsense/blob/master/backend/src/config/db.js"
click node_user_model "https://github.com/krishbansal10/wordsense/blob/master/backend/src/models/User.js"
click node_game_model "https://github.com/krishbansal10/wordsense/blob/master/backend/src/models/Game.js"
click node_guess_model "https://github.com/krishbansal10/wordsense/blob/master/backend/src/models/Guess.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_bootstrap,node_auth_context,node_protected_route,node_game_page,node_game_ui,node_other_pages,node_client_services toneBlue
class node_http_server,node_routes,node_request_controls,node_auth_user_controllers,node_game_controller,node_game_service toneAmber
class node_embedding_service,node_similarity_service,node_embedding_data,node_vocabulary toneMint
class node_db_connection,node_user_model,node_game_model,node_guess_model toneRose
```

## Components

### React frontend

- [App bootstrap & routing](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/main.jsx): React/Vite entry · `frontend/src/main.jsx`
- [Auth session context](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/context/AuthContext.jsx): React context · `frontend/src/context/AuthContext.jsx`
- [Protected routes](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/components/ProtectedRoute.jsx): route guard · `frontend/src/components/ProtectedRoute.jsx`
- [Gameplay page](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/pages/GamePage.jsx): page · `frontend/src/pages/GamePage.jsx`
- [Guess & rank UI](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/components/GuessInput.jsx): components · Submits guesses and presents ranks and closest words. · `frontend/src/components/GuessInput.jsx`
- [Discovery & stats pages](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/pages/ClosestWordsPage.jsx): pages · Landing, closest-word discovery, and player statistics. · `frontend/src/pages/ClosestWordsPage.jsx`
- [API client services](https://github.com/krishbansal10/wordsense/blob/master/frontend/src/config/api.js): client API boundary · Auth, game, and user requests use centralized API configuration. · `frontend/src/config/api.js`

### Node backend

- [HTTP server](https://github.com/krishbansal10/wordsense/blob/master/backend/src/server.js): Node entry · `backend/src/server.js`
- [API routes](https://github.com/krishbansal10/wordsense/blob/master/backend/src/routes/gameRoutes.js): route modules · Authentication, game, and user endpoints. · `backend/src/routes/gameRoutes.js`
- [Auth, limits & errors](https://github.com/krishbansal10/wordsense/blob/master/backend/src/middleware/authMiddleware.js): middleware · Enforces authentication, throttles guesses and hints, and normalizes failures. · `backend/src/middleware/authMiddleware.js`
- [Auth & user controllers](https://github.com/krishbansal10/wordsense/blob/master/backend/src/controllers/authController.js): controllers · `backend/src/controllers/authController.js`
- [Game controller](https://github.com/krishbansal10/wordsense/blob/master/backend/src/controllers/gameController.js): controller · HTTP boundary for game lifecycle, guesses, hints, and progress. · `backend/src/controllers/gameController.js`
- [Game lifecycle service](https://github.com/krishbansal10/wordsense/blob/master/backend/src/services/gameService.js): domain service · Selects targets, validates guesses, and coordinates ranking and state updates. · `backend/src/services/gameService.js`

### Semantic ranking

- [Embedding loader](https://github.com/krishbansal10/wordsense/blob/master/backend/src/services/embeddingService.js): semantic service · `backend/src/services/embeddingService.js`
- [Cosine similarity](https://github.com/krishbansal10/wordsense/blob/master/backend/src/services/similarityService.js): ranking service · Builds or retrieves target-specific vocabulary ranks. · `backend/src/services/similarityService.js`
- [GloVe 100d embeddings](https://github.com/krishbansal10/wordsense/blob/master/backend/src/data/embeddings/glove.6B.100d.wordsense.txt): pretrained local data · `backend/src/data/embeddings/glove.6B.100d.wordsense.txt`
- [Valid vocabulary](https://github.com/krishbansal10/wordsense/blob/master/backend/src/data/words.json): local word data · `backend/src/data/words.json`

### Persistence

- [Database connection](https://github.com/krishbansal10/wordsense/blob/master/backend/src/config/db.js): database setup · `backend/src/config/db.js`
- [Player accounts](https://github.com/krishbansal10/wordsense/blob/master/backend/src/models/User.js): User model · `backend/src/models/User.js`
- [Game state](https://github.com/krishbansal10/wordsense/blob/master/backend/src/models/Game.js): Game model · `backend/src/models/Game.js`
- [Guess history](https://github.com/krishbansal10/wordsense/blob/master/backend/src/models/Guess.js): Guess model · Each guess is stored independently for history, rankings, and statistics. · `backend/src/models/Guess.js`

## Connections

- App bootstrap & routing → Auth session context: provides session
- App bootstrap & routing → Protected routes: guards routes
- Protected routes → Gameplay page: renders
- App bootstrap & routing → Discovery & stats pages: routes to
- Gameplay page → Guess & rank UI: composes
- Auth session context → API client services: auth requests
- Guess & rank UI → API client services: game requests
- Discovery & stats pages → API client services: user/game requests
- API client services → HTTP server: HTTP API
- HTTP server → API routes: mounts
- API routes → Auth, limits & errors: applies
- Auth, limits & errors → Auth & user controllers: auth/user endpoints
- Auth, limits & errors → Game controller: protected game endpoints
- Auth & user controllers → Player accounts: accounts
- Game controller → Game lifecycle service: orchestrates
- Game controller → Guess history: persists guesses
- Game lifecycle service → Game state: creates and updates
- Game lifecycle service → Valid vocabulary: selects and validates words
- Game lifecycle service → Cosine similarity: gets target ranks
- Cosine similarity → Embedding loader: uses vectors
- Embedding loader → GloVe 100d embeddings: loads
- Cosine similarity → Valid vocabulary: ranks vocabulary
- Database connection → Player accounts: connects
- Database connection → Game state: connects
- Database connection → Guess history: connects
