-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
127 lines (109 loc) · 3.19 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
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import { Server } from "socket.io";
const app = express();
const PORT = process.env.PORT || 3000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "view")));
app.use(express.static(path.join(__dirname, "images")));
app.get("/", (req, res, next) => {
res.sendFile("index.html");
});
const server = app.listen(PORT);
const io = new Server(server);
let room = [];
io.on("connection", (socket) => {
console.log("client connected");
socket.on("disconnect", () => {
console.log("Client disconnected");
});
socket.on("createRoom", (roomID) => {
room[roomID] = { p1Choice: null };
room[roomID] = { p1Score: 0 };
socket.join(roomID);
socket.to(roomID).emit("playersConnected", { roomID: roomID });
});
socket.on("joinRoom", (roomID) => {
if (!io.sockets.adapter.rooms.has(roomID)) {
return socket.emit("Not a ValidToken");
}
const roomSize = io.sockets.adapter.rooms.get(roomID).size;
if (roomSize > 1) {
return socket.emit("roomFull");
}
if (io.sockets.adapter.rooms.has(roomID)) {
socket.join(roomID);
room[roomID] = { p2Choice: null };
socket.to(roomID).emit("playersConnected");
return socket.emit("playersConnected");
}
});
socket.on("p1Choice", (data) => {
if (data) {
const choice = data.rpschoice;
const roomID = data.roomID;
room[roomID].p1Choice = choice;
socket
.to(roomID)
.emit("p1Choice", { rpsValue: choice, score: room[roomID].p1Score });
if (room[roomID].p2Choice) {
return declareWinner(roomID);
}
}
});
socket.on("p2Choice", (data) => {
if (data) {
const choice = data.rpschoice;
const roomID = data.roomID;
room[roomID].p2Choice = choice;
socket
.to(roomID)
.emit("p2Choice", { rpsValue: choice, score: room[roomID].p2Score });
if (room[roomID].p1Choice) {
return declareWinner(roomID);
}
}
});
socket.on("playerClicked", (data) => {
const roomID = data.roomID;
room[roomID].p1Choice = null;
return socket.to(roomID).emit("playAgain");
});
socket.on("exitGame", (data) => {
const roomID = data.roomID;
if (data.player) {
socket.to(roomID).emit("player1Left");
} else {
socket.to(roomID).emit("player2Left");
}
return socket.leave(roomID);
});
});
const declareWinner = (roomID) => {
let winner;
if (room[roomID].p1Choice == room[roomID].p2Choice) {
winner = "draw";
} else if (room[roomID].p1Choice == "rock") {
if (room[roomID].p2Choice == "scissor") {
winner = "p1";
} else {
winner = "p2";
}
} else if (room[roomID].p1Choice == "paper") {
if (room[roomID].p2Choice == "scissor") {
winner = "p2";
} else {
winner = "p1";
}
} else if (room[roomID].p1Choice == "scissor") {
if (room[roomID].p2Choice == "rock") {
winner = "p2";
} else {
winner = "p1";
}
}
return io.sockets.to(roomID).emit("winner", winner);
};