Spaces:
Configuration error
Configuration error
| // js/multiplayer.js - إدارة الاتصالات والمزامنة الأونلاين | |
| class MultiplayerManager { | |
| constructor() { | |
| this.socket = null; | |
| this.roomId = null; | |
| this.playerNum = null; | |
| this.isOnline = false; | |
| this.terrainSeed = null; | |
| this.terrainSettings = null; | |
| this.callbacks = {}; | |
| } | |
| // اتصل بالسيرفر | |
| connect(onlineURL = 'http://localhost:3000') { | |
| return new Promise((resolve, reject) => { | |
| try { | |
| this.socket = io(onlineURL, { | |
| reconnection: true, | |
| reconnectionDelay: 1000, | |
| reconnectionDelayMax: 5000, | |
| reconnectionAttempts: 5 | |
| }); | |
| this.socket.on('connect', () => { | |
| console.log('✅ اتصلنا بالسيرفر:', this.socket.id); | |
| resolve(); | |
| }); | |
| this.socket.on('connect_error', (error) => { | |
| console.error('❌ خطأ في الاتصال:', error); | |
| reject(error); | |
| }); | |
| this.setupEventListeners(); | |
| } catch (error) { | |
| reject(error); | |
| } | |
| }); | |
| } | |
| // إعداد جميع المستمعين (Event Listeners) | |
| setupEventListeners() { | |
| // عند تعيين رقم اللاعب | |
| this.socket.on('playerAssigned', (data) => { | |
| this.playerNum = data.playerNum; | |
| this.roomId = data.roomId; | |
| this.terrainSeed = data.terrainSeed; | |
| this.terrainSettings = data.terrainSettings; | |
| this.isOnline = true; | |
| console.log(`🎮 أنت اللاعب ${data.playerNum}`); | |
| this.call('PlayerAssigned', data); | |
| }); | |
| // في انتظار لاعب ثاني | |
| this.socket.on('waitingForPlayer', (data) => { | |
| console.log('⏳ ' + data.message); | |
| this.call('WaitingForPlayer', data); | |
| }); | |
| // بدء اللعبة | |
| this.socket.on('gameStart', (data) => { | |
| console.log('🎮 اللعبة بدأت!'); | |
| this.call('GameStart', data); | |
| }); | |
| // حركة العدو | |
| this.socket.on('enemyMoved', (data) => { | |
| this.call('EnemyMoved', data); | |
| }); | |
| // إطلاق العدو | |
| this.socket.on('enemyFired', (data) => { | |
| this.call('EnemyFired', data); | |
| }); | |
| // الضرر على اللاعب | |
| this.socket.on('takeDamage', (data) => { | |
| this.call('TakeDamage', data); | |
| }); | |
| // الدور التالي | |
| this.socket.on('nextTurn', (data) => { | |
| this.call('NextTurn', data); | |
| }); | |
| // نهاية اللعبة | |
| this.socket.on('gameEnded', (data) => { | |
| this.call('GameEnded', data); | |
| }); | |
| // اللاعب الآخر قطع الاتصال | |
| this.socket.on('playerDisconnected', (data) => { | |
| this.call('PlayerDisconnected', data); | |
| }); | |
| } | |
| // طلب الانضمام للعبة | |
| joinGame(mapType, playerSkin) { | |
| this.socket.emit('joinGame', { | |
| mapType: mapType || 'forest', | |
| playerSkin: playerSkin || 'classic' | |
| }); | |
| } | |
| // إرسال حركة الدبابة | |
| sendMove(x, fuel) { | |
| if (this.isOnline && this.socket) { | |
| this.socket.emit('playerMove', { | |
| x: x, | |
| fuel: fuel | |
| }); | |
| } | |
| } | |
| // إرسال بيانات الإطلاق | |
| sendFire(angle, power, x, y) { | |
| if (this.isOnline && this.socket) { | |
| this.socket.emit('playerFire', { | |
| angle: angle, | |
| power: power, | |
| x: x, | |
| y: y | |
| }); | |
| } | |
| } | |
| // إرسال الضرر | |
| sendDamage(damage, targetHealth) { | |
| if (this.isOnline && this.socket) { | |
| this.socket.emit('damageDealt', { | |
| damage: damage, | |
| targetHealth: targetHealth | |
| }); | |
| } | |
| } | |
| // إرسال نهاية الدور | |
| sendTurnEnd() { | |
| if (this.isOnline && this.socket) { | |
| this.socket.emit('turnEnd', {}); | |
| } | |
| } | |
| // إرسال نهاية اللعبة | |
| sendGameOver(winner) { | |
| if (this.isOnline && this.socket) { | |
| this.socket.emit('gameOver', { | |
| winner: winner | |
| }); | |
| } | |
| } | |
| // تسجيل دالة callback | |
| on(event, callback) { | |
| this.callbacks['on' + event] = callback; | |
| } | |
| // استدعاء callback معين | |
| call(event, data) { | |
| const key = 'on' + event; | |
| if (this.callbacks[key]) { | |
| this.callbacks[key](data); | |
| } | |
| } | |
| // قطع الاتصال | |
| disconnect() { | |
| if (this.socket) { | |
| this.socket.disconnect(); | |
| this.isOnline = false; | |
| } | |
| } | |
| } | |
| // إنشاء instance عام | |
| window.multiplayer = new MultiplayerManager(); | |