Commit 524bec53 authored by IamI's avatar IamI

Add drop-reline support.

parent 350315b5
...@@ -10,6 +10,7 @@ const config = JSON.parse(fs.readFileSync("./config.json")); ...@@ -10,6 +10,7 @@ const config = JSON.parse(fs.readFileSync("./config.json"));
let athleticUserPool = []; let athleticUserPool = [];
let entertainUserPool = []; let entertainUserPool = [];
let deadUserPool = []; let deadUserPool = [];
let playingPlayerPool = new Map();
let predictedEntertainTime = 600, predictedAthleticTime = 600; let predictedEntertainTime = 600, predictedAthleticTime = 600;
let entertainRequestCountInTime = 0, athleticRequestCountInTime = 0; let entertainRequestCountInTime = 0, athleticRequestCountInTime = 0;
...@@ -215,6 +216,7 @@ let pair = function (userARes, userBRes, serverName) { ...@@ -215,6 +216,7 @@ let pair = function (userARes, userBRes, serverName) {
"port": server.port, "port": server.port,
"password": password "password": password
}); });
playingPlayerPool.set(client.username, result);
client.writeHead(200, {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'}); client.writeHead(200, {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'});
client.end(result); client.end(result);
} }
...@@ -257,7 +259,7 @@ let errorUser = function (res) { ...@@ -257,7 +259,7 @@ let errorUser = function (res) {
localLog(res.username + " errored for get user information."); localLog(res.username + " errored for get user information.");
res.statusCode = 400; res.statusCode = 400;
res.end(); res.end();
} };
// 当用户断开连接时 // 当用户断开连接时
let closedUser = function (res, pool) { let closedUser = function (res, pool) {
...@@ -274,7 +276,17 @@ let closedUser = function (res, pool) { ...@@ -274,7 +276,17 @@ let closedUser = function (res, pool) {
// 若用户未在匹配池中,挂黑名单 // 若用户未在匹配池中,挂黑名单
else else
deadUserPool.push(res); deadUserPool.push(res);
} };
// 当 srvpro 通知本服务器游戏已正常结束时
let finishUser = function (json) {
let userA = json.usernameA;
let userB = json.usernameB;
for (let user in [userA, userB]) {
if (!playingPlayerPool.delete(user))
localLog("Unknown player left the game: " + user);
}
};
// 计算预期时间 // 计算预期时间
let calculatePredictedTime = function() { let calculatePredictedTime = function() {
...@@ -304,6 +316,11 @@ let matchResponse = function(req, res) { ...@@ -304,6 +316,11 @@ let matchResponse = function(req, res) {
if (!username || !password) { if (!username || !password) {
throw 'auth'; throw 'auth';
} }
// 检定是否掉线重连
if (playingPlayerPool.has(username)) {
res.writeHead(200, {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'});
res.end(result);
}
let arg = url.parse(req.url, true).query; let arg = url.parse(req.url, true).query;
if (!arg.arena) arg.arena = 'entertain'; if (!arg.arena) arg.arena = 'entertain';
localLog(username + ' apply for a ' + arg.arena + ' match.'); localLog(username + ' apply for a ' + arg.arena + ' match.');
...@@ -334,7 +351,7 @@ let matchResponse = function(req, res) { ...@@ -334,7 +351,7 @@ let matchResponse = function(req, res) {
res.end(); res.end();
return; return;
} }
} };
// 时间(GET /stats) // 时间(GET /stats)
let getTimeResponse = function(parsedUrl, res) { let getTimeResponse = function(parsedUrl, res) {
...@@ -344,18 +361,29 @@ let getTimeResponse = function(parsedUrl, res) { ...@@ -344,18 +361,29 @@ let getTimeResponse = function(parsedUrl, res) {
textResponse(res, predictedAthleticTime.toString()); textResponse(res, predictedAthleticTime.toString());
else else
notFoundResponse(res); notFoundResponse(res);
} };
let textResponse = function (res, text) { let textResponse = function (res, text) {
res.statusCode = 200; res.statusCode = 200;
res.contentType = 'text/plain'; res.contentType = 'text/plain';
res.end(text); res.end(text);
} };
// 结束游戏 (POST /finish)
let endUserResponse = function(req, res) {
let json = '';
req.on('data', (data) => json += data);
req.on('end', function () {
let result = finishUser(json);
res.statusCode = 200;
res.end('ok');
})
};
let notFoundResponse = function(res) { let notFoundResponse = function(res) {
res.statusCode = 404; res.statusCode = 404;
res.end(); res.end();
} };
// 创建服务器 // 创建服务器
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
...@@ -364,11 +392,13 @@ const server = http.createServer((req, res) => { ...@@ -364,11 +392,13 @@ const server = http.createServer((req, res) => {
matchResponse(req, res); matchResponse(req, res);
else if (req.method === 'GET' && parsedUrl.pathname.startsWith('/stats')) else if (req.method === 'GET' && parsedUrl.pathname.startsWith('/stats'))
getTimeResponse(parsedUrl, res); getTimeResponse(parsedUrl, res);
else if (req.method === 'POST' && parsedUrl.pathname.startsWith('/finish'))
endUserResponse(req, res);
else else
notFoundResponse(res); notFoundResponse(res);
}) });
server.timeout = 0 server.timeout = 0;
server.listen(1025); server.listen(1025);
setInterval(update, config.match.timeInterval); setInterval(update, config.match.timeInterval);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment