ttpg-darrell 1.0.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.
Files changed (39) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +2 -0
  3. package/build/cjs/hello.d.ts +0 -0
  4. package/build/cjs/hello.js +2 -0
  5. package/build/cjs/layout/layout-objects.d.ts +36 -0
  6. package/build/cjs/layout/layout-objects.js +151 -0
  7. package/build/cjs/layout/layout.d.ts +8 -0
  8. package/build/cjs/layout/layout.js +17 -0
  9. package/build/cjs/locale/locale.d.ts +8 -0
  10. package/build/cjs/locale/locale.js +44 -0
  11. package/build/cjs/nsid/nsid.d.ts +54 -0
  12. package/build/cjs/nsid/nsid.js +84 -0
  13. package/build/cjs/shuffle/shuffle.d.ts +4 -0
  14. package/build/cjs/shuffle/shuffle.js +17 -0
  15. package/build/cjs/spawn/spawn.d.ts +12 -0
  16. package/build/cjs/spawn/spawn.js +42 -0
  17. package/build/cjs/triggerable-multicast-delegate/triggerable-multicast-delegate.d.ts +30 -0
  18. package/build/cjs/triggerable-multicast-delegate/triggerable-multicast-delegate.js +73 -0
  19. package/build/cjs/vault/vault.d.ts +8 -0
  20. package/build/cjs/vault/vault.js +15 -0
  21. package/build/esm/hello.d.ts +0 -0
  22. package/build/esm/hello.js +2 -0
  23. package/build/esm/layout/layout-objects.d.ts +36 -0
  24. package/build/esm/layout/layout-objects.js +147 -0
  25. package/build/esm/layout/layout.d.ts +8 -0
  26. package/build/esm/layout/layout.js +13 -0
  27. package/build/esm/locale/locale.d.ts +8 -0
  28. package/build/esm/locale/locale.js +40 -0
  29. package/build/esm/nsid/nsid.d.ts +54 -0
  30. package/build/esm/nsid/nsid.js +80 -0
  31. package/build/esm/shuffle/shuffle.d.ts +4 -0
  32. package/build/esm/shuffle/shuffle.js +13 -0
  33. package/build/esm/spawn/spawn.d.ts +12 -0
  34. package/build/esm/spawn/spawn.js +38 -0
  35. package/build/esm/triggerable-multicast-delegate/triggerable-multicast-delegate.d.ts +30 -0
  36. package/build/esm/triggerable-multicast-delegate/triggerable-multicast-delegate.js +69 -0
  37. package/build/esm/vault/vault.d.ts +8 -0
  38. package/build/esm/vault/vault.js +11 -0
  39. package/package.json +38 -0
