-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
141 lines (124 loc) · 3.96 KB
/
Copy pathserver.js
File metadata and controls
141 lines (124 loc) · 3.96 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const { v4: uuidv4 } = require('uuid');
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
const PORT = process.env.PORT || 3000;
// Serve static files
app.use(express.static('.'));
// Game rooms
const rooms = new Map();
io.on('connection', (socket) => {
console.log('A user connected:', socket.id);
// Create a new room
socket.on('createRoom', () => {
const roomId = uuidv4().substring(0, 6).toUpperCase();
rooms.set(roomId, {
players: [socket.id],
board: Array(9).fill(null),
currentPlayer: 'X',
gameOver: false,
winner: null,
scores: { X: 0, O: 0 }
});
socket.join(roomId);
socket.emit('roomCreated', roomId);
console.log(`Room ${roomId} created by ${socket.id}`);
});
// Join a room
socket.on('joinRoom', (roomId) => {
const room = rooms.get(roomId);
if (!room) {
socket.emit('error', 'Room not found');
return;
}
if (room.players.length >= 2) {
socket.emit('error', 'Room is full');
return;
}
room.players.push(socket.id);
socket.join(roomId);
socket.emit('roomJoined', roomId);
io.to(roomId).emit('gameStart', room);
console.log(`${socket.id} joined room ${roomId}`);
});
// Make a move
socket.on('makeMove', (data) => {
const { roomId, index } = data;
const room = rooms.get(roomId);
if (!room || room.gameOver || room.board[index] !== null) return;
const playerIndex = room.players.indexOf(socket.id);
const playerSymbol = playerIndex === 0 ? 'X' : 'O';
if (room.currentPlayer !== playerSymbol) return;
room.board[index] = playerSymbol;
const winner = checkWinner(room.board);
if (winner) {
room.gameOver = true;
room.winner = winner;
room.scores[winner]++;
} else if (room.board.every(cell => cell !== null)) {
room.gameOver = true;
} else {
room.currentPlayer = room.currentPlayer === 'X' ? 'O' : 'X';
}
io.to(roomId).emit('updateGame', room);
});
// Restart game
socket.on('restartGame', (roomId) => {
const room = rooms.get(roomId);
if (!room) return;
room.board = Array(9).fill(null);
room.currentPlayer = 'X';
room.gameOver = false;
room.winner = null;
io.to(roomId).emit('updateGame', room);
});
// Reset scores
socket.on('resetScores', (roomId) => {
const room = rooms.get(roomId);
if (!room) return;
room.scores = { X: 0, O: 0 };
io.to(roomId).emit('updateGame', room);
});
// Disconnect
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
// Remove from rooms
for (const [roomId, room] of rooms) {
const index = room.players.indexOf(socket.id);
if (index !== -1) {
room.players.splice(index, 1);
if (room.players.length === 0) {
rooms.delete(roomId);
} else {
io.to(roomId).emit('playerDisconnected');
}
break;
}
}
});
});
function checkWinner(board) {
const winPatterns = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
for (const pattern of winPatterns) {
const [a, b, c] = pattern;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return board[a];
}
}
return null;
}
server.listen(PORT, '0.0.0.0' , () => {
console.log(`Server running on port ${PORT}`);
});