-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
162 lines (142 loc) · 4.36 KB
/
server.js
File metadata and controls
162 lines (142 loc) · 4.36 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import express from "express";
import { Server } from "socket.io";
import http from "http";
import { Chess } from "chess.js";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
},
});
let chess = new Chess();
let players = {};
let currentPlayer = "w";
app.use(express.static(path.join(__dirname, "dist")));
io.on("connection", function (socket) {
console.log("A new client connected:", socket.id);
socket.on("joinGame", () => {
console.log(`Socket ${socket.id} requesting to join game`);
// Check if player is already in the game
if (players.white === socket.id || players.black === socket.id) {
console.log(`Player ${socket.id} already in game`);
return;
}
if (!players.white) {
players.white = socket.id;
console.log("Player joined as White (Waiting)");
} else if (!players.black) {
players.black = socket.id;
console.log("Player joined as Black");
} else {
console.log("Spectator joined");
socket.emit("spectatorRole");
socket.emit("boardState", {
fen: chess.fen(),
history: chess.history(),
});
return;
}
if (players.white && players.black) {
console.log("Both players present. Starting new game...");
chess = new Chess(); // Start a fresh game
console.log("New Game FEN:", chess.fen());
io.to(players.white).emit("gameStarted", {
role: "w",
fen: chess.fen(),
history: chess.history(),
});
io.to(players.black).emit("gameStarted", {
role: "b",
fen: chess.fen(),
history: chess.history(),
});
console.log("Game Started emitted to players");
}
});
socket.on("leaveQueue", () => {
if (players.white === socket.id) {
delete players.white;
console.log("White left queue");
} else if (players.black === socket.id) {
delete players.black;
console.log("Black left queue");
}
});
socket.on("disconnect", function () {
console.log("User disconnected:", socket.id);
if (socket.id === players.white) {
delete players.white;
io.emit("playerDisconnected", "w");
if (players.black) {
io.to(players.black).emit("gameOver", {
winner: "b",
reason: "opponent_disconnected",
});
}
} else if (socket.id === players.black) {
delete players.black;
io.emit("playerDisconnected", "b");
if (players.white) {
io.to(players.white).emit("gameOver", {
winner: "w",
reason: "opponent_disconnected",
});
}
}
});
socket.on("resign", () => {
const playerColor = socket.id === players.white ? "w" : "b";
const opponentColor = playerColor === "w" ? "b" : "w";
io.emit("gameOver", { winner: opponentColor, reason: "resignation" });
});
socket.on("abort", () => {
io.emit("gameOver", { winner: "draw", reason: "aborted" });
});
socket.on("move", (move) => {
try {
if (chess.turn() === "w" && socket.id !== players.white) return;
if (chess.turn() === "b" && socket.id !== players.black) return;
const result = chess.move(move);
if (result) {
currentPlayer = chess.turn();
io.emit("move", move);
io.emit("boardState", {
fen: chess.fen(),
history: chess.history(),
});
if (chess.isGameOver()) {
let winner = null;
let reason = "";
if (chess.isCheckmate()) {
winner = chess.turn() === "w" ? "b" : "w";
reason = "checkmate";
} else if (chess.isDraw()) {
winner = "draw";
reason = "draw";
}
io.emit("gameOver", { winner, reason });
}
if (result.captured) {
io.emit("capture", result.captured);
}
} else {
console.log("Invalid move");
socket.emit("invalidMove", move);
}
} catch (error) {
console.log(error);
socket.emit("invalidMove", move);
}
});
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
server.listen(process.env.PORT || 3000, function () {
console.log("listening on *:3000");
});