package/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # ttpg-darrell
2
+ Tabletop Playground utilities, mostly libraries for in-game scripts plus some asset wrangling tools.
File without changes
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ console.log("hello");
@@ -0,0 +1,36 @@
1
+ import { StaticObject, Vector } from "@tabletop-playground/api";
2
+ export type LayoutObjectsSize = {
3
+ w: number;
4
+ h: number;
5
+ };
6
+ export declare class LayoutObjects {
7
+ private _children;
8
+ private _horizontalAlignment;
9
+ private _verticalAlignment;
10
+ private _childDistance;
11
+ private _isVertical;
12
+ private _overrideHeight;
13
+ private _overrideWidth;
14
+ constructor();
15
+ setChildDistanace(value: number): this;
16
+ setHorizontalAlignment(value: number): this;
17
+ setVerticalAlignment(value: number): this;
18
+ setIsVertical(value: boolean): this;
19
+ setOverrideHeight(value: number): this;
20
+ setOverrideWidth(value: number): this;
21
+ add(item: StaticObject | LayoutObjects): this;
22
+ /**
23
+ * Get size of self, applying any overrides.
24
+ *
25
+ * @returns {LayoutObjectsSize}
26
+ */
27
+ calculateSize(): LayoutObjectsSize;
28
+ /**
29
+ * Get size from laying out children (ignore override on self).
30
+ *
31
+ * @returns {LayoutObjectsSize}
32
+ */
33
+ calculateChildrenSize(): LayoutObjectsSize;
34
+ _calculateChildSize(child: StaticObject | LayoutObjects): LayoutObjectsSize;
35
+ doLayoutAtPoint(center: Vector, yaw: number): this;
36
+ }
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LayoutObjects = void 0;
4
+ const api_1 = require("@tabletop-playground/api");
5
+ class LayoutObjects {
6
+ constructor() {
7
+ this._children = [];
8
+ this._horizontalAlignment = api_1.HorizontalAlignment.Center;
9
+ this._verticalAlignment = api_1.VerticalAlignment.Center;
10
+ this._childDistance = 0;
11
+ this._isVertical = false;
12
+ this._overrideHeight = 0;
13
+ this._overrideWidth = 0;
14
+ }
15
+ setChildDistanace(value) {
16
+ this._childDistance = value;
17
+ return this;
18
+ }
19
+ setHorizontalAlignment(value) {
20
+ this._horizontalAlignment = value;
21
+ return this;
22
+ }
23
+ setVerticalAlignment(value) {
24
+ this._verticalAlignment = value;
25
+ return this;
26
+ }
27
+ setIsVertical(value) {
28
+ this._isVertical = value;
29
+ return this;
30
+ }
31
+ setOverrideHeight(value) {
32
+ this._overrideHeight = value;
33
+ return this;
34
+ }
35
+ setOverrideWidth(value) {
36
+ this._overrideWidth = value;
37
+ return this;
38
+ }
39
+ // ----------------------------------
40
+ add(item) {
41
+ this._children.push(item);
42
+ return this;
43
+ }
44
+ /**
45
+ * Get size of self, applying any overrides.
46
+ *
47
+ * @returns {LayoutObjectsSize}
48
+ */
49
+ calculateSize() {
50
+ let size = this.calculateChildrenSize();
51
+ if (this._overrideHeight > 0) {
52
+ size.h = this._overrideHeight;
53
+ }
54
+ if (this._overrideWidth > 0) {
55
+ size.w = this._overrideWidth;
56
+ }
57
+ return size;
58
+ }
59
+ /**
60
+ * Get size from laying out children (ignore override on self).
61
+ *
62
+ * @returns {LayoutObjectsSize}
63
+ */
64
+ calculateChildrenSize() {
65
+ const size = { w: 0, h: 0 };
66
+ // Account for child distance.
67
+ const spacing = Math.max(this._children.length - 1, 0) * this._childDistance;
68
+ if (this._isVertical) {
69
+ size.h = spacing;
70
+ }
71
+ else {
72
+ size.w = spacing;
73
+ }
74
+ // Apply each child.
75
+ for (const child of this._children) {
76
+ const childSize = this._calculateChildSize(child);
77
+ if (this._isVertical) {
78
+ size.w = Math.max(size.w, childSize.w);
79
+ size.h += childSize.h;
80
+ }
81
+ else {
82
+ size.h = Math.max(size.h, childSize.h);
83
+ size.w += childSize.w;
84
+ }
85
+ }
86
+ return size;
87
+ }
88
+ _calculateChildSize(child) {
89
+ let childSize;
90
+ if (child instanceof api_1.StaticObject) {
91
+ const currentRotation = true;
92
+ const includeGeometry = false;
93
+ const extent = child.getExtent(currentRotation, includeGeometry);
94
+ childSize = { w: extent.y * 2, h: extent.x * 2 };
95
+ }
96
+ else {
97
+ childSize = child.calculateSize();
98
+ }
99
+ return childSize;
100
+ }
101
+ doLayoutAtPoint(center, yaw) {
102
+ const size = this.calculateSize();
103
+ const childrenSize = this.calculateChildrenSize();
104
+ let padLeft;
105
+ if (this._horizontalAlignment === api_1.HorizontalAlignment.Left) {
106
+ padLeft = 0;
107
+ }
108
+ else if (this._horizontalAlignment === api_1.HorizontalAlignment.Right) {
109
+ padLeft = size.w - childrenSize.w;
110
+ }
111
+ else {
112
+ padLeft = (size.w - childrenSize.w) / 2; // center (even if "Fill")
113
+ }
114
+ let padTop;
115
+ if (this._verticalAlignment === api_1.VerticalAlignment.Top) {
116
+ padTop = 0;
117
+ }
118
+ else if (this._verticalAlignment === api_1.VerticalAlignment.Bottom) {
119
+ padTop = size.w - childrenSize.w;
120
+ }
121
+ else {
122
+ padTop = (size.w - childrenSize.w) / 2; // center (even if "Fill")
123
+ }
124
+ let left = -size.w / 2 + padLeft;
125
+ let top = -size.h / 2 + padTop;
126
+ for (const child of this._children) {
127
+ const childSize = this._calculateChildSize(child);
128
+ // Calculate child center (world).
129
+ const childCenter = new api_1.Vector(top + childSize.h / 2, left + childSize.w / 2, 0)
130
+ .rotateAngleAxis(yaw, [0, 0, 1])
131
+ .add(center);
132
+ // Position child.
133
+ if (child instanceof api_1.StaticObject) {
134
+ child.setPosition(childCenter);
135
+ child.setRotation(child.getRotation().compose([0, yaw, 0]));
136
+ }
137
+ else {
138
+ child.doLayoutAtPoint(childCenter, yaw);
139
+ }
140
+ // Move "cursor" to next open spot top-left.
141
+ if (this._isVertical) {
142
+ top += childSize.h + this._childDistance;
143
+ }
144
+ else {
145
+ left += childSize.w + this._childDistance;
146
+ }
147
+ }
148
+ return this;
149
+ }
150
+ }
151
+ exports.LayoutObjects = LayoutObjects;
@@ -0,0 +1,8 @@
1
+ import { GameObject } from "@tabletop-playground/api";
2
+ export declare class Layout {
3
+ private _anchorX;
4
+ private _anchorY;
5
+ getWidth(): number;
6
+ getHeight(): number;
7
+ add(obj: GameObject, weight?: number): void;
8
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Layout = void 0;
4
+ class Layout {
5
+ constructor() {
6
+ this._anchorX = 0.5;
7
+ this._anchorY = 0.5;
8
+ }
9
+ getWidth() {
10
+ return 0;
11
+ }
12
+ getHeight() {
13
+ return 0;
14
+ }
15
+ add(obj, weight = 0) { }
16
+ }
17
+ exports.Layout = Layout;
@@ -0,0 +1,8 @@
1
+ export declare const locale: {
2
+ (key: string, replacement?: {
3
+ [key: string]: string | number;
4
+ } | undefined): string;
5
+ inject(dict: {
6
+ [key: string]: string;
7
+ }): void;
8
+ };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.locale = void 0;
4
+ const REPLACE_REGEX = /(?<!\\){(?!#)(.*?)(?<!\\)}/gm;
5
+ const PLURAL_REGEX = /(?<!\\){#(.*?)(?<!\\)}/gm;
6
+ const PLURAL_SEPERATOR = /(?<!\\)\|/gm;
7
+ const _lang = {};
8
+ // Original author: ThatRobHuman
9
+ const locale = (key, replacement = undefined) => {
10
+ const str = _lang[key];
11
+ if (!str) {
12
+ return key; // not registered, use key as stand-in
13
+ }
14
+ else if (!replacement) {
15
+ return str; // no need for regex
16
+ }
17
+ return str
18
+ .replace(REPLACE_REGEX, (match) => {
19
+ const r = replacement[match.substring(1, match.length - 1)].toString();
20
+ if (r === undefined) {
21
+ return match;
22
+ }
23
+ return r;
24
+ })
25
+ .replace(PLURAL_REGEX, (match) => {
26
+ const [val, singular, plural, zeroForm] = match
27
+ .substring(2, match.length - 1)
28
+ .split(PLURAL_SEPERATOR);
29
+ const num = Number(replacement[val]);
30
+ if (isNaN(num) || num === 0) {
31
+ return zeroForm || plural;
32
+ }
33
+ if (num > 1) {
34
+ return plural;
35
+ }
36
+ return singular;
37
+ });
38
+ };
39
+ exports.locale = locale;
40
+ exports.locale.inject = (dict) => {
41
+ for (const [k, v] of Object.entries(dict)) {
42
+ _lang[k] = v;
43
+ }
44
+ };
@@ -0,0 +1,54 @@
1
+ import { Card, StaticObject } from "@tabletop-playground/api";
2
+ export type ParsedNSID = {
3
+ nsid: string;
4
+ type: string[];
5
+ source: string[];
6
+ name: string[];
7
+ extra: string | undefined;
8
+ };
9
+ export declare const DECK_NSID = "deck:?/?";
10
+ /**
11
+ * Object metadata field uses a simple "type:source/name|extra" string.
12
+ *
13
+ * Each component is a dot (".") delimited series of strings.
14
+ *
15
+ * TYPE delineates the hierarchy to the specific object type. Entries should
16
+ * start generic and get more specific, for instance "card.action" is a card
17
+ * from the action deck. The TYPE field should be sufficient to locate where
18
+ * the item belongs, in some cases augment with owning player slot.
19
+ *
20
+ * SOURCE is the produce/release with the item. For instance, "base" could
21
+ * mean the base game, or use an official expansion name. Recommend homebrew
22
+ * always start with "homebrew.{x}" where {x} is the identifier (to avoid
23
+ * confusion with canon content sources).
24
+ *
25
+ * NAME is the item name. It may include dot-delimited discriminators for
26
+ * different versions of the same item (e.g. "direct_hit.1" vs "direct_hit.2").
27
+ *
28
+ * EXTRA should be avoided, there may be rare cases wanting fruther metadata.
29
+ *
30
+ * @see https://github.com/TI4-Online/TI4-TTPG/wiki/NSID-Namespace
31
+ */
32
+ export declare abstract class NSID {
33
+ /**
34
+ * Create NSID from a metadata string or object. A deck with multiple cards
35
+ * gets a special "deck" NSID, consumers should call `stack` to get by card.
36
+ *
37
+ * @param input
38
+ * @returns NSID string
39
+ */
40
+ static get(input: StaticObject): string;
41
+ /**
42
+ * Get NSIDs for each card in a deck.
43
+ *
44
+ * @param input deck
45
+ * @returns NSID array, per-card values
46
+ */
47
+ static getDeck(input: Card): string[];
48
+ /**
49
+ * Parse this NSID into components (and sub-components, if dot delimited).
50
+ *
51
+ * @returns parsed
52
+ */
53
+ static parse(nsid: string): ParsedNSID | undefined;
54
+ }
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NSID = exports.DECK_NSID = void 0;
4
+ const api_1 = require("@tabletop-playground/api");
5
+ exports.DECK_NSID = "deck:?/?";
6
+ /**
7
+ * Object metadata field uses a simple "type:source/name|extra" string.
8
+ *
9
+ * Each component is a dot (".") delimited series of strings.
10
+ *
11
+ * TYPE delineates the hierarchy to the specific object type. Entries should
12
+ * start generic and get more specific, for instance "card.action" is a card
13
+ * from the action deck. The TYPE field should be sufficient to locate where
14
+ * the item belongs, in some cases augment with owning player slot.
15
+ *
16
+ * SOURCE is the produce/release with the item. For instance, "base" could
17
+ * mean the base game, or use an official expansion name. Recommend homebrew
18
+ * always start with "homebrew.{x}" where {x} is the identifier (to avoid
19
+ * confusion with canon content sources).
20
+ *
21
+ * NAME is the item name. It may include dot-delimited discriminators for
22
+ * different versions of the same item (e.g. "direct_hit.1" vs "direct_hit.2").
23
+ *
24
+ * EXTRA should be avoided, there may be rare cases wanting fruther metadata.
25
+ *
26
+ * @see https://github.com/TI4-Online/TI4-TTPG/wiki/NSID-Namespace
27
+ */
28
+ class NSID {
29
+ /**
30
+ * Create NSID from a metadata string or object. A deck with multiple cards
31
+ * gets a special "deck" NSID, consumers should call `stack` to get by card.
32
+ *
33
+ * @param input
34
+ * @returns NSID string
35
+ */
36
+ static get(input) {
37
+ let metadata = "";
38
+ if (input instanceof api_1.Card) {
39
+ if (input.getStackSize() === 1) {
40
+ // Singleton card: use the card metadata.
41
+ metadata = input.getCardDetails().metadata;
42
+ }
43
+ else {
44
+ // Deck: do not attempt to extract more here.
45
+ metadata = exports.DECK_NSID;
46
+ }
47
+ }
48
+ else if (input instanceof api_1.StaticObject) {
49
+ // Non-card table or game object. Use template metadata.
50
+ metadata = input.getTemplateMetadata();
51
+ }
52
+ return metadata;
53
+ }
54
+ /**
55
+ * Get NSIDs for each card in a deck.
56
+ *
57
+ * @param input deck
58
+ * @returns NSID array, per-card values
59
+ */
60
+ static getDeck(input) {
61
+ return input.getAllCardDetails().map((cardDetails) => {
62
+ return cardDetails.metadata;
63
+ });
64
+ }
65
+ /**
66
+ * Parse this NSID into components (and sub-components, if dot delimited).
67
+ *
68
+ * @returns parsed
69
+ */
70
+ static parse(nsid) {
71
+ const m = nsid.match(/^([^:]+):([^/]+)\/([^|]+)\|?(.*)$/);
72
+ if (!m) {
73
+ return undefined;
74
+ }
75
+ return {
76
+ nsid: nsid,
77
+ type: m[1].split("."),
78
+ source: m[2].split("."),
79
+ name: m[3].split("."),
80
+ extra: m[4].length > 0 ? m[4] : undefined,
81
+ };
82
+ }
83
+ }
84
+ exports.NSID = NSID;
@@ -0,0 +1,4 @@
1
+ export declare class Shuffle {
2
+ static shuffle<T>(items: T[]): T[];
3
+ static choice<T>(items: T[]): T;
4
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Shuffle = void 0;
4
+ class Shuffle {
5
+ static shuffle(items) {
6
+ // Fisher-Yates
7
+ for (let i = items.length - 1; i > 0; i--) {
8
+ let j = Math.floor(Math.random() * (i + 1));
9
+ [items[i], items[j]] = [items[j], items[i]];
10
+ }
11
+ return items;
12
+ }
13
+ static choice(items) {
14
+ return items[Math.floor(Math.random() * items.length)];
15
+ }
16
+ }
17
+ exports.Shuffle = Shuffle;
@@ -0,0 +1,12 @@
1
+ import { GameObject, Vector } from "@tabletop-playground/api";
2
+ /**
3
+ * Registry for NSID to template id.
4
+ */
5
+ export declare abstract class Spawn {
6
+ private static _nsidToTemplateId;
7
+ static spawn(nsid: string, position: Vector | [x: number, y: number, z: number]): GameObject | undefined;
8
+ static inject(dict: {
9
+ [key: string]: string;
10
+ }): void;
11
+ static validate(): void;
12
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Spawn = void 0;
4
+ const api_1 = require("@tabletop-playground/api");
5
+ /**
6
+ * Registry for NSID to template id.
7
+ */
8
+ class Spawn {
9
+ static spawn(nsid, position) {
10
+ const templateId = Spawn._nsidToTemplateId[nsid];
11
+ if (!templateId) {
12
+ throw new Error(`spawn nsid "${nsid}" has no templateId`);
13
+ }
14
+ const obj = api_1.world.createObjectFromTemplate(templateId, position);
15
+ return obj;
16
+ }
17
+ static inject(dict) {
18
+ for (const [k, v] of Object.entries(dict)) {
19
+ Spawn._nsidToTemplateId[k] = v;
20
+ }
21
+ }
22
+ static validate() {
23
+ const templateIds = new Set();
24
+ for (const pkg of api_1.world.getAllowedPackages()) {
25
+ for (const templateId of pkg.getTemplateIds()) {
26
+ templateIds.add(templateId);
27
+ }
28
+ }
29
+ const missing = [];
30
+ for (const [nsid, templateId] of Object.entries(Spawn._nsidToTemplateId)) {
31
+ if (!templateIds.has(templateId)) {
32
+ missing.push(`${templateId} ("${nsid}")`);
33
+ }
34
+ }
35
+ if (missing.length > 0) {
36
+ throw new Error(`Spawn.validate missing templateIds (${missing.length}):\n` +
37
+ missing.join("\n"));
38
+ }
39
+ }
40
+ }
41
+ exports.Spawn = Spawn;
42
+ Spawn._nsidToTemplateId = {};
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Lookalike for TTPG's MulticastDelegate, but with a trigger method.
3
+ */
4
+ export declare class TriggerableMulticastDelegate<T> {
5
+ private readonly _listeners;
6
+ /**
7
+ * Add a function to the trigger set.
8
+ *
9
+ * @param fn
10
+ */
11
+ add(fn: T): void;
12
+ /**
13
+ * Remove a function from the trigger set.
14
+ *
15
+ * @param fn
16
+ */
17
+ remove(fn: T): void;
18
+ /**
19
+ * Clear the trigger set.
20
+ */
21
+ clear(): void;
22
+ /**
23
+ * Call every function in the trigger set.
24
+ *
25
+ * Call every function even if one throws, raise gathered errors at end.
26
+ *
27
+ * @param args
28
+ */
29
+ trigger(...args: any): void;
30
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TriggerableMulticastDelegate = void 0;
4
+ /**
5
+ * Lookalike for TTPG's MulticastDelegate, but with a trigger method.
6
+ */
7
+ class TriggerableMulticastDelegate {
8
+ constructor() {
9
+ this._listeners = [];
10
+ }
11
+ /**
12
+ * Add a function to the trigger set.
13
+ *
14
+ * @param fn
15
+ */
16
+ add(fn) {
17
+ this._listeners.push(fn);
18
+ }
19
+ /**
20
+ * Remove a function from the trigger set.
21
+ *
22
+ * @param fn
23
+ */
24
+ remove(fn) {
25
+ for (let i = this._listeners.length - 1; i >= 0; i--) {
26
+ if (this._listeners[i] === fn) {
27
+ this._listeners.splice(i, 1);
28
+ }
29
+ }
30
+ }
31
+ /**
32
+ * Clear the trigger set.
33
+ */
34
+ clear() {
35
+ this._listeners.splice(0);
36
+ }
37
+ /**
38
+ * Call every function in the trigger set.
39
+ *
40
+ * Call every function even if one throws, raise gathered errors at end.
41
+ *
42
+ * @param args
43
+ */
44
+ trigger(...args) {
45
+ const errors = [];
46
+ for (const fn of this._listeners) {
47
+ try {
48
+ if (typeof fn === "function") {
49
+ fn(...args);
50
+ }
51
+ }
52
+ catch (e) {
53
+ if (e instanceof Error) {
54
+ errors.push(e);
55
+ }
56
+ }
57
+ }
58
+ if (errors.length === 1) {
59
+ throw errors[0];
60
+ }
61
+ else if (errors.length > 1) {
62
+ const message = `ERRORS (${errors.length}):\n` +
63
+ errors.map((e) => e.message).join("\n");
64
+ const stack = errors
65
+ .map((e) => `-----\n${e.stack ? e.stack : "N/A"}`)
66
+ .join("\n");
67
+ const error = new Error(message);
68
+ error.stack = stack;
69
+ throw error;
70
+ }
71
+ }
72
+ }
73
+ exports.TriggerableMulticastDelegate = TriggerableMulticastDelegate;
@@ -0,0 +1,8 @@
1
+ export declare class Vault {
2
+ private readonly _uid;
3
+ constructor(uid: string);
4
+ clear(): void;
5
+ delete(key: string): void;
6
+ put(key: string, value: string): void;
7
+ get(key: string): string | undefined;
8
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Vault = void 0;
4
+ class Vault {
5
+ constructor(uid) {
6
+ this._uid = uid;
7
+ }
8
+ clear() { }
9
+ delete(key) { }
10
+ put(key, value) { }
11
+ get(key) {
12
+ return undefined;
13
+ }
14
+ }
15
+ exports.Vault = Vault;
File without changes
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ console.log("hello");
@@ -0,0 +1,36 @@
1
+ import { StaticObject, Vector } from "@tabletop-playground/api";
2
+ export type LayoutObjectsSize = {
3
+ w: number;
4
+ h: number;
5
+ };
6
+ export declare class LayoutObjects {
7
+ private _children;
8
+ private _horizontalAlignment;
9
+ private _verticalAlignment;
10
+ private _childDistance;
11
+ private _isVertical;
12
+ private _overrideHeight;
13
+ private _overrideWidth;
14
+ constructor();
15
+ setChildDistanace(value: number): this;
16
+ setHorizontalAlignment(value: number): this;
17
+ setVerticalAlignment(value: number): this;
18
+ setIsVertical(value: boolean): this;
19
+ setOverrideHeight(value: number): this;
20
+ setOverrideWidth(value: number): this;
21
+ add(item: StaticObject | LayoutObjects): this;
22
+ /**
23
+ * Get size of self, applying any overrides.
24
+ *
25
+ * @returns {LayoutObjectsSize}
26
+ */
27
+ calculateSize(): LayoutObjectsSize;
28
+ /**
29
+ * Get size from laying out children (ignore override on self).
30
+ *
31
+ * @returns {LayoutObjectsSize}
32
+ */
33
+ calculateChildrenSize(): LayoutObjectsSize;
34
+ _calculateChildSize(child: StaticObject | LayoutObjects): LayoutObjectsSize;
35
+ doLayoutAtPoint(center: Vector, yaw: number): this;
36
+ }
@@ -0,0 +1,147 @@
1
+ import { HorizontalAlignment, StaticObject, Vector, VerticalAlignment, } from "@tabletop-playground/api";
2
+ export class LayoutObjects {
3
+ constructor() {
4
+ this._children = [];
5
+ this._horizontalAlignment = HorizontalAlignment.Center;
6
+ this._verticalAlignment = VerticalAlignment.Center;
7
+ this._childDistance = 0;
8
+ this._isVertical = false;
9
+ this._overrideHeight = 0;
10
+ this._overrideWidth = 0;
11
+ }
12
+ setChildDistanace(value) {
13
+ this._childDistance = value;
14
+ return this;
15
+ }
16
+ setHorizontalAlignment(value) {
17
+ this._horizontalAlignment = value;
18
+ return this;
19
+ }
20
+ setVerticalAlignment(value) {
21
+ this._verticalAlignment = value;
22
+ return this;
23
+ }
24
+ setIsVertical(value) {
25
+ this._isVertical = value;
26
+ return this;
27
+ }
28
+ setOverrideHeight(value) {
29
+ this._overrideHeight = value;
30
+ return this;
31
+ }
32
+ setOverrideWidth(value) {
33
+ this._overrideWidth = value;
34
+ return this;
35
+ }
36
+ // ----------------------------------
37
+ add(item) {
38
+ this._children.push(item);
39
+ return this;
40
+ }
41
+ /**
42
+ * Get size of self, applying any overrides.
43
+ *
44
+ * @returns {LayoutObjectsSize}
45
+ */
46
+ calculateSize() {
47
+ let size = this.calculateChildrenSize();
48
+ if (this._overrideHeight > 0) {
49
+ size.h = this._overrideHeight;
50
+ }
51
+ if (this._overrideWidth > 0) {
52
+ size.w = this._overrideWidth;
53
+ }
54
+ return size;
55
+ }
56
+ /**
57
+ * Get size from laying out children (ignore override on self).
58
+ *
59
+ * @returns {LayoutObjectsSize}
60
+ */
61
+ calculateChildrenSize() {
62
+ const size = { w: 0, h: 0 };
63
+ // Account for child distance.
64
+ const spacing = Math.max(this._children.length - 1, 0) * this._childDistance;
65
+ if (this._isVertical) {
66
+ size.h = spacing;
67
+ }
68
+ else {
69
+ size.w = spacing;
70
+ }
71
+ // Apply each child.
72
+ for (const child of this._children) {
73
+ const childSize = this._calculateChildSize(child);
74
+ if (this._isVertical) {
75
+ size.w = Math.max(size.w, childSize.w);
76
+ size.h += childSize.h;
77
+ }
78
+ else {
79
+ size.h = Math.max(size.h, childSize.h);
80
+ size.w += childSize.w;
81
+ }
82
+ }
83
+ return size;
84
+ }
85
+ _calculateChildSize(child) {
86
+ let childSize;
87
+ if (child instanceof StaticObject) {
88
+ const currentRotation = true;
89
+ const includeGeometry = false;
90
+ const extent = child.getExtent(currentRotation, includeGeometry);
91
+ childSize = { w: extent.y * 2, h: extent.x * 2 };
92
+ }
93
+ else {
94
+ childSize = child.calculateSize();
95
+ }
96
+ return childSize;
97
+ }
98
+ doLayoutAtPoint(center, yaw) {
99
+ const size = this.calculateSize();
100
+ const childrenSize = this.calculateChildrenSize();
101
+ let padLeft;
102
+ if (this._horizontalAlignment === HorizontalAlignment.Left) {
103
+ padLeft = 0;
104
+ }
105
+ else if (this._horizontalAlignment === HorizontalAlignment.Right) {
106
+ padLeft = size.w - childrenSize.w;
107
+ }
108
+ else {
109
+ padLeft = (size.w - childrenSize.w) / 2; // center (even if "Fill")
110
+ }
111
+ let padTop;
112
+ if (this._verticalAlignment === VerticalAlignment.Top) {
113
+ padTop = 0;
114
+ }
115
+ else if (this._verticalAlignment === VerticalAlignment.Bottom) {
116
+ padTop = size.w - childrenSize.w;
117
+ }
118
+ else {
119
+ padTop = (size.w - childrenSize.w) / 2; // center (even if "Fill")
120
+ }
121
+ let left = -size.w / 2 + padLeft;
122
+ let top = -size.h / 2 + padTop;
123
+ for (const child of this._children) {
124
+ const childSize = this._calculateChildSize(child);
125
+ // Calculate child center (world).
126
+ const childCenter = new Vector(top + childSize.h / 2, left + childSize.w / 2, 0)
127
+ .rotateAngleAxis(yaw, [0, 0, 1])
128
+ .add(center);
129
+ // Position child.
130
+ if (child instanceof StaticObject) {
131
+ child.setPosition(childCenter);
132
+ child.setRotation(child.getRotation().compose([0, yaw, 0]));
133
+ }
134
+ else {
135
+ child.doLayoutAtPoint(childCenter, yaw);
136
+ }
137
+ // Move "cursor" to next open spot top-left.
138
+ if (this._isVertical) {
139
+ top += childSize.h + this._childDistance;
140
+ }
141
+ else {
142
+ left += childSize.w + this._childDistance;
143
+ }
144
+ }
145
+ return this;
146
+ }
147
+ }
@@ -0,0 +1,8 @@
1
+ import { GameObject } from "@tabletop-playground/api";
2
+ export declare class Layout {
3
+ private _anchorX;
4
+ private _anchorY;
5
+ getWidth(): number;
6
+ getHeight(): number;
7
+ add(obj: GameObject, weight?: number): void;
8
+ }
@@ -0,0 +1,13 @@
1
+ export class Layout {
2
+ constructor() {
3
+ this._anchorX = 0.5;
4
+ this._anchorY = 0.5;
5
+ }
6
+ getWidth() {
7
+ return 0;
8
+ }
9
+ getHeight() {
10
+ return 0;
11
+ }
12
+ add(obj, weight = 0) { }
13
+ }
@@ -0,0 +1,8 @@
1
+ export declare const locale: {
2
+ (key: string, replacement?: {
3
+ [key: string]: string | number;
4
+ } | undefined): string;
5
+ inject(dict: {
6
+ [key: string]: string;
7
+ }): void;
8
+ };
@@ -0,0 +1,40 @@
1
+ const REPLACE_REGEX = /(?<!\\){(?!#)(.*?)(?<!\\)}/gm;
2
+ const PLURAL_REGEX = /(?<!\\){#(.*?)(?<!\\)}/gm;
3
+ const PLURAL_SEPERATOR = /(?<!\\)\|/gm;
4
+ const _lang = {};
5
+ // Original author: ThatRobHuman
6
+ export const locale = (key, replacement = undefined) => {
7
+ const str = _lang[key];
8
+ if (!str) {
9
+ return key; // not registered, use key as stand-in
10
+ }
11
+ else if (!replacement) {
12
+ return str; // no need for regex
13
+ }
14
+ return str
15
+ .replace(REPLACE_REGEX, (match) => {
16
+ const r = replacement[match.substring(1, match.length - 1)].toString();
17
+ if (r === undefined) {
18
+ return match;
19
+ }
20
+ return r;
21
+ })
22
+ .replace(PLURAL_REGEX, (match) => {
23
+ const [val, singular, plural, zeroForm] = match
24
+ .substring(2, match.length - 1)
25
+ .split(PLURAL_SEPERATOR);
26
+ const num = Number(replacement[val]);
27
+ if (isNaN(num) || num === 0) {
28
+ return zeroForm || plural;
29
+ }
30
+ if (num > 1) {
31
+ return plural;
32
+ }
33
+ return singular;
34
+ });
35
+ };
36
+ locale.inject = (dict) => {
37
+ for (const [k, v] of Object.entries(dict)) {
38
+ _lang[k] = v;
39
+ }
40
+ };
@@ -0,0 +1,54 @@
1
+ import { Card, StaticObject } from "@tabletop-playground/api";
2
+ export type ParsedNSID = {
3
+ nsid: string;
4
+ type: string[];
5
+ source: string[];
6
+ name: string[];
7
+ extra: string | undefined;
8
+ };
9
+ export declare const DECK_NSID = "deck:?/?";
10
+ /**
11
+ * Object metadata field uses a simple "type:source/name|extra" string.
12
+ *
13
+ * Each component is a dot (".") delimited series of strings.
14
+ *
15
+ * TYPE delineates the hierarchy to the specific object type. Entries should
16
+ * start generic and get more specific, for instance "card.action" is a card
17
+ * from the action deck. The TYPE field should be sufficient to locate where
18
+ * the item belongs, in some cases augment with owning player slot.
19
+ *
20
+ * SOURCE is the produce/release with the item. For instance, "base" could
21
+ * mean the base game, or use an official expansion name. Recommend homebrew
22
+ * always start with "homebrew.{x}" where {x} is the identifier (to avoid
23
+ * confusion with canon content sources).
24
+ *
25
+ * NAME is the item name. It may include dot-delimited discriminators for
26
+ * different versions of the same item (e.g. "direct_hit.1" vs "direct_hit.2").
27
+ *
28
+ * EXTRA should be avoided, there may be rare cases wanting fruther metadata.
29
+ *
30
+ * @see https://github.com/TI4-Online/TI4-TTPG/wiki/NSID-Namespace
31
+ */
32
+ export declare abstract class NSID {
33
+ /**
34
+ * Create NSID from a metadata string or object. A deck with multiple cards
35
+ * gets a special "deck" NSID, consumers should call `stack` to get by card.
36
+ *
37
+ * @param input
38
+ * @returns NSID string
39
+ */
40
+ static get(input: StaticObject): string;
41
+ /**
42
+ * Get NSIDs for each card in a deck.
43
+ *
44
+ * @param input deck
45
+ * @returns NSID array, per-card values
46
+ */
47
+ static getDeck(input: Card): string[];
48
+ /**
49
+ * Parse this NSID into components (and sub-components, if dot delimited).
50
+ *
51
+ * @returns parsed
52
+ */
53
+ static parse(nsid: string): ParsedNSID | undefined;
54
+ }
@@ -0,0 +1,80 @@
1
+ import { Card, StaticObject } from "@tabletop-playground/api";
2
+ export const DECK_NSID = "deck:?/?";
3
+ /**
4
+ * Object metadata field uses a simple "type:source/name|extra" string.
5
+ *
6
+ * Each component is a dot (".") delimited series of strings.
7
+ *
8
+ * TYPE delineates the hierarchy to the specific object type. Entries should
9
+ * start generic and get more specific, for instance "card.action" is a card
10
+ * from the action deck. The TYPE field should be sufficient to locate where
11
+ * the item belongs, in some cases augment with owning player slot.
12
+ *
13
+ * SOURCE is the produce/release with the item. For instance, "base" could
14
+ * mean the base game, or use an official expansion name. Recommend homebrew
15
+ * always start with "homebrew.{x}" where {x} is the identifier (to avoid
16
+ * confusion with canon content sources).
17
+ *
18
+ * NAME is the item name. It may include dot-delimited discriminators for
19
+ * different versions of the same item (e.g. "direct_hit.1" vs "direct_hit.2").
20
+ *
21
+ * EXTRA should be avoided, there may be rare cases wanting fruther metadata.
22
+ *
23
+ * @see https://github.com/TI4-Online/TI4-TTPG/wiki/NSID-Namespace
24
+ */
25
+ export class NSID {
26
+ /**
27
+ * Create NSID from a metadata string or object. A deck with multiple cards
28
+ * gets a special "deck" NSID, consumers should call `stack` to get by card.
29
+ *
30
+ * @param input
31
+ * @returns NSID string
32
+ */
33
+ static get(input) {
34
+ let metadata = "";
35
+ if (input instanceof Card) {
36
+ if (input.getStackSize() === 1) {
37
+ // Singleton card: use the card metadata.
38
+ metadata = input.getCardDetails().metadata;
39
+ }
40
+ else {
41
+ // Deck: do not attempt to extract more here.
42
+ metadata = DECK_NSID;
43
+ }
44
+ }
45
+ else if (input instanceof StaticObject) {
46
+ // Non-card table or game object. Use template metadata.
47
+ metadata = input.getTemplateMetadata();
48
+ }
49
+ return metadata;
50
+ }
51
+ /**
52
+ * Get NSIDs for each card in a deck.
53
+ *
54
+ * @param input deck
55
+ * @returns NSID array, per-card values
56
+ */
57
+ static getDeck(input) {
58
+ return input.getAllCardDetails().map((cardDetails) => {
59
+ return cardDetails.metadata;
60
+ });
61
+ }
62
+ /**
63
+ * Parse this NSID into components (and sub-components, if dot delimited).
64
+ *
65
+ * @returns parsed
66
+ */
67
+ static parse(nsid) {
68
+ const m = nsid.match(/^([^:]+):([^/]+)\/([^|]+)\|?(.*)$/);
69
+ if (!m) {
70
+ return undefined;
71
+ }
72
+ return {
73
+ nsid: nsid,
74
+ type: m[1].split("."),
75
+ source: m[2].split("."),
76
+ name: m[3].split("."),
77
+ extra: m[4].length > 0 ? m[4] : undefined,
78
+ };
79
+ }
80
+ }
@@ -0,0 +1,4 @@
1
+ export declare class Shuffle {
2
+ static shuffle<T>(items: T[]): T[];
3
+ static choice<T>(items: T[]): T;
4
+ }
@@ -0,0 +1,13 @@
1
+ export class Shuffle {
2
+ static shuffle(items) {
3
+ // Fisher-Yates
4
+ for (let i = items.length - 1; i > 0; i--) {
5
+ let j = Math.floor(Math.random() * (i + 1));
6
+ [items[i], items[j]] = [items[j], items[i]];
7
+ }
8
+ return items;
9
+ }
10
+ static choice(items) {
11
+ return items[Math.floor(Math.random() * items.length)];
12
+ }
13
+ }
@@ -0,0 +1,12 @@
1
+ import { GameObject, Vector } from "@tabletop-playground/api";
2
+ /**
3
+ * Registry for NSID to template id.
4
+ */
5
+ export declare abstract class Spawn {
6
+ private static _nsidToTemplateId;
7
+ static spawn(nsid: string, position: Vector | [x: number, y: number, z: number]): GameObject | undefined;
8
+ static inject(dict: {
9
+ [key: string]: string;
10
+ }): void;
11
+ static validate(): void;
12
+ }
@@ -0,0 +1,38 @@
1
+ import { world } from "@tabletop-playground/api";
2
+ /**
3
+ * Registry for NSID to template id.
4
+ */
5
+ export class Spawn {
6
+ static spawn(nsid, position) {
7
+ const templateId = Spawn._nsidToTemplateId[nsid];
8
+ if (!templateId) {
9
+ throw new Error(`spawn nsid "${nsid}" has no templateId`);
10
+ }
11
+ const obj = world.createObjectFromTemplate(templateId, position);
12
+ return obj;
13
+ }
14
+ static inject(dict) {
15
+ for (const [k, v] of Object.entries(dict)) {
16
+ Spawn._nsidToTemplateId[k] = v;
17
+ }
18
+ }
19
+ static validate() {
20
+ const templateIds = new Set();
21
+ for (const pkg of world.getAllowedPackages()) {
22
+ for (const templateId of pkg.getTemplateIds()) {
23
+ templateIds.add(templateId);
24
+ }
25
+ }
26
+ const missing = [];
27
+ for (const [nsid, templateId] of Object.entries(Spawn._nsidToTemplateId)) {
28
+ if (!templateIds.has(templateId)) {
29
+ missing.push(`${templateId} ("${nsid}")`);
30
+ }
31
+ }
32
+ if (missing.length > 0) {
33
+ throw new Error(`Spawn.validate missing templateIds (${missing.length}):\n` +
34
+ missing.join("\n"));
35
+ }
36
+ }
37
+ }
38
+ Spawn._nsidToTemplateId = {};
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Lookalike for TTPG's MulticastDelegate, but with a trigger method.
3
+ */
4
+ export declare class TriggerableMulticastDelegate<T> {
5
+ private readonly _listeners;
6
+ /**
7
+ * Add a function to the trigger set.
8
+ *
9
+ * @param fn
10
+ */
11
+ add(fn: T): void;
12
+ /**
13
+ * Remove a function from the trigger set.
14
+ *
15
+ * @param fn
16
+ */
17
+ remove(fn: T): void;
18
+ /**
19
+ * Clear the trigger set.
20
+ */
21
+ clear(): void;
22
+ /**
23
+ * Call every function in the trigger set.
24
+ *
25
+ * Call every function even if one throws, raise gathered errors at end.
26
+ *
27
+ * @param args
28
+ */
29
+ trigger(...args: any): void;
30
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Lookalike for TTPG's MulticastDelegate, but with a trigger method.
3
+ */
4
+ export class TriggerableMulticastDelegate {
5
+ constructor() {
6
+ this._listeners = [];
7
+ }
8
+ /**
9
+ * Add a function to the trigger set.
10
+ *
11
+ * @param fn
12
+ */
13
+ add(fn) {
14
+ this._listeners.push(fn);
15
+ }
16
+ /**
17
+ * Remove a function from the trigger set.
18
+ *
19
+ * @param fn
20
+ */
21
+ remove(fn) {
22
+ for (let i = this._listeners.length - 1; i >= 0; i--) {
23
+ if (this._listeners[i] === fn) {
24
+ this._listeners.splice(i, 1);
25
+ }
26
+ }
27
+ }
28
+ /**
29
+ * Clear the trigger set.
30
+ */
31
+ clear() {
32
+ this._listeners.splice(0);
33
+ }
34
+ /**
35
+ * Call every function in the trigger set.
36
+ *
37
+ * Call every function even if one throws, raise gathered errors at end.
38
+ *
39
+ * @param args
40
+ */
41
+ trigger(...args) {
42
+ const errors = [];
43
+ for (const fn of this._listeners) {
44
+ try {
45
+ if (typeof fn === "function") {
46
+ fn(...args);
47
+ }
48
+ }
49
+ catch (e) {
50
+ if (e instanceof Error) {
51
+ errors.push(e);
52
+ }
53
+ }
54
+ }
55
+ if (errors.length === 1) {
56
+ throw errors[0];
57
+ }
58
+ else if (errors.length > 1) {
59
+ const message = `ERRORS (${errors.length}):\n` +
60
+ errors.map((e) => e.message).join("\n");
61
+ const stack = errors
62
+ .map((e) => `-----\n${e.stack ? e.stack : "N/A"}`)
63
+ .join("\n");
64
+ const error = new Error(message);
65
+ error.stack = stack;
66
+ throw error;
67
+ }
68
+ }
69
+ }
@@ -0,0 +1,8 @@
1
+ export declare class Vault {
2
+ private readonly _uid;
3
+ constructor(uid: string);
4
+ clear(): void;
5
+ delete(key: string): void;
6
+ put(key: string, value: string): void;
7
+ get(key: string): string | undefined;
8
+ }
@@ -0,0 +1,11 @@
1
+ export class Vault {
2
+ constructor(uid) {
3
+ this._uid = uid;
4
+ }
5
+ clear() { }
6
+ delete(key) { }
7
+ put(key, value) { }
8
+ get(key) {
9
+ return undefined;
10
+ }
11
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "ttpg-darrell",
3
+ "version": "1.0.0",
4
+ "description": "TTPG TypeScript library",
5
+ "main": "./build/cjs/index.js",
6
+ "module": "./build/esm/index.js",
7
+ "files": [
8
+ "build/"
9
+ ],
10
+ "scripts": {
11
+ "test": "jest",
12
+ "build": "tsc -p tsconfig-esm.json && tsc -p tsconfig-cjs.json",
13
+ "prepublishOnly": "yarn run build"
14
+ },
15
+ "author": "Darrell",
16
+ "license": "Unlicense",
17
+ "devDependencies": {
18
+ "@jest/globals": "^29.7.0",
19
+ "@types/fs-extra": "^11.0.4",
20
+ "@types/jest": "^29.5.10",
21
+ "@types/klaw-sync": "^6.0.5",
22
+ "@types/node": "^20.10.0",
23
+ "@types/yargs": "^17.0.32",
24
+ "fs-extra": "^11.2.0",
25
+ "jest": "^29.7.0",
26
+ "jest-environment-jsdom": "^29.7.0",
27
+ "klaw-sync": "^6.0.0",
28
+ "node": "^21.2.0",
29
+ "sharp": "^0.32.6",
30
+ "ts-jest": "^29.1.1",
31
+ "ts-node": "^10.9.1",
32
+ "ttpg-mock": "^1.0.7",
33
+ "typescript": "^5.3.2"
34
+ },
35
+ "dependencies": {
36
+ "@tabletop-playground/api": "^0.39.0"
37
+ }
38
+ }