@dice-plus/sdk 1.0.0 → 1.3.1

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/index.js CHANGED
@@ -33,7 +33,16 @@ var RELAY_LIMITS = {
33
33
  /** Wrong Passcodes for the room within 15 minutes before it refuses Passcode joins for 15 minutes. */
34
34
  passcodeTriesPerRoom: 50,
35
35
  passcodeRoomWindowMs: 900 * 1e3,
36
- passcodeRoomLockMs: 900 * 1e3
36
+ passcodeRoomLockMs: 900 * 1e3,
37
+ /** Roll Links' roll requests, per socket: the burst, then one every two seconds. */
38
+ rollRequestBurst: 5,
39
+ rollRequestsPerSecond: .5,
40
+ /** A roll request as JSON, and its notation. */
41
+ maxRollRequestBytes: 4 * 1024,
42
+ maxNotationLength: 500,
43
+ rollIdLength: 64,
44
+ /** Roll Links linked to one player at once. */
45
+ rollLinksPerPlayer: 3
37
46
  };
38
47
  /** A Room Code: 8 Crockford base32 characters, stored upper case without the dash. */
39
48
  var ROOM_CODE = /^[0-9A-HJKMNP-TV-Z]{8}$/;
@@ -60,7 +69,7 @@ function formatRoomCode(roomId) {
60
69
  }
61
70
  //#endregion
62
71
  //#region src/index.ts
63
- var SDK_VERSION = "1.0.0";
72
+ var SDK_VERSION = "1.3.1";
64
73
  var DEFAULT_BASE_URL = "https://dice-plus.missinglinkdev.com";
65
74
  /** Room Code and Passcode helpers, so a page never has to guess our formats. */
