-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
206 lines (175 loc) · 6.03 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const io = require('socket.io')(server);
const path = require('path');
const mongoose = require('mongoose');
const { disconnect } = require("process");
app.use(express.json());
mongoose.connect('your Mongo DB address');
const db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
// If database connected failed
db.on('error', (err) => {
console.error(err);
});
// If database connected successfully
db.once('open', (db) => {
console.log('Connected to MongoDB');
});
// Define a database schema for users' data storage
const playersDBSchema = new mongoose.Schema({
name: String,
acc: String,
pw: String,
character: Object
});
const playersData = mongoose.model('playersData', playersDBSchema);
//Declaring a list of players
var playersList = [];
//Declaring a list of players for real-time handling
const playersList2 = [];
const playerCache = {};
const connectPlayerCache = {}
app.use('/public', express.static(path.join(__dirname,'./static')));
app.get('/',(req, res)=>{
res.sendFile(path.join(__dirname, 'index.html'));
});
app.post('/storeInput', (req, res) => {
let playerName = req.body.name;
let playerAcc = req.body.acc;
let playerPw = req.body.pw;
if (playerName && playerAcc && playerPw) {
playerCache.name = playerName;
playerCache.acc = playerAcc;
playerCache.pw = playerPw;
io.emit('playerInit', playerName);
} else {
// Send a bad request response
return res.sendStatus(400);
}
});
app.post('/storeInputEx', (req, res) => {
let playerAcc = req.body.acc2;
let playerPw = req.body.pw2;
if (playerAcc && playerPw) {
playersData.findOne({ acc: playerAcc, pw: playerPw })
.then((foundPlayer) => {
if(foundPlayer){
io.emit('playerInit2', foundPlayer);
return res.sendStatus(200);
}
else {
return res.sendStatus(401);
}
})
.catch((err) => {
console.error('Error querying the database:', err);
return res.sendStatus(500);
})
} else {
// Send a bad request response
return res.sendStatus(400);
}
});
//////////////////////////////////////////////////////////////////////
io.on('connection', (socket) => {
console.log('a user connected');
});
// WebSocket event listener
io.on('connection', function (socket) {
socket.on('aaaa', (playerName) => {
if(playersList.some(el => el.id === socket.id) === false){
let newPlayer = {'type': 0, 'id': socket.id, 'name': playerName};
playersList.push(newPlayer);
console.log('新玩家加入', playersList);
socket.emit('gameBallsInit', playersList2);
socket.emit('playersList', playersList)
}
})
socket.on('aaaa2', (playerName, foundPlayer) => {
if(playersList.some(el => el.id === socket.id) === false){
let newPlayer = {'type': 0, 'id': socket.id, 'name': playerName};
playersList.push(newPlayer);
connectPlayerCache[socket.id] = foundPlayer;
console.log('新玩家加入', playersList);
socket.emit('gameBallsInit', playersList2);
socket.emit('playersListEx', foundPlayer)
console.log("foundPlayerfoundPlayer", foundPlayer);
}
})
// Updated the players' list after user created their own "player"
socket.on('newPlayerCreateReturn', (newPlayer) => {
playersList2.push(newPlayer);
isPlayersInfoChanged = true;
//last object saved into the cache
playerCache.character = newPlayer;
connectPlayerCache[socket.id] = newPlayer;
//save it to the database
let newPlayerData = new playersData(playerCache);
newPlayerData.save()
.then(()=>{
console.log('new player ' + newPlayer.name + 'saved into database successfully!');
})
.catch(()=>{
console.log('new player saved into database failed...');
})
})
// re-draw the player for the existed players, and updated the players list
socket.on('newPlayerCreateReturn2', (newPlayer) => {
playersList2.push(newPlayer);
isPlayersInfoChanged = true;
})
socket.on('playerIsMoving', (mover) => {
let updatePlayer = playersList2.find(el => el.id === mover.id);
if(updatePlayer){
updatePlayer.x = mover.x;
updatePlayer.y = mover.y;
updatePlayer.vx = mover.vx;
updatePlayer.vy = mover.vy;
isPlayersInfoChanged = true;
}
})
// An event listener to detect the user's disconnection, and remove the player from the 'online' list
socket.on('disconnect', function () {
console.log('user disconnected');
// remove this player from playersList
let indexRemove;
let tempCache;
playersList2.forEach(el => {
if (el.id === socket.id){
indexRemove = playersList2.indexOf(el);
tempCache = el;
}
})
playersList2.splice(indexRemove, 1);
playersList.splice(indexRemove, 1);
isPlayersInfoChanged = true;
if(connectPlayerCache[socket.id]){
playersData.findOneAndUpdate({acc: connectPlayerCache[socket.id].acc, pw: connectPlayerCache[socket.id].pw}, {character: tempCache})
.then(()=>{
console.log(tempCache.name + "has been disconnected");
})
.catch(()=>{
console.log(tempCache.name + "has been disconnected and we lost him... QAQ...");
})
}
});
});
let isPlayersInfoChanged = false;
// FPS updating rate on the server side, every one second (1000ms) update 60 times
let serverUpdate = setInterval(mainUpdate, 1000/60);
function mainUpdate(){
if(isPlayersInfoChanged === true){
io.emit('playersDataUpdate', playersList2);
isPlayersInfoChanged = false;
}
}
io.on('connection', (socket) => {
socket.on('chat message', (msg2, talker, msgStyle) => {
io.emit('chat message', msg2, talker, msgStyle);
});
});
server.listen(3351, () => {
console.log("Starting at", 3351);
});