-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
84 lines (76 loc) · 3.25 KB
/
main.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
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('./ssl.key', 'utf8');
var certificate = fs.readFileSync('./ssl.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/inti', function (req, res) {
console.log(req.method)
console.log(req.headers)
})
var user_aera = []
var package_area = []
function handleRequset(request, db_user, db_package){
console.log("Online players:" + db_user.length)
if(db_user.length > 0){
for (var i=0; i<db_user.length; i++)
{
if(db_user[i] === request.userid){
console.log("Found, so update the date which be found to request's date...")
console.log("old package: ")
console.log(db_package[i])
db_package[i] = request
console.log("new package: ")
console.log(db_package[i])
return
}
}
console.log("not found, so add request's date...")
console.log("old package: ")
console.log(db_package)
console.log("Online players:" + db_user.length)
db_user[db_user.length] = request.userid
db_package[db_package.length] = request
console.log("new package: ")
console.log(db_package)
}else if(db_user.length == 0){
console.log("Received the first request...")
db_user[0] = request.userid
db_package[0] = request
console.log("new package: ")
console.log(db_package)
}
}
var hp = 100;
var mp = 100;
var ex = 0;
app.post('/inti', function (req, res) {
console.log(req.method)
console.log(req.body)
if(req.body.tag == "login"){
var userid = Date.now()
var loginDate = {'userid': userid, 'res': 'ArthurSlog', 'country': 'Shenzhen', 'userId': 'building', 'video': 'https://space.bilibili.com/57386850', 'wechart': 'xiaowang_dalai', 'tag': userid, 'hp': hp, 'mp': mp, 'ex': ex}
handleRequset(loginDate, user_aera, package_area)
res.send(loginDate)
console.log(userid)
}else if(req.body.tag == "reconnect"){
var reconnectDate = {'userid': req.body.userid, 'res': 'ArthurSlog', 'country': 'Shenzhen', 'userId': 'building', 'video': 'https://space.bilibili.com/57386850', 'wechart': 'xiaowang_dalai', 'tag': req.body.userid, 'hp': hp, 'mp': mp, 'ex': ex}
handleRequset(reconnectDate, user_aera, package_area)
res.send(package_area)
}
else{
var synchronizedData = {'userid': req.body.userid, 'tag': req.body.userid, 'x': req.body.x, 'y': req.body.y, 'message': req.body.message, "scene": req.body.scene, 'hp': hp, 'mp': mp, 'ex': ex, 'res': 'ArthurSlog', 'country': 'Shenzhen', 'userId': 'building', 'video': 'https://space.bilibili.com/57386850', 'wechart': 'xiaowang_dalai'}
handleRequset(synchronizedData, user_aera, package_area)
res.send(package_area)
console.log("Respon client: ")
console.log(package_area)
}
})
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(80);
httpsServer.listen(443);