Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
1
Issues
1
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
hex
ygopro2
Commits
c7d0c79b
Commit
c7d0c79b
authored
Feb 10, 2026
by
hex
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(mycard): harden request flow and connection resilience
parent
d6536a39
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
369 additions
and
127 deletions
+369
-127
Assets/SibylSystem/MyCard/MyCard.cs
Assets/SibylSystem/MyCard/MyCard.cs
+108
-54
Assets/SibylSystem/MyCard/MyCardHelper.cs
Assets/SibylSystem/MyCard/MyCardHelper.cs
+261
-73
No files found.
Assets/SibylSystem/MyCard/MyCard.cs
View file @
c7d0c79b
using
UnityEngine
;
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Threading
;
public
class
MyCard
:
WindowServantSP
{
public
bool
isMatching
=
false
;
public
bool
isRequesting
=
false
;
//const string mycardTiramisuAddress = "tiramisu.mycard.moe";
//const string mycardTiramisuAthleticPort = "8911";
//const string mycardTiramisuEntertainPort = "7911";
private
Coroutine
requestCoroutine
=
null
;
private
Coroutine
requestCoroutine
=
null
;
private
Thread
joinThread
=
null
;
MyCardHelper
mycardHelper
;
UIInput
inputUsername
;
UIInput
inputPsw
;
...
...
@@ -48,10 +43,19 @@ public class MyCard : WindowServantSP
public
void
TerminateRequest
()
{
if
(
mycardHelper
!=
null
)
{
mycardHelper
.
CancelCurrentRequest
();
}
if
(
requestCoroutine
!=
null
)
{
Program
.
I
().
StopCoroutine
(
requestCoroutine
);
requestCoroutine
=
null
;
}
if
(
isRequesting
)
{
isRequesting
=
false
;
Program
.
PrintToChat
(
InterString
.
Get
(
"匹配已中断。"
));
}
...
...
@@ -80,56 +84,102 @@ public class MyCard : WindowServantSP
Application
.
OpenURL
(
"https://ygobbs.com/"
);
}
IEnumerator
MatchCoroutine
(
string
username
,
string
password
,
string
matchType
)
{
isRequesting
=
true
;
Program
.
PrintToChat
(
InterString
.
Get
(
"正在登录至 MyCard。"
));
bool
loginSuccess
=
false
;
string
failReason
=
""
;
// 启动并等待登录协程完成
yield
return
Program
.
I
().
StartCoroutine
(
mycardHelper
.
Login
(
username
,
password
,
(
success
,
reason
)
=>
{
loginSuccess
=
success
;
failReason
=
reason
;
}));
if
(!
loginSuccess
)
{
Program
.
PrintToChat
(
InterString
.
Get
(
"MyCard 登录失败。原因: "
)
+
failReason
);
isRequesting
=
false
;
yield
break
;
// 结束协程
bool
TryStartJoinThread
(
MatchResultObject
matchResultObject
)
{
if
(
matchResultObject
==
null
)
{
return
false
;
}
Program
.
PrintToChat
(
InterString
.
Get
(
"MyCard 登录成功,用户名: "
)
+
mycardHelper
.
username
);
// Program.PrintToChat(InterString.Get("正在获取匹配秘钥。"));
yield
return
Program
.
I
().
StartCoroutine
(
mycardHelper
.
GetUserU16Secret
((
success
,
reason
)
=>
if
(
joinThread
!=
null
&&
joinThread
.
IsAlive
)
{
loginSuccess
=
success
;
failReason
=
reason
;
}));
if
(!
loginSuccess
)
return
false
;
}
string
address
=
matchResultObject
.
address
;
string
userName
=
mycardHelper
.
username
;
string
port
=
matchResultObject
.
port
.
ToString
();
string
roomPassword
=
matchResultObject
.
password
;
string
version
=
"0x"
+
string
.
Format
(
"{0:X}"
,
Config
.
ClientVersion
);
joinThread
=
new
Thread
(()
=>
{
Program
.
PrintToChat
(
InterString
.
Get
(
"获取用户密钥失败。请重新登录。原因: "
)
+
failReason
);
isRequesting
=
false
;
yield
break
;
// 结束协程
TcpHelper
.
join
(
address
,
userName
,
port
,
roomPassword
,
version
);
});
joinThread
.
Name
=
"MyCardJoinThread"
;
joinThread
.
IsBackground
=
true
;
joinThread
.
Start
();
return
true
;
}
IEnumerator
MatchCoroutine
(
string
username
,
string
password
,
string
matchType
)
{
isRequesting
=
true
;
try
{
Program
.
PrintToChat
(
InterString
.
Get
(
"正在登录至 MyCard。"
));
bool
loginSuccess
=
false
;
string
failReason
=
""
;
yield
return
Program
.
I
().
StartCoroutine
(
mycardHelper
.
Login
(
username
,
password
,
(
success
,
reason
)
=>
{
loginSuccess
=
success
;
failReason
=
reason
;
}));
if
(!
loginSuccess
)
{
Program
.
PrintToChat
(
InterString
.
Get
(
"MyCard 登录失败。原因: "
)
+
failReason
);
yield
break
;
}
Program
.
PrintToChat
(
InterString
.
Get
(
"MyCard 登录成功,用户名: "
)
+
mycardHelper
.
username
);
yield
return
Program
.
I
().
StartCoroutine
(
mycardHelper
.
GetUserU16Secret
((
success
,
reason
)
=>
{
loginSuccess
=
success
;
failReason
=
reason
;
}));
if
(!
loginSuccess
)
{
Program
.
PrintToChat
(
InterString
.
Get
(
"获取用户密钥失败。请重新登录。原因: "
)
+
failReason
);
yield
break
;
}
Program
.
PrintToChat
(
InterString
.
Get
(
"正在请求匹配。匹配类型: "
)
+
matchType
);
MatchResultObject
matchResultObject
=
null
;
yield
return
Program
.
I
().
StartCoroutine
(
mycardHelper
.
RequestMatch
(
matchType
,
(
result
,
reason
)
=>
{
matchResultObject
=
result
;
failReason
=
reason
;
}));
if
(
matchResultObject
==
null
)
{
Program
.
PrintToChat
(
InterString
.
Get
(
"匹配请求失败。原因: "
)
+
failReason
);
yield
break
;
}
if
(!
TryStartJoinThread
(
matchResultObject
))
{
Program
.
PrintToChat
(
InterString
.
Get
(
"连接任务已存在,请稍后再试。"
));
yield
break
;
}
Program
.
PrintToChat
(
InterString
.
Get
(
"匹配成功。正在进入房间。"
));
isMatching
=
true
;
}
// Program.PrintToChat(InterString.Get("获取匹配秘钥成功。"));
Program
.
PrintToChat
(
InterString
.
Get
(
"正在请求匹配。匹配类型: "
)
+
matchType
);
MatchResultObject
matchResultObject
=
null
;
// 启动并等待匹配协程完成
yield
return
Program
.
I
().
StartCoroutine
(
mycardHelper
.
RequestMatch
(
matchType
,
(
result
,
reason
)
=>
{
matchResultObject
=
result
;
failReason
=
reason
;
}));
if
(
matchResultObject
==
null
)
{
Program
.
PrintToChat
(
InterString
.
Get
(
"匹配请求失败。原因: "
)
+
failReason
);
finally
{
isRequesting
=
false
;
yield
break
;
requestCoroutine
=
null
;
}
Program
.
PrintToChat
(
InterString
.
Get
(
"匹配成功。正在进入房间。"
));
this
.
isMatching
=
true
;
// TcpHelper.join 内部自己创建并管理线程,这里直接调用是安全的
// 只要 TcpHelper.join 本身及其后续操作不调用 Unity API
(
new
Thread
(()
=>
{
TcpHelper
.
join
(
matchResultObject
.
address
,
mycardHelper
.
username
,
matchResultObject
.
port
.
ToString
(),
matchResultObject
.
password
,
"0x"
+
String
.
Format
(
"{0:X}"
,
Config
.
ClientVersion
));
})).
Start
();
isRequesting
=
false
;
requestCoroutine
=
null
;
}
void
StartMatch
(
string
matchType
)
{
string
username
=
inputUsername
.
value
;
...
...
@@ -143,16 +193,20 @@ public class MyCard : WindowServantSP
{
TerminateRequest
();
}
SaveUser
();
Program
.
PrintToChat
(
InterString
.
Get
(
"已开始匹配。"
));
// 启动协程,而不是线程
mycardHelper
.
ResetCancellation
();
requestCoroutine
=
Program
.
I
().
StartCoroutine
(
MatchCoroutine
(
username
,
password
,
matchType
));
}
void
onClickJoinAthletic
()
{
void
onClickJoinAthletic
()
{
StartMatch
(
"athletic"
);
}
void
onClickJoinEntertain
()
{
void
onClickJoinEntertain
()
{
StartMatch
(
"entertain"
);
}
}
Assets/SibylSystem/MyCard/MyCardHelper.cs
View file @
c7d0c79b
This diff is collapsed.
Click to expand it.
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