-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
150 lines (136 loc) · 4.44 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
require("dotenv").config();
const http = require("http");
const express = require("express");
const socketio = require("socket.io");
const mongoose = require("mongoose");
const cors = require("cors");
const routes = require("./api/routes");
const { getOpponent, chooseSides, resetPlayers } = require("./utilities/utils");
const Timer = require("./utilities/timer");
const socketAddPoints = require("./services/socketAddPoints");
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(cors());
routes(app);
mongoose.connect(process.env.DATABASEURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});
io.on("connection", (socket) => {
console.log("New WebSocket connection");
setInterval(() => {
if (socket.opponent && !socket.gameover) {
if (io.of("/").connected[socket.opponent]) {
if (socket.time.getTime() === 0) {
socket.emit("winner", socket.side === "X" ? "O" : "X");
socket
.to(socket.opponent)
.emit("winner", socket.side === "X" ? "O" : "X");
resetPlayers(socket, io.of("/").connected[socket.opponent]);
}
if (io.of("/").connected[socket.opponent].time.getTime() === 0) {
socket.emit(
"winner",
io.of("/").connected[socket.opponent].side === "X" ? "O" : "X"
);
socket
.to(socket.opponent)
.emit(
"winner",
io.of("/").connected[socket.opponent].side === "X" ? "O" : "X"
);
resetPlayers(socket, io.of("/").connected[socket.opponent]);
}
}
}
}, 1000);
socket.on("winner", ({ side, opponentId }) => {
resetPlayers(socket, io.of("/").connected[opponentId]);
socket.to(opponentId).emit("winner", side);
});
socket.on("play", ({ box, opponentId }) => {
socket.time.pause();
io.of("/").connected[opponentId].time.start();
socket.to(opponentId).emit("play", {
box,
timeObject: {
user: socket.time.getTime(),
opponent: io.of("/").connected[opponentId].time.getTime(),
},
});
});
socket.on("setPlayers", (username) => {
socket.name = username;
socket.time = new Timer(30);
socket.gameover = false;
socket.join("waiting");
io.in("waiting").clients((error, clients) => {
if (error) throw error;
const opponentId = getOpponent(socket, clients, io);
if (opponentId) {
io.of("/").connected[opponentId].leave("waiting");
socket.leave("waiting");
socket.opponent = opponentId;
io.of("/").connected[opponentId].opponent = socket.id;
const { side1, side2 } = chooseSides();
const createPayload = (socket, side, initialPayload) => {
socket.side = side;
side === "X" ? socket.time.setTime(34) : socket.time.setTime(30);
return {
...initialPayload,
callback: side === "X" ? socket.time.start() : null,
};
};
socket.to(opponentId).emit(
"matched",
createPayload(socket, side1, {
username,
id: socket.id,
side: side1,
timeObject: {
opponent: socket.time.getTime(),
user: io.of("/").connected[opponentId].time.getTime(),
},
})
);
socket.emit(
"matched",
createPayload(io.of("/").connected[opponentId], side2, {
username: io.of("/").connected[opponentId].name,
id: opponentId,
side: side2,
timeObject: {
user: socket.time.getTime(),
opponent: io.of("/").connected[opponentId].time.getTime(),
},
})
);
}
});
});
socket.on("cancel", () => {
socket.leave("waiting");
});
socket.on("updateuserpoints", async (token) => {
const error = await socketAddPoints(token);
if (error) return socket.emit("an error", error);
socket.emit("updated");
});
socket.on("disconnect", () => {
if (!socket.gameover && socket.opponent) {
socket
.to(socket.opponent)
.emit("winner", socket.side === "X" ? "O" : "X");
resetPlayers(socket, io.of("/").connected[socket.opponent]);
console.log("disconnected");
}
});
});
server.listen(port, () => {
console.log(`Server is up on port ${port}!`);
});