Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro-match
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
ygopro-match
Commits
95789df3
Commit
95789df3
authored
Dec 04, 2016
by
Peter Xin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FEATURE: closed client removed from pool.
parent
043f0ca9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
9 deletions
+39
-9
main.js
main.js
+39
-9
No files found.
main.js
View file @
95789df3
...
@@ -9,6 +9,7 @@ const url = require('url');
...
@@ -9,6 +9,7 @@ const url = require('url');
const
config
=
JSON
.
parse
(
fs
.
readFileSync
(
"
./config.json
"
));
const
config
=
JSON
.
parse
(
fs
.
readFileSync
(
"
./config.json
"
));
let
athleticUserPool
=
[];
let
athleticUserPool
=
[];
let
entertainUserPool
=
[];
let
entertainUserPool
=
[];
let
deadUserPool
=
[];
let
getUserConfig
=
function
(
user
,
callback
)
{
let
getUserConfig
=
function
(
user
,
callback
)
{
// HTTP GET 抓取数据。
// HTTP GET 抓取数据。
...
@@ -208,13 +209,21 @@ let pair = function (userARes, userBRes) {
...
@@ -208,13 +209,21 @@ let pair = function (userARes, userBRes) {
// 将用户加入待回池
// 将用户加入待回池
let
joinPool
=
function
(
res
,
data
,
pool
)
{
let
joinPool
=
function
(
res
,
data
,
pool
)
{
// 辣鸡性能,先迁就前面的 TrueSKill 算法
// 辣鸡性能,先迁就前面的 TrueSKill 算法
// 检查用户是否已被挂黑名单
let
index
=
deadUserPool
.
indexOf
(
res
);
if
(
index
>
0
)
{
console
.
log
(
res
.
username
+
"
has closed the connection. Reject joining the pool.
"
)
deadUserPool
.
splice
(
index
,
1
);
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
;
}
}
...
@@ -239,6 +248,23 @@ let errorUser = function(res) {
...
@@ -239,6 +248,23 @@ let errorUser = function(res) {
res
.
end
();
res
.
end
();
}
}
// 当用户断开连接时
let
closedUser
=
function
(
res
,
pool
)
{
let
index
=
-
1
;
// 查询用户是否已在匹配池中
for
(
let
i
=
0
;
i
<
pool
.
length
;
i
++
)
if
(
pool
[
i
].
client
==
res
)
index
=
i
;
// 若用户已在匹配池中,移除
if
(
index
>=
0
)
{
console
.
log
(
res
.
username
+
"
has closed the connection. Removed from the pool.
"
);
pool
.
splice
(
index
,
1
);
}
// 若用户未在匹配池中,挂黑名单
else
deadUserPool
.
push
(
res
);
}
// 创建服务器
// 创建服务器
http
.
createServer
((
req
,
res
)
=>
{
http
.
createServer
((
req
,
res
)
=>
{
try
try
...
@@ -255,16 +281,20 @@ http.createServer((req, res) => {
...
@@ -255,16 +281,20 @@ http.createServer((req, res) => {
console
.
log
(
username
+
'
apply for a
'
+
arg
.
arena
+
'
match.
'
);
console
.
log
(
username
+
'
apply for a
'
+
arg
.
arena
+
'
match.
'
);
res
.
username
=
username
;
res
.
username
=
username
;
res
.
password
=
password
;
res
.
password
=
password
;
//
送读取数据
//
选择匹配池
// 如果收到了奇怪的数据,一概认为是娱乐对局
let
pool
=
null
;
if
(
arg
.
arena
==
'
athletic
'
)
if
(
arg
.
arena
==
'
athletic
'
)
getUserConfig
(
res
,
(
ans
)
=>
{
pool
=
athleticUserPool
;
joinPool
(
res
,
ans
,
athleticUserPool
);
});
else
else
getUserConfig
(
res
,
(
ans
)
=>
{
pool
=
entertainUserPool
;
joinPool
(
res
,
ans
,
entertainUserPool
);
// 如果连接断开了,把它从匹配池中移除
});
res
.
on
(
'
close
'
,
()
=>
{
closedUser
(
res
,
pool
);
});
// 送读取数据
// 如果收到了奇怪的数据,一概认为是娱乐对局
getUserConfig
(
res
,
(
ans
)
=>
{
joinPool
(
res
,
ans
,
pool
);
});
}
}
catch
(
error
)
catch
(
error
)
{
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment