How haramrit09k/spaceterra works
Project type and purpose
• This is a full-stack, real-time single-player browser game (“SpaceTerra”) built with Node.js/Express on the backend, Phaser.js on the frontend, Socket.io for WebSocket communication, and MongoDB for persistent leaderboard storage.
• The README confirms the game engine (Phaser), backend stack (Express + Node), real-time comms (Socket.io) and database (MongoDB), all deployed to Heroku.
File structure
• Top-level files:
• server.js – application entry point where Express and Socket.io are initialized
• package.json / package-lock.json – dependency and script info
• Procfile – Heroku deployment instruction
• Directories:
• public/ – static assets served to the client (JavaScript game code, media, CSS, video demos)
• routes/ – Express route modules (index, users, chat, request)
• views/ – HTML pages (index, register, chatroom, error, sponsors)
• screenshots/ – documentation/screenshots
• No separate sub-services; everything runs as one Node/Express process.
README for additional insights
• Real-time leaderboard via WebSockets (Socket.io).
• Phaser.js game runs in browser, communicates back to server when score updates occur.
• Database operations: write new scores, read top scores for leaderboard.
• Hosted on Heroku with MongoDB (likely Atlas) as an external service.
Main components and their interactions
a. Client (Browser)
• Loads HTML from views via HTTP GET
• Loads Phaser.js game code and assets (audio, video, CSS, images) from public/
• Renders game, captures user input
• Opens a WebSocket connection to the server via Socket.io
• Emits events (e.g., “scoreSubmit”), listens for events (e.g., “leaderboardUpdate”)
b. Web Server (Node.js + Express)
• Serves static assets and HTML pages
• Defines REST/HTTP routes in routes/*.js for any non-WebSocket requests (registration, user info, etc.)
• Integrates Socket.io on the same HTTP server (server.js connects both)
c. Socket.io (Real-Time Layer)
• Handles bi-directional real-time messages
• On “scoreSubmit” from client, invokes server-side handler
• Queries or updates MongoDB, then broadcasts leaderboard changes back to all connected clients
d. Database (MongoDB)
• Stores user profiles (routes/users.js) and score records
• Read/write by the Express/Socket.io server using native driver or Mongoose
e. External Services
• Heroku platform hosting the Node process
• MongoDB Atlas (or self-hosted) as a managed database
• Optional third-party (e.g., CDN for static assets if used later)
Architectural patterns and design principles
• Monolithic backend combining HTTP routes and WebSocket handlers in one process
• MVC-style separation at the route level (routes folder holds controller logic)
• Static-asset separation (public/) vs. template views (views/)
• Event-driven real-time communication (Socket.io)
• Asynchronous, non-blocking I/O (Node.js)
Elements to include for clarity
• Clear labels on all components
• Optionally, group related files under each component (e.g., list public/* under “Static Assets”)