-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathreal-time.js
106 lines (94 loc) · 2.47 KB
/
real-time.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
const { getAllServers, getServerPlayers } = require("./DB/Servers");
const { getPlayer } = require("./DB/PlayersSockets");
const { io } = require("./server");
const API = require("./api");
io.on("connection", (socket) => {
socket.on("get-servers", (_, cb = () => {}) => {
try {
cb(null, getAllServers());
} catch (error) {
cb(error);
console.log(error);
}
});
socket.on("get-server-players", (_, cb = () => {}) => {
try {
const { serverId } = getPlayer(socket.id);
cb(null, getServerPlayers(serverId));
} catch (error) {
cb(error);
console.log(error);
}
});
socket.on(
"create-server",
({ serverName, serverPassword, player }, cb = () => {}) => {
try {
const serverId = API.createServer({ serverName, serverPassword });
API.joinServer({ serverId, serverPassword, player, socket, cb });
} catch (error) {
cb(error);
console.log(error);
}
}
);
socket.on(
"join-server",
({ serverId, serverPassword, player }, cb = () => {}) => {
try {
API.joinServer({ serverId, serverPassword, player, socket, cb });
} catch (error) {
cb(error);
console.log(error);
}
}
);
socket.on("add-bots", (_, cb = () => {}) => {
try {
API.addBots({ socket });
} catch (error) {
cb(error);
console.log(error);
}
});
socket.on("start-game", (_, cb = () => {}) => {
try {
const { serverId } = getPlayer(socket.id);
API.startGame(serverId);
} catch (err) {
cb(err);
}
});
socket.on("move", ({ cardId, draw }, cb = () => {}) => {
try {
API.move({ socket, cardId, draw });
cb(null);
} catch (error) {
cb(error);
console.log(error);
}
});
// socket.on("play-again", ({ player }, cb = () => {}) => {
// try {
// const { playerId, serverId } = getPlayer(socket.id);
// const server = getServer(serverId);
// if (!server.gameRunning) {
// server.join(player);
// io.to(serverId).emit("players-changed", server.players);
// if (server.players.length === server.numberOfPlayers) {
// startGame(server);
// }
// cb(null, playerId);
// }
// } catch (error) {
// cb(error);
// console.log(error);
// }
// });
socket.on("leave-server", () => {
API.leaveServer(socket);
});
socket.on("disconnect", () => {
API.leaveServer(socket);
});
});