-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
153 lines (129 loc) · 3.94 KB
/
server.js
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
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
app.use(express.static("./"));
const rooms = new Map();
function generateRoomId() {
return Math.random().toString(36).substring(2, 15);
}
function createGameState() {
return {
fruits: [],
scores: {},
lives: {},
swordX: {},
swordY: {},
players: new Map()
};
}
function gameLoop(roomId) {
const gameState = rooms.get(roomId);
if (!gameState) return;
gameState.players.forEach((playerName, socketId) => {
const opponentId = Array.from(gameState.players.keys()).find((id) => id !== socketId);
if (opponentId) {
io.to(opponentId).emit("updateOpponentState", {
swordX: gameState.swordX[socketId],
swordY: gameState.swordY[socketId],
fruits: gameState.fruits[socketId],
score: gameState.scores[socketId],
lives: gameState.lives[socketId],
});
}
});
}
io.on("connection", (socket) => {
console.log("A user connected");
let currentRoom = null;
socket.on("joinRoom", (playerName) => {
let roomToJoin = null;
// Find a room with only one player or create a new room
for (const [roomId, gameState] of rooms) {
if (gameState.players.size === 1) {
roomToJoin = roomId;
break;
}
}
if (!roomToJoin) {
roomToJoin = generateRoomId();
rooms.set(roomToJoin, createGameState());
}
currentRoom = roomToJoin;
socket.join(currentRoom);
const gameState = rooms.get(currentRoom);
gameState.players.set(socket.id, playerName);
gameState.scores[socket.id] = 0;
gameState.lives[socket.id] = 3;
gameState.swordX[socket.id] = 0;
gameState.swordY[socket.id] = 0;
gameState.fruits[socket.id] = [];
socket.emit("joinedRoom", { roomId: currentRoom });
io.to(currentRoom).emit("playerConnected", Array.from(gameState.players.values()));
if (gameState.players.size === 2) {
io.to(currentRoom).emit("startGame");
setInterval(() => gameLoop(currentRoom), 1000 / 60); // 60 FPS
}
});
socket.on("updateOpponentData", (data) => {
if (!currentRoom) return;
const gameState = rooms.get(currentRoom);
const opponentId = Array.from(gameState.players.keys()).find(
(id) => id !== socket.id
);
if (opponentId) {
io.to(opponentId).emit("updateOpponentState", data);
}
});
socket.on("disconnect", () => {
console.log("A player disconnected");
if (!currentRoom) return;
const gameState = rooms.get(currentRoom);
if (!gameState) return;
io.to(currentRoom).emit("playerDisconnected", {
roomId: currentRoom,
disconnectedPlayerName: gameState.players.get(socket.id)
});
gameState.players.delete(socket.id);
delete gameState.scores[socket.id];
delete gameState.lives[socket.id];
delete gameState.swordX[socket.id];
delete gameState.swordY[socket.id];
delete gameState.fruits[socket.id];
if (gameState.players.size === 1) {
const winningPlayerName = Array.from(gameState.players.values())[0];
io.to(currentRoom).emit("gameOver", {
roomId: currentRoom,
winner: winningPlayerName
});
} else if (gameState.players.size === 0) {
rooms.delete(currentRoom);
}
socket.leave(currentRoom);
});
socket.on("gameOver", (data) => {
if (currentRoom) {
io.to(currentRoom).emit("gameOver", {
roomId: currentRoom,
loser: data.loser
});
}
});
socket.on("leaveRoom", () => {
if (currentRoom) {
const gameState = rooms.get(currentRoom);
if (gameState) {
gameState.players.delete(socket.id);
if (gameState.players.size === 0) {
rooms.delete(currentRoom);
}
}
socket.leave(currentRoom);
currentRoom = null;
}
});
});
const PORT = process.env.PORT || 3000;
http.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});