A real-time multiplayer math game where two players compete in a tug-of-war style mathematical challenge. Built with Laravel and WebSocket technology for seamless real-time interactions.
Tug of Math is an educational game that combines mathematics with competitive gameplay. Two players use their mobile devices as controllers to answer math questions while a central monitor displays the game progress in real-time. The game features:
- Real-time gameplay using WebSocket technology
- 3-device setup: 1 monitor (game screen) + 2 mobile devices (controllers)
- Dynamic math questions with varying difficulty (addition, subtraction, multiplication, division)
- QR code scanning for quick player joining
- Live score updates and tug-of-war visualization
- Addition, subtraction, multiplication, and division
- Randomized numbers with difficulty scaling
- Smart answer validation
- Instant feedback on correct/incorrect answers
- Monitor: Displays game screen with QR codes, scores, and questions
- Mobile Controllers: Players input answers using custom number pad
- Real-time sync: All devices stay synchronized via WebSocket
- Laravel 10.x - PHP Framework
- Redis - Message broker for broadcasting
- MySQL/MariaDB - Database
- Laravel Broadcasting - Event broadcasting system
- Laravel Echo Server 1.6.3 - WebSocket server (Node.js)
- Socket.IO v2.3.0 - Real-time communication
- Redis Pub/Sub - Message distribution
- Tailwind CSS - Utility-first CSS framework
- Laravel Echo v1.11.3 - WebSocket client wrapper
- Axios - HTTP client
- QRCode.js - QR code generation
Before you begin, ensure you have the following installed:
- PHP >= 8.1
- Composer
- Node.js >= 14.x
- Redis Server
- MySQL/MariaDB
- Git
git clone https://github.com/ekyaaa/tug_of_math.git
cd tug_of_mathcomposer installnpm install -g laravel-echo-server# Copy environment file
cp .env.example .env
# Generate application key
php artisan key:generateEdit .env file:
APP_NAME="Tug of Math"
APP_URL=http://localhost:8000
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tug_of_math
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379# Run migrations
php artisan migrate
# Optional: Seed database
php artisan db:seed# Windows
redis-server
# Linux/Mac
sudo systemctl start redislaravel-echo-server startphp artisan serve- Open browser and navigate to:
http://localhost:8000 - Click "START GAME"
- You'll see the Lobby with two QR codes (Left & Right)
- Player 1: Scan the LEFT QR code with your phone
- Player 2: Scan the RIGHT QR code with your phone
- Enter your name on your device
- Wait for both players to join
- Once both players have joined, the monitor will show:
- Player avatars replacing QR codes
- "START GAME" button becomes active
- Click "START GAME" on the monitor
- Math questions appear on both devices
- Read the question displayed on your mobile screen
- Enter your answer using the number pad
- Submit by tapping the green checkmark (โ)
- Watch the tug-of-war animation on the monitor
- First player to reach 10 correct answers wins!
Edit laravel-echo-server.json:
{
"authHost": "http://127.0.0.1:8000",
"authEndpoint": "/broadcasting/auth",
"database": "redis",
"databaseConfig": {
"redis": {
"port": "6379",
"host": "127.0.0.1",
"prefix": ""
}
},
"devMode": true,
"host": "0.0.0.0",
"port": "6001",
"protocol": "http",
"socketio": {
"pingTimeout": 60000,
"pingInterval": 25000
}
}To play across devices on the same WiFi:
-
Find your PC's local IP address:
# Windows ipconfig # Linux/Mac ifconfig
-
Update IP in
resources/views/layouts/app.blade.php:window.Echo = new Echo({ broadcaster: 'socket.io', host: 'YOUR_IP:6001', // e.g., '192.168.1.100:6001' transports: ['websocket', 'polling', 'flashsocket'] });
-
Access from mobile:
http://YOUR_IP:8000
This project is open-sourced software licensed under the MIT license.
- Laravel Framework
- Socket.IO
- Laravel Echo Server
- Tailwind CSS
- QRCode.js
Made with โค๏ธ and โ for educational purposes
Happy Gaming! ๐ฎโจ