@anweather/game-plugin-ssdk 1.8.0 → 1.10.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.
@@ -0,0 +1,74 @@
1
+ export const blobOwner = {
2
+ save: (saveFileId) => `save:${saveFileId}`,
3
+ player: (participantId) => `player:${participantId}`,
4
+ world: (worldId) => `world:${worldId}`,
5
+ };
6
+ export class BlobStoreError extends Error {
7
+ type;
8
+ status;
9
+ details;
10
+ constructor(type, status, details) {
11
+ super(`${type}: ${String(details.message ?? status)}`);
12
+ this.type = type;
13
+ this.status = status;
14
+ this.details = details;
15
+ this.name = "BlobStoreError";
16
+ }
17
+ }
18
+ export function isVersionConflict(e) {
19
+ return e instanceof BlobStoreError && e.type === "VersionConflict";
20
+ }
21
+ const ERROR_TYPES = new Set([
22
+ "VersionConflict", "BlobTooLarge", "BlobQuotaExceeded", "BlobOwnerNotFound", "InvalidBlobRequest", "PluginUnauthorized",
23
+ ]);
24
+ export function createBlobClient(options) {
25
+ const doFetch = options.fetchImpl ?? fetch;
26
+ const timeoutMs = options.timeoutMs ?? 5000;
27
+ const root = `${options.platformUrl}/internal/plugins/${encodeURIComponent(options.gameType)}/blobs`;
28
+ async function call(method, path, body) {
29
+ const headers = {};
30
+ if (options.authToken)
31
+ headers["X-Plugin-Auth"] = `Bearer ${options.authToken}`;
32
+ if (body !== undefined)
33
+ headers["Content-Type"] = "application/json";
34
+ const res = await doFetch(`${root}/${path}`, {
35
+ method,
36
+ headers,
37
+ body: body === undefined ? undefined : JSON.stringify(body),
38
+ signal: AbortSignal.timeout(timeoutMs),
39
+ });
40
+ const text = await res.text();
41
+ const parsed = text ? JSON.parse(text) : {};
42
+ if (res.ok)
43
+ return parsed;
44
+ const type = (res.headers.get("X-Amzn-Errortype") ?? parsed.__type ?? "");
45
+ if (ERROR_TYPES.has(type))
46
+ throw new BlobStoreError(type, res.status, parsed);
47
+ throw new Error(`blob store ${method} ${path}: ${res.status} ${text.slice(0, 200)}`);
48
+ }
49
+ const at = (owner, key) => key === undefined ? encodeURIComponent(owner) : `${encodeURIComponent(owner)}/${encodeURIComponent(key)}`;
50
+ return {
51
+ get: (owner, key) => call("GET", at(owner, key)),
52
+ put: (owner, key, value, expectedVersion) => call("PUT", at(owner, key), { value, expectedVersion }),
53
+ delete: (owner, key, expectedVersion) => call("DELETE", at(owner, key) + (expectedVersion === undefined ? "" : `?expectedVersion=${expectedVersion}`)),
54
+ batchGet: async (owner, keys) => (await call("POST", `${at(owner)}/batch-get`, { keys })).items,
55
+ batchPut: async (owner, writes) => (await call("POST", `${at(owner)}/batch-put`, { writes })).items,
56
+ list: (owner) => call("GET", at(owner)),
57
+ };
58
+ }
59
+ export async function casUpdate(client, owner, key, change, maxAttempts = 5) {
60
+ for (let attempt = 1;; attempt++) {
61
+ const current = await client.get(owner, key);
62
+ const next = change(current.version === 0 ? undefined : current.value);
63
+ if (next === undefined)
64
+ return { value: current.value, version: current.version };
65
+ try {
66
+ const { version } = await client.put(owner, key, next, current.version);
67
+ return { value: next, version };
68
+ }
69
+ catch (e) {
70
+ if (!isVersionConflict(e) || attempt >= maxAttempts)
71
+ throw e;
72
+ }
73
+ }
74
+ }
@@ -4,3 +4,4 @@ export { servePlugin, createPluginApp } from "./servePlugin";
4
4
  export { createRegistrationLoop } from "./registration";
5
5
  export { runConformance, assertConformance } from "./conformance";
6
6
  export { PLUGIN_PROTOCOL_VERSION, runtimeVersion } from "./version";
7
+ export { createBlobClient, casUpdate, blobOwner, BlobStoreError, isVersionConflict } from "./blobClient";
@@ -108,6 +108,14 @@ export var PluginStateUpdate;
108
108
  memberValidators["awaitingParticipantIds"] = new __CompositeCollectionValidator(new __NoOpValidator(), new __NoOpValidator());
109
109
  break;
110
110
  }
111
+ case "undoDisabled": {
112
+ memberValidators["undoDisabled"] = new __NoOpValidator();
113
+ break;
114
+ }
115
+ case "undoBarrier": {
116
+ memberValidators["undoBarrier"] = new __NoOpValidator();
117
+ break;
118
+ }
111
119
  }
112
120
  }
113
121
  return memberValidators[member];
@@ -117,6 +125,8 @@ export var PluginStateUpdate;
117
125
  ...getMemberValidator("pluginState").validate(obj.pluginState, `${path}/pluginState`),
118
126
  ...getMemberValidator("loggedPayload").validate(obj.loggedPayload, `${path}/loggedPayload`),
119
127
  ...getMemberValidator("awaitingParticipantIds").validate(obj.awaitingParticipantIds, `${path}/awaitingParticipantIds`),
128
+ ...getMemberValidator("undoDisabled").validate(obj.undoDisabled, `${path}/undoDisabled`),
129
+ ...getMemberValidator("undoBarrier").validate(obj.undoBarrier, `${path}/undoBarrier`),
120
130
  ];
121
131
  };
122
132
  })(PluginStateUpdate || (PluginStateUpdate = {}));
@@ -428,6 +428,8 @@ export const serializeApplyMoveResponse = async (input, ctx) => {
428
428
  'loggedPayload': _ => se_Document(_, context),
429
429
  'nextTurnParticipantId': [],
430
430
  'pluginState': _ => se_Document(_, context),
431
+ 'undoBarrier': [],
432
+ 'undoDisabled': [],
431
433
  }));
432
434
  if (body && Object.keys(headers).map((str) => str.toLowerCase()).indexOf('content-length') === -1) {
433
435
  const length = calculateBodyLength(body);
@@ -740,6 +742,8 @@ export const serializeInitializeGameResponse = async (input, ctx) => {
740
742
  'loggedPayload': _ => se_Document(_, context),
741
743
  'nextTurnParticipantId': [],
742
744
  'pluginState': _ => se_Document(_, context),
745
+ 'undoBarrier': [],
746
+ 'undoDisabled': [],
743
747
  }));
744
748
  if (body && Object.keys(headers).map((str) => str.toLowerCase()).indexOf('content-length') === -1) {
745
749
  const length = calculateBodyLength(body);