Commit 2f5b6dc5 authored by Peter Xin's avatar Peter Xin

Time log

parent cb6fcb60
...@@ -11,50 +11,49 @@ let athleticUserPool = []; ...@@ -11,50 +11,49 @@ let athleticUserPool = [];
let entertainUserPool = []; let entertainUserPool = [];
let deadUserPool = []; let deadUserPool = [];
let getUserConfig = function(user, callback) { let localLog = function (content) {
console.log("[" + new Date().toLocaleString() + "] " + content)
}
let getUserConfig = function (user, callback) {
// HTTP GET 抓取数据。 // HTTP GET 抓取数据。
// 原 HTTP POST 抓取数据保留 // 原 HTTP POST 抓取数据保留
let address = config.arena.address; let address = config.arena.address;
// let ak = config.arena.ak; // let ak = config.arena.ak;
request.get(address + encodeURIComponent(user.username), function (err, res, body) { request.get(address + encodeURIComponent(user.username), function (err, res, body) {
if (err) if (err) {
{ localLog("failed to load user data for" + user.username + "for error" + err);
console.log ("failed to load user data for" + user.username + "for error" + err);
// Kick out // Kick out
errorUser(user); errorUser(user);
} }
else if (res.statusCode != 200) else if (res.statusCode != 200) {
{
try { try {
console.log("failed to load user data for " + user.username + " with code " + res.statusCode); localLog("failed to load user data for " + user.username + " with code " + res.statusCode);
console.log("response: " + JSON.stringify(res) + "\nBODY: " + body); localLog("response: " + JSON.stringify(res) + "\nBODY: " + body);
} }
catch(e) catch (e) {
{
} }
// Kick out // Kick out
errorUser(user); errorUser(user);
} }
else else {
{
try { try {
let value = JSON.parse(body); let value = JSON.parse(body);
callback(value); callback(value);
} }
catch(e) catch (e) {
{ localLog("failed to call back user " + user.username);
console.log("failed to call back user " + user.username); localLog(e);
console.log(e);
errorUser(user); errorUser(user);
} }
} }
}); });
/* /*
request.post(address, {form:{ ak, username: user.username, password: user.password }}, function (err, res, body) { request.post(address, {form:{ ak, username: user.username, password: user.password }}, function (err, res, body) {
callback(); callback();
}); });
*/ */
}; };
...@@ -69,7 +68,7 @@ let athleticTrueSkillMatchPoint = function (userA, userB) { ...@@ -69,7 +68,7 @@ let athleticTrueSkillMatchPoint = function (userA, userB) {
}; };
// 刷新竞技玩家池 // 刷新竞技玩家池
let updateAthleticMatch = function() { let updateAthleticMatch = function () {
let length = athleticUserPool.length; let length = athleticUserPool.length;
// 数量少于 2,什么都不做 // 数量少于 2,什么都不做
if (length < 2) return; if (length < 2) return;
...@@ -78,9 +77,13 @@ let updateAthleticMatch = function() { ...@@ -78,9 +77,13 @@ let updateAthleticMatch = function() {
for (let i = 0; i < length; i++) for (let i = 0; i < length; i++)
for (let j = 0; j < length; j++) for (let j = 0; j < length; j++)
if (i === j) if (i === j)
values[length * j + i] = { i, j, value: 0 }; values[length * j + i] = {i, j, value: 0};
else else
values[length * j + i] = { i, j, value: athleticTrueSkillMatchPoint(athleticUserPool[i].data, athleticUserPool[j].data) }; values[length * j + i] = {
i,
j,
value: athleticTrueSkillMatchPoint(athleticUserPool[i].data, athleticUserPool[j].data)
};
// 含参排序 // 含参排序
values.sort((a, b) => b.value - a.value); values.sort((a, b) => b.value - a.value);
// 生成 mask 表 // 生成 mask 表
...@@ -111,21 +114,18 @@ updateAthleticMatch = function () { ...@@ -111,21 +114,18 @@ updateAthleticMatch = function () {
if (length < 2) return; if (length < 2) return;
athleticUserPool.sort((a, b) => b.pt - a.pt); athleticUserPool.sort((a, b) => b.pt - a.pt);
let newPool = []; let newPool = [];
for (let i = 0; i < length; i++) for (let i = 0; i < length; i++) {
{
let userA = athleticUserPool[i]; let userA = athleticUserPool[i];
let userB = athleticUserPool[i + 1]; let userB = athleticUserPool[i + 1];
// 移出边界时的处理 // 移出边界时的处理
if (userA === undefined) if (userA === undefined)
break; break;
if (userB === undefined) if (userB === undefined) {
{
newPool.push(userA); newPool.push(userA);
break; break;
} }
// 若 exp 之差小于门限,则匹配房间 // 若 exp 之差小于门限,则匹配房间
if (userA.data.pt - userB.data.pt < config.match.atheleticPtGate) if (userA.data.pt - userB.data.pt < config.match.atheleticPtGate) {
{
pair(userA.client, userB.client, 'athletic'); pair(userA.client, userB.client, 'athletic');
i += 1; i += 1;
} }
...@@ -145,21 +145,18 @@ let updateEntertainMatch = function () { ...@@ -145,21 +145,18 @@ let updateEntertainMatch = function () {
// 从高到低进行贪心配对 // 从高到低进行贪心配对
let newPool = []; let newPool = [];
// TODO: 加入时间分界 // TODO: 加入时间分界
for (let i = 0; i < length; i++) for (let i = 0; i < length; i++) {
{
let userA = entertainUserPool[i]; let userA = entertainUserPool[i];
let userB = entertainUserPool[i + 1]; let userB = entertainUserPool[i + 1];
// 移出边界时的处理 // 移出边界时的处理
if (userA === undefined) if (userA === undefined)
break; break;
if (userB === undefined) if (userB === undefined) {
{
newPool.push(userA); newPool.push(userA);
break; break;
} }
// 若 exp 之差小于门限,则匹配房间 // 若 exp 之差小于门限,则匹配房间
if (userA.data.exp - userB.data.exp < config.match.entertainExpGate) if (userA.data.exp - userB.data.exp < config.match.entertainExpGate) {
{
pair(userA.client, userB.client, 'entertain'); pair(userA.client, userB.client, 'entertain');
i += 1; i += 1;
} }
...@@ -189,9 +186,8 @@ let pair = function (userARes, userBRes, serverName) { ...@@ -189,9 +186,8 @@ let pair = function (userARes, userBRes, serverName) {
checksum -= options_buffer.readUInt8(i) checksum -= options_buffer.readUInt8(i)
} }
options_buffer.writeUInt8(checksum & 0xFF, 0); options_buffer.writeUInt8(checksum & 0xFF, 0);
console.log(userARes.username + " and " + userBRes.username + " matched on room " + room_id); localLog(userARes.username + " and " + userBRes.username + " matched on room " + room_id);
for (let client of [userARes, userBRes]) for (let client of [userARes, userBRes]) {
{
let buffer = new Buffer(6); let buffer = new Buffer(6);
let secret = parseInt(client.password) % 65535 + 1; let secret = parseInt(client.password) % 65535 + 1;
for (let i = 0; i < options_buffer.length; i += 2) { for (let i = 0; i < options_buffer.length; i += 2) {
...@@ -213,18 +209,15 @@ let joinPool = function (res, data, pool) { ...@@ -213,18 +209,15 @@ let joinPool = function (res, data, pool) {
// 辣鸡性能,先迁就前面的 TrueSKill 算法 // 辣鸡性能,先迁就前面的 TrueSKill 算法
// 检查用户是否已被挂黑名单 // 检查用户是否已被挂黑名单
let index = deadUserPool.indexOf(res); let index = deadUserPool.indexOf(res);
if (index > 0) if (index > 0) {
{ localLog(res.username + " has closed the connection. Reject joining the pool.")
console.log(res.username + " has closed the connection. Reject joining the pool.")
deadUserPool.splice(index, 1); deadUserPool.splice(index, 1);
return; return;
} }
// 检查用户是否已在匹配池中 // 检查用户是否已在匹配池中
for(let i = 0; i < pool.length; i++) for (let i = 0; i < pool.length; i++) {
{
let user = pool[i]; let user = pool[i];
if (user.client.username === res.username) if (user.client.username === res.username) {
{
rejectUser(user.client); rejectUser(user.client);
pool.splice(i, 1); pool.splice(i, 1);
i -= 1; i -= 1;
...@@ -237,15 +230,15 @@ let joinPool = function (res, data, pool) { ...@@ -237,15 +230,15 @@ let joinPool = function (res, data, pool) {
}; };
// 当用户双开时,回绝之 // 当用户双开时,回绝之
let rejectUser = function(res) { let rejectUser = function (res) {
console.log(res.username + " is kicked for over 1 client requested."); localLog(res.username + " is kicked for over 1 client requested.");
res.statusCode = 409; res.statusCode = 409;
res.end(); res.end();
}; };
// 当没有正确收到消息时, // 当没有正确收到消息时,
let errorUser = function(res) { let errorUser = function (res) {
console.log(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();
} }
...@@ -254,7 +247,7 @@ let errorUser = function(res) { ...@@ -254,7 +247,7 @@ let errorUser = function(res) {
let closedUser = function (res, pool) { let closedUser = function (res, pool) {
let index = -1; let index = -1;
// 查询用户是否已在匹配池中 // 查询用户是否已在匹配池中
for(let i = 0; i < pool.length; i++) for (let i = 0; i < pool.length; i++)
if (pool[i].client == res) if (pool[i].client == res)
index = i; index = i;
// 若用户已在匹配池中,移除 // 若用户已在匹配池中,移除
...@@ -269,8 +262,7 @@ let closedUser = function (res, pool) { ...@@ -269,8 +262,7 @@ let closedUser = function (res, pool) {
// 创建服务器 // 创建服务器
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
try try {
{
// 读取数据 // 读取数据
let credentials = new Buffer(req.headers['authorization'].split(' ')[1], 'base64').toString().split(':'); let credentials = new Buffer(req.headers['authorization'].split(' ')[1], 'base64').toString().split(':');
let username = credentials[0]; let username = credentials[0];
...@@ -280,7 +272,7 @@ const server = http.createServer((req, res) => { ...@@ -280,7 +272,7 @@ const server = http.createServer((req, res) => {
} }
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';
console.log(username + ' apply for a ' + arg.arena + ' match.'); localLog(username + ' apply for a ' + arg.arena + ' match.');
res.username = username; res.username = username;
res.password = password; res.password = password;
// 选择匹配池 // 选择匹配池
...@@ -290,7 +282,9 @@ const server = http.createServer((req, res) => { ...@@ -290,7 +282,9 @@ const server = http.createServer((req, res) => {
else else
pool = entertainUserPool; pool = entertainUserPool;
// 如果连接断开了,把它从匹配池中移除 // 如果连接断开了,把它从匹配池中移除
res.on('close', () => { closedUser(res, pool); }); res.on('close', () => {
closedUser(res, pool);
});
// 送读取数据 // 送读取数据
// 如果收到了奇怪的数据,一概认为是娱乐对局 // 如果收到了奇怪的数据,一概认为是娱乐对局
getUserConfig(res, (ans) => { getUserConfig(res, (ans) => {
...@@ -298,9 +292,8 @@ const server = http.createServer((req, res) => { ...@@ -298,9 +292,8 @@ const server = http.createServer((req, res) => {
}); });
} }
catch (error) catch (error) {
{ localLog(error);
console.log(error);
res.statusCode = 500; res.statusCode = 500;
res.end(); res.end();
return; return;
......
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