@puzzmo/cli 1.0.81 → 1.0.82

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,59 @@
1
+ import { graphql, query } from "../util/belay.js";
2
+ const gamesForAccessTokenQuery = graphql `
3
+ query cliGamesForAccessTokenQuery($token: String!) {
4
+ gamesForAccessToken(token: $token) {
5
+ slug
6
+ }
7
+ }
8
+ `;
9
+ const validatePoolPuzzlesQuery = graphql `
10
+ query cliValidatePoolPuzzlesQuery($token: String!, $gameSlug: String!, $puzzles: [McpPuzzleFileInput!]!) {
11
+ validatePoolPuzzles(token: $token, gameSlug: $gameSlug, puzzles: $puzzles) {
12
+ filename
13
+ valid
14
+ errors
15
+ }
16
+ }
17
+ `;
18
+ const addPuzzlesToPoolMutation = graphql `
19
+ mutation cliAddPuzzlesToPoolMutation($token: String!, $gameSlug: String!, $puzzles: [McpPuzzleFileInput!]!) {
20
+ addPuzzlesToPool(token: $token, gameSlug: $gameSlug, puzzles: $puzzles) {
21
+ uploadedFilenames
22
+ failed {
23
+ filename
24
+ message
25
+ }
26
+ poolCount
27
+ }
28
+ }
29
+ `;
30
+ /** The slugs of the games a token can manage, or null when the server rejects the token. */
31
+ export const fetchGameSlugsForToken = async (apiURL, token) => {
32
+ const { data } = await query(gamesForAccessTokenQuery, { variables: { token }, url: `${apiURL}/graphql` });
33
+ return data?.gamesForAccessToken?.map((g) => g.slug) ?? null;
34
+ };
35
+ /** Checks puzzle files against the game's pool rules without uploading them. */
36
+ export const validatePoolPuzzles = async (apiURL, token, gameSlug, puzzles) => {
37
+ const { data, errors } = await query(validatePoolPuzzlesQuery, {
38
+ variables: { token, gameSlug, puzzles },
39
+ url: `${apiURL}/graphql`,
40
+ });
41
+ if (errors?.length)
42
+ throw new Error(errors.map((e) => e.message).join("; "));
43
+ if (!data?.validatePoolPuzzles)
44
+ throw new Error(`No game named "${gameSlug}" is accessible with that token.`);
45
+ return data.validatePoolPuzzles;
46
+ };
47
+ /** Adds puzzle files to the end of the game's pool; invalid files come back in `failed`. */
48
+ export const addPuzzlesToPool = async (apiURL, token, gameSlug, puzzles) => {
49
+ const { data, errors } = await query(addPuzzlesToPoolMutation, {
50
+ variables: { token, gameSlug, puzzles },
51
+ url: `${apiURL}/graphql`,
52
+ });
53
+ if (errors?.length)
54
+ throw new Error(errors.map((e) => e.message).join("; "));
55
+ if (!data?.addPuzzlesToPool)
56
+ throw new Error(`No game named "${gameSlug}" is accessible with that token.`);
57
+ return data.addPuzzlesToPool;
58
+ };
59
+ //# sourceMappingURL=puzzlePool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"puzzlePool.js","sourceRoot":"","sources":["../../src/queries/puzzlePool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAKjD,MAAM,wBAAwB,GAAG,OAAO,CAAA;;;;;;CAMvC,CAAA;AAED,MAAM,wBAAwB,GAAG,OAAO,CAAA;;;;;;;;CAQvC,CAAA;AAED,MAAM,wBAAwB,GAAG,OAAO,CAAA;;;;;;;;;;;CAWvC,CAAA;AAID,4FAA4F;AAC5F,MAAM,CAAC,MAAM,sBAAsB,GAAG,KAAK,EAAE,MAAc,EAAE,KAAa,EAA4B,EAAE;IACtG,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,KAAK,CAA8B,wBAAwB,EAAE,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,GAAG,MAAM,UAAU,EAAE,CAAC,CAAA;IACvI,OAAO,IAAI,EAAE,mBAAmB,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAA;AAC9D,CAAC,CAAA;AAED,gFAAgF;AAChF,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EAAE,MAAc,EAAE,KAAa,EAAE,QAAgB,EAAE,OAAqB,EAAE,EAAE;IAClH,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,KAAK,CAA8B,wBAAwB,EAAE;QAC1F,SAAS,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE;QACvC,GAAG,EAAE,GAAG,MAAM,UAAU;KACzB,CAAC,CAAA;IACF,IAAI,MAAM,EAAE,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IAC5E,IAAI,CAAC,IAAI,EAAE,mBAAmB;QAAE,MAAM,IAAI,KAAK,CAAC,kBAAkB,QAAQ,kCAAkC,CAAC,CAAA;IAC7G,OAAO,IAAI,CAAC,mBAAmB,CAAA;AACjC,CAAC,CAAA;AAED,4FAA4F;AAC5F,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,EAAE,MAAc,EAAE,KAAa,EAAE,QAAgB,EAAE,OAAqB,EAAE,EAAE;IAC/G,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,KAAK,CAA8B,wBAAwB,EAAE;QAC1F,SAAS,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE;QACvC,GAAG,EAAE,GAAG,MAAM,UAAU;KACzB,CAAC,CAAA;IACF,IAAI,MAAM,EAAE,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IAC5E,IAAI,CAAC,IAAI,EAAE,gBAAgB;QAAE,MAAM,IAAI,KAAK,CAAC,kBAAkB,QAAQ,kCAAkC,CAAC,CAAA;IAC1G,OAAO,IAAI,CAAC,gBAAgB,CAAA;AAC9B,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puzzmo/cli",
3
- "version": "1.0.81",
3
+ "version": "1.0.82",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "puzzmo": "./lib/index.js"
@@ -0,0 +1,121 @@
1
+ import fs from "node:fs"
2
+ import path from "node:path"
3
+
4
+ import { addPuzzlesToPool, fetchGameSlugsForToken, type PuzzleFile, validatePoolPuzzles } from "../queries/puzzlePool.js"
5
+ import { getTokens, isServerReachable, sortByServerPriority, sourceToURL, tokenHelp, type TokenEntry } from "../util/config.js"
6
+
7
+ type PuzzlesUploadOptions = {
8
+ game: string
9
+ /** Validate every file without uploading any */
10
+ dryRun?: boolean
11
+ }
12
+
13
+ // Matches the API's per-call cap
14
+ const maxBatchCount = 100
15
+ // The API's /graphql route takes 1MiB bodies, leave room for the query and JSON escaping
16
+ const maxBatchBytes = 900_000
17
+
18
+ /** CLI command: puzzmo puzzles upload <path> --game <slug> — adds puzzle files to the end of a game's puzzle pool */
19
+ export const puzzlesUpload = async (target: string, options: PuzzlesUploadOptions) => {
20
+ const files = readPuzzleFiles(target)
21
+ if (!files.length) {
22
+ console.error(`No puzzle files found at ${target}`)
23
+ process.exit(1)
24
+ }
25
+
26
+ const entry = await findTokenForGame(options.game)
27
+ if (!entry) {
28
+ console.error(`None of your saved tokens can manage a game with the slug "${options.game}".\n${tokenHelp}`)
29
+ process.exit(1)
30
+ }
31
+ const apiURL = sourceToURL(entry.source)
32
+
33
+ const oversized = files.filter((f) => payloadBytes(f) > maxBatchBytes)
34
+ if (oversized.length) {
35
+ for (const { filename } of oversized)
36
+ console.error(`FAIL ${filename}\n File is too large to upload (max ~${maxBatchBytes / 1000}KB)`)
37
+ console.error(`\n${oversized.length} of ${files.length} puzzle files are too large, nothing was uploaded.`)
38
+ process.exit(1)
39
+ }
40
+ const batches = toBatches(files)
41
+
42
+ // Validate everything up front so a bad file doesn't leave half a batch in the pool
43
+ const invalid: { filename: string; errors: readonly string[] }[] = []
44
+ for (const batch of batches) {
45
+ const results = await validatePoolPuzzles(apiURL, entry.token, options.game, batch)
46
+ invalid.push(...results.filter((r) => !r.valid))
47
+ }
48
+
49
+ if (invalid.length) {
50
+ for (const { filename, errors } of invalid) console.error(`FAIL ${filename}\n ${errors.join("\n ")}`)
51
+ console.error(`\n${invalid.length} of ${files.length} puzzle files are invalid, nothing was uploaded.`)
52
+ process.exit(1)
53
+ }
54
+
55
+ if (options.dryRun) {
56
+ console.log(`All ${files.length} puzzle files are valid for ${options.game}. Nothing was uploaded (--dry-run).`)
57
+ return
58
+ }
59
+
60
+ let uploaded = 0
61
+ let poolCount = 0
62
+ const failed: { filename: string; message: string }[] = []
63
+ for (const batch of batches) {
64
+ const result = await addPuzzlesToPool(apiURL, entry.token, options.game, batch)
65
+ uploaded += result.uploadedFilenames.length
66
+ poolCount = result.poolCount
67
+ failed.push(...result.failed)
68
+ }
69
+
70
+ for (const { filename, message } of failed) console.error(`FAIL ${filename}: ${message}`)
71
+ console.log(`Uploaded ${uploaded} puzzle${uploaded === 1 ? "" : "s"} to ${options.game}'s pool (${poolCount} waiting in the pool).`)
72
+ if (failed.length) process.exit(1)
73
+ }
74
+
75
+ /** Reads a single file, or every non-hidden file directly inside a directory, sorted by name */
76
+ const readPuzzleFiles = (target: string): PuzzleFile[] => {
77
+ const resolved = path.resolve(target)
78
+ if (!fs.existsSync(resolved)) return []
79
+
80
+ const paths = fs.statSync(resolved).isDirectory()
81
+ ? fs
82
+ .readdirSync(resolved, { withFileTypes: true })
83
+ .filter((d) => d.isFile() && !d.name.startsWith("."))
84
+ .map((d) => path.join(resolved, d.name))
85
+ : [resolved]
86
+
87
+ return paths
88
+ .sort((a, b) => path.basename(a).localeCompare(path.basename(b)))
89
+ .map((p) => ({ filename: path.basename(p), content: fs.readFileSync(p, "utf-8") }))
90
+ }
91
+
92
+ /** Finds the first saved token, in server priority order, whose team owns the game */
93
+ const findTokenForGame = async (gameSlug: string): Promise<TokenEntry | null> => {
94
+ for (const entry of sortByServerPriority(getTokens())) {
95
+ if (!(await isServerReachable(entry.source))) continue
96
+ const slugs = await fetchGameSlugsForToken(sourceToURL(entry.source), entry.token).catch(() => null)
97
+ if (slugs?.includes(gameSlug)) return entry
98
+ }
99
+ return null
100
+ }
101
+
102
+ /** Splits files into batches which stay under both the API's per-call count and its request size limit */
103
+ const toBatches = (files: PuzzleFile[]): PuzzleFile[][] => {
104
+ const batches: PuzzleFile[][] = []
105
+ let current: PuzzleFile[] = []
106
+ let currentBytes = 0
107
+ for (const file of files) {
108
+ const bytes = payloadBytes(file)
109
+ if (current.length && (current.length >= maxBatchCount || currentBytes + bytes > maxBatchBytes)) {
110
+ batches.push(current)
111
+ current = []
112
+ currentBytes = 0
113
+ }
114
+ current.push(file)
115
+ currentBytes += bytes
116
+ }
117
+ if (current.length) batches.push(current)
118
+ return batches
119
+ }
120
+
121
+ const payloadBytes = (file: PuzzleFile) => Buffer.byteLength(JSON.stringify(file), "utf-8")
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ import { type ChangedAgainst, changed } from "./commands/changed.js"
7
7
  import { gameCreate } from "./commands/game/create.js"
