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
a5e4a6f2
Commit
a5e4a6f2
authored
Aug 24, 2018
by
不如归去
Committed by
GitHub
Aug 24, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2 from moecube/drop-reline
Drop reline
parents
350315b5
c461c49c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
13 deletions
+80
-13
configSample.json
configSample.json
+3
-1
main.js
main.js
+77
-12
No files found.
configSample.json
View file @
a5e4a6f2
...
@@ -16,6 +16,8 @@
...
@@ -16,6 +16,8 @@
"timeInterval"
:
10000
,
"timeInterval"
:
10000
,
"athleticTrueSkillGate"
:
0.4
,
"athleticTrueSkillGate"
:
0.4
,
"entertainExpGate"
:
100
,
"entertainExpGate"
:
100
,
"atheleticPtGate"
:
[
350
,
50
,
500
]
"atheleticPtGate"
:
[
350
,
50
,
500
],
"longestGameTime"
:
7200000
,
"reconnect"
:
"reconnect"
}
}
}
}
main.js
View file @
a5e4a6f2
...
@@ -10,6 +10,9 @@ const config = JSON.parse(fs.readFileSync("./config.json"));
...
@@ -10,6 +10,9 @@ 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
playingPlayerOpponents
=
new
Map
();
let
playingPlayerTimeout
=
new
Map
();
let
predictedEntertainTime
=
600
,
predictedAthleticTime
=
600
;
let
predictedEntertainTime
=
600
,
predictedAthleticTime
=
600
;
let
entertainRequestCountInTime
=
0
,
athleticRequestCountInTime
=
0
;
let
entertainRequestCountInTime
=
0
,
athleticRequestCountInTime
=
0
;
...
@@ -63,7 +66,7 @@ let setUserLimit = function(data) {
...
@@ -63,7 +66,7 @@ let setUserLimit = function(data) {
if
(
Array
.
isArray
(
config
.
match
.
atheleticPtGate
))
if
(
Array
.
isArray
(
config
.
match
.
atheleticPtGate
))
data
.
limit
=
config
.
match
.
atheleticPtGate
[
0
];
data
.
limit
=
config
.
match
.
atheleticPtGate
[
0
];
else
if
(
Number
.
isInteger
(
config
.
match
.
atheleticPtGate
))
else
if
(
Number
.
isInteger
(
config
.
match
.
atheleticPtGate
))
data
.
limit
=
config
.
match
.
atheleticPtGat
match
e
;
data
.
limit
=
config
.
match
.
atheleticPtGate
;
else
else
data
.
limit
=
500
;
data
.
limit
=
500
;
};
};
...
@@ -203,6 +206,8 @@ let pair = function (userARes, userBRes, serverName) {
...
@@ -203,6 +206,8 @@ let pair = function (userARes, userBRes, serverName) {
}
}
options_buffer
.
writeUInt8
(
checksum
&
0xFF
,
0
);
options_buffer
.
writeUInt8
(
checksum
&
0xFF
,
0
);
localLog
(
userARes
.
username
+
"
and
"
+
userBRes
.
username
+
"
matched on room
"
+
room_id
);
localLog
(
userARes
.
username
+
"
and
"
+
userBRes
.
username
+
"
matched on room
"
+
room_id
);
playingPlayerOpponents
.
set
(
userARes
.
username
,
userBRes
.
username
);
playingPlayerOpponents
.
set
(
userBRes
.
username
,
userARes
.
username
);
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
;
...
@@ -213,8 +218,10 @@ let pair = function (userARes, userBRes, serverName) {
...
@@ -213,8 +218,10 @@ let pair = function (userARes, userBRes, serverName) {
let
result
=
JSON
.
stringify
({
let
result
=
JSON
.
stringify
({
"
address
"
:
server
.
address
,
"
address
"
:
server
.
address
,
"
port
"
:
server
.
port
,
"
port
"
:
server
.
port
,
"
password
"
:
password
"
password
"
:
password
,
});
});
playingPlayerPool
.
set
(
client
.
username
,
result
);
playingPlayerTimeout
.
set
(
client
.
username
,
setTimeout
(
timeoutUser
,
config
.
match
.
longestMatchTime
,
client
.
username
));
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 +264,7 @@ let errorUser = function (res) {
...
@@ -257,7 +264,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 +281,32 @@ let closedUser = function (res, pool) {
...
@@ -274,7 +281,32 @@ let closedUser = function (res, pool) {
// 若用户未在匹配池中,挂黑名单
// 若用户未在匹配池中,挂黑名单
else
else
deadUserPool
.
push
(
res
);
deadUserPool
.
push
(
res
);
}
};
// 当 srvpro 通知本服务器游戏已正常结束时
let
finishUser
=
function
(
json
)
{
let
userA
=
json
.
usernameA
?
decodeURIComponent
(
json
.
usernameA
)
:
undefiend
;
let
userB
=
json
.
usernameB
?
decodeURIComponent
(
json
.
usernameB
)
:
undefined
;
if
(
!
userA
&&
!
userB
)
return
;
if
(
!
userA
&&
playingPlayerOpponents
.
has
(
userB
))
userA
=
playingPlayerOpponents
.
get
(
userB
);
if
(
!
userB
&&
playingPlayerOpponents
.
has
(
userA
))
userB
=
playingPlayerOpponents
.
get
(
userA
);
for
(
let
user
of
[
userA
,
userB
])
{
if
(
!
user
)
continue
;
if
(
!
playingPlayerPool
.
delete
(
user
))
localLog
(
"
Unknown player left the game:
"
+
user
);
clearTimeout
(
playingPlayerTimeout
.
get
(
user
));
playingPlayerTimeout
.
delete
(
user
);
}
localLog
(
"
Player
"
+
userA
+
"
and
"
+
userB
+
"
finished the game.
"
);
};
// 当超过时间,而 srvpro 从未通知基本服务器游戏已结束时
let
timeoutUser
=
function
(
user
)
{
if
(
playingPlayerPool
.
delete
(
user
))
localLog
(
"
With timeout, user is seen as had left the game:
"
+
user
);
playingPlayerOpponents
.
delete
(
user
);
playingPlayerTimeout
.
delete
(
user
);
};
// 计算预期时间
// 计算预期时间
let
calculatePredictedTime
=
function
()
{
let
calculatePredictedTime
=
function
()
{
...
@@ -304,11 +336,25 @@ let matchResponse = function(req, res) {
...
@@ -304,11 +336,25 @@ let matchResponse = function(req, res) {
if
(
!
username
||
!
password
)
{
if
(
!
username
||
!
password
)
{
throw
'
auth
'
;
throw
'
auth
'
;
}
}
res
.
username
=
username
;
res
.
password
=
password
;
// 检定是否掉线重连
if
(
playingPlayerPool
.
has
(
username
))
{
switch
(
config
.
match
.
reconnect
)
{
case
"
reconnect
"
:
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
application/json
'
,
'
Cache-Control
'
:
'
no-cache
'
});
res
.
end
(
playingPlayerPool
.
get
(
username
));
return
;
case
"
drop
"
:
rejectUser
(
res
);
return
;
default
:
break
;
// 什么都不做,继续加入匹配池。
}
}
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.
'
);
res
.
username
=
username
;
res
.
password
=
password
;
// 选择匹配池
// 选择匹配池
let
pool
=
null
;
let
pool
=
null
;
if
(
arg
.
arena
==
'
athletic
'
)
if
(
arg
.
arena
==
'
athletic
'
)
...
@@ -334,7 +380,7 @@ let matchResponse = function(req, res) {
...
@@ -334,7 +380,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 +390,35 @@ let getTimeResponse = function(parsedUrl, res) {
...
@@ -344,18 +390,35 @@ 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
form
=
''
;
req
.
on
(
'
data
'
,
(
data
)
=>
form
+=
data
);
req
.
on
(
'
end
'
,
function
()
{
let
json
=
{};
let
hashes
=
form
.
slice
(
form
.
indexOf
(
'
?
'
)
+
1
).
split
(
'
&
'
);
for
(
let
i
=
0
;
i
<
hashes
.
length
;
i
++
)
{
let
hash
=
hashes
[
i
].
split
(
'
=
'
);
json
[
hash
[
0
]]
=
hash
[
1
];
}
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 +427,13 @@ const server = http.createServer((req, res) => {
...
@@ -364,11 +427,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
);
...
...
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