This is an extension of Maxim's original X-Tic-Tac-Toe app: https://github.com/xims/X-ttt/tree/master, by adding analytics collection and display features.
- Node.js 14 (recommend using nvm)
- Ports: frontend on 3000, backend on 3001
Important: Every terminal needs Node 14. Recommend using nvm:
nvm use 14 or just nvm use
Start a new terminal → Under repo root, run:
nvm use 14 && cd WS && npm install && npm startStart a new terminal → Under repo root, run:
nvm use 14 && cd react_ws_src && npm install && npm start- Frontend: http://localhost:3000
- Backend health check: http://localhost:3001/api/analytics?mode=live
The SQLite database file is created automatically at
WS/db/game_results.db(gitignored).
- Play one or more games at http://localhost:3000/ttt, for both Live and Computer mode
- Visit http://localhost:3000/analytics and see the analytics data
- Want to see more data? Open a new terminal → Under project root, run:
Use
nvm use 14 && cd WS && node scripts/init-test-data.js
--clear-dbflag to clear previous data first. The new data should show up at http://localhost:3000/analytics
This is the new feature added to the existing X-Tic-Tac-Toe app.
Submits game results to the backend and saves in SQLite database for analytics.
- Only completed games are submitted (incomplete/aborted games are ignored)
- Happens behind the scenes without interrupting users
- Supports both Live and Computer modes
- In Live mode, only the master player submits to avoid duplication
- Draw results are submitted but not displayed in analytics yet
Shows game analytics in three sections:
- Leaderboard - Top 5 players with most wins
- Game Duration Distribution - Game lengths bucketed into time ranges (<30s, 30s-1min, 1min-2min, 2min+)
- Top Winning Patterns - Top 4 most common winning patterns
Configuration: Limits and time buckets can be adjusted in WS/configs/AnalyticsConfig.js
Submits a completed game result.
Request body:
{
"mode": "live" | "computer",
"winnerName": "string" | null,
"gameLength": 45,
"pattern": "xxx_o____"
}Validation:
modemust be "live" or "computer"winnerNameis optional (null for draws, trimmed and normalized to "(no name)" if empty)gameLengthmust be non-negative integer (seconds)patternmust be exactly 9 characters (lowercase x/o or underscore for empty cells)
Response: { success: true, id: <number> }
Fetches analytics data for the specified game mode.
Query params:
mode(required): "live" or "computer"
Response:
{
"leaderboard": [{ "winnerName": "Alice", "winCount": 10 }, ...],
"gameLength": [{ "name": "Less than 30s", "count": 15 }, ...],
"topPatterns": [{ "pattern": ["X","X","X",null,...], "count": 8 }, ...]
}- Input validation using
express-validator - Security headers using
helmet - SQLite parameterized queries to prevent SQL injection
Backend (WS/):
lib/db.js- SQLite database initialization with game_results tablemodels/analytics.js- Data access layer for analytics queriescontrollers/analytics.js- Business logic for analytics (bucketing, formatting)controllers/game-results.js- Handles game result submission with name normalizationroutes/analytics.js- GET endpoint with validationroutes/game-results.js- POST endpoint with validationconfigs/AnalyticsConfig.js- Configurable limits for leaderboard/patterns/bucketsscripts/init-test-data.js- Generates 200 realistic test gamesXttt.js- Registers new API routes__tests__/- Jest test suite (unit + integration tests)
Frontend (react_ws_src/src/):
views/ttt/GameMain.js- Modified to collect game timing and submit resultsviews/analytics/Analytics.js- Main analytics page with mode switcherviews/analytics/Leaderboard.js- Top winners visualizationviews/analytics/GameLengthChart.js- Time distribution chartviews/analytics/TopPatterns.js- Winning patterns displayapp.js- Added /analytics route
Run backend tests:
nvm use 14 && cd WS && npm test- More analytics aspects, such as draw rates, first move win rates, step count, etc
- Logging for debugging and maintenance purposes
- Better strategy for anti-cheating in leaderboard
Major libraries used on front end:
- react
- webpack
- babel
- react-router
- ampersand
- sass
- jest
Major libraries used on server:
- node.js
- socket.io
- express
- WS - server side and compiled front end
- react_ws_src - React development source and testing
https://x-ttt.herokuapp.com/ws_conf.xml
##For demonstration purposes only.