-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (39 loc) · 1.38 KB
/
Copy pathserver.js
File metadata and controls
48 lines (39 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
app.use(express.static('public'));
let players = [];
io.on('connection', (socket) => {
console.log('بازیکن وصل شد:', socket.id);
players.push(socket);
if (players.length === 2) {
players[0].emit('start', 'منتظر انتخاب شما هستیم...');
players[1].emit('start', 'منتظر انتخاب شما هستیم...');
}
socket.on('choice', (choice) => {
socket.choice = choice;
if (players.every(p => p.choice)) {
const [p1, p2] = players;
const result = getResult(p1.choice, p2.choice);
p1.emit('result', { you: p1.choice, opponent: p2.choice, result: result[0] });
p2.emit('result', { you: p2.choice, opponent: p1.choice, result: result[1] });
players.forEach(p => p.choice = null);
}
});
socket.on('disconnect', () => {
players = players.filter(p => p !== socket);
});
});
function getResult(c1, c2) {
if (c1 === c2) return ['مساوی', 'مساوی'];
if (
(c1 === 'rock' && c2 === 'scissors') ||
(c1 === 'paper' && c2 === 'rock') ||
(c1 === 'scissors' && c2 === 'paper')
) return ['بردی', 'باختی'];
else return ['باختی', 'بردی'];
}
server.listen(process.env.PORT || 3000);