-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
117 lines (112 loc) · 3.45 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
//require("appmetrics-dash").monitor();
const Express = require("express");
const mongoose = require("mongoose");
const app = Express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const MatchManager = require("./game/matchManager");
const User = require("./models/User");
const MonitorGame = require("./tools/MonitorGame");
const io = new Server(server, {
cors: {
origin: "*",
},
});
const PORT = process.env.PORT || 3000;
//MongoDB URL
const URL = require("./conf.json").MongoURL;
const Options = require("./conf.json").MongoOpt;
// //mongose connection
mongoose
.connect(URL, Options)
.then(() => console.log(`mongoose conected to Data Base...`))
.catch((err) => console.log(err));
const matchM = new MatchManager(io);
const Monitor = new MonitorGame(matchM);
setInterval(() => {
if (Monitor.socket) {
Monitor.Update();
}
}, Math.round(1000 / Monitor.frameRate));
io.on("connection", (socket) => {
//console.log("a user connected");
socket.on("init", (data) => {
const user = new User(data.name, data.id, socket);
matchM.players[socket.id] = user;
// Object.values(matchM.players).forEach((player) => {
// console.log(`player[${player.socket.id}] with name:${player.name}`);
// });
});
socket.on("DebugGameInit", (data) => {
Monitor.Init(socket, io);
});
socket.on("DebugGameChangeSetting", (data) => {
if (data.room) {
Monitor.changeRoom(data.room);
}
if (data.framerate) {
Monitor.changeFrameRate(data.framerate);
}
if (data.deleteRoom) {
Monitor.deleteRoom();
}
});
socket.on("ReadyToPlayCustom", (data) => {
matchM.CustomMatchReady(socket);
});
socket.on("PlayerReadyLobby", (data) => {
//console.log(data);
const user = new User(data.name, data.id, socket);
matchM.PlayerReady(user);
});
socket.on("PlayerReadyCustomLobby", (data) => {
const user = new User(data.name, data.id, socket);
matchM.PlayerReadyCustom(user);
});
socket.on("PlayerJoinCustomLobby", (data) => {
// player join in a custom lobby
const user = new User(data.name, data.userID, socket);
matchM.PlayerJoinCustom(user, data.room);
});
socket.on("ChangePosition", (data) => {
// changing the player position for custom lobby
matchM.PlayerChangePosition(socket, data);
});
socket.on("RequestToJoin", (data) => {
Object.entries(matchM.players).forEach((obj) => {
if (data.userID == obj[1].id) {
//console.log("send RequestToJoin");
io.to(obj[0]).emit("RequestToJoin", { room: data.room, name: data.name });
}
});
});
socket.on("ReadySignal", (data) => {
matchM.ReadySignal(socket, data);
});
socket.on("setStage", (data) => {
matchM.NextStage(socket, data);
});
socket.on("SendChat", (data) => {
io.to(matchM.rooms[socket.id]).emit("GetChat", data);
});
socket.on("PlayerLeaveLobby", (data) => {
// remove the player from lobby
matchM.playerLeaveLobby(socket);
});
socket.on("disconnect", () => {
delete matchM.players[socket.id];
matchM.playerDisconnect(socket);
Monitor.disconnect();
console.log(`user [${socket.id}] disconnected`);
});
});
io.of("/").adapter.on("create-room", (room) => {
console.log(`room ${room} was created`);
});
io.of("/").adapter.on("join-room", (room, id) => {
console.log(`socket ${id} has joined room ${room}`);
});
server.listen(PORT, () => {
console.log(`listening on ${PORT} . . .`);
});