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
74b28df3
Commit
74b28df3
authored
Sep 15, 2016
by
Peter Xin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added arena param & fix
parent
2f31c6e8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
19 deletions
+48
-19
main.js
main.js
+48
-19
No files found.
main.js
View file @
74b28df3
...
@@ -4,6 +4,7 @@ const request = require('request');
...
@@ -4,6 +4,7 @@ const request = require('request');
const
http
=
require
(
'
http
'
);
const
http
=
require
(
'
http
'
);
const
crypto
=
require
(
'
crypto
'
);
const
crypto
=
require
(
'
crypto
'
);
const
fs
=
require
(
'
fs
'
);
const
fs
=
require
(
'
fs
'
);
const
url
=
require
(
'
url
'
);
const
config
=
JSON
.
parse
(
fs
.
readFileSync
(
"
./config.json
"
));
const
config
=
JSON
.
parse
(
fs
.
readFileSync
(
"
./config.json
"
));
let
athleticUserPool
=
[];
let
athleticUserPool
=
[];
...
@@ -17,7 +18,7 @@ let getUserConfig = function(user, callback) {
...
@@ -17,7 +18,7 @@ let getUserConfig = function(user, callback) {
request
.
get
(
address
+
user
.
username
,
function
(
err
,
res
,
body
)
{
request
.
get
(
address
+
user
.
username
,
function
(
err
,
res
,
body
)
{
if
(
res
.
statusCode
!=
200
)
if
(
res
.
statusCode
!=
200
)
{
{
console
.
log
(
"
failed to load user data for
#{user}
"
);
console
.
log
(
"
failed to load user data for
"
+
user
+
"
with code
"
+
res
.
statusCode
);
}
}
else
else
{
{
...
@@ -34,7 +35,6 @@ let getUserConfig = function(user, callback) {
...
@@ -34,7 +35,6 @@ let getUserConfig = function(user, callback) {
// TrueSkill
// TrueSkill
// 参考于 https://zh.wikipedia.org/zh-hans/TrueSkill评分系统
// 参考于 https://zh.wikipedia.org/zh-hans/TrueSkill评分系统
let
athleticTrueSkillMatchPoint
=
function
(
userA
,
userB
)
{
let
athleticTrueSkillMatchPoint
=
function
(
userA
,
userB
)
{
let
trueSkillA
=
userA
.
trueskill
;
let
trueSkillA
=
userA
.
trueskill
;
let
trueSkillB
=
userB
.
trueskill
;
let
trueSkillB
=
userB
.
trueskill
;
...
@@ -69,8 +69,8 @@ let updateAthleticMatch = function() {
...
@@ -69,8 +69,8 @@ let updateAthleticMatch = function() {
if
(
masks
[
value
.
i
]
||
masks
[
value
.
j
])
if
(
masks
[
value
.
i
]
||
masks
[
value
.
j
])
continue
;
continue
;
pair
(
athleticUserPool
[
value
.
i
].
client
,
athleticUserPool
[
value
.
j
].
client
);
pair
(
athleticUserPool
[
value
.
i
].
client
,
athleticUserPool
[
value
.
j
].
client
);
masks
[
i
]
=
true
;
masks
[
value
.
i
]
=
true
;
masks
[
j
]
=
true
;
masks
[
value
.
j
]
=
true
;
}
}
// 移除用户
// 移除用户
let
newPool
=
[];
let
newPool
=
[];
...
@@ -131,6 +131,7 @@ let pair = function (userARes, userBRes) {
...
@@ -131,6 +131,7 @@ let pair = function (userARes, userBRes) {
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
);
for
(
let
client
of
[
userARes
,
userBRes
])
for
(
let
client
of
[
userARes
,
userBRes
])
{
{
let
buffer
=
new
Buffer
(
6
);
let
buffer
=
new
Buffer
(
6
);
...
@@ -144,12 +145,38 @@ let pair = function (userARes, userBRes) {
...
@@ -144,12 +145,38 @@ let pair = function (userARes, userBRes) {
"
port
"
:
server
.
port
,
"
port
"
:
server
.
port
,
"
password
"
:
password
"
password
"
:
password
});
});
console
.
log
(
userARes
.
username
+
"
and
"
+
userBRes
.
username
+
"
matched on room
"
+
room_id
);
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
);
}
}
};
};
// 将用户加入待回池
let
joinPool
=
function
(
res
,
data
,
pool
)
{
// 辣鸡性能,先迁就前面的 TrueSKill 算法
for
(
let
i
=
0
;
i
<
pool
.
length
;
i
++
)
{
let
user
=
pool
[
i
];
if
(
user
.
client
.
username
===
res
.
username
)
{
rejectUser
(
user
.
client
);
// 脏
pool
.
splice
(
i
,
1
);
i
-=
1
;
}
}
pool
.
push
({
client
:
res
,
data
:
data
});
};
// 当用户双开时,回绝之
let
rejectUser
=
function
(
res
)
{
console
.
log
(
res
.
username
+
"
is kicked for over 1 client requested.
"
);
res
.
statusCode
=
409
;
res
.
end
();
};
// 创建服务器
// 创建服务器
http
.
createServer
((
req
,
res
)
=>
{
http
.
createServer
((
req
,
res
)
=>
{
try
try
...
@@ -161,28 +188,30 @@ http.createServer((req, res) => {
...
@@ -161,28 +188,30 @@ http.createServer((req, res) => {
if
(
!
username
||
!
password
)
{
if
(
!
username
||
!
password
)
{
throw
'
auth
'
;
throw
'
auth
'
;
}
}
console
.
log
(
username
+
'
apply for a match.
'
);
let
arg
=
url
.
parse
(
req
.
url
,
true
).
query
;
if
(
!
arg
.
arena
)
arg
.
arena
=
'
entertain
'
;
console
.
log
(
username
+
'
apply for a
'
+
arg
.
arena
+
'
match.
'
);
res
.
username
=
username
;
res
.
username
=
username
;
res
.
password
=
password
;
res
.
password
=
password
;
// 送读取数据
// 送读取数据
getUserConfig
(
res
,
(
ans
)
=>
{
// 如果收到了奇怪的数据,一概认为是娱乐对局
athleticUserPool
.
push
({
client
:
res
,
data
:
ans
});
if
(
arg
.
arena
==
'
athletic
'
)
});
getUserConfig
(
res
,
(
ans
)
=>
{
joinPool
(
res
,
ans
,
athleticUserPool
);
});
else
getUserConfig
(
res
,
(
ans
)
=>
{
joinPool
(
res
,
ans
,
entertainUserPool
);
});
}
}
catch
(
error
)
catch
(
error
)
{
{
res
.
statusCode
=
403
;
console
.
log
(
error
);
res
.
statusCode
=
500
;
res
.
end
();
res
.
end
();
return
;
return
;
}
}
});
}).
listen
(
1025
);
setInterval
(
update
,
config
.
match
.
timeInterval
);
getUserConfig
({
username
:
"
userA
"
},
(
ans
)
=>
{
setInterval
(
update
,
config
.
match
.
timeInterval
);
entertainUserPool
.
push
({
client
:
{},
data
:
ans
});
\ No newline at end of file
});
getUserConfig
({
username
:
"
userB
"
},
(
ans
)
=>
{
entertainUserPool
.
push
({
client
:
{},
data
:
ans
});
});
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