dredless 0.2.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 +21 -0
- package/README.md +48 -0
- package/docs/API_REFERENCE.md +2481 -0
- package/docs/BROWSER_API.md +98 -0
- package/docs/NODE_API.md +359 -0
- package/package.json +173 -0
- package/src/browser.d.ts +212 -0
- package/src/browser.js +133 -0
- package/src/client/core.js +1202 -0
- package/src/client/domains/index.js +1818 -0
- package/src/client/index.d.ts +2 -0
- package/src/client/index.js +1 -0
- package/src/compression/lz4.d.ts +1 -0
- package/src/compression/lz4.js +112 -0
- package/src/constants.js +74 -0
- package/src/crypto/chacha-native.d.ts +9 -0
- package/src/crypto/chacha-native.js +48 -0
- package/src/crypto/chacha.d.ts +25 -0
- package/src/crypto/chacha.js +274 -0
- package/src/data/item-schema.js +2235 -0
- package/src/data/official-client-data.js +156 -0
- package/src/events.js +39 -0
- package/src/index.d.ts +2277 -0
- package/src/index.js +125 -0
- package/src/network/browser-session.js +291 -0
- package/src/network/connection.d.ts +1 -0
- package/src/network/connection.js +31 -0
- package/src/network/cookies.js +85 -0
- package/src/network/http.js +34 -0
- package/src/network/index.d.ts +4 -0
- package/src/network/index.js +4 -0
- package/src/network/servers.d.ts +7 -0
- package/src/network/servers.js +133 -0
- package/src/network/session.d.ts +7 -0
- package/src/network/session.js +320 -0
- package/src/network/ships.d.ts +12 -0
- package/src/network/ships.js +73 -0
- package/src/node.d.ts +2 -0
- package/src/node.js +8 -0
- package/src/protocol/codec/binary.d.ts +3 -0
- package/src/protocol/codec/binary.js +52 -0
- package/src/protocol/codec/msgpack.d.ts +3 -0
- package/src/protocol/codec/msgpack.js +555 -0
- package/src/protocol/inbound/frame.d.ts +1 -0
- package/src/protocol/inbound/frame.js +8 -0
- package/src/protocol/inbound/world-payload.d.ts +3 -0
- package/src/protocol/inbound/world-payload.js +21 -0
- package/src/protocol/index.d.ts +11 -0
- package/src/protocol/index.js +19 -0
- package/src/protocol/outbound/blueprint.d.ts +22 -0
- package/src/protocol/outbound/blueprint.js +36 -0
- package/src/protocol/outbound/commands.d.ts +3 -0
- package/src/protocol/outbound/commands.js +149 -0
- package/src/protocol/outbound/comms.d.ts +28 -0
- package/src/protocol/outbound/comms.js +40 -0
- package/src/protocol/outbound/inventory.d.ts +64 -0
- package/src/protocol/outbound/inventory.js +147 -0
- package/src/protocol/outbound/ship-management.d.ts +84 -0
- package/src/protocol/outbound/ship-management.js +221 -0
- package/src/protocol/outbound/sign.d.ts +18 -0
- package/src/protocol/outbound/sign.js +45 -0
- package/src/protocol/outbound/ui-config.d.ts +157 -0
- package/src/protocol/outbound/ui-config.js +578 -0
- package/src/runtime.js +36 -0
- package/src/state/generator-maze.d.ts +32 -0
- package/src/state/generator-maze.js +370 -0
- package/src/state/index.d.ts +3 -0
- package/src/state/index.js +3 -0
- package/src/state/items.js +20 -0
- package/src/state/model/decoder.js +1554 -0
- package/src/state/model/index.d.ts +33 -0
- package/src/state/model/index.js +2 -0
- package/src/state/model/loader-config.js +1020 -0
- package/src/state/model/reader.js +69 -0
- package/src/state/model/state.js +1257 -0
- package/src/state/overworld.js +72 -0
- package/src/state/tilesets.js +53 -0
- package/src/state/world/index.d.ts +29 -0
- package/src/state/world/index.js +180 -0
- package/src/state/world/state.js +297 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import {COMMAND_DEFAULT_FORMATS, COMMAND_FIELDS, encoder} from "../../constants.js";
|
|
2
|
+
import {concatBytes, toUint8Array} from "../codec/binary.js";
|
|
3
|
+
import {cloneDragValue, encodeMapHeader, encodeStringBytes, encodeValue} from "../codec/msgpack.js";
|
|
4
|
+
|
|
5
|
+
const SHA256_K = new Uint32Array([
|
|
6
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
7
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
8
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
9
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
10
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
11
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
12
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
13
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
function buildCommandDefaults(command = {}) {
|
|
17
|
+
const out = {};
|
|
18
|
+
for (const key of COMMAND_FIELDS) {
|
|
19
|
+
if (key === "drag") {
|
|
20
|
+
out[key] = cloneDragValue(command[key]);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (command[key] !== undefined) out[key] = command[key];
|
|
24
|
+
else if (COMMAND_DEFAULT_FORMATS[key] === "nil") out[key] = null;
|
|
25
|
+
else if (COMMAND_DEFAULT_FORMATS[key] === "bool") out[key] = false;
|
|
26
|
+
else if (COMMAND_DEFAULT_FORMATS[key] === "float32") out[key] = 0;
|
|
27
|
+
else out[key] = 0;
|
|
28
|
+
}
|
|
29
|
+
out.type = command.type ?? 0;
|
|
30
|
+
out.n = command.n ?? null;
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Field names and the map header are identical for every command, so encode
|
|
35
|
+
// them once instead of on each send.
|
|
36
|
+
const COMMAND_MAP_HEADER = encodeMapHeader(COMMAND_FIELDS.length);
|
|
37
|
+
const COMMAND_FIELD_KEY_BYTES = COMMAND_FIELDS.map((key) => encodeStringBytes(key));
|
|
38
|
+
|
|
39
|
+
function buildCommandBody(command) {
|
|
40
|
+
const normalized = buildCommandDefaults(command);
|
|
41
|
+
const parts = [COMMAND_MAP_HEADER];
|
|
42
|
+
for (let i = 0; i < COMMAND_FIELDS.length; i++) {
|
|
43
|
+
parts.push(COMMAND_FIELD_KEY_BYTES[i]);
|
|
44
|
+
parts.push(encodeValue(COMMAND_FIELDS[i], normalized[COMMAND_FIELDS[i]], null));
|
|
45
|
+
}
|
|
46
|
+
return concatBytes(parts);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The prefix depends only on the session id, which is fixed for a connection.
|
|
50
|
+
let signaturePrefixSid = null;
|
|
51
|
+
let signaturePrefixBytes = null;
|
|
52
|
+
|
|
53
|
+
function buildSignaturePrefix(sessionId) {
|
|
54
|
+
const sid = Number(sessionId) >>> 0;
|
|
55
|
+
if (sid === signaturePrefixSid && signaturePrefixBytes) return signaturePrefixBytes;
|
|
56
|
+
const a = (sid ^ 52481) >>> 0;
|
|
57
|
+
const b = ((sid << 2) + 69) >>> 0;
|
|
58
|
+
const c = ((((sid >>> 1) * 5) >>> 0) + 420) >>> 0;
|
|
59
|
+
signaturePrefixBytes = encoder.encode(`MJAF${a}CANH${b}SCLJ${c}LODV`);
|
|
60
|
+
signaturePrefixSid = sid;
|
|
61
|
+
return signaturePrefixBytes;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function rightRotate(value, bits) {
|
|
65
|
+
return (value >>> bits) | (value << (32 - bits));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function sha256(bytes) {
|
|
69
|
+
const input = toUint8Array(bytes);
|
|
70
|
+
const bitLength = input.length * 8;
|
|
71
|
+
const paddingLength = ((56 - ((input.length + 1) % 64)) + 64) % 64;
|
|
72
|
+
const padded = new Uint8Array(input.length + 1 + paddingLength + 8);
|
|
73
|
+
padded.set(input, 0);
|
|
74
|
+
padded[input.length] = 0x80;
|
|
75
|
+
const view = new DataView(padded.buffer);
|
|
76
|
+
const high = Math.floor(bitLength / 0x100000000);
|
|
77
|
+
const low = bitLength >>> 0;
|
|
78
|
+
view.setUint32(padded.length - 8, high, false);
|
|
79
|
+
view.setUint32(padded.length - 4, low, false);
|
|
80
|
+
|
|
81
|
+
const state = new Uint32Array([
|
|
82
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
83
|
+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
|
84
|
+
]);
|
|
85
|
+
const schedule = new Uint32Array(64);
|
|
86
|
+
|
|
87
|
+
for (let offset = 0; offset < padded.length; offset += 64) {
|
|
88
|
+
for (let index = 0; index < 16; index++) schedule[index] = view.getUint32(offset + index * 4, false);
|
|
89
|
+
for (let index = 16; index < 64; index++) {
|
|
90
|
+
const s0 = rightRotate(schedule[index - 15], 7) ^ rightRotate(schedule[index - 15], 18) ^ (schedule[index - 15] >>> 3);
|
|
91
|
+
const s1 = rightRotate(schedule[index - 2], 17) ^ rightRotate(schedule[index - 2], 19) ^ (schedule[index - 2] >>> 10);
|
|
92
|
+
schedule[index] = (((schedule[index - 16] + s0) >>> 0) + ((schedule[index - 7] + s1) >>> 0)) >>> 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let a = state[0], b = state[1], c = state[2], d = state[3], e = state[4], f = state[5], g = state[6],
|
|
96
|
+
h = state[7];
|
|
97
|
+
for (let index = 0; index < 64; index++) {
|
|
98
|
+
const S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
|
|
99
|
+
const ch = (e & f) ^ (~e & g);
|
|
100
|
+
const temp1 = (((((h + S1) >>> 0) + ch) >>> 0) + ((SHA256_K[index] + schedule[index]) >>> 0)) >>> 0;
|
|
101
|
+
const S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
|
|
102
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
103
|
+
const temp2 = (S0 + maj) >>> 0;
|
|
104
|
+
h = g;
|
|
105
|
+
g = f;
|
|
106
|
+
f = e;
|
|
107
|
+
e = (d + temp1) >>> 0;
|
|
108
|
+
d = c;
|
|
109
|
+
c = b;
|
|
110
|
+
b = a;
|
|
111
|
+
a = (temp1 + temp2) >>> 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
state[0] = (state[0] + a) >>> 0;
|
|
115
|
+
state[1] = (state[1] + b) >>> 0;
|
|
116
|
+
state[2] = (state[2] + c) >>> 0;
|
|
117
|
+
state[3] = (state[3] + d) >>> 0;
|
|
118
|
+
state[4] = (state[4] + e) >>> 0;
|
|
119
|
+
state[5] = (state[5] + f) >>> 0;
|
|
120
|
+
state[6] = (state[6] + g) >>> 0;
|
|
121
|
+
state[7] = (state[7] + h) >>> 0;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const out = new Uint8Array(32);
|
|
125
|
+
const outView = new DataView(out.buffer);
|
|
126
|
+
for (let i = 0; i < 8; i++) outView.setUint32(i * 4, state[i], false);
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function signCommandBody(body, sessionId) {
|
|
131
|
+
return sha256(concatBytes([buildSignaturePrefix(sessionId), body, encoder.encode("YXTG")]));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function buildSignedCommandPacket(command, sessionId) {
|
|
135
|
+
const body = buildCommandBody(command);
|
|
136
|
+
const signature = signCommandBody(body, sessionId);
|
|
137
|
+
return concatBytes([body, signature]);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
export {
|
|
142
|
+
buildCommandDefaults,
|
|
143
|
+
buildCommandBody,
|
|
144
|
+
buildSignaturePrefix,
|
|
145
|
+
rightRotate,
|
|
146
|
+
sha256,
|
|
147
|
+
signCommandBody,
|
|
148
|
+
buildSignedCommandPacket
|
|
149
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface CommsMessage {
|
|
2
|
+
type: 3;
|
|
3
|
+
msg: string;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface NormalizedCommsMessage {
|
|
7
|
+
raw: unknown;
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface CommsEvent {
|
|
12
|
+
type: "comms";
|
|
13
|
+
filter?: number;
|
|
14
|
+
ent_id: number | null;
|
|
15
|
+
entity: number | null;
|
|
16
|
+
msgs_text: unknown[];
|
|
17
|
+
rawMessages: unknown[];
|
|
18
|
+
messages: NormalizedCommsMessage[];
|
|
19
|
+
update?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function buildCommsMessage(message?: string): CommsMessage;
|
|
23
|
+
|
|
24
|
+
export function normalizeCommsEvent(event: unknown): CommsEvent;
|
|
25
|
+
|
|
26
|
+
export function flattenRichText(value: unknown): string;
|
|
27
|
+
|
|
28
|
+
export const COMMS_MESSAGE_TYPE: 3;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const COMMS_MESSAGE_TYPE = 3;
|
|
2
|
+
|
|
3
|
+
function buildCommsMessage(message = "") {
|
|
4
|
+
return {
|
|
5
|
+
type: COMMS_MESSAGE_TYPE,
|
|
6
|
+
msg: String(message)
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function normalizeCommsEvent(event) {
|
|
11
|
+
const rawMessages = Array.isArray(event?.msgs_text) ? event.msgs_text : [];
|
|
12
|
+
return {
|
|
13
|
+
...event,
|
|
14
|
+
entity: event?.ent_id != null && Number.isFinite(Number(event.ent_id)) ? Number(event.ent_id) : null,
|
|
15
|
+
rawMessages,
|
|
16
|
+
messages: rawMessages.map((message) => ({
|
|
17
|
+
raw: message,
|
|
18
|
+
text: flattenRichText(message)
|
|
19
|
+
}))
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function flattenRichText(value) {
|
|
24
|
+
if (value == null) return "";
|
|
25
|
+
if (typeof value === "string") return value;
|
|
26
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
27
|
+
if (Array.isArray(value)) return value.map(flattenRichText).join("");
|
|
28
|
+
if (typeof value === "object") {
|
|
29
|
+
if (Object.hasOwn(value, "c")) return flattenRichText(value.c);
|
|
30
|
+
if (Object.hasOwn(value, "text")) return flattenRichText(value.text);
|
|
31
|
+
}
|
|
32
|
+
return "";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export {
|
|
36
|
+
COMMS_MESSAGE_TYPE,
|
|
37
|
+
buildCommsMessage,
|
|
38
|
+
flattenRichText,
|
|
39
|
+
normalizeCommsEvent
|
|
40
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export const EquipmentSlot: {
|
|
2
|
+
readonly Head: "head";
|
|
3
|
+
readonly Face: "face";
|
|
4
|
+
readonly Body: "body";
|
|
5
|
+
readonly Back: "back";
|
|
6
|
+
readonly Hands: "hands";
|
|
7
|
+
readonly Feet: "feet";
|
|
8
|
+
};
|
|
9
|
+
export type EquipmentSlot = typeof EquipmentSlot[keyof typeof EquipmentSlot];
|
|
10
|
+
|
|
11
|
+
export interface InventoryDragCommand {
|
|
12
|
+
drag: {
|
|
13
|
+
source: number;
|
|
14
|
+
target: number;
|
|
15
|
+
split: boolean;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface InventorySlotSnapshot {
|
|
20
|
+
index: number;
|
|
21
|
+
itemId: number | null;
|
|
22
|
+
itemName: string | null;
|
|
23
|
+
count: number;
|
|
24
|
+
kind: "hotbar" | "equipment";
|
|
25
|
+
equipmentSlot: "head" | "face" | "body" | "back" | "hands" | "feet" | null;
|
|
26
|
+
empty: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface InventoryState {
|
|
30
|
+
type: "inventory";
|
|
31
|
+
filter?: number;
|
|
32
|
+
hotbarSize: number;
|
|
33
|
+
slots: InventorySlotSnapshot[];
|
|
34
|
+
hotbar: InventorySlotSnapshot[];
|
|
35
|
+
equipment: {
|
|
36
|
+
head: InventorySlotSnapshot;
|
|
37
|
+
face: InventorySlotSnapshot;
|
|
38
|
+
body: InventorySlotSnapshot;
|
|
39
|
+
back: InventorySlotSnapshot;
|
|
40
|
+
hands: InventorySlotSnapshot;
|
|
41
|
+
feet: InventorySlotSnapshot;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function normalizeEquipmentSlot(slot: EquipmentSlot): 16 | 17 | 18 | 19 | 20 | 21;
|
|
46
|
+
|
|
47
|
+
export function equipmentSlotName(slot: number): "head" | "face" | "body" | "back" | "hands" | "feet" | null;
|
|
48
|
+
|
|
49
|
+
export function buildInventoryDragCommand(source: number, target: number, split?: boolean): InventoryDragCommand;
|
|
50
|
+
|
|
51
|
+
export function buildEquipItemCommand(source: number, slot: EquipmentSlot, split?: boolean): InventoryDragCommand;
|
|
52
|
+
|
|
53
|
+
export function buildUnequipItemCommand(slot: EquipmentSlot, target?: number, split?: boolean): InventoryDragCommand;
|
|
54
|
+
|
|
55
|
+
export function normalizeInventoryEvent(event: unknown): InventoryState;
|
|
56
|
+
|
|
57
|
+
export const EQUIPMENT_SLOT_INDEXES: Readonly<{
|
|
58
|
+
head: 16;
|
|
59
|
+
face: 17;
|
|
60
|
+
body: 18;
|
|
61
|
+
back: 19;
|
|
62
|
+
hands: 20;
|
|
63
|
+
feet: 21;
|
|
64
|
+
}>;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import {itemNameFromId} from "../../state/items.js";
|
|
2
|
+
|
|
3
|
+
export const EquipmentSlot = Object.freeze({
|
|
4
|
+
Head: "head",
|
|
5
|
+
Face: "face",
|
|
6
|
+
Body: "body",
|
|
7
|
+
Back: "back",
|
|
8
|
+
Hands: "hands",
|
|
9
|
+
Feet: "feet"
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const EQUIPMENT_SLOT_INDEXES = Object.freeze({
|
|
13
|
+
[EquipmentSlot.Head]: 16,
|
|
14
|
+
[EquipmentSlot.Face]: 17,
|
|
15
|
+
[EquipmentSlot.Body]: 18,
|
|
16
|
+
[EquipmentSlot.Back]: 19,
|
|
17
|
+
[EquipmentSlot.Hands]: 20,
|
|
18
|
+
[EquipmentSlot.Feet]: 21
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const EQUIPMENT_SLOT_NAMES = new Map([
|
|
22
|
+
[16, EquipmentSlot.Head],
|
|
23
|
+
[17, EquipmentSlot.Face],
|
|
24
|
+
[18, EquipmentSlot.Body],
|
|
25
|
+
[19, EquipmentSlot.Back],
|
|
26
|
+
[20, EquipmentSlot.Hands],
|
|
27
|
+
[21, EquipmentSlot.Feet]
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
const EQUIPMENT_SLOT_VALUES = new Map([
|
|
31
|
+
[EquipmentSlot.Head, 16],
|
|
32
|
+
[EquipmentSlot.Face, 17],
|
|
33
|
+
[EquipmentSlot.Body, 18],
|
|
34
|
+
[EquipmentSlot.Back, 19],
|
|
35
|
+
[EquipmentSlot.Hands, 20],
|
|
36
|
+
[EquipmentSlot.Feet, 21]
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
function normalizeEquipmentSlot(slot) {
|
|
40
|
+
const normalized = EQUIPMENT_SLOT_VALUES.get(slot);
|
|
41
|
+
if (!EQUIPMENT_SLOT_NAMES.has(normalized)) {
|
|
42
|
+
throw new RangeError(`equipment slot must be an EquipmentSlot value`);
|
|
43
|
+
}
|
|
44
|
+
return normalized;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function equipmentSlotName(slot) {
|
|
48
|
+
return EQUIPMENT_SLOT_NAMES.get(Number(slot)) ?? null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function buildInventoryDragCommand(source, target, split = false) {
|
|
52
|
+
return {
|
|
53
|
+
drag: {
|
|
54
|
+
source: normalizeInventoryIndex(source, "source"),
|
|
55
|
+
target: normalizeInventoryIndex(target, "target"),
|
|
56
|
+
split: Boolean(split)
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function buildEquipItemCommand(source, slot, split = false) {
|
|
62
|
+
return buildInventoryDragCommand(source, normalizeEquipmentSlot(slot), split);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function buildUnequipItemCommand(slot, target = 0, split = false) {
|
|
66
|
+
return buildInventoryDragCommand(normalizeEquipmentSlot(slot), target, split);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function normalizeInventoryEvent(event) {
|
|
70
|
+
const items = Array.isArray(event?.items) ? event.items : [];
|
|
71
|
+
const counts = Array.isArray(event?.item_counts) ? event.item_counts : [];
|
|
72
|
+
const rawHotbarSize = Number(event?.general_slots ?? 0);
|
|
73
|
+
const hotbarSize = Number.isFinite(rawHotbarSize) && rawHotbarSize > 0 ? Math.trunc(rawHotbarSize) : 0;
|
|
74
|
+
|
|
75
|
+
const slotSnapshot = (index, kind) => {
|
|
76
|
+
const count = normalizeCount(counts[index]);
|
|
77
|
+
const itemId = normalizeItemId(items[index], count);
|
|
78
|
+
return {
|
|
79
|
+
index,
|
|
80
|
+
itemId,
|
|
81
|
+
itemName: itemNameFromId(itemId),
|
|
82
|
+
count,
|
|
83
|
+
kind,
|
|
84
|
+
equipmentSlot: equipmentSlotName(index),
|
|
85
|
+
empty: itemId == null || count <= 0
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const slotsByIndex = new Map();
|
|
90
|
+
for (let index = 0; index < hotbarSize; index++) {
|
|
91
|
+
slotsByIndex.set(index, slotSnapshot(index, "hotbar"));
|
|
92
|
+
}
|
|
93
|
+
slotsByIndex.set(EQUIPMENT_SLOT_INDEXES.head, slotSnapshot(EQUIPMENT_SLOT_INDEXES.head, "equipment"));
|
|
94
|
+
slotsByIndex.set(EQUIPMENT_SLOT_INDEXES.face, slotSnapshot(EQUIPMENT_SLOT_INDEXES.face, "equipment"));
|
|
95
|
+
slotsByIndex.set(EQUIPMENT_SLOT_INDEXES.body, slotSnapshot(EQUIPMENT_SLOT_INDEXES.body, "equipment"));
|
|
96
|
+
slotsByIndex.set(EQUIPMENT_SLOT_INDEXES.back, slotSnapshot(EQUIPMENT_SLOT_INDEXES.back, "equipment"));
|
|
97
|
+
slotsByIndex.set(EQUIPMENT_SLOT_INDEXES.hands, slotSnapshot(EQUIPMENT_SLOT_INDEXES.hands, "equipment"));
|
|
98
|
+
slotsByIndex.set(EQUIPMENT_SLOT_INDEXES.feet, slotSnapshot(EQUIPMENT_SLOT_INDEXES.feet, "equipment"));
|
|
99
|
+
|
|
100
|
+
const slots = [...slotsByIndex.values()].sort((a, b) => a.index - b.index);
|
|
101
|
+
const hotbar = slots.filter((slot) => slot.kind === "hotbar");
|
|
102
|
+
const equipment = {
|
|
103
|
+
head: slotsByIndex.get(EQUIPMENT_SLOT_INDEXES.head),
|
|
104
|
+
face: slotsByIndex.get(EQUIPMENT_SLOT_INDEXES.face),
|
|
105
|
+
body: slotsByIndex.get(EQUIPMENT_SLOT_INDEXES.body),
|
|
106
|
+
back: slotsByIndex.get(EQUIPMENT_SLOT_INDEXES.back),
|
|
107
|
+
hands: slotsByIndex.get(EQUIPMENT_SLOT_INDEXES.hands),
|
|
108
|
+
feet: slotsByIndex.get(EQUIPMENT_SLOT_INDEXES.feet)
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
type: "inventory",
|
|
113
|
+
filter: event?.filter,
|
|
114
|
+
hotbarSize,
|
|
115
|
+
slots,
|
|
116
|
+
hotbar,
|
|
117
|
+
equipment
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function normalizeInventoryIndex(value, name) {
|
|
122
|
+
const number = Number(value);
|
|
123
|
+
if (!Number.isInteger(number) || number < 0) throw new RangeError(`${name} must be a non-negative integer inventory slot`);
|
|
124
|
+
return number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function normalizeCount(value) {
|
|
128
|
+
const number = Number(value ?? 0);
|
|
129
|
+
return Number.isFinite(number) ? number : 0;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function normalizeItemId(value, count) {
|
|
133
|
+
if (value == null) return null;
|
|
134
|
+
const number = Number(value);
|
|
135
|
+
if (!Number.isFinite(number)) return null;
|
|
136
|
+
return number === 0 && count === 0 ? null : number;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export {
|
|
140
|
+
EQUIPMENT_SLOT_INDEXES,
|
|
141
|
+
buildEquipItemCommand,
|
|
142
|
+
buildInventoryDragCommand,
|
|
143
|
+
buildUnequipItemCommand,
|
|
144
|
+
equipmentSlotName,
|
|
145
|
+
normalizeEquipmentSlot,
|
|
146
|
+
normalizeInventoryEvent
|
|
147
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type ShipPrivacy = 0 | 1 | boolean | "public" | "private";
|
|
2
|
+
export type ShipPlayerRank = 0 | 1 | 3 | "guest" | "crew" | "captain";
|
|
3
|
+
|
|
4
|
+
export interface ShipManagementMessage {
|
|
5
|
+
type: 4;
|
|
6
|
+
act: string;
|
|
7
|
+
arg: unknown;
|
|
8
|
+
rank?: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ShipConfig {
|
|
12
|
+
privacy: number | null;
|
|
13
|
+
privacyName: "public" | "private" | null;
|
|
14
|
+
inviteKey: string | null;
|
|
15
|
+
teamId: number | null;
|
|
16
|
+
patronPerks: unknown[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CaptainSubrank {
|
|
20
|
+
subrank: number | null;
|
|
21
|
+
enableCheats: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PlayerListEntry {
|
|
25
|
+
refId: number | null;
|
|
26
|
+
discrim: string | null;
|
|
27
|
+
discrimColor: number | null;
|
|
28
|
+
teamRank: number | null;
|
|
29
|
+
captainRank: number | null;
|
|
30
|
+
isCaptain: boolean;
|
|
31
|
+
isShipOwner: boolean;
|
|
32
|
+
canBeManaged: boolean;
|
|
33
|
+
time: number | null;
|
|
34
|
+
items: unknown[];
|
|
35
|
+
aliasDiscrims: unknown[];
|
|
36
|
+
extraAliasCount: number;
|
|
37
|
+
onlineCount: number | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ShipPlayerList {
|
|
41
|
+
ownerCaptainRank: number | null;
|
|
42
|
+
shipOwners: PlayerListEntry[];
|
|
43
|
+
players: PlayerListEntry[];
|
|
44
|
+
changes: PlayerListEntry[];
|
|
45
|
+
removedPlayers: number[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function buildShipManagementMessage(act: string, arg?: unknown, extra?: Record<string, unknown> | null): ShipManagementMessage;
|
|
49
|
+
|
|
50
|
+
export function buildShipPrivacyMessage(privacy: ShipPrivacy): ShipManagementMessage;
|
|
51
|
+
|
|
52
|
+
export function buildStarterRecoveryMessage(itemId: number): ShipManagementMessage;
|
|
53
|
+
|
|
54
|
+
export function buildPlayerListMessage(): ShipManagementMessage;
|
|
55
|
+
|
|
56
|
+
export function buildInviteResetMessage(): ShipManagementMessage;
|
|
57
|
+
|
|
58
|
+
export function buildSetPlayerRankMessage(refId: number, rank: ShipPlayerRank): ShipManagementMessage;
|
|
59
|
+
|
|
60
|
+
export function buildKickPlayerMessage(refId: number): ShipManagementMessage;
|
|
61
|
+
|
|
62
|
+
export function buildBanPlayerMessage(refId: number): ShipManagementMessage;
|
|
63
|
+
|
|
64
|
+
export function buildDemoteSelfMessage(): ShipManagementMessage;
|
|
65
|
+
|
|
66
|
+
export function normalizePrivacy(privacy: ShipPrivacy): 0 | 1;
|
|
67
|
+
|
|
68
|
+
export function normalizePlayerRank(rank: ShipPlayerRank): 0 | 1 | 3;
|
|
69
|
+
|
|
70
|
+
export function normalizeShipConfig(event: unknown): ShipConfig;
|
|
71
|
+
|
|
72
|
+
export function normalizeCaptainSubrank(event: unknown): CaptainSubrank;
|
|
73
|
+
|
|
74
|
+
export function normalizeShipPlayerList(event: unknown, previous?: ShipPlayerList | null, currentCaptainSubrank?: CaptainSubrank | null): ShipPlayerList;
|
|
75
|
+
|
|
76
|
+
export const BAN_ACTION: "ban";
|
|
77
|
+
export const DEMOTE_SELF_ACTION: "demote_self";
|
|
78
|
+
export const KICK_ACTION: "kick";
|
|
79
|
+
export const PLAYER_LIST_ACTION: "player_list";
|
|
80
|
+
export const INVITE_RESET_ACTION: "invite_reset";
|
|
81
|
+
export const SET_PRIVACY_ACTION: "set_privacy";
|
|
82
|
+
export const SET_RANK_ACTION: "set_rank";
|
|
83
|
+
export const SHIP_MANAGEMENT_TYPE: 4;
|
|
84
|
+
export const STARTER_RECOVERY_ACTION: "starter_recovery";
|