66
75
  var rooms = {
@@ -77,7 +86,7 @@ var rooms = {
77
86
  };
78
87
  /** One of our iframes: the handshake, calls and events every embed shares. */
79
88
  var Embed = class {
80
- constructor(options, path, title, style) {
89
+ constructor(options, path, title, style, attributes = {}) {
81
90
  this.listeners = /* @__PURE__ */ new Map();
82
91
  this.pending = /* @__PURE__ */ new Map();
83
92
  this.queued = [];
@@ -89,6 +98,7 @@ var Embed = class {
89
98
  this.onMessage = (event) => this.receive(event);
90
99
  if (!options?.container) throw new Error(`${title} needs a container element.`);
91
100
  this.apiKey = options.apiKey;
101
+ this.theme = options.theme;
92
102
  const base = new URL(options.baseUrl ?? "https://dice-plus.missinglinkdev.com");
93
103
  this.origin = base.origin;
94
104
  let settle;
@@ -104,6 +114,7 @@ var Embed = class {
104
114
  const frame = document.createElement("iframe");
105
115
  frame.src = new URL(path, base).href;
106
116
  frame.title = title;
117
+ for (const [name, value] of Object.entries(attributes)) frame.setAttribute(name, value);
107
118
  Object.assign(frame.style, {
108
119
  position: "absolute",
109
120
  inset: "0",
@@ -146,6 +157,11 @@ var Embed = class {
146
157
  this.pending.clear();
147
158
  this.listeners.clear();
148
159
  }
160
+ /** Your site's colors for the embed's UI; null for ours. */
161
+ setTheme(theme) {
162
+ this.theme = theme ?? void 0;
163
+ return this.call("setTheme", theme);
164
+ }
149
165
  /** The latest of an event, if it has come yet. */
150
166
  last(name) {
151
167
  return this.lastEvents.get(name);
@@ -185,6 +201,7 @@ var Embed = class {
185
201
  protocol: 1,
186
202
  sdkVersion: SDK_VERSION,
187
203
  apiKey: this.apiKey,
204
+ ...this.theme ? { theme: this.theme } : {},
188
205
  ...this.helloExtras()
189
206
  });
190
207
  }
@@ -248,13 +265,14 @@ var DicePlusTray = class extends Embed {
248
265
  background: "transparent",
249
266
  colorScheme: "normal",
250
267
  pointerEvents: "none"
268
+ }, {
269
+ allow: "autoplay",
270
+ "aria-hidden": "true",
271
+ tabindex: "-1"
251
272
  });
252
273
  this.settings = { ...options.settings ?? {} };
253
274
  this.identity = options.identity;
254
275
  this.dice = options.dice;
255
- this.frame.setAttribute("allow", "autoplay");
256
- this.frame.setAttribute("aria-hidden", "true");
257
- this.frame.tabIndex = -1;
258
276
  }
259
277
  helloExtras() {
260
278
  return {
@@ -318,6 +336,9 @@ var DicePlusTray = class extends Embed {
318
336
  unlink(guestId) {
319
337
  return this.invoke("unlink", guestId);
320
338
  }
339
+ answerLink(guestId, allow) {
340
+ return this.invoke("answerLink", guestId, allow);
341
+ }
321
342
  chooseNewPlayer() {
322
343
  return this.invoke("chooseNewPlayer");
323
344
  }
@@ -335,8 +356,7 @@ var DicePlusTray = class extends Embed {
335
356
  */
336
357
  var DicePlusBag = class extends Embed {
337
358
  constructor(options) {
338
- super(options, "/embed/bag.html", "Dice+ dice bag", {});
339
- this.frame.setAttribute("allow", "clipboard-write");
359
+ super(options, "/embed/bag.html", "Dice+ dice bag", {}, { allow: "clipboard-write" });
340
360
  }
341
361
  /** Whether the player is signed in and a patron, once the bag has said. */
342
362
  premium() {
@@ -1,8 +1,8 @@
1
- import { type BagEvents, type HelloMessage, type SiteDice, type TrayEvents, type TrayIdentity, type TrayMethods, type TraySettings, type UnauthorizedReason } from '../../src/embed/protocol';
1
+ import { type BagEvents, type EmbedTheme, type HelloMessage, type SiteDice, type TrayEvents, type TrayIdentity, type TrayMethods, type TraySettings, type UnauthorizedReason } from '../../src/embed/protocol';
2
2
  import { normalizeRoomId } from '../../src/platform/relay/protocol';
3
3
  import { formatRoomCode } from '../../src/platform/relay/roomCode';
4
4
  export type * from '../../src/embed/protocol';
5
- export declare const SDK_VERSION = "1.0.0";
5
+ export declare const SDK_VERSION = "1.3.1";
6
6
  export declare const DEFAULT_BASE_URL = "https://dice-plus.missinglinkdev.com";
7
7
  /** Room Code and Passcode helpers, so a page never has to guess our formats. */
8
8
  export declare const rooms: {
@@ -24,10 +24,12 @@ interface EmbedOptions {
24
24
  container: HTMLElement;
25
25
  /** Where our embeds are served from. Leave out except to test against a development build. */
26
26
  baseUrl?: string;
27
+ /** Your site's colors for the embed's UI. Dice keep their own look. */
28
+ theme?: EmbedTheme;
27
29
  }
28
30
  export interface DicePlusTrayOptions extends EmbedOptions {
29
31
  settings?: TraySettings;
30
- /** The player's name and colour as others in the Room see them */
32
+ /** The player's name and color as others in the Room see them */
31
33
  identity?: TrayIdentity;
32
34
  /** Your site's look for dice the player's bag does not cover */
33
35
  dice?: SiteDice;
@@ -66,13 +68,16 @@ declare abstract class Embed<Events extends CommonEvents> {
66
68
  private isReady;
67
69
  private destroyed;
68
70
  private refused;
71
+ private theme;
69
72
  private readonly onMessage;
70
- protected constructor(options: EmbedOptions, path: string, title: string, style: Partial<CSSStyleDeclaration>);
73
+ protected constructor(options: EmbedOptions, path: string, title: string, style: Partial<CSSStyleDeclaration>, attributes?: Record<string, string>);
71
74
  /** Listen for an event. Returns an unsubscribe. */
72
75
  on<E extends keyof Events>(name: E, listener: Listener<Events[E]>): () => void;
73
76
  once<E extends keyof Events>(name: E, listener: Listener<Events[E]>): () => void;
74
77
  /** Remove the embed and stop listening. */
75
78
  destroy(): void;
79
+ /** Your site's colors for the embed's UI; null for ours. */
80
+ setTheme(theme: EmbedTheme | null): Promise<void>;
76
81
  /** The latest of an event, if it has come yet. */
77
82
  protected last<E extends keyof Events>(name: E): Events[E] | undefined;
78
83
  /** What else this embed's hello carries. */
@@ -111,6 +116,7 @@ export declare class DicePlusTray extends Embed<DicePlusTrayEvents> {
111
116
  requestLink(playerId: string): Promise<void>;
112
117
  cancelLinkRequest(): Promise<void>;
113
118
  unlink(guestId: string): Promise<void>;
119
+ answerLink(guestId: string, allow: boolean): Promise<void>;
114
120
  chooseNewPlayer(): Promise<void>;
115
121
  reconnect(): Promise<void>;
116
122
  /** The latest of an event the tray repeats as state, if any yet. */
@@ -135,6 +135,22 @@ export interface TrayRollRequest {
135
135
  /** Look for dice the player's bag does not cover */
136
136
  dice?: SiteDice;
137
137
  }
138
+ /**
139
+ * A site's colors for an embed's UI, all `#rrggbb` and optional. Anything
140
+ * left out is worked out from the rest. Dice never take these.
141
+ */
142
+ export interface EmbedTheme {
143
+ /** Light or dark base; left out, it follows `background` */
144
+ mode?: 'light' | 'dark';
145
+ /** Buttons, the active tab, selected chips and links */
146
+ primary?: string;
147
+ /** The panel behind everything */
148
+ background?: string;
149
+ /** Cards, menus and dialogs; left out, a step off `background` */
150
+ surface?: string;
151
+ /** Text; left out, black or white to suit `mode` */
152
+ text?: string;
153
+ }
138
154
  export interface HelloMessage {
139
155
  tag: typeof EMBED_TAG;
140
156
  type: 'hello';
@@ -144,6 +160,7 @@ export interface HelloMessage {
144
160
  settings?: TraySettings;
145
161
  identity?: TrayIdentity;
146
162
  dice?: SiteDice;
163
+ theme?: EmbedTheme;
147
164
  }
148
165
  export type TrayMethods = {
149
166
  roll: (request: TrayRollRequest) => {
@@ -152,6 +169,7 @@ export type TrayMethods = {
152
169
  setSettings: (settings: TraySettings) => void;
153
170
  setIdentity: (identity: TrayIdentity) => void;
154
171
  setSiteDice: (dice: SiteDice | null) => void;
172
+ setTheme: (theme: EmbedTheme | null) => void;
155
173
  getHistory: () => HistoryEntry[];
156
174
  /** A new room with this browser as Host. In a room already, a new Room Code and Passcode: everyone else must rejoin */
157
175
  createRoom: () => void;
@@ -166,6 +184,8 @@ export type TrayMethods = {
166
184
  cancelLinkRequest: () => void;
167
185
  /** End a link: this browser's own, or (the player or a GM) another's */
168
186
  unlink: (guestId: string) => void;
187
+ /** This player's answer to one of `RoomState.linkAsks`. Unanswered, an ask lapses as a denial after 60 s */
188
+ answerLink: (guestId: string, allow: boolean) => void;
169
189
  /** Join an OBR room as a new player, not as one of its players */
170
190
  chooseNewPlayer: () => void;
171
191
  /** Try the room again now after the connection gave up */
@@ -259,6 +279,23 @@ export interface RoomState {
259
279
  };
260
280
  /** Joined a home room as a new player rather than as one of its players */
261
281
  asNewPlayer: boolean;
282
+ /**
283
+ * Others asking to roll as this player, oldest first: another screen of
284
+ * theirs, or the Dice+ browser extension. Show each with Allow and Deny
285
+ * (`answerLink`); unanswered, they lapse as a denial after 60 s.
286
+ */
287
+ linkAsks: LinkAskState[];
288
+ }
289
+ export interface LinkAskState {
290
+ guest: {
291
+ id: string;
292
+ name: string;
293
+ color: string;
294
+ };
295
+ /** 'extension': the Dice+ browser extension; 'web': a browser on a Dice+ site */
296
+ platform: string;
297
+ /** Milliseconds since the epoch; it lapses 60 s later */
298
+ at: number;
262
299
  }
263
300
  export interface NoticeEvent {
264
301
  message: string;
@@ -296,8 +333,10 @@ export interface PremiumState {
296
333
  /** A paying patron: premium materials, shapes, fonts and unlimited dice */
297
334
  isPremium: boolean;
298
335
  }
299
- /** The Bag Embed has nothing to call in v1; the player drives it. */
300
- export type BagMethods = Record<never, never>;
336
+ /** The player drives the bag; the site only sets its colors. */
337
+ export type BagMethods = {
338
+ setTheme: (theme: EmbedTheme | null) => void;
339
+ };
301
340
  export type BagEvents = {
302
341
  'premium-changed': PremiumState;
303
342
  /** The player pressed the bag's close button; hide it if you show it as a dialog */
@@ -33,8 +33,6 @@ export declare const STYLE_COMPONENT_MAP: Partial<Record<DiceType, Partial<Recor
33
33
  mesh: string;
34
34
  collider: string;
35
35
  }>>>>;
36
- /** The die that is physically thrown: a d100 is two d10s, a Fudge die is a d6 marked −1, 0, +1. */
37
- export declare function physicalDiceType(diceType: string): string;
38
36
  export declare function getDiceVariant(diceType: DiceType): DiceVariant | undefined;
39
37
  export declare function getDiceVariantSafe(diceType: DiceType): DiceVariant;
40
38
  export declare function getComponentsForStyle(diceType: DiceType, style: DiceStyle): {
@@ -1,9 +1,9 @@
1
1
  import type { DiceStyle, DiceType } from '../dice/types/DiceTypes';
2
2
  /** One die's look, from the free fields of the standard material. Anything left out keeps our default. */
3
3
  export interface StandardDiceStyle {
4
- /** Body colour, `#rrggbb` */
4
+ /** Body color, `#rrggbb` */
5
5
  color?: string;
6
- /** Number colour, `#rrggbb` */
6
+ /** Number color, `#rrggbb` */
7
7
  numberColor?: string;
8
8
  /** Free fonts only: 'standard', 'norse' or 'aelfa' */
9
9
  font?: string;
@@ -20,8 +20,13 @@ export interface StandardDiceStyle {
20
20
  /** 0–1, how deep the numbers are engraved */
21
21
  numberDepth?: number;
22
22
  }
23
- /** The look for a roll, or for the whole site: a default, and per die type. */
23
+ /** The look for a roll, or for the whole site: a default, per die type, and per named die. */
24
24
  export interface SiteDice {
25
25
  default?: StandardDiceStyle;
26
26
  byType?: Partial<Record<DiceType, StandardDiceStyle>>;
27
+ /**
28
+ * Dice the notation calls by name, as in `1d12{Hope}`, for players whose
29
+ * bag has no die of that name
30
+ */
31
+ byName?: Record<string, StandardDiceStyle>;
27
32
  }
@@ -1,5 +1,10 @@
1
1
  export declare const RELAY_PROTOCOL = 1;
2
- export type RelayPlatform = 'web' | 'obr' | 'embed' | 'foundry';
2
+ /** 'extension': a Roll Link (the Dice+ browser extension), which rolls on its linked player's tray. */
3
+ export type RelayPlatform = 'web' | 'obr' | 'embed' | 'foundry' | 'extension';
4
+ /** The `source` a Roll Link's roll requests carry, so its errors can find their way back. */
5
+ export declare const ROLL_LINK_SOURCE = "browser-extension";
6
+ /** The channel a Roll Link's roll request reaches its player's tray on: the external roll request. */
7
+ export declare const ROLL_LINK_REQUEST_CHANNEL = "dice-plus/roll-request";
3
8
  /**
4
9
  * 'web' rooms are temporary sessions created on the website; 'home' rooms
5
10
  * belong to a dedicated client's own room (an OBR room, a Foundry world).
@@ -20,7 +25,7 @@ export interface RelayMember extends RelayPlayer {
20
25
  gm: boolean;
21
26
  /**
22
27
  * A Linked Guest: the Home Group player it rolls as. Its messages come from
23
- * that id, and its name, colour and GM Tag are that player's.
28
+ * that id, and its name, color and GM Tag are that player's.
24
29
  */
25
30
  linkedTo?: string;
26
31
  }
@@ -36,8 +41,20 @@ export type LinkRefusal = 'denied'
36
41
  | 'cooldown'
37
42
  /** That player denied this connection too often. */
38
43
  | 'blocked'
39
- /** Not a home Room. */
40
- | 'unavailable';
44
+ /** The Home Group and the Host cannot link, nor can anyone link to themselves. */
45
+ | 'unavailable'
46
+ /** That player already has as many Roll Links as the Relay allows. */
47
+ | 'full';
48
+ /** Why the Relay did not pass on a Roll Link's roll request. */
49
+ export type RollRefusal =
50
+ /** Not linked to a player. */
51
+ 'unlinked'
52
+ /** The linked player has no connection that can roll. */
53
+ | 'unavailable'
54
+ /** Too many roll requests; wait a moment. */
55
+ | 'rateLimited'
56
+ /** Missing a roll id, the notation is too long, or the request is too large. */
57
+ | 'invalid';
41
58
  /** Who a message is for. Absent: everyone. Otherwise GM-tagged members and/or these player ids. */
42
59
  export interface Audience {
43
60
  gm?: boolean;
@@ -86,7 +103,7 @@ export type ClientMessage = {
86
103
  t: 'setPasscode';
87
104
  passcode: string | null;
88
105
  }
89
- /** A Guest in a home Room: ask this Home Group player to let it roll as them. */
106
+ /** A Guest: ask this player (one in their own right, not a Linked Guest) to let it roll as them. */
90
107
  | {
91
108
  t: 'linkRequest';
92
109
  player: string;
@@ -106,6 +123,15 @@ export type ClientMessage = {
106
123
  | {
107
124
  t: 'unlink';
108
125
  guest: string;
126
+ }
127
+ /**
128
+ * A Linked Guest with no tray of its own (a Roll Link): roll this on the
129
+ * linked player's tray. `data` is a dice-plus/roll-request; the Relay sets
130
+ * its player.
131
+ */
132
+ | {
133
+ t: 'rollRequest';
134
+ data: unknown;
109
135
  };
110
136
  export interface RelayEnvelope {
111
137
  from: string;
@@ -124,10 +150,11 @@ export type ServerMessage = {
124
150
  } | ({
125
151
  t: 'msg';
126
152
  } & RelayEnvelope)
127
- /** To the asked player's sockets: a Guest wants to roll as them. */
153
+ /** To the asked player's sockets: a Guest wants to roll as them, from this platform (absent from older relays). */
128
154
  | {
129
155
  t: 'linkAsk';
130
156
  guest: RelayPlayer;
157
+ platform?: RelayPlatform;
131
158
  }
132
159
  /** To the asked player's sockets: the request is over (answered, cancelled, or the Guest left). */
133
160
  | {
@@ -147,6 +174,12 @@ export type ServerMessage = {
147
174
  /** To a Linked Guest: the link was ended. */
148
175
  | {
149
176
  t: 'unlinked';
177
+ }
178
+ /** To a Roll Link: its roll request went nowhere. */
179
+ | {
180
+ t: 'rollRefused';
181
+ rollId: string;
182
+ reason: RollRefusal;
150
183
  };
151
184
  /** WebSocket close codes the Relay uses; clients stop reconnecting on all but `replaced`. */
152
185
  export declare const RELAY_CLOSE: {
@@ -191,6 +224,15 @@ export declare const RELAY_LIMITS: {
191
224
  readonly passcodeTriesPerRoom: 50;
192
225
  readonly passcodeRoomWindowMs: number;
193
226
  readonly passcodeRoomLockMs: number;
227
+ /** Roll Links' roll requests, per socket: the burst, then one every two seconds. */
228
+ readonly rollRequestBurst: 5;
229
+ readonly rollRequestsPerSecond: 0.5;
230
+ /** A roll request as JSON, and its notation. */
231
+ readonly maxRollRequestBytes: number;
232
+ readonly maxNotationLength: 500;
233
+ readonly rollIdLength: 64;
234
+ /** Roll Links linked to one player at once. */
235
+ readonly rollLinksPerPlayer: 3;
194
236
  };
195
237
  export declare const ROOM_ID_PATTERN: RegExp;
196
238
  /** A Passcode: six digits, leading zeros kept. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dice-plus/sdk",
3
- "version": "1.0.0",
3
+ "version": "1.3.1",
4
4
  "description": "Embed the Dice+ 3D physics dice tray and dice bag on your site",
5
5
  "type": "module",
6
6
  "license": "MIT",