Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
Neos
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
2
Merge Requests
2
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
Neos
Commits
5e3a7019
Commit
5e3a7019
authored
Jul 11, 2024
by
Chunchi Che
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor ai predict
parent
e0e5c7a6
Pipeline
#28402
passed with stages
in 9 minutes and 56 seconds
Changes
8
Pipelines
1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
940 additions
and
843 deletions
+940
-843
src/api/ygoAgent/index.ts
src/api/ygoAgent/index.ts
+1
-0
src/api/ygoAgent/schema.ts
src/api/ygoAgent/schema.ts
+32
-701
src/api/ygoAgent/transaction.ts
src/api/ygoAgent/transaction.ts
+778
-0
src/common.ts
src/common.ts
+62
-55
src/service/duel/agent.ts
src/service/duel/agent.ts
+20
-86
src/service/duel/util.ts
src/service/duel/util.ts
+45
-0
src/stores/matStore/types.ts
src/stores/matStore/types.ts
+1
-0
src/ui/Duel/Message/NeosModal/index.tsx
src/ui/Duel/Message/NeosModal/index.tsx
+1
-1
No files found.
src/api/ygoAgent/index.ts
View file @
5e3a7019
export
*
from
"
./create
"
;
export
*
from
"
./create
"
;
export
*
from
"
./delete
"
;
export
*
from
"
./delete
"
;
export
*
from
"
./predict
"
;
export
*
from
"
./predict
"
;
export
*
from
"
./transaction
"
;
src/api/ygoAgent/schema.ts
View file @
5e3a7019
This diff is collapsed.
Click to expand it.
src/api/ygoAgent/transaction.ts
0 → 100644
View file @
5e3a7019
This diff is collapsed.
Click to expand it.
src/common.ts
View file @
5e3a7019
...
@@ -3,31 +3,38 @@ import PhaseType = ygopro.StocGameMessage.MsgNewPhase.PhaseType;
...
@@ -3,31 +3,38 @@ import PhaseType = ygopro.StocGameMessage.MsgNewPhase.PhaseType;
import
{
CardMeta
}
from
"
@/api
"
;
import
{
CardMeta
}
from
"
@/api
"
;
//! 一些Neos中基础的数据结构
//! 一些Neos中基础的数据结构
// Position
export
const
FACEUP_ATTACK
=
0x1
;
export
const
FACEDOWN_ATTACK
=
0x2
;
export
const
FACEUP_DEFENSE
=
0x4
;
export
const
FACEDOWN_DEFENSE
=
0x8
;
// 类型
// 类型
const
TYPE_MONSTER
=
0x1
;
//
export
const
TYPE_MONSTER
=
0x1
;
//
const
TYPE_SPELL
=
0x2
;
//
export
const
TYPE_SPELL
=
0x2
;
//
const
TYPE_TRAP
=
0x4
;
//
export
const
TYPE_TRAP
=
0x4
;
//
const
TYPE_NORMAL
=
0x10
;
//
export
const
TYPE_NORMAL
=
0x10
;
//
const
TYPE_EFFECT
=
0x20
;
//
export
const
TYPE_EFFECT
=
0x20
;
//
const
TYPE_FUSION
=
0x40
;
//
export
const
TYPE_FUSION
=
0x40
;
//
const
TYPE_RITUAL
=
0x80
;
//
export
const
TYPE_RITUAL
=
0x80
;
//
const
TYPE_TRAPMONSTER
=
0x100
;
//
export
const
TYPE_TRAPMONSTER
=
0x100
;
//
const
TYPE_SPIRIT
=
0x200
;
//
export
const
TYPE_SPIRIT
=
0x200
;
//
const
TYPE_UNION
=
0x400
;
//
export
const
TYPE_UNION
=
0x400
;
//
const
TYPE_DUAL
=
0x800
;
//
export
const
TYPE_DUAL
=
0x800
;
//
const
TYPE_TUNER
=
0x1000
;
//
export
const
TYPE_TUNER
=
0x1000
;
//
const
TYPE_SYNCHRO
=
0x2000
;
//
export
const
TYPE_SYNCHRO
=
0x2000
;
//
export
const
TYPE_TOKEN
=
0x4000
;
//
export
const
TYPE_TOKEN
=
0x4000
;
//
const
TYPE_QUICKPLAY
=
0x10000
;
//
export
const
TYPE_QUICKPLAY
=
0x10000
;
//
const
TYPE_CONTINUOUS
=
0x20000
;
//
export
const
TYPE_CONTINUOUS
=
0x20000
;
//
const
TYPE_EQUIP
=
0x40000
;
//
export
const
TYPE_EQUIP
=
0x40000
;
//
const
TYPE_FIELD
=
0x80000
;
//
export
const
TYPE_FIELD
=
0x80000
;
//
const
TYPE_COUNTER
=
0x100000
;
//
export
const
TYPE_COUNTER
=
0x100000
;
//
const
TYPE_FLIP
=
0x200000
;
//
export
const
TYPE_FLIP
=
0x200000
;
//
const
TYPE_TOON
=
0x400000
;
//
export
const
TYPE_TOON
=
0x400000
;
//
const
TYPE_XYZ
=
0x800000
;
//
export
const
TYPE_XYZ
=
0x800000
;
//
const
TYPE_PENDULUM
=
0x1000000
;
//
export
const
TYPE_PENDULUM
=
0x1000000
;
//
const
TYPE_SPSUMMON
=
0x2000000
;
//
export
const
TYPE_SPSUMMON
=
0x2000000
;
//
export
const
TYPE_LINK
=
0x4000000
;
//
export
const
TYPE_LINK
=
0x4000000
;
//
/*
/*
...
@@ -147,13 +154,13 @@ export function isPendulumMonster(typeCode: number): boolean {
...
@@ -147,13 +154,13 @@ export function isPendulumMonster(typeCode: number): boolean {
// 属性
// 属性
// const ATTRIBUTE_ALL = 0x7f; //
// const ATTRIBUTE_ALL = 0x7f; //
const
ATTRIBUTE_EARTH
=
0x01
;
//
export
const
ATTRIBUTE_EARTH
=
0x01
;
//
const
ATTRIBUTE_WATER
=
0x02
;
//
export
const
ATTRIBUTE_WATER
=
0x02
;
//
const
ATTRIBUTE_FIRE
=
0x04
;
//
export
const
ATTRIBUTE_FIRE
=
0x04
;
//
const
ATTRIBUTE_WIND
=
0x08
;
//
export
const
ATTRIBUTE_WIND
=
0x08
;
//
const
ATTRIBUTE_LIGHT
=
0x10
;
//
export
const
ATTRIBUTE_LIGHT
=
0x10
;
//
const
ATTRIBUTE_DARK
=
0x20
;
//
export
const
ATTRIBUTE_DARK
=
0x20
;
//
const
ATTRIBUTE_DEVINE
=
0x40
;
//
export
const
ATTRIBUTE_DEVINE
=
0x40
;
//
export
const
Attribute2StringCodeMap
:
Map
<
number
,
number
>
=
new
Map
([
export
const
Attribute2StringCodeMap
:
Map
<
number
,
number
>
=
new
Map
([
[
ATTRIBUTE_EARTH
,
1010
],
[
ATTRIBUTE_EARTH
,
1010
],
...
@@ -166,31 +173,31 @@ export const Attribute2StringCodeMap: Map<number, number> = new Map([
...
@@ -166,31 +173,31 @@ export const Attribute2StringCodeMap: Map<number, number> = new Map([
]);
]);
// 种族
// 种族
const
RACE_WARRIOR
=
0x1
;
//
export
const
RACE_WARRIOR
=
0x1
;
//
const
RACE_SPELLCASTER
=
0x2
;
//
export
const
RACE_SPELLCASTER
=
0x2
;
//
const
RACE_FAIRY
=
0x4
;
//
export
const
RACE_FAIRY
=
0x4
;
//
const
RACE_FIEND
=
0x8
;
//
export
const
RACE_FIEND
=
0x8
;
//
const
RACE_ZOMBIE
=
0x10
;
//
export
const
RACE_ZOMBIE
=
0x10
;
//
const
RACE_MACHINE
=
0x20
;
//
export
const
RACE_MACHINE
=
0x20
;
//
const
RACE_AQUA
=
0x40
;
//
export
const
RACE_AQUA
=
0x40
;
//
const
RACE_PYRO
=
0x80
;
//
export
const
RACE_PYRO
=
0x80
;
//
const
RACE_ROCK
=
0x100
;
//
export
const
RACE_ROCK
=
0x100
;
//
const
RACE_WINDBEAST
=
0x200
;
//
export
const
RACE_WINDBEAST
=
0x200
;
//
const
RACE_PLANT
=
0x400
;
//
export
const
RACE_PLANT
=
0x400
;
//
const
RACE_INSECT
=
0x800
;
//
export
const
RACE_INSECT
=
0x800
;
//
const
RACE_THUNDER
=
0x1000
;
//
export
const
RACE_THUNDER
=
0x1000
;
//
const
RACE_DRAGON
=
0x2000
;
//
export
const
RACE_DRAGON
=
0x2000
;
//
const
RACE_BEAST
=
0x4000
;
//
export
const
RACE_BEAST
=
0x4000
;
//
const
RACE_BEASTWARRIOR
=
0x8000
;
//
export
const
RACE_BEASTWARRIOR
=
0x8000
;
//
const
RACE_DINOSAUR
=
0x10000
;
//
export
const
RACE_DINOSAUR
=
0x10000
;
//
const
RACE_FISH
=
0x20000
;
//
export
const
RACE_FISH
=
0x20000
;
//
const
RACE_SEASERPENT
=
0x40000
;
//
export
const
RACE_SEASERPENT
=
0x40000
;
//
const
RACE_REPTILE
=
0x80000
;
//
export
const
RACE_REPTILE
=
0x80000
;
//
const
RACE_PSYCHO
=
0x100000
;
//
export
const
RACE_PSYCHO
=
0x100000
;
//
const
RACE_DEVINE
=
0x200000
;
//
export
const
RACE_DEVINE
=
0x200000
;
//
const
RACE_CREATORGOD
=
0x400000
;
//
export
const
RACE_CREATORGOD
=
0x400000
;
//
const
RACE_WYRM
=
0x800000
;
//
export
const
RACE_WYRM
=
0x800000
;
//
const
RACE_CYBERSE
=
0x1000000
;
//
export
const
RACE_CYBERSE
=
0x1000000
;
//
export
const
Race2StringCodeMap
:
Map
<
number
,
number
>
=
new
Map
([
export
const
Race2StringCodeMap
:
Map
<
number
,
number
>
=
new
Map
([
[
RACE_WARRIOR
,
1020
],
[
RACE_WARRIOR
,
1020
],
...
...
src/service/duel/agent.ts
View file @
5e3a7019
...
@@ -12,61 +12,32 @@ import {
...
@@ -12,61 +12,32 @@ import {
}
from
"
@/api
"
;
}
from
"
@/api
"
;
import
{
predictDuel
}
from
"
@/api/ygoAgent/predict
"
;
import
{
predictDuel
}
from
"
@/api/ygoAgent/predict
"
;
import
{
import
{
convertActionMsg
,
convertCard
,
convertDeckCard
,
convertPhase
,
Global
,
Global
,
Input
,
Input
,
MsgSelectSum
,
MsgSelectSum
,
MultiSelectMsg
,
MultiSelectMsg
,
parsePlayerFromMsg
,
}
from
"
@/api/ygoAgent/schema
"
;
}
from
"
@/api/ygoAgent/schema
"
;
import
{
convertActionMsg
,
convertCard
,
convertDeckCard
,
convertPhase
,
convertPositionResponse
,
parsePlayerFromMsg
,
}
from
"
@/api/ygoAgent/transaction
"
;
import
{
cardStore
,
matStore
}
from
"
@/stores
"
;
import
{
cardStore
,
matStore
}
from
"
@/stores
"
;
function
computeSetDifference
(
a1
:
number
[],
a2
:
number
[]):
number
[]
{
import
{
argmax
,
computeSetDifference
}
from
"
./util
"
;
const
freq1
=
new
Map
<
number
,
number
>
();
const
freq2
=
new
Map
<
number
,
number
>
();
for
(
const
num
of
a1
)
{
freq1
.
set
(
num
,
(
freq1
.
get
(
num
)
||
0
)
+
1
);
}
for
(
const
num
of
a2
)
{
freq2
.
set
(
num
,
(
freq2
.
get
(
num
)
||
0
)
+
1
);
}
for
(
const
[
num
,
count
]
of
freq2
)
{
if
(
freq1
.
has
(
num
))
{
freq1
.
set
(
num
,
freq1
.
get
(
num
)
!
-
count
);
}
}
const
difference
:
number
[]
=
[];
for
(
const
[
num
,
count
]
of
freq1
)
{
if
(
count
>
0
)
{
difference
.
push
(...
Array
(
count
).
fill
(
num
));
}
}
return
difference
;
const
{
DECK
,
HAND
,
MZONE
,
SZONE
,
GRAVE
,
REMOVED
,
EXTRA
}
=
ygopro
.
CardZone
;
}
export
function
genInput
(
msg
:
ygopro
.
StocGameMessage
):
Input
{
/*
// 全局信息可以从 `matStore` 里面拿
* Generate input data for `YgoAgent Server`
* */
export
function
genAgentInput
(
msg
:
ygopro
.
StocGameMessage
):
Input
{
const
mat
=
matStore
;
const
mat
=
matStore
;
// 卡片信息可以从 `cardStore` 里面拿
// TODO (ygo-agent): TZONE
// TODO (ygo-agent): TZONE
const
zones
=
[
const
zones
=
[
DECK
,
HAND
,
MZONE
,
SZONE
,
GRAVE
,
REMOVED
,
EXTRA
];
ygopro
.
CardZone
.
DECK
,
ygopro
.
CardZone
.
HAND
,
ygopro
.
CardZone
.
MZONE
,
ygopro
.
CardZone
.
SZONE
,
ygopro
.
CardZone
.
GRAVE
,
ygopro
.
CardZone
.
REMOVED
,
ygopro
.
CardZone
.
EXTRA
,
];
// select_xxx msg 从参数 `msg` 里获取
// 这里已经保证 `msg` 是众多 `select_xxx` msg 中的一个
const
player
=
parsePlayerFromMsg
(
msg
);
const
player
=
parsePlayerFromMsg
(
msg
);
const
opponent
=
1
-
player
;
const
opponent
=
1
-
player
;
...
@@ -74,10 +45,7 @@ export function genInput(msg: ygopro.StocGameMessage): Input {
...
@@ -74,10 +45,7 @@ export function genInput(msg: ygopro.StocGameMessage): Input {
.
filter
(
.
filter
(
(
card
)
=>
(
card
)
=>
zones
.
includes
(
card
.
location
.
zone
)
&&
zones
.
includes
(
card
.
location
.
zone
)
&&
!
(
!
(
card
.
location
.
zone
===
DECK
&&
card
.
location
.
controller
===
player
),
card
.
location
.
zone
===
ygopro
.
CardZone
.
DECK
&&
card
.
location
.
controller
===
player
),
)
)
.
map
((
card
)
=>
convertCard
(
card
,
player
));
.
map
((
card
)
=>
convertCard
(
card
,
player
));
...
@@ -108,23 +76,22 @@ export function genInput(msg: ygopro.StocGameMessage): Input {
...
@@ -108,23 +76,22 @@ export function genInput(msg: ygopro.StocGameMessage): Input {
const
actionMsg
=
convertActionMsg
(
msg
);
const
actionMsg
=
convertActionMsg
(
msg
);
return
{
return
{
global
:
global
,
global
,
cards
:
deckCardsMe
.
concat
(
cards
),
cards
:
deckCardsMe
.
concat
(
cards
),
action_msg
:
actionMsg
,
action_msg
:
actionMsg
,
};
};
}
}
async
function
sendRequest
(
req
:
PredictReq
)
{
async
function
sendRequest
(
req
:
PredictReq
)
{
console
.
log
(
"
Sending predict request:
"
,
req
);
const
duelId
=
matStore
.
duelId
;
const
duelId
=
matStore
.
duelId
;
const
resp
=
await
predictDuel
(
duelId
,
req
);
const
resp
=
await
predictDuel
(
duelId
,
req
);
console
.
log
(
"
Got predict response:
"
,
resp
);
if
(
resp
!==
undefined
)
{
if
(
resp
!==
undefined
)
{
matStore
.
agentIndex
=
resp
.
index
;
matStore
.
agentIndex
=
resp
.
index
;
}
else
{
}
else
{
throw
new
Error
(
"
Failed to get predict response
"
);
throw
new
Error
(
"
Failed to get predict response
"
);
}
}
// TODO: 下面这些逻辑不应该在这个函数里面,应该在UI组件里面实现
const
preds
=
resp
.
predict_results
.
action_preds
;
const
preds
=
resp
.
predict_results
.
action_preds
;
const
actionIdx
=
argmax
(
preds
,
(
r
)
=>
r
.
prob
);
const
actionIdx
=
argmax
(
preds
,
(
r
)
=>
r
.
prob
);
matStore
.
prevActionIndex
=
actionIdx
;
matStore
.
prevActionIndex
=
actionIdx
;
...
@@ -132,8 +99,9 @@ async function sendRequest(req: PredictReq) {
...
@@ -132,8 +99,9 @@ async function sendRequest(req: PredictReq) {
return
pred
;
return
pred
;
}
}
// TODO: 这个函数的逻辑也需要拆分下
export
async
function
sendAIPredictAsResponse
(
msg
:
ygopro
.
StocGameMessage
)
{
export
async
function
sendAIPredictAsResponse
(
msg
:
ygopro
.
StocGameMessage
)
{
const
input
=
genInput
(
msg
);
const
input
=
gen
Agent
Input
(
msg
);
const
msgName
=
input
.
action_msg
.
data
.
msg_type
;
const
msgName
=
input
.
action_msg
.
data
.
msg_type
;
const
multiSelectMsgs
=
[
"
select_card
"
,
"
select_tribute
"
,
"
select_sum
"
];
const
multiSelectMsgs
=
[
"
select_card
"
,
"
select_tribute
"
,
"
select_sum
"
];
...
@@ -240,37 +208,3 @@ export async function sendAIPredictAsResponse(msg: ygopro.StocGameMessage) {
...
@@ -240,37 +208,3 @@ export async function sendAIPredictAsResponse(msg: ygopro.StocGameMessage) {
}
}
}
}
}
}
function
argmax
<
T
>
(
arr
:
T
[],
getValue
:
(
item
:
T
)
=>
number
):
number
{
if
(
arr
.
length
===
0
)
{
throw
new
Error
(
"
Array is empty
"
);
}
let
maxIndex
=
0
;
let
maxValue
=
getValue
(
arr
[
0
]);
for
(
let
i
=
1
;
i
<
arr
.
length
;
i
++
)
{
const
currentValue
=
getValue
(
arr
[
i
]);
if
(
currentValue
>
maxValue
)
{
maxValue
=
currentValue
;
maxIndex
=
i
;
}
}
return
maxIndex
;
}
function
convertPositionResponse
(
response
:
number
):
ygopro
.
CardPosition
{
switch
(
response
)
{
case
0x1
:
return
ygopro
.
CardPosition
.
FACEUP_ATTACK
;
case
0x2
:
return
ygopro
.
CardPosition
.
FACEDOWN_ATTACK
;
case
0x4
:
return
ygopro
.
CardPosition
.
FACEUP_DEFENSE
;
case
0x8
:
return
ygopro
.
CardPosition
.
FACEDOWN_DEFENSE
;
default
:
throw
new
Error
(
`Invalid position response:
${
response
}
`
);
}
}
src/service/duel/util.ts
View file @
5e3a7019
...
@@ -8,3 +8,48 @@ export function isAllOnField(locations: ygopro.CardLocation[]): boolean {
...
@@ -8,3 +8,48 @@ export function isAllOnField(locations: ygopro.CardLocation[]): boolean {
return
locations
.
find
((
location
)
=>
!
isOnField
(
location
))
===
undefined
;
return
locations
.
find
((
location
)
=>
!
isOnField
(
location
))
===
undefined
;
}
}
export
function
computeSetDifference
(
set1
:
number
[],
set2
:
number
[]):
number
[]
{
const
freq1
=
new
Map
<
number
,
number
>
();
const
freq2
=
new
Map
<
number
,
number
>
();
for
(
const
num
of
set1
)
{
freq1
.
set
(
num
,
(
freq1
.
get
(
num
)
||
0
)
+
1
);
}
for
(
const
num
of
set2
)
{
freq2
.
set
(
num
,
(
freq2
.
get
(
num
)
||
0
)
+
1
);
}
for
(
const
[
num
,
count
]
of
freq2
)
{
if
(
freq1
.
has
(
num
))
{
freq1
.
set
(
num
,
freq1
.
get
(
num
)
!
-
count
);
}
}
const
difference
:
number
[]
=
[];
for
(
const
[
num
,
count
]
of
freq1
)
{
if
(
count
>
0
)
{
difference
.
push
(...
Array
(
count
).
fill
(
num
));
}
}
return
difference
;
}
export
function
argmax
<
T
>
(
arr
:
T
[],
getValue
:
(
item
:
T
)
=>
number
):
number
{
if
(
arr
.
length
===
0
)
{
throw
new
Error
(
"
Array is empty
"
);
}
let
maxIndex
=
0
;
let
maxValue
=
getValue
(
arr
[
0
]);
for
(
let
i
=
1
;
i
<
arr
.
length
;
i
++
)
{
const
currentValue
=
getValue
(
arr
[
i
]);
if
(
currentValue
>
maxValue
)
{
maxValue
=
currentValue
;
maxIndex
=
i
;
}
}
return
maxIndex
;
}
src/stores/matStore/types.ts
View file @
5e3a7019
...
@@ -50,6 +50,7 @@ export interface MatState {
...
@@ -50,6 +50,7 @@ export interface MatState {
/** 根据自己的先后手判断是否是自己 */
/** 根据自己的先后手判断是否是自己 */
isMe
:
(
player
:
number
)
=>
boolean
;
isMe
:
(
player
:
number
)
=>
boolean
;
// 下面其中一些貌似可以封装成为`AgentInfo`
turnCount
:
number
;
turnCount
:
number
;
duelId
:
string
;
duelId
:
string
;
agentIndex
:
number
;
agentIndex
:
number
;
...
...
src/ui/Duel/Message/NeosModal/index.tsx
View file @
5e3a7019
...
@@ -35,7 +35,7 @@ export const NeosModal: React.FC<ModalProps> = (props) => {
...
@@ -35,7 +35,7 @@ export const NeosModal: React.FC<ModalProps> = (props) => {
maskClosable=
{
true
}
maskClosable=
{
true
}
onCancel=
{
()
=>
setMini
(
!
mini
)
}
onCancel=
{
()
=>
setMini
(
!
mini
)
}
closeIcon=
{
mini
?
<
UpOutlined
/>
:
<
MinusOutlined
/>
}
closeIcon=
{
mini
?
<
UpOutlined
/>
:
<
MinusOutlined
/>
}
bodyS
tyle=
{
{
padding
:
"
10px 0
"
}
}
s
tyle=
{
{
padding
:
"
10px 0
"
}
}
mask=
{
!
mini
}
mask=
{
!
mini
}
wrapClassName=
{
classNames
({
[
styles
.
wrap
]:
mini
})
}
wrapClassName=
{
classNames
({
[
styles
.
wrap
]:
mini
})
}
closable=
{
true
}
closable=
{
true
}
...
...
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