8
8
  import { login } from "./commands/login.js"
9
9
  import { migrate } from "./commands/migrate.js"
10
+ import { puzzlesUpload } from "./commands/puzzles.js"
10
11
  import { upload } from "./commands/upload.js"
11
12
  import { validate } from "./commands/validate.js"
12
13
  import { defaultSource } from "./util/config.js"
@@ -79,6 +80,21 @@ const gamesCommand = defineCommand({
79
80
  subCommands: { changed: changedCommand, upload: uploadCommand, validate: validateCommand },
80
81
  })
81
82
 
83
+ const puzzlesUploadCommand = defineCommand({
84
+ meta: { name: "upload", description: "Add puzzle files to the end of a game's puzzle pool" },
85
+ args: {
86
+ path: { type: "positional", description: "A puzzle file, or a directory of them", required: true },
87
+ game: { type: "string", description: "The game slug to upload puzzles for", required: true },
88
+ "dry-run": { type: "boolean", description: "Validate the files without uploading them" },
89
+ },
90
+ run: ({ args }) => puzzlesUpload(args.path, { game: args.game, dryRun: args["dry-run"] }),
91
+ })
92
+
93
+ const puzzlesCommand = defineCommand({
94
+ meta: { name: "puzzles", description: "Manage the puzzles scheduled for your games" },
95
+ subCommands: { upload: puzzlesUploadCommand },
96
+ })
97
+
82
98
  const migrateCommand = defineCommand({
83
99
  meta: { name: "migrate", description: "List and select migration skills from workshop.puzzmo.com" },
84
100
  run: () => migrate(),
@@ -145,6 +161,7 @@ const main = defineCommand({
145
161
  subCommands: {
146
162
  login: loginCommand,
147
163
  games: gamesCommand,
164
+ puzzles: puzzlesCommand,
148
165
  migrate: migrateCommand,
149
166
  game: gameCommand,
150
167
  upload: uploadAlias,
@@ -0,0 +1,157 @@
1
+ /**
2
+ * @generated SignedSource<<892c3aa4f6c7673e590b4a9bcc8514c2>>
3
+ * @lightSyntaxTransform
4
+ */
5
+
6
+ /* tslint:disable */
7
+ /* eslint-disable */
8
+ // @ts-nocheck
9
+
10
+ import { ConcreteRequest } from 'relay-runtime';
11
+ export type McpPuzzleFileInput = {
12
+ content: string;
13
+ filename: string;
14
+ };
15
+ export type cliAddPuzzlesToPoolMutation$variables = {
16
+ gameSlug: string;
17
+ puzzles: ReadonlyArray<McpPuzzleFileInput>;
18
+ token: string;
19
+ };
20
+ export type cliAddPuzzlesToPoolMutation$data = {
21
+ readonly addPuzzlesToPool: {
22
+ readonly failed: ReadonlyArray<{
23
+ readonly filename: string;
24
+ readonly message: string;
25
+ }>;
26
+ readonly poolCount: number;
27
+ readonly uploadedFilenames: ReadonlyArray<string>;
28
+ } | null | undefined;
29
+ };
30
+ export type cliAddPuzzlesToPoolMutation = {
31
+ response: cliAddPuzzlesToPoolMutation$data;
32
+ variables: cliAddPuzzlesToPoolMutation$variables;
33
+ };
34
+
35
+ const node: ConcreteRequest = (function(){
36
+ var v0 = {
37
+ "defaultValue": null,
38
+ "kind": "LocalArgument",
39
+ "name": "gameSlug"
40
+ },
41
+ v1 = {
42
+ "defaultValue": null,
43
+ "kind": "LocalArgument",
44
+ "name": "puzzles"
45
+ },
46
+ v2 = {
47
+ "defaultValue": null,
48
+ "kind": "LocalArgument",
49
+ "name": "token"
50
+ },
51
+ v3 = [
52
+ {
53
+ "alias": null,
54
+ "args": [
55
+ {
56
+ "kind": "Variable",
57
+ "name": "gameSlug",
58
+ "variableName": "gameSlug"
59
+ },
60
+ {
61
+ "kind": "Variable",
62
+ "name": "puzzles",
63
+ "variableName": "puzzles"
64
+ },
65
+ {
66
+ "kind": "Variable",
67
+ "name": "token",
68
+ "variableName": "token"
69
+ }
70
+ ],
71
+ "concreteType": "McpPoolUploadResult",
72
+ "kind": "LinkedField",
73
+ "name": "addPuzzlesToPool",
74
+ "plural": false,
75
+ "selections": [
76
+ {
77
+ "alias": null,
78
+ "args": null,
79
+ "kind": "ScalarField",
80
+ "name": "uploadedFilenames",
81
+ "storageKey": null
82
+ },
83
+ {
84
+ "alias": null,
85
+ "args": null,
86
+ "concreteType": "McpPoolUploadFailure",
87
+ "kind": "LinkedField",
88
+ "name": "failed",
89
+ "plural": true,
90
+ "selections": [
91
+ {
92
+ "alias": null,
93
+ "args": null,
94
+ "kind": "ScalarField",
95
+ "name": "filename",
96
+ "storageKey": null
97
+ },
98
+ {
99
+ "alias": null,
100
+ "args": null,
101
+ "kind": "ScalarField",
102
+ "name": "message",
103
+ "storageKey": null
104
+ }
105
+ ],
106
+ "storageKey": null
107
+ },
108
+ {
109
+ "alias": null,
110
+ "args": null,
111
+ "kind": "ScalarField",
112
+ "name": "poolCount",
113
+ "storageKey": null
114
+ }
115
+ ],
116
+ "storageKey": null
117
+ }
118
+ ];
119
+ return {
120
+ "fragment": {
121
+ "argumentDefinitions": [
122
+ (v0/*:: as any*/),
123
+ (v1/*:: as any*/),
124
+ (v2/*:: as any*/)
125
+ ],
126
+ "kind": "Fragment",
127
+ "metadata": null,
128
+ "name": "cliAddPuzzlesToPoolMutation",
129
+ "selections": (v3/*:: as any*/),
130
+ "type": "Mutation",
131
+ "abstractKey": null
132
+ },
133
+ "kind": "Request",
134
+ "operation": {
135
+ "argumentDefinitions": [
136
+ (v2/*:: as any*/),
137
+ (v0/*:: as any*/),
138
+ (v1/*:: as any*/)
139
+ ],
140
+ "kind": "Operation",
141
+ "name": "cliAddPuzzlesToPoolMutation",
142
+ "selections": (v3/*:: as any*/)
143
+ },
144
+ "params": {
145
+ "cacheID": "a779ada059e34a22a12c7175df0e50fc",
146
+ "id": null,
147
+ "metadata": {},
148
+ "name": "cliAddPuzzlesToPoolMutation",
149
+ "operationKind": "mutation",
150
+ "text": "mutation cliAddPuzzlesToPoolMutation(\n $token: String!\n $gameSlug: String!\n $puzzles: [McpPuzzleFileInput!]!\n) {\n addPuzzlesToPool(token: $token, gameSlug: $gameSlug, puzzles: $puzzles) {\n uploadedFilenames\n failed {\n filename\n message\n }\n poolCount\n }\n}\n"
151
+ }
152
+ };
153
+ })();
154
+
155
+ (node as any).hash = "f79544fe70b713f8304a444df02b2016";
156
+
157
+ export default node;
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @generated SignedSource<<76fc8a9b6e4d8ebe2c47a8b32617c0be>>
3
+ * @lightSyntaxTransform
4
+ */
5
+
6
+ /* tslint:disable */
7
+ /* eslint-disable */
8
+ // @ts-nocheck
9
+
10
+ import { ConcreteRequest } from 'relay-runtime';
11
+ export type cliGamesForAccessTokenQuery$variables = {
12
+ token: string;
13
+ };
14
+ export type cliGamesForAccessTokenQuery$data = {
15
+ readonly gamesForAccessToken: ReadonlyArray<{
16
+ readonly slug: string;
17
+ }> | null | undefined;
18
+ };
19
+ export type cliGamesForAccessTokenQuery = {
20
+ response: cliGamesForAccessTokenQuery$data;
21
+ variables: cliGamesForAccessTokenQuery$variables;
22
+ };
23
+
24
+ const node: ConcreteRequest = (function(){
25
+ var v0 = [
26
+ {
27
+ "defaultValue": null,
28
+ "kind": "LocalArgument",
29
+ "name": "token"
30
+ }
31
+ ],
32
+ v1 = [
33
+ {
34
+ "alias": null,
35
+ "args": [
36
+ {
37
+ "kind": "Variable",
38
+ "name": "token",
39
+ "variableName": "token"
40
+ }
41
+ ],
42
+ "concreteType": "AccessibleGame",
43
+ "kind": "LinkedField",
44
+ "name": "gamesForAccessToken",
45
+ "plural": true,
46
+ "selections": [
47
+ {
48
+ "alias": null,
49
+ "args": null,
50
+ "kind": "ScalarField",
51
+ "name": "slug",
52
+ "storageKey": null
53
+ }
54
+ ],
55
+ "storageKey": null
56
+ }
57
+ ];
58
+ return {
59
+ "fragment": {
60
+ "argumentDefinitions": (v0/*:: as any*/),
61
+ "kind": "Fragment",
62
+ "metadata": null,
63
+ "name": "cliGamesForAccessTokenQuery",
64
+ "selections": (v1/*:: as any*/),
65
+ "type": "Query",
66
+ "abstractKey": null
67
+ },
68
+ "kind": "Request",
69
+ "operation": {
70
+ "argumentDefinitions": (v0/*:: as any*/),
71
+ "kind": "Operation",
72
+ "name": "cliGamesForAccessTokenQuery",
73
+ "selections": (v1/*:: as any*/)
74
+ },
75
+ "params": {
76
+ "cacheID": "95e52459ee4e3b5b63aebf0a956d84b9",
77
+ "id": null,
78
+ "metadata": {},
79
+ "name": "cliGamesForAccessTokenQuery",
80
+ "operationKind": "query",
81
+ "text": "query cliGamesForAccessTokenQuery(\n $token: String!\n) {\n gamesForAccessToken(token: $token) {\n slug\n }\n}\n"
82
+ }
83
+ };
84
+ })();
85
+
86
+ (node as any).hash = "5e25f82f0177e712ee4d7e9e95245d28";
87
+
88
+ export default node;
@@ -0,0 +1,136 @@
1
+ /**
2
+ * @generated SignedSource<<8b45a4259087b0ba56f45d09b2de210b>>
3
+ * @lightSyntaxTransform
4
+ */
5
+
6
+ /* tslint:disable */
7
+ /* eslint-disable */
8
+ // @ts-nocheck
9
+
10
+ import { ConcreteRequest } from 'relay-runtime';
11
+ export type McpPuzzleFileInput = {
12
+ content: string;
13
+ filename: string;
14
+ };
15
+ export type cliValidatePoolPuzzlesQuery$variables = {
16
+ gameSlug: string;
17
+ puzzles: ReadonlyArray<McpPuzzleFileInput>;
18
+ token: string;
19
+ };
20
+ export type cliValidatePoolPuzzlesQuery$data = {
21
+ readonly validatePoolPuzzles: ReadonlyArray<{
22
+ readonly errors: ReadonlyArray<string>;
23
+ readonly filename: string;
24
+ readonly valid: boolean;
25
+ }> | null | undefined;
26
+ };
27
+ export type cliValidatePoolPuzzlesQuery = {
28
+ response: cliValidatePoolPuzzlesQuery$data;
29
+ variables: cliValidatePoolPuzzlesQuery$variables;
30
+ };
31
+
32
+ const node: ConcreteRequest = (function(){
33
+ var v0 = {
34
+ "defaultValue": null,
35
+ "kind": "LocalArgument",
36
+ "name": "gameSlug"
37
+ },
38
+ v1 = {
39
+ "defaultValue": null,
40
+ "kind": "LocalArgument",
41
+ "name": "puzzles"
42
+ },
43
+ v2 = {
44
+ "defaultValue": null,
45
+ "kind": "LocalArgument",
46
+ "name": "token"
47
+ },
48
+ v3 = [
49
+ {
50
+ "alias": null,
51
+ "args": [
52
+ {
53
+ "kind": "Variable",
54
+ "name": "gameSlug",
55
+ "variableName": "gameSlug"
56
+ },
57
+ {
58
+ "kind": "Variable",
59
+ "name": "puzzles",
60
+ "variableName": "puzzles"
61
+ },
62
+ {
63
+ "kind": "Variable",
64
+ "name": "token",
65
+ "variableName": "token"
66
+ }
67
+ ],
68
+ "concreteType": "McpPoolPuzzleValidation",
69
+ "kind": "LinkedField",
70
+ "name": "validatePoolPuzzles",
71
+ "plural": true,
72
+ "selections": [
73
+ {
74
+ "alias": null,
75
+ "args": null,
76
+ "kind": "ScalarField",
77
+ "name": "filename",
78
+ "storageKey": null
79
+ },
80
+ {
81
+ "alias": null,
82
+ "args": null,
83
+ "kind": "ScalarField",
84
+ "name": "valid",
85
+ "storageKey": null
86
+ },
87
+ {
88
+ "alias": null,
89
+ "args": null,
90
+ "kind": "ScalarField",
91
+ "name": "errors",
92
+ "storageKey": null
93
+ }
94
+ ],
95
+ "storageKey": null
96
+ }
97
+ ];
98
+ return {
99
+ "fragment": {
100
+ "argumentDefinitions": [
101
+ (v0/*:: as any*/),
102
+ (v1/*:: as any*/),
103
+ (v2/*:: as any*/)
104
+ ],
105
+ "kind": "Fragment",
106
+ "metadata": null,
107
+ "name": "cliValidatePoolPuzzlesQuery",
108
+ "selections": (v3/*:: as any*/),
109
+ "type": "Query",
110
+ "abstractKey": null
111
+ },
112
+ "kind": "Request",
113
+ "operation": {
114
+ "argumentDefinitions": [
115
+ (v2/*:: as any*/),
116
+ (v0/*:: as any*/),
117
+ (v1/*:: as any*/)
118
+ ],
119
+ "kind": "Operation",
120
+ "name": "cliValidatePoolPuzzlesQuery",
121
+ "selections": (v3/*:: as any*/)
122
+ },
123
+ "params": {
124
+ "cacheID": "934ec64898f9b820ebc69f223fd50cc5",
125
+ "id": null,
126
+ "metadata": {},
127
+ "name": "cliValidatePoolPuzzlesQuery",
128
+ "operationKind": "query",
129
+ "text": "query cliValidatePoolPuzzlesQuery(\n $token: String!\n $gameSlug: String!\n $puzzles: [McpPuzzleFileInput!]!\n) {\n validatePoolPuzzles(token: $token, gameSlug: $gameSlug, puzzles: $puzzles) {\n filename\n valid\n errors\n }\n}\n"
130
+ }
131
+ };
132
+ })();
133
+
134
+ (node as any).hash = "08673f877fe016f880516c4a9fdcf03d";
135
+
136
+ export default node;
@@ -0,0 +1,65 @@
1
+ import { graphql, query } from "../util/belay.js"
2
+ import type { cliAddPuzzlesToPoolMutation } from "./__generated__/cliAddPuzzlesToPoolMutation.graphql.js"
3
+ import type { cliGamesForAccessTokenQuery } from "./__generated__/cliGamesForAccessTokenQuery.graphql.js"
4
+ import type { cliValidatePoolPuzzlesQuery } from "./__generated__/cliValidatePoolPuzzlesQuery.graphql.js"
5
+
6
+ const gamesForAccessTokenQuery = graphql`
7
+ query cliGamesForAccessTokenQuery($token: String!) {
8
+ gamesForAccessToken(token: $token) {
9
+ slug
10
+ }
11
+ }
12
+ `
13
+
14
+ const validatePoolPuzzlesQuery = graphql`
15
+ query cliValidatePoolPuzzlesQuery($token: String!, $gameSlug: String!, $puzzles: [McpPuzzleFileInput!]!) {
16
+ validatePoolPuzzles(token: $token, gameSlug: $gameSlug, puzzles: $puzzles) {
17
+ filename
18
+ valid
19
+ errors
20
+ }
21
+ }
22
+ `
23
+
24
+ const addPuzzlesToPoolMutation = graphql`
25
+ mutation cliAddPuzzlesToPoolMutation($token: String!, $gameSlug: String!, $puzzles: [McpPuzzleFileInput!]!) {
26
+ addPuzzlesToPool(token: $token, gameSlug: $gameSlug, puzzles: $puzzles) {
27
+ uploadedFilenames
28
+ failed {
29
+ filename
30
+ message
31
+ }
32
+ poolCount
33
+ }
34
+ }
35
+ `
36
+
37
+ export type PuzzleFile = { filename: string; content: string }
38
+
39
+ /** The slugs of the games a token can manage, or null when the server rejects the token. */
40
+ export const fetchGameSlugsForToken = async (apiURL: string, token: string): Promise<string[] | null> => {
41
+ const { data } = await query<cliGamesForAccessTokenQuery>(gamesForAccessTokenQuery, { variables: { token }, url: `${apiURL}/graphql` })
42
+ return data?.gamesForAccessToken?.map((g) => g.slug) ?? null
43
+ }
44
+
45
+ /** Checks puzzle files against the game's pool rules without uploading them. */
46
+ export const validatePoolPuzzles = async (apiURL: string, token: string, gameSlug: string, puzzles: PuzzleFile[]) => {
47
+ const { data, errors } = await query<cliValidatePoolPuzzlesQuery>(validatePoolPuzzlesQuery, {
48
+ variables: { token, gameSlug, puzzles },
49
+ url: `${apiURL}/graphql`,
50
+ })
51
+ if (errors?.length) throw new Error(errors.map((e) => e.message).join("; "))
52
+ if (!data?.validatePoolPuzzles) throw new Error(`No game named "${gameSlug}" is accessible with that token.`)
53
+ return data.validatePoolPuzzles
54
+ }
55
+
56
+ /** Adds puzzle files to the end of the game's pool; invalid files come back in `failed`. */
57
+ export const addPuzzlesToPool = async (apiURL: string, token: string, gameSlug: string, puzzles: PuzzleFile[]) => {
58
+ const { data, errors } = await query<cliAddPuzzlesToPoolMutation>(addPuzzlesToPoolMutation, {
59
+ variables: { token, gameSlug, puzzles },
60
+ url: `${apiURL}/graphql`,
61
+ })
62
+ if (errors?.length) throw new Error(errors.map((e) => e.message).join("; "))
63
+ if (!data?.addPuzzlesToPool) throw new Error(`No game named "${gameSlug}" is accessible with that token.`)
64
+ return data.addPuzzlesToPool
65
+ }