|
1 | | -# 2026-lightning-talk |
| 1 | +# 🎈 Ball Drop - Real-Time Interactive Application |
| 2 | + |
| 3 | +A real-time interactive ball drop application featuring physics simulation with Matter.js, WebSocket communication, and Gravatar integration. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +### 🖥️ Admin Display Page |
| 8 | +- **Secure Login**: Fixed password authentication for admin access |
| 9 | +- **Physics Engine**: Real-time 2D physics simulation using Matter.js |
| 10 | +- **Customizable Physics**: Adjust gravity and elasticity in real-time |
| 11 | +- **Ball Visualization**: Profile images from Gravatar as falling balls with various colors |
| 12 | +- **Countdown Timer**: Set and display countdown with automatic start/stop |
| 13 | +- **Manual Controls**: Start/stop ball dropping manually |
| 14 | +- **Reset Function**: Remove floor to make all balls fall off screen |
| 15 | + |
| 16 | +### 👥 User Page |
| 17 | +- **Email Entry**: Users enter their email address |
| 18 | +- **Gravatar Preview**: Automatic avatar display for confirmation |
| 19 | +- **Waiting Room**: Hold screen until game starts |
| 20 | +- **Timer Display**: Real-time countdown when active |
| 21 | +- **Drop Button**: Interactive button to drop balls (appears during active period) |
| 22 | +- **Rate Limiting**: Maximum one ball per second per user |
| 23 | +- **Color Selection**: Choose from 8 vibrant colors for your ball |
| 24 | + |
| 25 | +### 🔧 Server Features |
| 26 | +- **WebSocket Rooms**: Separate rooms for display, controllers, and users |
| 27 | +- **Rate Limiting**: Server-side enforcement of 1 ball per second per user |
| 28 | +- **Payload Validation**: Size limits and data validation |
| 29 | +- **Room Filtering**: Messages sent only to relevant rooms |
| 30 | +- **Security**: Password protection and input sanitization |
| 31 | + |
| 32 | +## Installation |
| 33 | + |
| 34 | +```bash |
| 35 | +# Install dependencies |
| 36 | +pnpm install |
| 37 | + |
| 38 | +# Start the server |
| 39 | +pnpm start |
| 40 | +``` |
| 41 | + |
| 42 | +The server will run on `http://localhost:3000` by default. |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +### Environment Variables |
| 47 | + |
| 48 | +```bash |
| 49 | +PORT=3000 # Server port (default: 3000) |
| 50 | +ADMIN_PASSWORD=admin123 # Admin password (default: admin123) |
| 51 | +``` |
| 52 | + |
| 53 | +### Accessing the Application |
| 54 | + |
| 55 | +1. **Admin/Display Screen**: Open `http://localhost:3000/admin.html` |
| 56 | + - Login with the admin password |
| 57 | + - Control physics settings, countdown timer, and game state |
| 58 | + - View all balls dropping in real-time |
| 59 | + |
| 60 | +2. **User Screen**: Open `http://localhost:3000/` or `http://localhost:3000/index.html` |
| 61 | + - Enter your email address |
| 62 | + - Confirm your Gravatar avatar |
| 63 | + - Wait for the game to start |
| 64 | + - Drop balls when the button appears |
| 65 | + |
| 66 | +### Typical Flow |
| 67 | + |
| 68 | +1. Admin opens the display page and logs in |
| 69 | +2. Admin sets physics parameters (gravity, elasticity) |
| 70 | +3. Admin sets countdown timer (e.g., 60 seconds) |
| 71 | +4. Admin clicks "Start Countdown" |
| 72 | +5. Admin enables "Allow Ball Dropping" |
| 73 | +6. Users join and wait for countdown |
| 74 | +7. When countdown reaches zero, users can start dropping balls |
| 75 | +8. Each user can drop one ball per second |
| 76 | +9. Admin can stop dropping at any time |
| 77 | +10. Admin can reset to clear all balls |
| 78 | + |
| 79 | +## Architecture |
| 80 | + |
| 81 | +### WebSocket Rooms |
| 82 | + |
| 83 | +- **`display`**: Admin/display screens receive ball drop events |
| 84 | +- **`controllers`**: Admin controls room for sending commands |
| 85 | +- **`users`**: Regular users receive game state updates |
| 86 | + |
| 87 | +### Rate Limiting |
| 88 | + |
| 89 | +- Server enforces 1 request per second per socket |
| 90 | +- Client-side cooldown display |
| 91 | +- Prevents spam and ensures fair play |
| 92 | + |
| 93 | +### Security Features |
| 94 | + |
| 95 | +- Password-protected admin access |
| 96 | +- Input validation on all user data |
| 97 | +- Payload size limits (1MB max) |
| 98 | +- Email length validation |
| 99 | +- Color format validation |
| 100 | +- Room-based message filtering |
| 101 | + |
| 102 | +## Technology Stack |
| 103 | + |
| 104 | +- **Backend**: Node.js with Express |
| 105 | +- **WebSocket**: Socket.IO for real-time communication |
| 106 | +- **Physics**: Matter.js for 2D physics simulation |
| 107 | +- **Avatars**: Gravatar API for user profile images |
| 108 | +- **Frontend**: Vanilla JavaScript, HTML5, CSS3 |
| 109 | + |
| 110 | +## File Structure |
| 111 | + |
| 112 | +``` |
| 113 | +. |
| 114 | +├── server.js # Express + Socket.IO server |
| 115 | +├── public/ |
| 116 | +│ ├── index.html # User page |
| 117 | +│ └── admin.html # Admin/display page |
| 118 | +├── package.json |
| 119 | +└── README.md |
| 120 | +``` |
| 121 | + |
| 122 | +## API Events |
| 123 | + |
| 124 | +### Client → Server |
| 125 | + |
| 126 | +- `admin:login` - Admin authentication |
| 127 | +- `user:join` - User joins with email |
| 128 | +- `user:drop` - User drops a ball |
| 129 | +- `admin:updatePhysics` - Update physics settings |
| 130 | +- `admin:toggleDropping` - Enable/disable dropping |
| 131 | +- `admin:startCountdown` - Start countdown timer |
| 132 | +- `admin:reset` - Reset world (remove floor) |
| 133 | + |
| 134 | +### Server → Client |
| 135 | + |
| 136 | +- `admin:authenticated` - Authentication result |
| 137 | +- `user:joined` - User successfully joined |
| 138 | +- `user:error` - Error message for user |
| 139 | +- `user:dropped` - Confirmation of ball drop |
| 140 | +- `ball:add` - New ball to display |
| 141 | +- `physics:update` - Physics settings changed |
| 142 | +- `dropping:changed` - Dropping state changed |
| 143 | +- `countdown:start` - Countdown started |
| 144 | +- `countdown:end` - Countdown ended |
| 145 | +- `world:reset` - World reset triggered |
| 146 | + |
| 147 | +## Development |
| 148 | + |
| 149 | +```bash |
| 150 | +# Run in development mode |
| 151 | +pnpm dev |
| 152 | +``` |
| 153 | + |
| 154 | +## License |
| 155 | + |
| 156 | +Apache-2.0 |
| 157 | + |
| 158 | +## Author |
| 159 | + |
| 160 | +Elvis Mao |
0 commit comments