@benev/tact 0.4.0-7 → 0.4.0-8
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/README.md +4 -4
- package/package.json +3 -2
- package/s/lib/input/controller.ts +4 -1
- package/s/lib/input/data/intent/decode.ts +2 -2
- package/s/lib/input/data/intent/encode.ts +2 -2
- package/s/lib/input/data/intent/minihash.ts +5 -0
- package/s/lib/input/decoder.ts +2 -2
- package/s/lib/input/investigate/hash-bindings.ts +5 -3
- package/s/lib/input/investigate/investigate.ts +24 -9
- package/s/lib/input/test.ts +11 -0
- package/s/lib/input/testing/quickbind.ts +3 -3
- package/s/lib/ui/deck/collector.ts +3 -3
- package/s/lib/ui/deck/hub.ts +4 -3
- package/s/lib/utils/hash.ts +47 -0
- package/x/demo/main.bundle.min.js +104 -104
- package/x/demo/main.bundle.min.js.map +4 -4
- package/x/gamepad/index.html +1 -1
- package/x/index.html +2 -2
- package/x/lib/input/controller.js +1 -1
- package/x/lib/input/controller.js.map +1 -1
- package/x/lib/input/data/intent/decode.d.ts +1 -1
- package/x/lib/input/data/intent/decode.js +2 -2
- package/x/lib/input/data/intent/decode.js.map +1 -1
- package/x/lib/input/data/intent/encode.d.ts +1 -1
- package/x/lib/input/data/intent/encode.js +2 -2
- package/x/lib/input/data/intent/encode.js.map +1 -1
- package/x/lib/input/data/intent/minihash.d.ts +1 -0
- package/x/lib/input/data/intent/minihash.js +4 -0
- package/x/lib/input/data/intent/minihash.js.map +1 -0
- package/x/lib/input/decoder.d.ts +1 -1
- package/x/lib/input/decoder.js +2 -2
- package/x/lib/input/decoder.js.map +1 -1
- package/x/lib/input/investigate/hash-bindings.d.ts +1 -1
- package/x/lib/input/investigate/hash-bindings.js +5 -3
- package/x/lib/input/investigate/hash-bindings.js.map +1 -1
- package/x/lib/input/investigate/investigate.d.ts +2 -1
- package/x/lib/input/investigate/investigate.js +14 -9
- package/x/lib/input/investigate/investigate.js.map +1 -1
- package/x/lib/input/test.d.ts +1 -0
- package/x/lib/input/test.js +10 -0
- package/x/lib/input/test.js.map +1 -1
- package/x/lib/input/testing/quickbind.d.ts +3 -2
- package/x/lib/input/testing/quickbind.js +3 -3
- package/x/lib/input/testing/quickbind.js.map +1 -1
- package/x/lib/ui/deck/collector.js +2 -2
- package/x/lib/ui/deck/collector.js.map +1 -1
- package/x/lib/ui/deck/hub.js +4 -3
- package/x/lib/ui/deck/hub.js.map +1 -1
- package/x/lib/utils/hash.d.ts +1 -0
- package/x/lib/utils/hash.js +47 -0
- package/x/lib/utils/hash.js.map +1 -0
package/README.md
CHANGED
|
@@ -116,11 +116,11 @@ i care about the little things. all input types are boiled down to simple number
|
|
|
116
116
|
|
|
117
117
|
- ⚙️ **pov: your app isn't a game, no multiplayer, no gamepads.**
|
|
118
118
|
```ts
|
|
119
|
-
import {setupTact,
|
|
119
|
+
import {setupTact, Decoder} from "@benev/tact"
|
|
120
120
|
|
|
121
121
|
const tact = await setupTact({presets, maxPlayers: 1})
|
|
122
122
|
const controller = tact.mouseKeyboard()
|
|
123
|
-
const decoder = new
|
|
123
|
+
const decoder = new Decoder(bindings)
|
|
124
124
|
tact.registerElements()
|
|
125
125
|
|
|
126
126
|
setInterval(() => {
|
|
@@ -134,11 +134,11 @@ i care about the little things. all input types are boiled down to simple number
|
|
|
134
134
|
- 💡 *then the only ui you need is the `<tact-customize>` panel (also available as a sly view `CustomizeView`).*
|
|
135
135
|
- ⚙️ **pov: your app doesn't even want any ui, or persistence.**
|
|
136
136
|
```ts
|
|
137
|
-
import {Controller,
|
|
137
|
+
import {Controller, Decoder, KeyboardSource, PointerSource, Sources} from "@benev/tact"
|
|
138
138
|
|
|
139
139
|
const source = new Sources(new KeyboardSource(), new PointerSource())
|
|
140
140
|
const controller = new Controller({bindings, source})
|
|
141
|
-
const decoder = new
|
|
141
|
+
const decoder = new Decoder(bindings)
|
|
142
142
|
|
|
143
143
|
setInterval(() => {
|
|
144
144
|
const inputs = decoder.decode(controller.encode())
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@benev/tact",
|
|
3
|
-
"version": "0.4.0-
|
|
3
|
+
"version": "0.4.0-8",
|
|
4
4
|
"description": "🎮 from keypress to couch co-op",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Chase Moskal <chasemoskal@gmail.com>",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"lit": "^3.3.3"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
+
"@awasm/noble": "^0.1.4",
|
|
29
30
|
"@benev/math": "0.4.0-9",
|
|
30
31
|
"@e280/kv": "^0.2.4",
|
|
31
32
|
"@e280/shiny": "^0.1.1",
|
|
@@ -35,7 +36,7 @@
|
|
|
35
36
|
"@e280/hottie": "^0.1.7",
|
|
36
37
|
"@e280/octo": "^0.1.7",
|
|
37
38
|
"@e280/science": "^0.1.14",
|
|
38
|
-
"@e280/scute": "^0.5.
|
|
39
|
+
"@e280/scute": "^0.5.5",
|
|
39
40
|
"http-server": "^14.1.1",
|
|
40
41
|
"npm-run-all": "^4.1.5",
|
|
41
42
|
"typescript": "^7.0.2"
|
|
@@ -58,7 +58,10 @@ export class Controller<B extends Bindings = any> {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
encode(time = performance.now()) {
|
|
61
|
-
return encodeIntents(
|
|
61
|
+
return encodeIntents(
|
|
62
|
+
this.#investigation.minihash,
|
|
63
|
+
[...this.evaluate(time)],
|
|
64
|
+
)
|
|
62
65
|
}
|
|
63
66
|
|
|
64
67
|
clear() {
|
|
@@ -8,7 +8,7 @@ export function decodeIntents(buffer: ArrayBuffer) {
|
|
|
8
8
|
const dv = new DataView(buffer)
|
|
9
9
|
let cursor = 0
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const minihash = dv.getUint32(cursor, endian)
|
|
12
12
|
cursor += sizeHash
|
|
13
13
|
|
|
14
14
|
while (cursor < buffer.byteLength) {
|
|
@@ -21,6 +21,6 @@ export function decodeIntents(buffer: ArrayBuffer) {
|
|
|
21
21
|
intents.push({id, value})
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
return {
|
|
24
|
+
return {minihash, intents}
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -3,13 +3,13 @@ import {Intent} from "./types.js"
|
|
|
3
3
|
import {endian} from "../endian.js"
|
|
4
4
|
import {sizeHash, sizeId, sizeIntent, sizeValue} from "./sizes.js"
|
|
5
5
|
|
|
6
|
-
export function encodeIntents(
|
|
6
|
+
export function encodeIntents(minihash: number, intents: Intent[]) {
|
|
7
7
|
const byteCount = sizeHash + (intents.length * sizeIntent)
|
|
8
8
|
const buffer = new ArrayBuffer(byteCount)
|
|
9
9
|
const dv = new DataView(buffer)
|
|
10
10
|
let cursor = 0
|
|
11
11
|
|
|
12
|
-
dv.setUint32(cursor,
|
|
12
|
+
dv.setUint32(cursor, minihash, endian)
|
|
13
13
|
cursor += sizeHash
|
|
14
14
|
|
|
15
15
|
for (const intent of intents) {
|
package/s/lib/input/decoder.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {Inputs, Bindings} from "./types.js"
|
|
|
3
3
|
import {decodeIntents} from "./data/intent/decode.js"
|
|
4
4
|
import {investigate} from "./investigate/investigate.js"
|
|
5
5
|
|
|
6
|
-
export class
|
|
6
|
+
export class Decoder<B extends Bindings> {
|
|
7
7
|
#investigation
|
|
8
8
|
|
|
9
9
|
constructor(bindings: B) {
|
|
@@ -25,7 +25,7 @@ export class InputDecoder<B extends Bindings> {
|
|
|
25
25
|
for (const buffer of buffers) {
|
|
26
26
|
const decoded = decodeIntents(buffer)
|
|
27
27
|
|
|
28
|
-
if (decoded.
|
|
28
|
+
if (decoded.minihash !== this.#investigation.minihash)
|
|
29
29
|
throw new Error("intent bindings hash mismatch")
|
|
30
30
|
|
|
31
31
|
for (const input of inputs)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
2
|
+
import {hex} from "@e280/stz"
|
|
3
3
|
import {Bindings} from "../types.js"
|
|
4
|
+
import {hash} from "../../utils/hash.js"
|
|
4
5
|
import {isExpression} from "./is-expression.js"
|
|
5
6
|
|
|
6
7
|
export function hashBindings(bindings: Bindings) {
|
|
@@ -8,7 +9,7 @@ export function hashBindings(bindings: Bindings) {
|
|
|
8
9
|
|
|
9
10
|
function recurse(b: Bindings, path: string[]) {
|
|
10
11
|
if (isExpression(b)) {
|
|
11
|
-
paths.push(
|
|
12
|
+
paths.push(JSON.stringify(path))
|
|
12
13
|
}
|
|
13
14
|
else {
|
|
14
15
|
for (const [key, value] of Object.entries(b))
|
|
@@ -17,6 +18,7 @@ export function hashBindings(bindings: Bindings) {
|
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
recurse(bindings, ["b"])
|
|
20
|
-
|
|
21
|
+
paths.sort()
|
|
22
|
+
return hex(hash("tact/bindings/v1", ...paths).slice(16))
|
|
21
23
|
}
|
|
22
24
|
|
|
@@ -5,31 +5,46 @@ import {Expression} from "../expression.js"
|
|
|
5
5
|
import {hashBindings} from "./hash-bindings.js"
|
|
6
6
|
import {isExpression} from "./is-expression.js"
|
|
7
7
|
import {Bindings, Input, Inputs} from "../types.js"
|
|
8
|
+
import {minihash} from "../data/intent/minihash.js"
|
|
8
9
|
|
|
9
10
|
export function investigate<B extends Bindings>(bindings: B) {
|
|
10
11
|
const hash = hashBindings(bindings)
|
|
11
|
-
const inputList: Input[] = []
|
|
12
|
-
const rootList: Expression[] = []
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
const leaves: {
|
|
14
|
+
path: string
|
|
15
|
+
input: Input
|
|
16
|
+
expression: Expression
|
|
17
|
+
}[] = []
|
|
18
|
+
|
|
19
|
+
function makeInputs(b: Bindings, path: string[]): unknown {
|
|
15
20
|
if (isExpression(b)) {
|
|
16
21
|
const input = makeInput()
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
leaves.push({
|
|
23
|
+
path: JSON.stringify(path),
|
|
24
|
+
input,
|
|
25
|
+
expression: b,
|
|
26
|
+
})
|
|
19
27
|
return input
|
|
20
28
|
}
|
|
21
29
|
else {
|
|
22
|
-
return obmap(b,
|
|
30
|
+
return obmap(b, (value, key) =>
|
|
31
|
+
makeInputs(value, [...path, key as string])
|
|
32
|
+
)
|
|
23
33
|
}
|
|
24
34
|
}
|
|
25
35
|
|
|
26
|
-
const inputs = makeInputs(bindings) as Inputs<B>
|
|
36
|
+
const inputs = makeInputs(bindings, ["b"]) as Inputs<B>
|
|
37
|
+
|
|
38
|
+
leaves.sort((a, b) =>
|
|
39
|
+
a.path < b.path ? -1 : a.path > b.path ? 1 : 0
|
|
40
|
+
)
|
|
27
41
|
|
|
28
42
|
return {
|
|
29
43
|
hash,
|
|
44
|
+
minihash: minihash(hash),
|
|
30
45
|
inputs,
|
|
31
|
-
inputList,
|
|
32
|
-
rootList,
|
|
46
|
+
inputList: leaves.map(l => l.input),
|
|
47
|
+
rootList: leaves.map(l => l.expression),
|
|
33
48
|
}
|
|
34
49
|
}
|
|
35
50
|
|
package/s/lib/input/test.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {asBindings} from "./types.js"
|
|
|
5
5
|
import {Controller} from "./controller.js"
|
|
6
6
|
import {PlainSource} from "./sources/plain.js"
|
|
7
7
|
import {quickbind} from "./testing/quickbind.js"
|
|
8
|
+
import {isCompatible} from "./investigate/is-compatible.js"
|
|
8
9
|
|
|
9
10
|
import parcel from "./data/test.js"
|
|
10
11
|
|
|
@@ -23,6 +24,16 @@ export default suite({
|
|
|
23
24
|
expect(inputs.was).is(0)
|
|
24
25
|
}),
|
|
25
26
|
|
|
27
|
+
"reordered bindings are equivalent": test(async() => {
|
|
28
|
+
const able = quickbind({alpha: "KeyA", bravo: "KeyB"})
|
|
29
|
+
const baker = quickbind({bravo: "KeyB", alpha: "KeyA"})
|
|
30
|
+
expect(isCompatible(able.bindings, baker.bindings)).is(true)
|
|
31
|
+
able.sample({code: "KeyA", value: 1})
|
|
32
|
+
const inputs = baker.decoder.decode(able.controller.encode())
|
|
33
|
+
expect(inputs.alpha.value).is(1)
|
|
34
|
+
expect(inputs.bravo.value).is(0)
|
|
35
|
+
}),
|
|
36
|
+
|
|
26
37
|
"update bindings": test(async() => {
|
|
27
38
|
const {sample, solve, controller} = quickbind("KeyA")
|
|
28
39
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
2
|
+
import {Decoder} from "../decoder.js"
|
|
3
3
|
import {Controller} from "../controller.js"
|
|
4
4
|
import {Bindings, Sample} from "../types.js"
|
|
5
5
|
import {PlainSource} from "../sources/plain.js"
|
|
@@ -7,7 +7,7 @@ import {PlainSource} from "../sources/plain.js"
|
|
|
7
7
|
export function quickbind<B extends Bindings>(b: B) {
|
|
8
8
|
const source = new PlainSource()
|
|
9
9
|
const controller = new Controller({source, bindings: b})
|
|
10
|
-
const decoder = new
|
|
10
|
+
const decoder = new Decoder(controller.bindings)
|
|
11
11
|
const {inputs} = decoder
|
|
12
12
|
|
|
13
13
|
const sample = (...samples: Sample[]) =>
|
|
@@ -16,6 +16,6 @@ export function quickbind<B extends Bindings>(b: B) {
|
|
|
16
16
|
const solve = (time?: number) =>
|
|
17
17
|
decoder.decode(controller.encode(time))
|
|
18
18
|
|
|
19
|
-
return {source, controller, decoder, sample, solve, inputs}
|
|
19
|
+
return {bindings: b, source, controller, decoder, sample, solve, inputs}
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -4,12 +4,12 @@ import {guarantee} from "@e280/stz"
|
|
|
4
4
|
|
|
5
5
|
import {PlayerId} from "./types.js"
|
|
6
6
|
import {Bindings} from "../../input/types.js"
|
|
7
|
-
import {
|
|
7
|
+
import {Decoder} from "../../input/decoder.js"
|
|
8
8
|
import {decodeParcels} from "../../input/data/parcel/decode.js"
|
|
9
9
|
|
|
10
10
|
export class Collector<B extends Bindings> {
|
|
11
11
|
#bindings: B
|
|
12
|
-
#decoders = new RMap<PlayerId,
|
|
12
|
+
#decoders = new RMap<PlayerId, Decoder<B>>()
|
|
13
13
|
|
|
14
14
|
constructor(bindings: B) {
|
|
15
15
|
this.#bindings = bindings
|
|
@@ -26,7 +26,7 @@ export class Collector<B extends Bindings> {
|
|
|
26
26
|
return guarantee(
|
|
27
27
|
this.#decoders,
|
|
28
28
|
playerId,
|
|
29
|
-
() => new
|
|
29
|
+
() => new Decoder(this.#bindings),
|
|
30
30
|
)
|
|
31
31
|
}
|
|
32
32
|
|
package/s/lib/ui/deck/hub.ts
CHANGED
|
@@ -5,18 +5,19 @@ import {got, IdCounter} from "@e280/stz"
|
|
|
5
5
|
import {PlayerId} from "./types.js"
|
|
6
6
|
import {Bindings} from "../../input/types.js"
|
|
7
7
|
import {Controller} from "../../input/controller.js"
|
|
8
|
+
import {minihash} from "../../input/data/intent/minihash.js"
|
|
8
9
|
import {encodeParcels} from "../../input/data/parcel/encode.js"
|
|
9
10
|
import {encodeIntents} from "../../input/data/intent/encode.js"
|
|
10
11
|
import {hashBindings} from "../../input/investigate/hash-bindings.js"
|
|
11
12
|
|
|
12
13
|
export class Hub<B extends Bindings = any> {
|
|
13
|
-
#
|
|
14
|
+
#minihash
|
|
14
15
|
#ids = new IdCounter()
|
|
15
16
|
#unassigned = new RSet<Controller<B>>()
|
|
16
17
|
#players = new RMap<PlayerId, {controllers: RSet<Controller<B>>}>()
|
|
17
18
|
|
|
18
19
|
constructor(readonly bindings: B) {
|
|
19
|
-
this.#
|
|
20
|
+
this.#minihash = minihash(hashBindings(bindings))
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
get playerIds() {
|
|
@@ -86,7 +87,7 @@ export class Hub<B extends Bindings = any> {
|
|
|
86
87
|
.filter(([, value]) => value !== 0)
|
|
87
88
|
.map(([id, value]) => ({id, value}))
|
|
88
89
|
|
|
89
|
-
return encodeIntents(this.#
|
|
90
|
+
return encodeIntents(this.#minihash, changedIntents)
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
encodeAll(time = performance.now()) {
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
|
|
2
|
+
import {blake3} from "@awasm/noble/js.js"
|
|
3
|
+
import {dataViewFrom, txt} from "@e280/stz"
|
|
4
|
+
|
|
5
|
+
const endian = false
|
|
6
|
+
enum EntropyType {String, Number, Bytes}
|
|
7
|
+
|
|
8
|
+
export function hash(...entropy: (string | number | Uint8Array)[]) {
|
|
9
|
+
if (entropy.length === 0)
|
|
10
|
+
throw new RangeError("hash requires entropy")
|
|
11
|
+
|
|
12
|
+
const hasher = blake3.create()
|
|
13
|
+
const typeTag = new Uint8Array(1)
|
|
14
|
+
const lengthTag = new Uint8Array(4)
|
|
15
|
+
const lengthDataview = dataViewFrom(lengthTag)
|
|
16
|
+
|
|
17
|
+
for (const part of entropy) {
|
|
18
|
+
if (typeof part === "string") {
|
|
19
|
+
const bytes = txt.toBytes(part)
|
|
20
|
+
typeTag[0] = EntropyType.String
|
|
21
|
+
lengthDataview.setUint32(0, bytes.byteLength, endian)
|
|
22
|
+
hasher.update(typeTag)
|
|
23
|
+
hasher.update(lengthTag)
|
|
24
|
+
hasher.update(bytes)
|
|
25
|
+
}
|
|
26
|
+
else if (typeof part === "number") {
|
|
27
|
+
const bytes = new Uint8Array(8)
|
|
28
|
+
dataViewFrom(bytes).setFloat64(0, part, endian)
|
|
29
|
+
typeTag[0] = EntropyType.Number
|
|
30
|
+
lengthDataview.setUint32(0, bytes.byteLength, endian)
|
|
31
|
+
hasher.update(typeTag)
|
|
32
|
+
hasher.update(lengthTag)
|
|
33
|
+
hasher.update(bytes)
|
|
34
|
+
}
|
|
35
|
+
else if (part instanceof Uint8Array) {
|
|
36
|
+
typeTag[0] = EntropyType.Bytes
|
|
37
|
+
lengthDataview.setUint32(0, part.byteLength, endian)
|
|
38
|
+
hasher.update(typeTag)
|
|
39
|
+
hasher.update(lengthTag)
|
|
40
|
+
hasher.update(part)
|
|
41
|
+
}
|
|
42
|
+
else throw new Error("unknown part type")
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return hasher.digest() as Uint8Array
|
|
46
|
+
}
|
|
47
|
+
|