@dnd-ed/jsshenanigans 0.0.0-stage → 1.1.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/LICENSE +28 -0
- package/NOTICE.md +89 -0
- package/README.md +98 -3
- package/package.json +109 -4
- package/src/character.d.ts +82 -0
- package/src/character.js +335 -0
- package/src/codec.d.ts +20 -0
- package/src/codec.js +211 -0
- package/src/conditions.d.ts +40 -0
- package/src/conditions.js +79 -0
- package/src/defaults.d.ts +15 -0
- package/src/defaults.js +72 -0
- package/src/errors.d.ts +8 -0
- package/src/errors.js +20 -0
- package/src/index.d.ts +46 -0
- package/src/index.js +49 -0
- package/src/nodes.d.ts +356 -0
- package/src/nodes.js +739 -0
- package/src/tools.d.ts +28 -0
- package/src/tools.js +88 -0
- package/src/types.d.ts +875 -0
- package/src/types.js +3532 -0
- package/src/utils.d.ts +33 -0
- package/src/utils.js +52 -0
package/src/character.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `Character` builder and loader.
|
|
3
|
+
*
|
|
4
|
+
* Unlike libjjs, a `Character` can be *loaded* from an existing character code
|
|
5
|
+
* with `Character.from_code`. Loaded entries are kept verbatim, so fields the
|
|
6
|
+
* library does not model (`TOOL TIP`, awakening `COLOR`, editor `PREVIEW`
|
|
7
|
+
* metadata, unknown nodes, ...) survive a decode -> edit -> encode round trip
|
|
8
|
+
* untouched.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import * as codec from "./codec.js";
|
|
12
|
+
import { CONDITIONS_DEFAULTS, NODE_DEFAULTS, PROPERTIES_DEFAULTS } from "./defaults.js";
|
|
13
|
+
import { InvalidCharacterCodeError } from "./errors.js";
|
|
14
|
+
import { VISUAL } from "./nodes.js";
|
|
15
|
+
import { BodyParts } from "./types.js";
|
|
16
|
+
import { Properties } from "./utils.js";
|
|
17
|
+
|
|
18
|
+
const SKILL_KINDS = ["SKILL", "SPECIAL", "MELEE", "CHASE", "AWAKENING"];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Structural equality, so array defaults (e.g. `ANIM_USE`) compare by value.
|
|
22
|
+
* @param {unknown} a
|
|
23
|
+
* @param {unknown} b
|
|
24
|
+
*/
|
|
25
|
+
function sameValue(a, b) {
|
|
26
|
+
if (a === b) return true;
|
|
27
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
28
|
+
return a.length === b.length && a.every((v, i) => sameValue(v, b[i]));
|
|
29
|
+
}
|
|
30
|
+
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
31
|
+
const keysA = Object.keys(a);
|
|
32
|
+
const keysB = Object.keys(b);
|
|
33
|
+
return (
|
|
34
|
+
keysA.length === keysB.length && keysA.every((k) => sameValue(a[k], b[k]))
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Drop entries whose value equals the node type's default. */
|
|
41
|
+
function stripNodeDefaults(node) {
|
|
42
|
+
const defaults = NODE_DEFAULTS[node.K_NAME];
|
|
43
|
+
if (!defaults) return node;
|
|
44
|
+
const out = {};
|
|
45
|
+
for (const [key, value] of Object.entries(node)) {
|
|
46
|
+
if (key === "K_NAME" || !(key in defaults) || !sameValue(defaults[key], value)) {
|
|
47
|
+
out[key] = value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Drop entries whose value equals the condition type's default. */
|
|
54
|
+
function stripConditionDefaults(condition) {
|
|
55
|
+
const defaults = CONDITIONS_DEFAULTS[condition.K_NAME];
|
|
56
|
+
if (!defaults) return condition;
|
|
57
|
+
const out = {};
|
|
58
|
+
for (const [key, value] of Object.entries(condition)) {
|
|
59
|
+
if (key === "K_NAME" || !(key in defaults) || !sameValue(defaults[key], value)) {
|
|
60
|
+
out[key] = value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Drop properties whose value equals the property default. */
|
|
67
|
+
function stripPropertyDefaults(properties) {
|
|
68
|
+
const out = {};
|
|
69
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
70
|
+
if (key in PROPERTIES_DEFAULTS && sameValue(PROPERTIES_DEFAULTS[key], value)) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
out[key] = value;
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validate a decoded payload, throwing {@link InvalidCharacterCodeError}.
|
|
80
|
+
* Guards the whole `Character` API: without this a payload like `[1, 2, 3]`
|
|
81
|
+
* fails later with an opaque `TypeError` instead.
|
|
82
|
+
* @param {unknown} payload
|
|
83
|
+
* @returns {object[]}
|
|
84
|
+
*/
|
|
85
|
+
function validateSkills(payload) {
|
|
86
|
+
if (!Array.isArray(payload)) {
|
|
87
|
+
throw new InvalidCharacterCodeError("top-level payload is not a list of skills");
|
|
88
|
+
}
|
|
89
|
+
payload.forEach((entry, index) => {
|
|
90
|
+
if (!isPlainObject(entry)) {
|
|
91
|
+
throw new InvalidCharacterCodeError(
|
|
92
|
+
`skill #${index} is ${describeType(entry)}, expected an object`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return payload;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function isPlainObject(value) {
|
|
100
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function describeType(value) {
|
|
104
|
+
if (value === null) return "null";
|
|
105
|
+
if (Array.isArray(value)) return "array";
|
|
106
|
+
return typeof value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A Jujutsu Shenanigans moveset, built from scratch or loaded from a code. */
|
|
110
|
+
export class Character {
|
|
111
|
+
/**
|
|
112
|
+
* @param {object} [options]
|
|
113
|
+
* @param {boolean} [options.hide_character_extras] Prepend a hidden skill that
|
|
114
|
+
* hides the character's body (this *also* hides player accessories like
|
|
115
|
+
* hair). Ignored when `raw` is given.
|
|
116
|
+
* @param {object[] | null} [options.raw] An existing payload to wrap (e.g. the
|
|
117
|
+
* result of `codec.decode`). Entries are kept as-is.
|
|
118
|
+
*/
|
|
119
|
+
constructor({ hide_character_extras = false, raw = null } = {}) {
|
|
120
|
+
this.raw = raw !== null ? [...validateSkills(raw)] : [];
|
|
121
|
+
this.create = new CreateHelper(this);
|
|
122
|
+
if (raw === null && hide_character_extras) {
|
|
123
|
+
this.create.skill(
|
|
124
|
+
"init__HideCharacterExtras",
|
|
125
|
+
[
|
|
126
|
+
// hide everything
|
|
127
|
+
VISUAL({ effect: "Visibility", opacity: 1, alt_opacity: 1 }),
|
|
128
|
+
// show limbs
|
|
129
|
+
...BodyParts.filter((part) => part !== "HumanoidRootPart").map((limb) =>
|
|
130
|
+
VISUAL({ effect: "Visibility", opacity: 0, alt_opacity: 0, limb }),
|
|
131
|
+
),
|
|
132
|
+
],
|
|
133
|
+
// Visibility comes from hide_in_base AND hide_in_awakening (both are
|
|
134
|
+
// required -- a single flag only hides it in one mode), not from KEY.
|
|
135
|
+
// The key allocator counts this entry, so user skills still receive
|
|
136
|
+
// unique keys.
|
|
137
|
+
{
|
|
138
|
+
properties: Properties({
|
|
139
|
+
use_when_obtained: true,
|
|
140
|
+
hide_in_awakening: true,
|
|
141
|
+
hide_in_base: true,
|
|
142
|
+
}),
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// ---------------------------------------------------------------- //
|
|
149
|
+
// loading / exporting
|
|
150
|
+
// ---------------------------------------------------------------- //
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Load a character from a JJS character code. Every field is preserved
|
|
154
|
+
* verbatim, including ones this library does not understand.
|
|
155
|
+
* @param {string} code
|
|
156
|
+
* @returns {Character}
|
|
157
|
+
*/
|
|
158
|
+
static from_code(code) {
|
|
159
|
+
return new Character({ raw: codec.decode(code) });
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Load a character from a raw JSON string (the inner a code holds).
|
|
164
|
+
* @param {string} rawJson
|
|
165
|
+
* @returns {Character}
|
|
166
|
+
*/
|
|
167
|
+
static from_raw(rawJson) {
|
|
168
|
+
let payload;
|
|
169
|
+
try {
|
|
170
|
+
payload = JSON.parse(rawJson);
|
|
171
|
+
} catch (exc) {
|
|
172
|
+
throw new InvalidCharacterCodeError(
|
|
173
|
+
`raw payload is not valid JSON: ${exc.message ?? exc}`,
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
return new Character({ raw: payload });
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Append a raw skill object to the character. Checked shallowly: the entry
|
|
181
|
+
* must be an object, so the character can never export a code that its own
|
|
182
|
+
* loader rejects. The contents are left exactly as given.
|
|
183
|
+
* @param {object} skill
|
|
184
|
+
*/
|
|
185
|
+
create_raw_skill(skill) {
|
|
186
|
+
if (!isPlainObject(skill)) {
|
|
187
|
+
throw new InvalidCharacterCodeError(
|
|
188
|
+
`skill is ${describeType(skill)}, expected an object`,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
this.raw.push(skill);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Export the character as a raw JSON string. */
|
|
195
|
+
export_raw() {
|
|
196
|
+
return JSON.stringify(this.raw);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Export the character as a JJS-compatible character code. */
|
|
200
|
+
export_character_code() {
|
|
201
|
+
return codec.compress(this.export_raw());
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Alias for {@link Character#export_character_code}. */
|
|
205
|
+
to_code() {
|
|
206
|
+
return this.export_character_code();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ---------------------------------------------------------------- //
|
|
210
|
+
// introspection
|
|
211
|
+
// ---------------------------------------------------------------- //
|
|
212
|
+
|
|
213
|
+
get length() {
|
|
214
|
+
return this.raw.length;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
[Symbol.iterator]() {
|
|
218
|
+
return this.raw[Symbol.iterator]();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** A list of `{ kind, name }` for every entry, in order. */
|
|
222
|
+
get skills() {
|
|
223
|
+
return this.raw
|
|
224
|
+
.filter(isPlainObject)
|
|
225
|
+
.map((skill) => ({ kind: skill.K_NAME, name: skill.NAME }));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
_used_keys() {
|
|
229
|
+
const used = new Set();
|
|
230
|
+
for (const skill of this.raw) {
|
|
231
|
+
if (
|
|
232
|
+
isPlainObject(skill) &&
|
|
233
|
+
skill.K_NAME === "SKILL" &&
|
|
234
|
+
Number.isInteger(skill.KEY)
|
|
235
|
+
) {
|
|
236
|
+
used.add(skill.KEY);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return used;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
_next_key() {
|
|
243
|
+
const used = this._used_keys();
|
|
244
|
+
let key = 1;
|
|
245
|
+
while (used.has(key)) key += 1;
|
|
246
|
+
return key;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
class CreateHelper {
|
|
251
|
+
/** @param {Character} character */
|
|
252
|
+
constructor(character) {
|
|
253
|
+
this.character = character;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
_build_data(line, properties = null, conditions = null, branches = null) {
|
|
257
|
+
const data = {
|
|
258
|
+
Line: line.map(stripNodeDefaults),
|
|
259
|
+
Prop: stripPropertyDefaults(properties ?? {}),
|
|
260
|
+
Req: (conditions ?? []).map(stripConditionDefaults),
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const cleanBranches = {};
|
|
264
|
+
for (const [name, branch] of Object.entries(branches ?? {})) {
|
|
265
|
+
cleanBranches[name] = {
|
|
266
|
+
Line: (branch.nodes ?? []).map(stripNodeDefaults),
|
|
267
|
+
Req: (branch.conditions ?? []).map(stripConditionDefaults),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
if (Object.keys(cleanBranches).length > 0) data.Branch = cleanBranches;
|
|
271
|
+
|
|
272
|
+
return JSON.stringify(data);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Create a skill for the character.
|
|
277
|
+
*
|
|
278
|
+
* The node list may be passed positionally (an array) or as `options.line`,
|
|
279
|
+
* so both of these work:
|
|
280
|
+
*
|
|
281
|
+
* character.create.skill("Punch", [nodes.HITBOX()])
|
|
282
|
+
* character.create.skill("Punch", { line: [nodes.HITBOX()], cooldown: 5 })
|
|
283
|
+
*
|
|
284
|
+
* @param {string} name
|
|
285
|
+
* @param {object[]|object} [lineOrOptions]
|
|
286
|
+
* @param {object} [maybeOptions]
|
|
287
|
+
*/
|
|
288
|
+
skill(name, lineOrOptions = [], maybeOptions = {}) {
|
|
289
|
+
const lineIsArray = Array.isArray(lineOrOptions);
|
|
290
|
+
const line = lineIsArray ? lineOrOptions : lineOrOptions?.line ?? [];
|
|
291
|
+
const options = lineIsArray ? maybeOptions ?? {} : lineOrOptions ?? {};
|
|
292
|
+
|
|
293
|
+
const {
|
|
294
|
+
kind = "SKILL",
|
|
295
|
+
cooldown = 0,
|
|
296
|
+
key = null,
|
|
297
|
+
add = false,
|
|
298
|
+
duration = null,
|
|
299
|
+
delay = null,
|
|
300
|
+
properties = null,
|
|
301
|
+
conditions = null,
|
|
302
|
+
branches = null,
|
|
303
|
+
tooltip = null,
|
|
304
|
+
color = null,
|
|
305
|
+
extra = null,
|
|
306
|
+
} = options;
|
|
307
|
+
|
|
308
|
+
if (!SKILL_KINDS.includes(kind)) {
|
|
309
|
+
throw new RangeError(`unknown skill kind: ${kind}`);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const data = {
|
|
313
|
+
NAME: name,
|
|
314
|
+
DATA: this._build_data(line, properties, conditions, branches),
|
|
315
|
+
K_NAME: kind,
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
if (kind === "SKILL") {
|
|
319
|
+
if (cooldown !== null) data.COOLDOWN = cooldown;
|
|
320
|
+
data.KEY = key !== null ? key : this.character._next_key();
|
|
321
|
+
if (add) data.ADD = add;
|
|
322
|
+
} else if (kind === "SPECIAL" || kind === "CHASE") {
|
|
323
|
+
if (cooldown !== null) data.COOLDOWN = cooldown;
|
|
324
|
+
} else if (kind === "AWAKENING") {
|
|
325
|
+
if (duration !== null) data.DURATION = duration;
|
|
326
|
+
if (delay !== null) data.DELAY = delay;
|
|
327
|
+
if (color !== null) data.COLOR = color;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (tooltip !== null) data["TOOL TIP"] = tooltip;
|
|
331
|
+
if (extra) Object.assign(data, extra);
|
|
332
|
+
|
|
333
|
+
this.character.create_raw_skill(data);
|
|
334
|
+
}
|
|
335
|
+
}
|
package/src/codec.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Every zstd frame begins with these four bytes. */
|
|
2
|
+
export declare const ZSTD_MAGIC: Uint8Array;
|
|
3
|
+
|
|
4
|
+
/** Environment variable used to prefer a specific zstd backend. */
|
|
5
|
+
export declare const ENV_BACKEND: string;
|
|
6
|
+
|
|
7
|
+
/** Return the name of the zstd implementation currently in use. */
|
|
8
|
+
export declare function backend_name(): string;
|
|
9
|
+
|
|
10
|
+
/** zstd-compress a JSON string and base64-encode the result. */
|
|
11
|
+
export declare function compress(rawJson: string): string;
|
|
12
|
+
|
|
13
|
+
/** Base64-decode `code` and zstd-decompress it back into a JSON string. */
|
|
14
|
+
export declare function decompress(code: string): string;
|
|
15
|
+
|
|
16
|
+
/** Serialise a character payload (an array of skills) into a character code. */
|
|
17
|
+
export declare function encode(payload: unknown): string;
|
|
18
|
+
|
|
19
|
+
/** Decode a character code back into JavaScript objects. */
|
|
20
|
+
export declare function decode(code: string): unknown;
|
package/src/codec.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The character-code codec.
|
|
3
|
+
*
|
|
4
|
+
* A Jujutsu Shenanigans character code is:
|
|
5
|
+
*
|
|
6
|
+
* base64( zstd( json ) )
|
|
7
|
+
*
|
|
8
|
+
* where the JSON is the (minified) `Array` of skill objects. Nothing else is
|
|
9
|
+
* layered on top --- the zstd frame is a plain standard frame and the base64
|
|
10
|
+
* uses the standard alphabet, so decoding is fully reversible.
|
|
11
|
+
*
|
|
12
|
+
* This module is deliberately backend-agnostic. A zstd implementation is chosen
|
|
13
|
+
* automatically, in this order:
|
|
14
|
+
*
|
|
15
|
+
* 1. `node:zlib` (Node 22.15+ ships zstd)
|
|
16
|
+
* 2. `fzstd` (optional, decompression only)
|
|
17
|
+
*
|
|
18
|
+
* Set the `JSSHENANIGANS_ZSTD_BACKEND` environment variable (e.g. to "fzstd")
|
|
19
|
+
* to prefer a specific one; the rest stay as fallbacks.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { Buffer } from "node:buffer";
|
|
23
|
+
import { createRequire } from "node:module";
|
|
24
|
+
import zlib from "node:zlib";
|
|
25
|
+
|
|
26
|
+
import { BackendNotFoundError, InvalidCharacterCodeError } from "./errors.js";
|
|
27
|
+
|
|
28
|
+
const require = createRequire(import.meta.url);
|
|
29
|
+
|
|
30
|
+
/** Every zstd frame begins with these four bytes. */
|
|
31
|
+
export const ZSTD_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd]);
|
|
32
|
+
|
|
33
|
+
/** Environment variable used to prefer a specific zstd backend. */
|
|
34
|
+
export const ENV_BACKEND = "JSSHENANIGANS_ZSTD_BACKEND";
|
|
35
|
+
|
|
36
|
+
// The first four base64 characters of any character code (ZSTD_MAGIC).
|
|
37
|
+
const MAGIC_B64_PREFIX = "KLUv";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @typedef {object} Backend
|
|
41
|
+
* @property {string} name
|
|
42
|
+
* @property {((bytes: Buffer) => Buffer) | null} compress
|
|
43
|
+
* @property {(bytes: Buffer) => Buffer} decompress
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/** @returns {Backend} */
|
|
47
|
+
function loadZlib() {
|
|
48
|
+
if (
|
|
49
|
+
typeof zlib.zstdCompressSync !== "function" ||
|
|
50
|
+
typeof zlib.zstdDecompressSync !== "function"
|
|
51
|
+
) {
|
|
52
|
+
throw new Error("node:zlib has no zstd support (needs Node >= 22.15)");
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
name: "node:zlib",
|
|
56
|
+
compress: (bytes) => zlib.zstdCompressSync(bytes),
|
|
57
|
+
decompress: (bytes) => zlib.zstdDecompressSync(bytes),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @returns {Backend} */
|
|
62
|
+
function loadFzstd() {
|
|
63
|
+
const fzstd = require("fzstd");
|
|
64
|
+
return {
|
|
65
|
+
name: "fzstd",
|
|
66
|
+
compress: null,
|
|
67
|
+
decompress: (bytes) => Buffer.from(fzstd.decompress(bytes)),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @type {ReadonlyArray<[string, () => Backend]>} */
|
|
72
|
+
const BACKENDS = [
|
|
73
|
+
["node:zlib", loadZlib],
|
|
74
|
+
["fzstd", loadFzstd],
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
/** @type {Backend | null} */
|
|
78
|
+
let cachedBackend = null;
|
|
79
|
+
|
|
80
|
+
/** @returns {Backend} */
|
|
81
|
+
function loadBackend() {
|
|
82
|
+
const preferred = (process.env[ENV_BACKEND] ?? "").trim().toLowerCase();
|
|
83
|
+
const candidates = [...BACKENDS];
|
|
84
|
+
if (preferred) {
|
|
85
|
+
candidates.sort((a, b) => Number(a[0] !== preferred) - Number(b[0] !== preferred));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @type {string[]} */
|
|
89
|
+
const problems = [];
|
|
90
|
+
for (const [name, loader] of candidates) {
|
|
91
|
+
try {
|
|
92
|
+
return loader();
|
|
93
|
+
} catch (exc) {
|
|
94
|
+
problems.push(`${name}: ${exc.name ?? "Error"}: ${exc.message ?? exc}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
throw new BackendNotFoundError(
|
|
99
|
+
`no usable zstd backend found (tried ${problems.join(", ")}); ` +
|
|
100
|
+
"use Node 22.15+ or install 'fzstd'",
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @returns {Backend} */
|
|
105
|
+
function backend() {
|
|
106
|
+
if (cachedBackend === null) cachedBackend = loadBackend();
|
|
107
|
+
return cachedBackend;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Return the name of the zstd implementation currently in use. */
|
|
111
|
+
export function backend_name() {
|
|
112
|
+
return backend().name;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** @param {string} code @returns {string} */
|
|
116
|
+
function cleanCode(code) {
|
|
117
|
+
if (typeof code !== "string") {
|
|
118
|
+
throw new InvalidCharacterCodeError(
|
|
119
|
+
`expected a string character code, got ${typeof code}`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
let cleaned = code.replace(/\s+/g, "");
|
|
123
|
+
// Tolerate codes copied with a prefix in front of them (e.g. a "Code: "
|
|
124
|
+
// label) by resyncing on the magic.
|
|
125
|
+
const index = cleaned.indexOf(MAGIC_B64_PREFIX);
|
|
126
|
+
if (index > 0) cleaned = cleaned.slice(index);
|
|
127
|
+
return cleaned;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** @param {string} cleaned @returns {Buffer} */
|
|
131
|
+
function decodeBase64(cleaned) {
|
|
132
|
+
const padded = cleaned + "=".repeat((4 - (cleaned.length % 4)) % 4);
|
|
133
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(padded)) {
|
|
134
|
+
throw new InvalidCharacterCodeError("not valid base64");
|
|
135
|
+
}
|
|
136
|
+
return Buffer.from(padded, "base64");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** @param {string} code @returns {Buffer} */
|
|
140
|
+
function decodeBytes(code) {
|
|
141
|
+
const cleaned = cleanCode(code);
|
|
142
|
+
if (!cleaned) throw new InvalidCharacterCodeError("empty character code");
|
|
143
|
+
|
|
144
|
+
const raw = decodeBase64(cleaned);
|
|
145
|
+
if (!raw.subarray(0, 4).equals(ZSTD_MAGIC)) {
|
|
146
|
+
throw new InvalidCharacterCodeError(
|
|
147
|
+
"decoded payload is not a zstd frame; this does not look like a " +
|
|
148
|
+
"character code",
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let decompressed;
|
|
153
|
+
try {
|
|
154
|
+
decompressed = backend().decompress(raw);
|
|
155
|
+
} catch (exc) {
|
|
156
|
+
throw new InvalidCharacterCodeError(
|
|
157
|
+
`zstd decompression failed: ${exc.message ?? exc}`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
if (!decompressed || decompressed.length === 0) {
|
|
161
|
+
throw new InvalidCharacterCodeError(
|
|
162
|
+
"zstd decompression produced no data; the code is corrupt",
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
return decompressed;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* zstd-compress a JSON string and base64-encode the result.
|
|
170
|
+
* @param {string} rawJson
|
|
171
|
+
* @returns {string}
|
|
172
|
+
*/
|
|
173
|
+
export function compress(rawJson) {
|
|
174
|
+
const active = backend();
|
|
175
|
+
if (typeof active.compress !== "function") {
|
|
176
|
+
throw new BackendNotFoundError(`backend '${active.name}' cannot compress`);
|
|
177
|
+
}
|
|
178
|
+
return active.compress(Buffer.from(rawJson, "utf8")).toString("base64");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Base64-decode `code` and zstd-decompress it back into a JSON string.
|
|
183
|
+
* @param {string} code
|
|
184
|
+
* @returns {string}
|
|
185
|
+
*/
|
|
186
|
+
export function decompress(code) {
|
|
187
|
+
return decodeBytes(code).toString("utf8");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Serialise a character payload (an array of skills) into a character code.
|
|
192
|
+
* @param {unknown} payload
|
|
193
|
+
* @returns {string}
|
|
194
|
+
*/
|
|
195
|
+
export function encode(payload) {
|
|
196
|
+
return compress(JSON.stringify(payload));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Decode a character code back into JavaScript objects.
|
|
201
|
+
* @param {string} code
|
|
202
|
+
* @returns {unknown}
|
|
203
|
+
*/
|
|
204
|
+
export function decode(code) {
|
|
205
|
+
const json = decompress(code);
|
|
206
|
+
try {
|
|
207
|
+
return JSON.parse(json);
|
|
208
|
+
} catch (exc) {
|
|
209
|
+
throw new InvalidCharacterCodeError(`payload is not valid JSON: ${exc.message ?? exc}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export interface Condition {
|
|
2
|
+
K_NAME: string;
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface FlipOptions {
|
|
7
|
+
/** If false the check must pass; if true it must not. */
|
|
8
|
+
flip?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Check if the player is in the air. */
|
|
12
|
+
export declare function IN_AIR(options?: FlipOptions): Condition;
|
|
13
|
+
|
|
14
|
+
/** Check if the player is jumping. */
|
|
15
|
+
export declare function IS_JUMPING(options?: FlipOptions): Condition;
|
|
16
|
+
|
|
17
|
+
/** Check if the player is hovering their cursor over another player. */
|
|
18
|
+
export declare function HAS_TARGET(options?: FlipOptions): Condition;
|
|
19
|
+
|
|
20
|
+
/** Check if the player has their awakening. */
|
|
21
|
+
export declare function IS_AWAKENED(options?: FlipOptions): Condition;
|
|
22
|
+
|
|
23
|
+
/** Check how much of the awakening bar the player has. */
|
|
24
|
+
export declare function HAS_AWK_BAR(
|
|
25
|
+
options?: FlipOptions & { amount?: number },
|
|
26
|
+
): Condition;
|
|
27
|
+
|
|
28
|
+
/** Check how much durability the current move has. */
|
|
29
|
+
export declare function DURABILITY(options?: { durability?: number }): Condition;
|
|
30
|
+
|
|
31
|
+
/** Check how much health the player has. */
|
|
32
|
+
export declare function HAS_HEALTH(
|
|
33
|
+
options?: FlipOptions & { amount?: number },
|
|
34
|
+
): Condition;
|
|
35
|
+
|
|
36
|
+
/** Check if the player is in a domain. */
|
|
37
|
+
export declare function IS_IN_DOMAIN(options?: FlipOptions): Condition;
|
|
38
|
+
|
|
39
|
+
/** Check if the player is holding the skill key. */
|
|
40
|
+
export declare function IS_HOLDING(options?: FlipOptions): Condition;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Condition builders.
|
|
3
|
+
*
|
|
4
|
+
* Every builder takes an options object and returns a plain condition object.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Check if the player is in the air.
|
|
9
|
+
* @param {object} [options]
|
|
10
|
+
* @param {boolean} [options.flip] If false the player must be in the air; if true they must not be.
|
|
11
|
+
*/
|
|
12
|
+
export function IN_AIR({ flip = false } = {}) {
|
|
13
|
+
return { K_NAME: "AIR", FLIP: flip };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Check if the player is jumping.
|
|
18
|
+
* @param {object} [options]
|
|
19
|
+
*/
|
|
20
|
+
export function IS_JUMPING({ flip = false } = {}) {
|
|
21
|
+
return { K_NAME: "JUMP", FLIP: flip };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Check if the player is hovering their cursor over another player.
|
|
26
|
+
* @param {object} [options]
|
|
27
|
+
*/
|
|
28
|
+
export function HAS_TARGET({ flip = false } = {}) {
|
|
29
|
+
return { K_NAME: "AIM", FLIP: flip };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Check if the player has their awakening.
|
|
34
|
+
* @param {object} [options]
|
|
35
|
+
*/
|
|
36
|
+
export function IS_AWAKENED({ flip = false } = {}) {
|
|
37
|
+
return { K_NAME: "ULT", FLIP: flip };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Check how much of the awakening bar the player has.
|
|
42
|
+
* @param {object} [options]
|
|
43
|
+
* @param {number} [options.amount] Percentage to check. Defaults to 5.
|
|
44
|
+
*/
|
|
45
|
+
export function HAS_AWK_BAR({ amount = 5, flip = false } = {}) {
|
|
46
|
+
return { K_NAME: "BAR", AMOUNT: amount, FLIP: flip };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Check how much durability the current move has.
|
|
51
|
+
* @param {object} [options]
|
|
52
|
+
*/
|
|
53
|
+
export function DURABILITY({ durability = 1 } = {}) {
|
|
54
|
+
return { K_NAME: "DUR", DURABILITY: durability };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Check how much health the player has.
|
|
59
|
+
* @param {object} [options]
|
|
60
|
+
*/
|
|
61
|
+
export function HAS_HEALTH({ amount = 1, flip = false } = {}) {
|
|
62
|
+
return { K_NAME: "HP", AMOUNT: amount, FLIP: flip };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Check if the player is in a domain.
|
|
67
|
+
* @param {object} [options]
|
|
68
|
+
*/
|
|
69
|
+
export function IS_IN_DOMAIN({ flip = false } = {}) {
|
|
70
|
+
return { K_NAME: "DOMAIN", FLIP: flip };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Check if the player is holding the skill key.
|
|
75
|
+
* @param {object} [options]
|
|
76
|
+
*/
|
|
77
|
+
export function IS_HOLDING({ flip = false } = {}) {
|
|
78
|
+
return { K_NAME: "HOLD", FLIP: flip };
|
|
79
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Node } from "./nodes.js";
|
|
2
|
+
|
|
3
|
+
/** Node K_NAME -> builder name in `nodes`. */
|
|
4
|
+
export declare const _NODE_BUILDERS: Readonly<Record<string, string>>;
|
|
5
|
+
|
|
6
|
+
/** Default node objects, derived from the builders themselves. */
|
|
7
|
+
export declare const NODE_DEFAULTS: Readonly<Record<string, Node>>;
|
|
8
|
+
|
|
9
|
+
export declare const PROPERTIES_DEFAULTS: Readonly<
|
|
10
|
+
Record<string, number | boolean | string>
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
export declare const CONDITIONS_DEFAULTS: Readonly<
|
|
14
|
+
Record<string, Record<string, number | boolean>>
|
|
15
|
+
>;
|