diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-09-05 22:28:11 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-09-05 22:28:11 -0600 |
commit | 6708160cec15ccd4a79979ed8ba79310fbd9285f (patch) | |
tree | f716105066534b901399f2f2e5423c7caf7a773d /engine | |
parent | d19da30f6dbf316bf89355b9b840a6d77e5435ec (diff) | |
download | jumpstorm-6708160cec15ccd4a79979ed8ba79310fbd9285f.tar.gz jumpstorm-6708160cec15ccd4a79979ed8ba79310fbd9285f.zip |
Diffstat (limited to 'engine')
-rw-r--r-- | engine/utils/coding.ts | 18 | ||||
m--------- | engine/utils/compatto | 0 | ||||
-rw-r--r-- | engine/utils/dictionary.ts | 56 |
3 files changed, 71 insertions, 3 deletions
diff --git a/engine/utils/coding.ts b/engine/utils/coding.ts index 283844f..d0904fe 100644 --- a/engine/utils/coding.ts +++ b/engine/utils/coding.ts @@ -1,3 +1,8 @@ +import { compatto } from './compatto'; +import dictionary from './dictionary'; + +const { compress, decompress } = compatto({ dictionary }); + const replacer = (_key: any, value: any) => { if (value instanceof Map) { return { @@ -31,10 +36,17 @@ const reviver = (_key: any, value: any) => { }; // "deterministic" stringify -export const stringify = (obj: any) => { + +export const stringify = (obj: any): string => { return JSON.stringify(sortObj(obj), replacer); }; -export const parse = <T>(str: string) => { - return JSON.parse(str, reviver) as unknown as T; +export const serialize = (obj: any): Uint8Array => { + //return new Uint8Array(new TextEncoder().encode(stringify(obj))); + return compress(stringify(obj)); +}; + +export const parse = <T>(serialized: Uint8Array): T => { + //return JSON.parse(new TextDecoder().decode(serialized), reviver) as T; + return JSON.parse(decompress(serialized), reviver) as T; }; diff --git a/engine/utils/compatto b/engine/utils/compatto new file mode 160000 +Subproject 68628ac6f173f50b81cc2d83a75de176484b2c7 diff --git a/engine/utils/dictionary.ts b/engine/utils/dictionary.ts new file mode 100644 index 0000000..5a19c6d --- /dev/null +++ b/engine/utils/dictionary.ts @@ -0,0 +1,56 @@ +// basically a list of common strings between network components to help in compression +// can't be longer than 254 "words" +export default [ + '{', + '}', + ',', + '"', + "'", + ',', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + '0', + '.', + 'Map', + '[', + ']', + 'BoundingBox', + 'args', + 'height', + 'width', + 'name', + 'body', + 'Velocity', + 'Control', + 'Component', + 'NEW_ENTITIES', + 'REMOVE_ENTITIES', + 'UPDATE_ENTITIES', + 'NEW_INPUT', + 'REMOVE_INPUT', + 'boundingBox', + 'dTheta', + 'id', + 'Forces', + ':', + 'control', + 'controllableBy', + 'dimension', + 'rotation', + 'forces', + 'velocity', + 'center', + 'dCartesian', + 'x', + 'y', + '"body":[{"args":{', + 'controlVelocityComponent', + '"boundingBox":{"center":' +]; |