Projects / city /Β project_summary.md
git clone https://molodetz.nl/retoor/city.git
Raw source file available here .
# City Builder - Project Summary
Project Overview
A fully functional multiplayer city building game inspired by Transport Tycoon's visual style, built with FastAPI backend and Three.js frontend.
What Was Built
Backend (Python/FastAPI)
All backend files are in the
server/
directory:
-
main.py - FastAPI application entry point
- WebSocket endpoint for multiplayer
- Static file serving
- Game loop for economy ticks and persistence
-
websocket_manager.py - WebSocket connection management
- Player connection/disconnection
- Message broadcasting
- Cursor synchronization
-
models.py - Data models
- 13 building types with unique properties
- Player model with economy tracking
- Building configurations with costs and benefits
-
game_state.py - Game state management
- Building placement/removal logic
- Road network connectivity (flood fill algorithm)
- Zone size calculation for economy bonuses
-
economy.py - Economy engine
- Tick-based economy processing
- Offline player support (10% power)
- Connectivity-based income bonuses
-
database.py - SQLite persistence
- Auto-save every 10 seconds
- Player data persistence
- Building state persistence
Frontend (JavaScript/Three.js)
All frontend files are in the
static/
directory:
-
App.js - Main application controller
- Coordinates all systems
- Handles game state updates
- Player action processing
-
WebSocketClient.js - Real-time communication
- WebSocket connection management
- Message handling and routing
- Auto-reconnect logic
-
GameRenderer.js - Three.js 3D rendering
- Orthographic camera (Transport Tycoon style)
- Building mesh creation
- Tile highlighting
- Player cursor rendering
-
InputHandler.js - User input processing
- Mouse controls (left/right click, wheel)
- Keyboard shortcuts
- Camera pan and zoom
- Tile coordinate conversion
-
UIManager.js - UI component coordination
- Component initialization
- State updates
- Event routing
UI Components (Web Components)
All components extend HTMLElement:
- LoginScreen.js - Nickname entry screen
- StatsDisplay.js - Money and population display
- BuildingToolbox.js - Building selection menu
- ChatBox.js - IRC-style chat interface
- ContextMenu.js - Right-click building menu
Configuration Files
- requirements.txt - Python dependencies
- .gitignore - Git ignore rules
- run.py - Convenient startup script
- README.md - Complete documentation
Key Features Implemented
Core Gameplay
- β 13 unique building types (houses, shops, factories, infrastructure, special)
- β Realistic building costs ($500 - $100,000)
- β Economic system with income/expenses per tick
- β Population management
- β Building requirements (population, power)
Road System
- β Road construction ($500 per tile)
- β Connectivity algorithm (flood fill)
- β Economy bonuses based on network size (5% per road)
- β Multiple disconnected zones support
Multiplayer
- β Real-time WebSocket communication
- β Live cursor synchronization
- β Building synchronization across clients
- β Player-specific colors
- β Join/leave notifications
- β IRC-style chat system
Persistence
- β SQLite database storage
- β Auto-save every 10 seconds
- β Nickname-based login (no passwords)
- β Offline economy processing (10%)
- β Session resumption
User Interface
- β Login screen (Enter to submit)
- β Stats display (money, population)
- β Building toolbox with prices
- β Grayed out unaffordable options
- β Context menu (Edit/Delete)
- β Chat box with timestamps
- β No buttons - keyboard shortcuts
Controls
- β Right mouse drag - Pan camera
- β Mouse wheel - Zoom in/out
- β Left click - Place building
- β Right click on building - Context menu
- β Enter - Confirm input
- β Escape - Cancel input
- β Tile highlighting on hover
Rendering Optimization
- β Viewport culling (only render visible tiles)
- β Throttled cursor updates (100ms)
- β Efficient building mesh management
- β Three.js orthographic camera for performance
Game Mechanics
Building Types & Economics
Residential (Provide Population)
- Small House: -$50/tick, +10 pop
- Medium House: -$120/tick, +25 pop
- Large House: -$250/tick, +50 pop
Commercial (Generate Income)
- Small Shop: +$100/tick, -5 pop, needs 20 pop
- Supermarket: +$300/tick, -15 pop, needs 50 pop
- Mall: +$800/tick, -40 pop, needs 100 pop
Industrial (Generate Income)
- Small Factory: +$200/tick, -20 pop
- Large Factory: +$500/tick, -50 pop
Infrastructure
- Road: Connects buildings, boosts economy
- Park: -$20/tick, +5 pop
- Plaza: -$40/tick, +10 pop
Special
- Town Hall: -$100/tick, +100 pop
- Power Plant: -$500/tick, -30 pop, enables large buildings
Economy Formula
Building Income = Base Income Γ Connectivity Bonus
Connectivity Bonus = 1.0 + (Road Network Size Γ 0.05)
Offline Economy
When a player is offline, their economy continues at 10% power:
Offline Income = Base Income Γ 0.10
Technical Specifications
Server
- Framework : FastAPI 0.118.0
- WebSocket : Native FastAPI WebSocket support
- Database : SQLite3 (built-in)
- Host : 127.0.0.1:9901
- Game Tick : Every 10 seconds
- Persistence : Every 10 seconds
Client
- Renderer : Three.js r128
- View : Orthographic camera (Transport Tycoon style)
- Architecture : ES6 Modules
- Components : Web Components (Custom Elements)
- No frameworks : Pure vanilla JavaScript
Communication Protocol
All WebSocket messages are JSON with a
type
field:
Client β Server
-
cursor_move: {x, y} -
place_building: {building_type, x, y} -
remove_building: {x, y} -
edit_building: {x, y, name} -
chat: {message, timestamp}
Server β Client
-
init: Initial player and game state -
game_state_update: Periodic full state sync -
building_placed: Building added -
building_removed: Building removed -
building_updated: Building name changed -
cursor_move: Player cursor moved -
player_joined: Player connected -
player_left: Player disconnected -
chat: Chat message -
error: Error message
Project Structure
city-builder/
βββ server/ # Backend Python code
β βββ __init__.py
β βββ main.py
β βββ websocket_manager.py
β βββ game_state.py
β βββ economy.py
β βββ database.py
β βββ models.py
βββ static/ # Frontend code
β βββ index.html
β βββ css/
β β βββ style.css
β βββ js/
β βββ App.js
β βββ WebSocketClient.js
β βββ GameRenderer.js
β βββ InputHandler.js
β βββ UIManager.js
β βββ components/
β βββ LoginScreen.js
β βββ StatsDisplay.js
β βββ BuildingToolbox.js
β βββ ChatBox.js
β βββ ContextMenu.js
βββ data/ # SQLite database (auto-created)
β βββ game.db
βββ requirements.txt
βββ .gitignore
βββ run.py
βββ README.md
βββ PROJECT_SUMMARY.md
Development Approach
The project follows modern best practices:
- Separation of Concerns : Clear separation between backend, frontend, and UI
- Component Architecture : Each component is self-contained
- ES6 Modules : Modern JavaScript module system
- Web Components : Custom HTML elements for reusability
- Real-time Communication : WebSocket for instant updates
- Optimistic Updates : UI updates immediately, server validates
- Error Handling : Graceful error messages to users
- Performance : Only render what's visible
- Persistence : Auto-save prevents data loss
- Multiplayer : True multiplayer with shared game state
How to Run
# Install dependencies
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# Start server
python run.py
# Open in browser
http://127.0.0.1:9901
Testing the Game
- Open browser to http://127.0.0.1:9901
- Enter a nickname (press Enter)
-
Try building:
- Click "Small House" in the toolbox
- Click on the map to place it
- Build more houses to increase population
- Build roads to connect buildings
- Build shops once you have enough population
-
Try multiplayer:
- Open another browser tab/window
- Enter a different nickname
- See both players' cursors and buildings
-
Try chat:
- Type in the chat box at the bottom
- Press Enter to send
-
Try right-click menu:
- Right-click your own building
- Choose "Edit Name" or "Delete"
Completion Status
ALL REQUIREMENTS MET β
Every feature from the original specification has been implemented:
- Transport Tycoon visual style with Three.js
- Real-time multiplayer via WebSocket
- 10+ building types with economic gameplay
- Road connectivity system
- Player-specific colors
- Live cursor tracking
- IRC-style chat
- Nickname-based login
- Offline economy (10% power)
- Context menus
- No-button inputs (Enter/Escape)
- Performance optimizations
- SQLite persistence
The game is fully playable and ready for deployment!