ygopro-deck-encode 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +4 -0
- package/.eslintrc.js +25 -0
- package/.prettierrc +4 -0
- package/LICENSE +22 -0
- package/README.md +30 -0
- package/dist/index.cjs +133 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.mjs +107 -0
- package/dist/index.mjs.map +7 -0
- package/dist/src/utils.d.ts +1 -0
- package/index.ts +106 -0
- package/package.json +64 -0
- package/tsconfig.json +20 -0
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
parser: '@typescript-eslint/parser',
|
|
3
|
+
parserOptions: {
|
|
4
|
+
project: 'tsconfig.json',
|
|
5
|
+
tsconfigRootDir : __dirname,
|
|
6
|
+
sourceType: 'module',
|
|
7
|
+
},
|
|
8
|
+
plugins: ['@typescript-eslint/eslint-plugin'],
|
|
9
|
+
extends: [
|
|
10
|
+
'plugin:@typescript-eslint/recommended',
|
|
11
|
+
'plugin:prettier/recommended',
|
|
12
|
+
],
|
|
13
|
+
root: true,
|
|
14
|
+
env: {
|
|
15
|
+
node: true,
|
|
16
|
+
jest: true,
|
|
17
|
+
},
|
|
18
|
+
ignorePatterns: ['.eslintrc.js'],
|
|
19
|
+
rules: {
|
|
20
|
+
'@typescript-eslint/interface-name-prefix': 'off',
|
|
21
|
+
'@typescript-eslint/explicit-function-return-type': 'off',
|
|
22
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
23
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
24
|
+
},
|
|
25
|
+
};
|
package/.prettierrc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Nanahira
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# ygopro-deck-encode
|
|
2
|
+
|
|
3
|
+
YGOPro Deck encode and decode, with well-browser support.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const deck = new YGOProDeck();
|
|
9
|
+
// main: number[]
|
|
10
|
+
// extra: number[]
|
|
11
|
+
// side: number[]
|
|
12
|
+
|
|
13
|
+
const code = deck.toEncodedString(); // base64url-encoded deck code
|
|
14
|
+
const anotherDeck = YGOProDeck.fromEncodedString(code); // decode it back
|
|
15
|
+
|
|
16
|
+
const ydk = deck.toYdkString(); // YDK format
|
|
17
|
+
const yetAnotherDeck = YGOProDeck.fromYdkString(ydk); // decode it back
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## Encode format
|
|
22
|
+
|
|
23
|
+
The deck code is a base64url-encoded string, which is an unsigned 32-bit integer array, each number representing a card
|
|
24
|
+
|
|
25
|
+
- 28 bits: card ID
|
|
26
|
+
- 2 bits: card type
|
|
27
|
+
- 0: main deck
|
|
28
|
+
- 1: extra deck
|
|
29
|
+
- 2: side deck
|
|
30
|
+
- 2 bits: card count - 1. a.k.a. 0 means 1, 1 means 2, 2 means 3, 3 means 4
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// index.ts
|
|
20
|
+
var ygopro_deck_encode_exports = {};
|
|
21
|
+
__export(ygopro_deck_encode_exports, {
|
|
22
|
+
default: () => YGOProDeck
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(ygopro_deck_encode_exports);
|
|
25
|
+
|
|
26
|
+
// src/utils.ts
|
|
27
|
+
function countItems(arr) {
|
|
28
|
+
const map = /* @__PURE__ */ new Map();
|
|
29
|
+
for (const item of arr) {
|
|
30
|
+
map.set(item, (map.get(item) || 0) + 1);
|
|
31
|
+
}
|
|
32
|
+
return map;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// index.ts
|
|
36
|
+
var import_js_base64 = require("js-base64");
|
|
37
|
+
var YGOProDeck = class {
|
|
38
|
+
constructor() {
|
|
39
|
+
this.main = [];
|
|
40
|
+
this.extra = [];
|
|
41
|
+
this.side = [];
|
|
42
|
+
}
|
|
43
|
+
sort() {
|
|
44
|
+
this.main.sort();
|
|
45
|
+
this.extra.sort();
|
|
46
|
+
this.side.sort();
|
|
47
|
+
}
|
|
48
|
+
bufferLength() {
|
|
49
|
+
const counted = [this.main, this.extra, this.side].map(countItems);
|
|
50
|
+
return counted.reduce((a, b) => a + b.size * 4, 0);
|
|
51
|
+
}
|
|
52
|
+
toEncodedString() {
|
|
53
|
+
const counted = [this.main, this.extra, this.side].map(countItems);
|
|
54
|
+
const buf = new Uint8Array(counted.reduce((a, b) => a + b.size * 4, 0));
|
|
55
|
+
let pointer = 0;
|
|
56
|
+
const writeUint32LE = (value) => {
|
|
57
|
+
buf[pointer++] = value & 255;
|
|
58
|
+
buf[pointer++] = value >> 8 & 255;
|
|
59
|
+
buf[pointer++] = value >> 16 & 255;
|
|
60
|
+
buf[pointer++] = value >> 24 & 255;
|
|
61
|
+
};
|
|
62
|
+
const writeCards = (countMap, type) => {
|
|
63
|
+
for (const [id, count] of countMap.entries()) {
|
|
64
|
+
if (count > 4) {
|
|
65
|
+
throw new Error(`Too many cards: ${id}`);
|
|
66
|
+
}
|
|
67
|
+
const value = id & 268435455 | type << 28 | count - 1 << 30;
|
|
68
|
+
writeUint32LE(value);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
counted.forEach(writeCards);
|
|
72
|
+
return import_js_base64.Base64.fromUint8Array(buf, true);
|
|
73
|
+
}
|
|
74
|
+
toString() {
|
|
75
|
+
return this.toEncodedString();
|
|
76
|
+
}
|
|
77
|
+
fromEncodedString(str) {
|
|
78
|
+
const buf = import_js_base64.Base64.toUint8Array(str);
|
|
79
|
+
for (let i = 0; i < buf.length - 3; i += 4) {
|
|
80
|
+
const value = buf[i] | buf[i + 1] << 8 | buf[i + 2] << 16 | buf[i + 3] << 24;
|
|
81
|
+
const id = value & 268435455;
|
|
82
|
+
const type = value >> 28 & 3;
|
|
83
|
+
const count = (value >> 30 & 3) + 1;
|
|
84
|
+
const cards = [this.main, this.extra, this.side][type];
|
|
85
|
+
for (let j = 0; j < count; j++) {
|
|
86
|
+
cards.push(id);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
this.sort();
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
static fromEncodedString(str) {
|
|
93
|
+
return new YGOProDeck().fromEncodedString(str);
|
|
94
|
+
}
|
|
95
|
+
toYdkString() {
|
|
96
|
+
return [
|
|
97
|
+
"#created by ygopro-deck-encode",
|
|
98
|
+
"#main",
|
|
99
|
+
...this.main.map((id) => id.toString()),
|
|
100
|
+
"#extra",
|
|
101
|
+
...this.extra.map((id) => id.toString()),
|
|
102
|
+
"!side",
|
|
103
|
+
...this.side.map((id) => id.toString())
|
|
104
|
+
].join("\n");
|
|
105
|
+
}
|
|
106
|
+
fromYdkString(str) {
|
|
107
|
+
const lines = str.split("\n");
|
|
108
|
+
let current = this.main;
|
|
109
|
+
for (const _line of lines) {
|
|
110
|
+
const line = _line.trim();
|
|
111
|
+
if (line === "#main") {
|
|
112
|
+
current = this.main;
|
|
113
|
+
}
|
|
114
|
+
if (line === "#extra") {
|
|
115
|
+
current = this.extra;
|
|
116
|
+
}
|
|
117
|
+
if (line === "!side") {
|
|
118
|
+
current = this.side;
|
|
119
|
+
}
|
|
120
|
+
if (line.match(/^\d+$/)) {
|
|
121
|
+
current.push(parseInt(line, 10));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
this.sort();
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
static fromYdkString(str) {
|
|
128
|
+
return new YGOProDeck().fromYdkString(str);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
132
|
+
0 && (module.exports = {});
|
|
133
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../index.ts", "../src/utils.ts"],
|
|
4
|
+
"sourcesContent": ["import { countItems } from './src/utils';\nimport { Base64 } from 'js-base64';\n\nexport default class YGOProDeck {\n main: number[] = [];\n extra: number[] = [];\n side: number[] = [];\n\n private sort() {\n this.main.sort();\n this.extra.sort();\n this.side.sort();\n }\n\n bufferLength() {\n const counted = [this.main, this.extra, this.side].map(countItems);\n return counted.reduce((a, b) => a + b.size * 4, 0);\n }\n\n toEncodedString() {\n const counted = [this.main, this.extra, this.side].map(countItems);\n const buf = new Uint8Array(counted.reduce((a, b) => a + b.size * 4, 0));\n let pointer = 0;\n const writeUint32LE = (value: number) => {\n buf[pointer++] = value & 0xff;\n buf[pointer++] = (value >> 8) & 0xff;\n buf[pointer++] = (value >> 16) & 0xff;\n buf[pointer++] = (value >> 24) & 0xff;\n };\n const writeCards = (countMap: Map<number, number>, type: number) => {\n // 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)\n for (const [id, count] of countMap.entries()) {\n if (count > 4) {\n throw new Error(`Too many cards: ${id}`);\n }\n const value = (id & 0xfffffff) | (type << 28) | ((count - 1) << 30);\n writeUint32LE(value);\n }\n };\n counted.forEach(writeCards);\n return Base64.fromUint8Array(buf, true);\n }\n\n toString() {\n return this.toEncodedString();\n }\n\n fromEncodedString(str: string) {\n const buf = Base64.toUint8Array(str);\n for (let i = 0; i < buf.length - 3; i += 4) {\n const value =\n buf[i] | (buf[i + 1] << 8) | (buf[i + 2] << 16) | (buf[i + 3] << 24);\n const id = value & 0xfffffff;\n const type = (value >> 28) & 0x3;\n const count = ((value >> 30) & 0x3) + 1;\n const cards = [this.main, this.extra, this.side][type];\n for (let j = 0; j < count; j++) {\n cards.push(id);\n }\n }\n this.sort();\n return this;\n }\n\n static fromEncodedString(str: string) { \n return new YGOProDeck().fromEncodedString(str);\n }\n\n toYdkString() {\n return [\n '#created by ygopro-deck-encode',\n '#main',\n ...this.main.map((id) => id.toString()),\n '#extra',\n ...this.extra.map((id) => id.toString()),\n '!side',\n ...this.side.map((id) => id.toString()),\n ].join('\\n');\n }\n\n fromYdkString(str: string) {\n const lines = str.split('\\n');\n let current = this.main;\n for (const _line of lines) {\n const line = _line.trim();\n if (line === '#main') {\n current = this.main;\n }\n if (line === '#extra') {\n current = this.extra;\n }\n if (line === '!side') {\n current = this.side;\n }\n if (line.match(/^\\d+$/)) {\n current.push(parseInt(line, 10));\n }\n }\n this.sort();\n return this;\n }\n\n static fromYdkString(str: string) {\n return new YGOProDeck().fromYdkString(str);\n }\n}\n", "export function countItems<T>(arr: T[]) {\n const map = new Map<T, number>();\n for (const item of arr) {\n map.set(item, (map.get(item) || 0) + 1);\n }\n return map;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,SAAS,WAAc,KAAU;AACtC,QAAM,MAAM,oBAAI,IAAe;AAC/B,aAAW,QAAQ,KAAK;AACtB,QAAI,IAAI,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,CAAC;AAAA,EACxC;AACA,SAAO;AACT;;;ADLA,uBAAuB;AAEvB,IAAqB,aAArB,MAAgC;AAAA,EAAhC;AACE,gBAAiB,CAAC;AAClB,iBAAkB,CAAC;AACnB,gBAAiB,CAAC;AAAA;AAAA,EAEV,OAAO;AACb,SAAK,KAAK,KAAK;AACf,SAAK,MAAM,KAAK;AAChB,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,GAAG;AAAA,EAC3C;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default class YGOProDeck {
|
|
2
|
+
main: number[];
|
|
3
|
+
extra: number[];
|
|
4
|
+
side: number[];
|
|
5
|
+
private sort;
|
|
6
|
+
bufferLength(): number;
|
|
7
|
+
toEncodedString(): string;
|
|
8
|
+
toString(): string;
|
|
9
|
+
fromEncodedString(str: string): this;
|
|
10
|
+
static fromEncodedString(str: string): YGOProDeck;
|
|
11
|
+
toYdkString(): string;
|
|
12
|
+
fromYdkString(str: string): this;
|
|
13
|
+
static fromYdkString(str: string): YGOProDeck;
|
|
14
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
function countItems(arr) {
|
|
3
|
+
const map = /* @__PURE__ */ new Map();
|
|
4
|
+
for (const item of arr) {
|
|
5
|
+
map.set(item, (map.get(item) || 0) + 1);
|
|
6
|
+
}
|
|
7
|
+
return map;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// index.ts
|
|
11
|
+
import { Base64 } from "js-base64";
|
|
12
|
+
var YGOProDeck = class {
|
|
13
|
+
main = [];
|
|
14
|
+
extra = [];
|
|
15
|
+
side = [];
|
|
16
|
+
sort() {
|
|
17
|
+
this.main.sort();
|
|
18
|
+
this.extra.sort();
|
|
19
|
+
this.side.sort();
|
|
20
|
+
}
|
|
21
|
+
bufferLength() {
|
|
22
|
+
const counted = [this.main, this.extra, this.side].map(countItems);
|
|
23
|
+
return counted.reduce((a, b) => a + b.size * 4, 0);
|
|
24
|
+
}
|
|
25
|
+
toEncodedString() {
|
|
26
|
+
const counted = [this.main, this.extra, this.side].map(countItems);
|
|
27
|
+
const buf = new Uint8Array(counted.reduce((a, b) => a + b.size * 4, 0));
|
|
28
|
+
let pointer = 0;
|
|
29
|
+
const writeUint32LE = (value) => {
|
|
30
|
+
buf[pointer++] = value & 255;
|
|
31
|
+
buf[pointer++] = value >> 8 & 255;
|
|
32
|
+
buf[pointer++] = value >> 16 & 255;
|
|
33
|
+
buf[pointer++] = value >> 24 & 255;
|
|
34
|
+
};
|
|
35
|
+
const writeCards = (countMap, type) => {
|
|
36
|
+
for (const [id, count] of countMap.entries()) {
|
|
37
|
+
if (count > 4) {
|
|
38
|
+
throw new Error(`Too many cards: ${id}`);
|
|
39
|
+
}
|
|
40
|
+
const value = id & 268435455 | type << 28 | count - 1 << 30;
|
|
41
|
+
writeUint32LE(value);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
counted.forEach(writeCards);
|
|
45
|
+
return Base64.fromUint8Array(buf, true);
|
|
46
|
+
}
|
|
47
|
+
toString() {
|
|
48
|
+
return this.toEncodedString();
|
|
49
|
+
}
|
|
50
|
+
fromEncodedString(str) {
|
|
51
|
+
const buf = Base64.toUint8Array(str);
|
|
52
|
+
for (let i = 0; i < buf.length - 3; i += 4) {
|
|
53
|
+
const value = buf[i] | buf[i + 1] << 8 | buf[i + 2] << 16 | buf[i + 3] << 24;
|
|
54
|
+
const id = value & 268435455;
|
|
55
|
+
const type = value >> 28 & 3;
|
|
56
|
+
const count = (value >> 30 & 3) + 1;
|
|
57
|
+
const cards = [this.main, this.extra, this.side][type];
|
|
58
|
+
for (let j = 0; j < count; j++) {
|
|
59
|
+
cards.push(id);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
this.sort();
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
static fromEncodedString(str) {
|
|
66
|
+
return new YGOProDeck().fromEncodedString(str);
|
|
67
|
+
}
|
|
68
|
+
toYdkString() {
|
|
69
|
+
return [
|
|
70
|
+
"#created by ygopro-deck-encode",
|
|
71
|
+
"#main",
|
|
72
|
+
...this.main.map((id) => id.toString()),
|
|
73
|
+
"#extra",
|
|
74
|
+
...this.extra.map((id) => id.toString()),
|
|
75
|
+
"!side",
|
|
76
|
+
...this.side.map((id) => id.toString())
|
|
77
|
+
].join("\n");
|
|
78
|
+
}
|
|
79
|
+
fromYdkString(str) {
|
|
80
|
+
const lines = str.split("\n");
|
|
81
|
+
let current = this.main;
|
|
82
|
+
for (const _line of lines) {
|
|
83
|
+
const line = _line.trim();
|
|
84
|
+
if (line === "#main") {
|
|
85
|
+
current = this.main;
|
|
86
|
+
}
|
|
87
|
+
if (line === "#extra") {
|
|
88
|
+
current = this.extra;
|
|
89
|
+
}
|
|
90
|
+
if (line === "!side") {
|
|
91
|
+
current = this.side;
|
|
92
|
+
}
|
|
93
|
+
if (line.match(/^\d+$/)) {
|
|
94
|
+
current.push(parseInt(line, 10));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
this.sort();
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
static fromYdkString(str) {
|
|
101
|
+
return new YGOProDeck().fromYdkString(str);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
export {
|
|
105
|
+
YGOProDeck as default
|
|
106
|
+
};
|
|
107
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/utils.ts", "../index.ts"],
|
|
4
|
+
"sourcesContent": ["export function countItems<T>(arr: T[]) {\n const map = new Map<T, number>();\n for (const item of arr) {\n map.set(item, (map.get(item) || 0) + 1);\n }\n return map;\n}\n", "import { countItems } from './src/utils';\nimport { Base64 } from 'js-base64';\n\nexport default class YGOProDeck {\n main: number[] = [];\n extra: number[] = [];\n side: number[] = [];\n\n private sort() {\n this.main.sort();\n this.extra.sort();\n this.side.sort();\n }\n\n bufferLength() {\n const counted = [this.main, this.extra, this.side].map(countItems);\n return counted.reduce((a, b) => a + b.size * 4, 0);\n }\n\n toEncodedString() {\n const counted = [this.main, this.extra, this.side].map(countItems);\n const buf = new Uint8Array(counted.reduce((a, b) => a + b.size * 4, 0));\n let pointer = 0;\n const writeUint32LE = (value: number) => {\n buf[pointer++] = value & 0xff;\n buf[pointer++] = (value >> 8) & 0xff;\n buf[pointer++] = (value >> 16) & 0xff;\n buf[pointer++] = (value >> 24) & 0xff;\n };\n const writeCards = (countMap: Map<number, number>, type: number) => {\n // 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)\n for (const [id, count] of countMap.entries()) {\n if (count > 4) {\n throw new Error(`Too many cards: ${id}`);\n }\n const value = (id & 0xfffffff) | (type << 28) | ((count - 1) << 30);\n writeUint32LE(value);\n }\n };\n counted.forEach(writeCards);\n return Base64.fromUint8Array(buf, true);\n }\n\n toString() {\n return this.toEncodedString();\n }\n\n fromEncodedString(str: string) {\n const buf = Base64.toUint8Array(str);\n for (let i = 0; i < buf.length - 3; i += 4) {\n const value =\n buf[i] | (buf[i + 1] << 8) | (buf[i + 2] << 16) | (buf[i + 3] << 24);\n const id = value & 0xfffffff;\n const type = (value >> 28) & 0x3;\n const count = ((value >> 30) & 0x3) + 1;\n const cards = [this.main, this.extra, this.side][type];\n for (let j = 0; j < count; j++) {\n cards.push(id);\n }\n }\n this.sort();\n return this;\n }\n\n static fromEncodedString(str: string) { \n return new YGOProDeck().fromEncodedString(str);\n }\n\n toYdkString() {\n return [\n '#created by ygopro-deck-encode',\n '#main',\n ...this.main.map((id) => id.toString()),\n '#extra',\n ...this.extra.map((id) => id.toString()),\n '!side',\n ...this.side.map((id) => id.toString()),\n ].join('\\n');\n }\n\n fromYdkString(str: string) {\n const lines = str.split('\\n');\n let current = this.main;\n for (const _line of lines) {\n const line = _line.trim();\n if (line === '#main') {\n current = this.main;\n }\n if (line === '#extra') {\n current = this.extra;\n }\n if (line === '!side') {\n current = this.side;\n }\n if (line.match(/^\\d+$/)) {\n current.push(parseInt(line, 10));\n }\n }\n this.sort();\n return this;\n }\n\n static fromYdkString(str: string) {\n return new YGOProDeck().fromYdkString(str);\n }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AAM,OAAO,MAAM,KAAK;AACxB,UAAI,SAAS,SAAS;AACpB,kBAAU,KAAK;AAAA,MACjB;AACA,UAAI,SAAS,UAAU;AACrB,kBAAU,KAAK;AAAA,MACjB;AACA,UAAI,SAAS,SAAS;AACpB,kBAAU,KAAK;AAAA,MACjB;AACA,UAAI,KAAK,MAAM,OAAO,GAAG;AACvB,gBAAQ,KAAK,SAAS,MAAM,EAAE,CAAC;AAAA,MACjC;AAAA,IACF;AACA,SAAK,KAAK;AACV,WAAO;AAAA,EACT;AAAA,EAEA,OAAO,cAAc,KAAa;AAChC,WAAO,IAAI,WAAW,EAAE,cAAc,GAAG;AAAA,EAC3C;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function countItems<T>(arr: T[]): Map<T, number>;
|
package/index.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { countItems } from './src/utils';
|
|
2
|
+
import { Base64 } from 'js-base64';
|
|
3
|
+
|
|
4
|
+
export default class YGOProDeck {
|
|
5
|
+
main: number[] = [];
|
|
6
|
+
extra: number[] = [];
|
|
7
|
+
side: number[] = [];
|
|
8
|
+
|
|
9
|
+
private sort() {
|
|
10
|
+
this.main.sort();
|
|
11
|
+
this.extra.sort();
|
|
12
|
+
this.side.sort();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
bufferLength() {
|
|
16
|
+
const counted = [this.main, this.extra, this.side].map(countItems);
|
|
17
|
+
return counted.reduce((a, b) => a + b.size * 4, 0);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
toEncodedString() {
|
|
21
|
+
const counted = [this.main, this.extra, this.side].map(countItems);
|
|
22
|
+
const buf = new Uint8Array(counted.reduce((a, b) => a + b.size * 4, 0));
|
|
23
|
+
let pointer = 0;
|
|
24
|
+
const writeUint32LE = (value: number) => {
|
|
25
|
+
buf[pointer++] = value & 0xff;
|
|
26
|
+
buf[pointer++] = (value >> 8) & 0xff;
|
|
27
|
+
buf[pointer++] = (value >> 16) & 0xff;
|
|
28
|
+
buf[pointer++] = (value >> 24) & 0xff;
|
|
29
|
+
};
|
|
30
|
+
const writeCards = (countMap: Map<number, number>, type: number) => {
|
|
31
|
+
// 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)
|
|
32
|
+
for (const [id, count] of countMap.entries()) {
|
|
33
|
+
if (count > 4) {
|
|
34
|
+
throw new Error(`Too many cards: ${id}`);
|
|
35
|
+
}
|
|
36
|
+
const value = (id & 0xfffffff) | (type << 28) | ((count - 1) << 30);
|
|
37
|
+
writeUint32LE(value);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
counted.forEach(writeCards);
|
|
41
|
+
return Base64.fromUint8Array(buf, true);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
toString() {
|
|
45
|
+
return this.toEncodedString();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
fromEncodedString(str: string) {
|
|
49
|
+
const buf = Base64.toUint8Array(str);
|
|
50
|
+
for (let i = 0; i < buf.length - 3; i += 4) {
|
|
51
|
+
const value =
|
|
52
|
+
buf[i] | (buf[i + 1] << 8) | (buf[i + 2] << 16) | (buf[i + 3] << 24);
|
|
53
|
+
const id = value & 0xfffffff;
|
|
54
|
+
const type = (value >> 28) & 0x3;
|
|
55
|
+
const count = ((value >> 30) & 0x3) + 1;
|
|
56
|
+
const cards = [this.main, this.extra, this.side][type];
|
|
57
|
+
for (let j = 0; j < count; j++) {
|
|
58
|
+
cards.push(id);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
this.sort();
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static fromEncodedString(str: string) {
|
|
66
|
+
return new YGOProDeck().fromEncodedString(str);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
toYdkString() {
|
|
70
|
+
return [
|
|
71
|
+
'#created by ygopro-deck-encode',
|
|
72
|
+
'#main',
|
|
73
|
+
...this.main.map((id) => id.toString()),
|
|
74
|
+
'#extra',
|
|
75
|
+
...this.extra.map((id) => id.toString()),
|
|
76
|
+
'!side',
|
|
77
|
+
...this.side.map((id) => id.toString()),
|
|
78
|
+
].join('\n');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fromYdkString(str: string) {
|
|
82
|
+
const lines = str.split('\n');
|
|
83
|
+
let current = this.main;
|
|
84
|
+
for (const _line of lines) {
|
|
85
|
+
const line = _line.trim();
|
|
86
|
+
if (line === '#main') {
|
|
87
|
+
current = this.main;
|
|
88
|
+
}
|
|
89
|
+
if (line === '#extra') {
|
|
90
|
+
current = this.extra;
|
|
91
|
+
}
|
|
92
|
+
if (line === '!side') {
|
|
93
|
+
current = this.side;
|
|
94
|
+
}
|
|
95
|
+
if (line.match(/^\d+$/)) {
|
|
96
|
+
current.push(parseInt(line, 10));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
this.sort();
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static fromYdkString(str: string) {
|
|
104
|
+
return new YGOProDeck().fromYdkString(str);
|
|
105
|
+
}
|
|
106
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ygopro-deck-encode",
|
|
3
|
+
"description": "YGOPro Deck encode and decode, with well-browser support.",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"lint": "eslint --fix .",
|
|
10
|
+
"compile:cjs": "esbuild index.ts --outfile=dist/index.cjs --bundle --sourcemap --platform=node --target=es2019 --external:js-base64",
|
|
11
|
+
"compile:esm": "esbuild index.ts --outfile=dist/index.mjs --bundle --sourcemap --platform=neutral --target=esnext --external:js-base64",
|
|
12
|
+
"compile:types": "tsc --emitDeclarationOnly --declaration",
|
|
13
|
+
"build": "rimraf dist && npm run compile:cjs && npm run compile:esm && npm run compile:types",
|
|
14
|
+
"test": "jest --passWithNoTests",
|
|
15
|
+
"start": "node dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://code.mycard.moe/3rdeye/myproject.git"
|
|
20
|
+
},
|
|
21
|
+
"author": "Nanahira <nanahira@momobako.com>",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"keywords": [],
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://code.mycard.moe/3rdeye/myproject/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://code.mycard.moe/3rdeye/myproject",
|
|
28
|
+
"jest": {
|
|
29
|
+
"moduleFileExtensions": [
|
|
30
|
+
"js",
|
|
31
|
+
"json",
|
|
32
|
+
"ts"
|
|
33
|
+
],
|
|
34
|
+
"rootDir": "tests",
|
|
35
|
+
"testRegex": ".*\\.spec\\.ts$",
|
|
36
|
+
"transform": {
|
|
37
|
+
"^.+\\.(t|j)s$": "ts-jest"
|
|
38
|
+
},
|
|
39
|
+
"collectCoverageFrom": [
|
|
40
|
+
"**/*.(t|j)s"
|
|
41
|
+
],
|
|
42
|
+
"coverageDirectory": "../coverage",
|
|
43
|
+
"testEnvironment": "node"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/jest": "^29.4.0",
|
|
47
|
+
"@types/node": "^18.13.0",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
|
49
|
+
"@typescript-eslint/parser": "^5.51.0",
|
|
50
|
+
"esbuild": "^0.17.7",
|
|
51
|
+
"esbuild-register": "^3.4.2",
|
|
52
|
+
"eslint": "8.22.0",
|
|
53
|
+
"eslint-config-prettier": "^8.6.0",
|
|
54
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
55
|
+
"jest": "^29.4.2",
|
|
56
|
+
"prettier": "^2.8.4",
|
|
57
|
+
"rimraf": "^4.1.2",
|
|
58
|
+
"ts-jest": "^29.0.5",
|
|
59
|
+
"typescript": "^4.9.5"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"js-base64": "^3.7.5"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "dist",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"target": "es2021",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"emitDecoratorMetadata": true,
|
|
8
|
+
"experimentalDecorators": true,
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"sourceMap": true
|
|
11
|
+
},
|
|
12
|
+
"compileOnSave": true,
|
|
13
|
+
"allowJs": true,
|
|
14
|
+
"include": [
|
|
15
|
+
"*.ts",
|
|
16
|
+
"src/**/*.ts",
|
|
17
|
+
"test/**/*.ts",
|
|
18
|
+
"tests/**/*.ts"
|
|
19
|
+
]
|
|
20
|
+
}
|