@hadoku/platform-wire 0.1.265 → 0.1.266
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/dist/random.d.ts +20 -0
- package/dist/random.js +45 -0
- package/package.json +2 -2
package/dist/random.d.ts
CHANGED
|
@@ -11,3 +11,23 @@ export declare function secureRandom(): number;
|
|
|
11
11
|
* rather than sampled and hoped about.
|
|
12
12
|
*/
|
|
13
13
|
export declare function pickRandom<T>(items: readonly T[], rand?: () => number): T | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* A string as a 32-bit number (FNV-1a), the same on every run and in every
|
|
16
|
+
* runtime: what a seeded draw is keyed by, so a draft, a room's bot faces or a
|
|
17
|
+
* match's music comes out the same from the same id with nothing stored.
|
|
18
|
+
*/
|
|
19
|
+
export declare function hashSeed(seed: string): number;
|
|
20
|
+
/**
|
|
21
|
+
* A deterministic [0,1) from a 32-bit state (mulberry32: one multiply-xorshift
|
|
22
|
+
* round). Not cryptographic and not trying to be: it is portable and identical
|
|
23
|
+
* in Node and the browser, so a seed reproduces the same draws anywhere.
|
|
24
|
+
*/
|
|
25
|
+
export declare function mulberry32(state: number): () => number;
|
|
26
|
+
/** `items` in `rand`'s order (Fisher–Yates), as a new array: the caller's list is never mutated. */
|
|
27
|
+
export declare function shuffle<T>(items: readonly T[], rand: () => number): T[];
|
|
28
|
+
/**
|
|
29
|
+
* One of `items`, never `last` again while there is anything else to pick: a
|
|
30
|
+
* cell of several takes varies and never plays the same one twice in a row.
|
|
31
|
+
* Null when there are none.
|
|
32
|
+
*/
|
|
33
|
+
export declare function pickNotLast<T>(items: readonly T[], last: T | null, rand?: () => number): T | null;
|
package/dist/random.js
CHANGED
|
@@ -19,3 +19,48 @@ export function pickRandom(items, rand = secureRandom) {
|
|
|
19
19
|
return undefined;
|
|
20
20
|
return items[Math.min(items.length - 1, Math.floor(rand() * items.length))];
|
|
21
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* A string as a 32-bit number (FNV-1a), the same on every run and in every
|
|
24
|
+
* runtime: what a seeded draw is keyed by, so a draft, a room's bot faces or a
|
|
25
|
+
* match's music comes out the same from the same id with nothing stored.
|
|
26
|
+
*/
|
|
27
|
+
export function hashSeed(seed) {
|
|
28
|
+
let h = 0x811c9dc5;
|
|
29
|
+
for (let i = 0; i < seed.length; i++) {
|
|
30
|
+
h ^= seed.charCodeAt(i);
|
|
31
|
+
h = Math.imul(h, 0x01000193);
|
|
32
|
+
}
|
|
33
|
+
return h >>> 0;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A deterministic [0,1) from a 32-bit state (mulberry32: one multiply-xorshift
|
|
37
|
+
* round). Not cryptographic and not trying to be: it is portable and identical
|
|
38
|
+
* in Node and the browser, so a seed reproduces the same draws anywhere.
|
|
39
|
+
*/
|
|
40
|
+
export function mulberry32(state) {
|
|
41
|
+
let a = state >>> 0;
|
|
42
|
+
return () => {
|
|
43
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
44
|
+
let t = a;
|
|
45
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
46
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
47
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** `items` in `rand`'s order (Fisher–Yates), as a new array: the caller's list is never mutated. */
|
|
51
|
+
export function shuffle(items, rand) {
|
|
52
|
+
const out = items.slice();
|
|
53
|
+
for (let i = out.length - 1; i > 0; i--) {
|
|
54
|
+
const j = Math.floor(rand() * (i + 1));
|
|
55
|
+
[out[i], out[j]] = [out[j], out[i]];
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* One of `items`, never `last` again while there is anything else to pick: a
|
|
61
|
+
* cell of several takes varies and never plays the same one twice in a row.
|
|
62
|
+
* Null when there are none.
|
|
63
|
+
*/
|
|
64
|
+
export function pickNotLast(items, last, rand = secureRandom) {
|
|
65
|
+
return pickRandom(items.length > 1 && last !== null ? items.filter((item) => item !== last) : items, rand) ?? null;
|
|
66
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/platform-wire",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.266",
|
|
4
|
+
"gitHead": "33e217cc0042251707965b5aaac76997909d9fdf",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "The wire any game speaks: room ids, telemetry, presence, events and tables, the Discord relay, looks, the room request bodies and room view, room settings, the game-type registry, the restart notice, secure random and a generic server envelope. Knows no game; a game's own part rides as a type parameter. MIT; imports only zod.",
|