Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro-deck-encode
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-deck-encode
Commits
daa6eb35
Commit
daa6eb35
authored
Feb 24, 2023
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add toUpdateDeckPayload
parent
80a9cdc5
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
12 deletions
+81
-12
index.ts
index.ts
+16
-12
src/utils.ts
src/utils.ts
+47
-0
tests/sample.spec.ts
tests/sample.spec.ts
+18
-0
No files found.
index.ts
View file @
daa6eb35
import
{
countItems
}
from
'
./src/utils
'
;
import
{
BufferWriter
,
countItems
}
from
'
./src/utils
'
;
import
{
Base64
}
from
'
js-base64
'
;
export
default
class
YGOProDeck
{
...
...
@@ -13,14 +13,9 @@ export default class YGOProDeck {
toUint8Array
()
{
const
counted
=
[
this
.
main
,
this
.
extra
,
this
.
side
].
map
(
countItems
);
const
buf
=
new
Uint8Array
(
counted
.
reduce
((
a
,
b
)
=>
a
+
b
.
size
*
4
,
0
));
let
pointer
=
0
;
const
writeUint32LE
=
(
value
:
number
)
=>
{
buf
[
pointer
++
]
=
value
&
0xff
;
buf
[
pointer
++
]
=
(
value
>>
8
)
&
0xff
;
buf
[
pointer
++
]
=
(
value
>>
16
)
&
0xff
;
buf
[
pointer
++
]
=
(
value
>>
24
)
&
0xff
;
};
const
writer
=
new
BufferWriter
(
counted
.
reduce
((
a
,
b
)
=>
a
+
b
.
size
*
4
,
0
),
);
const
writeCards
=
(
countMap
:
Map
<
number
,
number
>
,
type
:
number
)
=>
{
// each card: 28 bits for id, 2 bits(0, 1, 2, 3) for type(0: main, 1: extra, 2: side, 3: unknown), 2 bits for count (0: 1, 1: 2, 2: 3, 3: 4)
for
(
const
[
id
,
count
]
of
countMap
.
entries
())
{
...
...
@@ -28,11 +23,11 @@ export default class YGOProDeck {
throw
new
Error
(
`Too many cards:
${
id
}
`
);
}
const
value
=
(
id
&
0xfffffff
)
|
(
type
<<
28
)
|
((
count
-
1
)
<<
30
);
writeUint32LE
(
value
);
write
r
.
write
Uint32LE
(
value
);
}
};
counted
.
forEach
(
writeCards
);
return
buf
;
return
writer
.
buffer
;
}
toEncodedString
()
{
...
...
@@ -106,4 +101,13 @@ export default class YGOProDeck {
static
fromYdkString
(
str
:
string
)
{
return
new
YGOProDeck
().
fromYdkString
(
str
);
}
toUpdateDeckPayload
()
{
const
cards
=
[...
this
.
main
,
...
this
.
extra
,
...
this
.
side
];
const
writer
=
new
BufferWriter
(
cards
.
length
*
4
+
8
)
.
writeUint32LE
(
this
.
main
.
length
+
this
.
extra
.
length
)
.
writeUint32LE
(
this
.
side
.
length
);
cards
.
forEach
((
id
)
=>
writer
.
writeUint32LE
(
id
));
return
writer
.
buffer
;
}
}
src/utils.ts
View file @
daa6eb35
...
...
@@ -5,3 +5,50 @@ export function countItems<T>(arr: T[]) {
}
return
map
;
}
export
class
BufferWriter
{
buffer
:
Uint8Array
;
pointer
=
0
;
constructor
(
length
:
number
)
{
this
.
buffer
=
new
Uint8Array
(
length
);
}
writeUint32LE
(
value
:
number
)
{
this
.
buffer
[
this
.
pointer
++
]
=
value
&
0xff
;
this
.
buffer
[
this
.
pointer
++
]
=
(
value
>>
8
)
&
0xff
;
this
.
buffer
[
this
.
pointer
++
]
=
(
value
>>
16
)
&
0xff
;
this
.
buffer
[
this
.
pointer
++
]
=
(
value
>>
24
)
&
0xff
;
return
this
;
}
writeUint32BE
(
value
:
number
)
{
this
.
buffer
[
this
.
pointer
++
]
=
(
value
>>
24
)
&
0xff
;
this
.
buffer
[
this
.
pointer
++
]
=
(
value
>>
16
)
&
0xff
;
this
.
buffer
[
this
.
pointer
++
]
=
(
value
>>
8
)
&
0xff
;
this
.
buffer
[
this
.
pointer
++
]
=
value
&
0xff
;
return
this
;
}
writeUint16LE
(
value
:
number
)
{
this
.
buffer
[
this
.
pointer
++
]
=
value
&
0xff
;
this
.
buffer
[
this
.
pointer
++
]
=
(
value
>>
8
)
&
0xff
;
return
this
;
}
writeUint16BE
(
value
:
number
)
{
this
.
buffer
[
this
.
pointer
++
]
=
(
value
>>
8
)
&
0xff
;
this
.
buffer
[
this
.
pointer
++
]
=
value
&
0xff
;
return
this
;
}
writeUint8
(
value
:
number
)
{
this
.
buffer
[
this
.
pointer
++
]
=
value
&
0xff
;
return
this
;
}
writeString
(
str
:
string
)
{
for
(
let
i
=
0
;
i
<
str
.
length
;
i
++
)
{
this
.
buffer
[
this
.
pointer
++
]
=
str
.
charCodeAt
(
i
);
}
return
this
;
}
}
tests/sample.spec.ts
View file @
daa6eb35
...
...
@@ -18,6 +18,24 @@ describe('Sample test.', () => {
encoded
.
replace
(
/
[
-_
]
/g
,
(
m0
)
=>
(
m0
==
'
-
'
?
'
+
'
:
'
/
'
)),
'
base64
'
,
);
expect
(
buf
.
readUInt32LE
(
0
)
&
0xfffffff
).
toBe
(
123
);
expect
((
buf
.
readUInt32LE
(
0
)
>>
28
)
&
0x3
).
toBe
(
0
);
expect
(((
buf
.
readUInt32LE
(
0
)
>>
30
)
&
0x3
)
+
1
).
toBe
(
3
);
expect
(
buf
.
readUInt32LE
(
4
)
&
0xfffffff
).
toBe
(
456
);
expect
((
buf
.
readUInt32LE
(
4
)
>>
28
)
&
0x3
).
toBe
(
0
);
expect
(((
buf
.
readUInt32LE
(
4
)
>>
30
)
&
0x3
)
+
1
).
toBe
(
2
);
expect
(
buf
.
readUInt32LE
(
8
)
&
0xfffffff
).
toBe
(
789
);
expect
((
buf
.
readUInt32LE
(
8
)
>>
28
)
&
0x3
).
toBe
(
0
);
expect
(((
buf
.
readUInt32LE
(
8
)
>>
30
)
&
0x3
)
+
1
).
toBe
(
1
);
expect
(
buf
.
readUInt32LE
(
12
)
&
0xfffffff
).
toBe
(
1234
);
expect
((
buf
.
readUInt32LE
(
12
)
>>
28
)
&
0x3
).
toBe
(
1
);
expect
(((
buf
.
readUInt32LE
(
12
)
>>
30
)
&
0x3
)
+
1
).
toBe
(
1
);
expect
(
buf
.
readUInt32LE
(
16
)
&
0xfffffff
).
toBe
(
5678
);
expect
((
buf
.
readUInt32LE
(
16
)
>>
28
)
&
0x3
).
toBe
(
1
);
expect
(((
buf
.
readUInt32LE
(
16
)
>>
30
)
&
0x3
)
+
1
).
toBe
(
1
);
expect
(
buf
.
readUInt32LE
(
20
)
&
0xfffffff
).
toBe
(
12345
);
expect
((
buf
.
readUInt32LE
(
20
)
>>
28
)
&
0x3
).
toBe
(
2
);
expect
(((
buf
.
readUInt32LE
(
20
)
>>
30
)
&
0x3
)
+
1
).
toBe
(
1
);
expect
(
buf
.
length
).
toBe
(
24
);
const
decoded
=
new
YGOProDeck
().
fromEncodedString
(
encoded
);
expect
(
decoded
.
main
).
toStrictEqual
(
deck
.
main
);
...
...
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