@slugbugblue/trax-cli 0.11.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,188 @@
1
+ /** @file Puzzles CLI
2
+ * @copyright 2022-2023
3
+ * @author Chad Transtrum <chad@transtrum.net>
4
+ * @license Apache-2.0
5
+ */
6
+
7
+ import { Trax } from '@slugbugblue/trax'
8
+ import { levels, puzzles, sources } from '@slugbugblue/trax-puzzles'
9
+
10
+ let maxLevel = 0
11
+ const puzzleIds = []
12
+ for (const puzzle of puzzles) {
13
+ maxLevel = Math.max(maxLevel, puzzle.level)
14
+ puzzleIds.push(puzzle.id)
15
+ }
16
+
17
+ const validLevels = levels.slice(0, maxLevel + 1)
18
+ const completions = [...validLevels, ...puzzleIds].join('|')
19
+
20
+ const listAliases = ['display', 'list', 'ls', 'show', 'view']
21
+ const startAliases = ['new', 'start']
22
+
23
+ export const puzzlesCmd = {
24
+ name: 'puzzles',
25
+ args: '["list"|"start"] [level|puzzle]',
26
+ comp: 'display|list|ls|new|show|start|view ' + completions,
27
+ desc: 'view and play puzzles',
28
+ help: [
29
+ 'Practice your skills with puzzles. Use "list" to see the puzzle',
30
+ 'categories, or "start" to play a puzzle.',
31
+ ],
32
+ }
33
+
34
+ const an = (word = '') => {
35
+ const a = /^[aeiou]/.test(word) ? 'an' : 'a'
36
+ return a + ' ' + word
37
+ }
38
+
39
+ const showPuzzle = (CLI, id, started = false) => {
40
+ let puzzle = puzzles.find((p) => p.id === id)
41
+ if (!puzzle) {
42
+ const possibles = puzzles.filter((p) => p.id.includes(id))
43
+ if (possibles.length === 1) puzzle = possibles[0]
44
+ }
45
+
46
+ if (!puzzle) return CLI.error(`Puzzle ${id} not found`)
47
+
48
+ const trax = new Trax(puzzle.game, puzzle.notation, 'cli')
49
+ const players = ['white', 'black']
50
+ CLI.display(trax, players)
51
+ let out = CLI.color.short(puzzle.id)
52
+ out += CLI.color(` is ${an(levels[puzzle.level])} puzzle`) + '\n'
53
+ if (puzzle.title) out += CLI.color(puzzle.title) + '\n'
54
+ if (puzzle.desc) out += CLI.color.help(puzzle.desc) + '\n'
55
+ out += CLI.color(players[puzzle.player - 1] + ' to win by move ' + puzzle.max)
56
+ const src = sources[puzzle.src]
57
+
58
+ if (src) {
59
+ let source = '\nPuzzle '
60
+ source += src.copyright
61
+ ? 'copyright ' + src.copyright + ' by'
62
+ : 'courtesy of'
63
+ source += ' ' + src.name
64
+ const url = src.url ? ' ' + CLI.color.help(src.url) : ''
65
+ out += CLI.color.white(source) + url
66
+ if (src.license) {
67
+ out += '\n' + CLI.color.optional('License: ' + src.license)
68
+ if (src.licenseUrl) out += CLI.color.black(' ' + src.licenseUrl)
69
+ }
70
+ }
71
+
72
+ CLI.out(out)
73
+
74
+ if (started) return
75
+
76
+ CLI.out(
77
+ CLI.color.short(
78
+ `Want to try this puzzle? Enter: "puzzles start ${puzzle.id}"`,
79
+ ),
80
+ )
81
+ }
82
+
83
+ const listPuzzles = (CLI) => {
84
+ let size = 1
85
+ const data = []
86
+ for (const name of validLevels) {
87
+ size = Math.max(size, name.length)
88
+ data.push({ name, count: 0, solved: 0, selected: false })
89
+ }
90
+
91
+ if (data[CLI.puzzleLevel]) data[CLI.puzzleLevel].selected = true
92
+
93
+ for (const puzzle of puzzles) {
94
+ data[puzzle.level].count += 1
95
+ if (CLI.puzzle(puzzle.id).solved > 0) data[puzzle.level].solved += 1
96
+ }
97
+
98
+ for (const category of data) {
99
+ let out = category.selected ? CLI.color('* ') : ' '
100
+ out += CLI.color.short(category.name.padEnd(size + 1))
101
+ if (category.solved === 0) {
102
+ out += CLI.color(CLI.plural(category.count, 'puzzle'))
103
+ } else if (category.solved === category.count) {
104
+ if (category.count > 3) out += CLI.color.success('all ')
105
+ out += CLI.color.success(CLI.plural(category.count, 'puzzle') + ' solved')
106
+ } else {
107
+ out += CLI.color.success(`${category.solved} solved`)
108
+ out += CLI.color(` of ${category.count} puzzles`)
109
+ }
110
+
111
+ CLI.out(out)
112
+ }
113
+ }
114
+
115
+ const listLevel = (CLI, levelName) => {
116
+ let size = 1
117
+ const level = levels.indexOf(levelName)
118
+ const data = []
119
+ for (const puzzle of puzzles) {
120
+ if (puzzle.level !== level) continue
121
+ size = Math.max(size, puzzle.id.length)
122
+ data.push({
123
+ id: puzzle.id,
124
+ title: puzzle.title,
125
+ solved: CLI.puzzle(puzzle.id).solved > 0,
126
+ })
127
+ }
128
+
129
+ for (const puzzle of data) {
130
+ let out = CLI.color.success(puzzle.solved ? '✓' : ' ')
131
+ out += CLI.color.short(' ' + puzzle.id)
132
+ if (puzzle.title) out += CLI.color(' ' + puzzle.title)
133
+ CLI.out(out)
134
+ }
135
+ }
136
+
137
+ const startPuzzle = (CLI, puzzle) => {
138
+ const id = CLI.newPuzzle(puzzle)
139
+ CLI.out(CLI.color(`Started puzzle ${puzzle.id} as game #${id}`))
140
+ showPuzzle(CLI, puzzle.id, true)
141
+ }
142
+
143
+ const startNextPuzzle = (CLI, level) => {
144
+ if (level === undefined) level = CLI.puzzleLevel
145
+ const unsolved = puzzles.filter((p) => !CLI.puzzle(p.id).solved)
146
+ if (unsolved.length === 0) {
147
+ CLI.out(CLI.success('You have already completed all the puzzles.'))
148
+ return CLI.error('Specify a specific puzzle to try one again.')
149
+ }
150
+
151
+ const next = unsolved.find((p) => p.level >= level) || unsolved[0]
152
+ startPuzzle(CLI, next)
153
+ }
154
+
155
+ puzzlesCmd.fn = (CLI, action, id) => {
156
+ action = action?.toLowerCase() ?? 'list'
157
+ if (startAliases.some((alias) => alias.startsWith(action))) {
158
+ if (!id) return startNextPuzzle(CLI)
159
+
160
+ const puzzle = puzzles.find((p) => p.id === id)
161
+ if (puzzle) {
162
+ return startPuzzle(CLI, puzzle)
163
+ }
164
+
165
+ if (validLevels.includes(id)) {
166
+ return startNextPuzzle(CLI, levels.indexOf(id))
167
+ }
168
+
169
+ const possibles = puzzles.filter((p) => p.id.includes(id))
170
+ if (possibles.length === 1) {
171
+ return startPuzzle(CLI, possibles[0])
172
+ }
173
+
174
+ return CLI.error('Could not find puzzle ' + id)
175
+ }
176
+
177
+ if (action) {
178
+ const x = id || action
179
+ if (validLevels.includes(x)) return listLevel(CLI, x)
180
+ if (listAliases.some((alias) => alias.startsWith(x))) {
181
+ return listPuzzles(CLI)
182
+ }
183
+
184
+ return showPuzzle(CLI, x)
185
+ }
186
+
187
+ listPuzzles(CLI)
188
+ }
@@ -0,0 +1,47 @@
1
+ /* Copyright 2022 Chad Transtrum
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ // CLI select command
17
+
18
+ export const selectCmd = {
19
+ name: 'select',
20
+ alt: ['choose', '#'],
21
+ args: '#id [command]',
22
+ comp: '<id> <cmd>',
23
+ desc: 'select a different game',
24
+ rx: /^#\d+$/,
25
+ help: [
26
+ 'Make another game the default game of future commands. If another',
27
+ 'command is included, that command will be run immediately.',
28
+ ],
29
+ }
30
+
31
+ selectCmd.fn = (CLI, id, ...cmds) => {
32
+ if (!id) {
33
+ CLI.error('Missing id.')
34
+ return CLI.do('help', 'select')
35
+ }
36
+
37
+ if (id.startsWith('#')) id = id.slice(1)
38
+ CLI.load(id)
39
+ if (String(CLI.GAME?.id) === id) {
40
+ CLI.out(CLI.color(`Game #${id} selected.`))
41
+ if (cmds.length > 0) {
42
+ CLI.doNext(cmds.join(' '))
43
+ }
44
+ } else {
45
+ CLI.error('Invalid id. Type "list" to see available games.')
46
+ }
47
+ }
@@ -0,0 +1,55 @@
1
+ /* Copyright 2022-2023 Chad Transtrum
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ // CLI suggest command
17
+
18
+ import { suggest } from '@slugbugblue/trax-analyst'
19
+ import { timeString } from '@slugbugblue/trax-cli/utils'
20
+
21
+ export const suggestCmd = {
22
+ name: 'suggest',
23
+ alt: ['bot'],
24
+ desc: 'suggest a move',
25
+ help: ['Suggest a move in the current game.'],
26
+ }
27
+
28
+ suggestCmd.fn = (CLI, debug) => {
29
+ if (!String(CLI.GAME?.id)) {
30
+ return CLI.error('No active game. Type "new" to start a game.')
31
+ }
32
+
33
+ const game = CLI.TRAX
34
+ const suggestion = suggest(game)
35
+
36
+ if (!suggestion.best) return
37
+
38
+ CLI.out(
39
+ CLI.bubble(
40
+ game.turn === 1 ? 'w' : 'b',
41
+ String(game.move + 1) + '. ' + suggestion.pick.move,
42
+ ),
43
+ )
44
+
45
+ if ('debug'.startsWith(debug)) {
46
+ const m = []
47
+ for (const move of suggestion.options) {
48
+ m.push(move.move + ' ' + move.score)
49
+ }
50
+
51
+ CLI.out(
52
+ '[' + m.join(', ') + '] ' + CLI.color.black(timeString(suggestion.ms)),
53
+ )
54
+ }
55
+ }
@@ -0,0 +1,40 @@
1
+ /* Copyright 2022 Chad Transtrum
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ // CLI undo command
17
+
18
+ export const undoCmd = {
19
+ name: 'undo',
20
+ args: '[n]',
21
+ comp: '<move>',
22
+ desc: 'undo the last move in a game',
23
+ help: [
24
+ 'Backtrack one or more moves in a game. Type "undo" to take back the last',
25
+ 'move, or "undo 2" to undo the last two moves.',
26
+ ],
27
+ }
28
+
29
+ undoCmd.fn = (CLI, n) => {
30
+ n = Math.max(1, Math.floor(Number(n) || 1)) // Get an integer 1 or higher
31
+
32
+ const { moves } = CLI.TRAX
33
+ const undo = moves.slice(0, 0 - n)
34
+ if (moves.length === undo.length) {
35
+ CLI.error('Unable to undo.')
36
+ } else {
37
+ CLI.setGame(undo)
38
+ CLI.do('view')
39
+ }
40
+ }
@@ -0,0 +1,65 @@
1
+ /* Copyright 2022-2023 Chad Transtrum
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ // CLI view command
17
+
18
+ export const viewCmd = {
19
+ name: 'view',
20
+ alt: ['show', 'display'],
21
+ args: '[move number]',
22
+ comp: '<move>',
23
+ desc: 'show the game board',
24
+ help: [
25
+ 'Show the current game. Include a move number to see the board as it was at',
26
+ 'that move.',
27
+ ],
28
+ }
29
+
30
+ viewCmd.fn = (CLI, move, ...extra) => {
31
+ if (!CLI.GAME?.id) {
32
+ return CLI.error('No active game. Type "new" to start a game.')
33
+ }
34
+
35
+ if (move?.length > 1 && 'puzzles'.startsWith(move)) {
36
+ return CLI.do('puzzles', 'list', ...extra)
37
+ }
38
+
39
+ const trax = CLI.TRAX
40
+
41
+ let show = Number.POSITIVE_INFINITY
42
+ if (move && Number(move) >= 0 && Number(move) <= trax.moves.length) {
43
+ show = Number(move)
44
+ }
45
+
46
+ const game = CLI.GAME
47
+ const players = game.players || ['white', 'black']
48
+ const puzzle = game.puzzle || ''
49
+ const { note } = [...(game.notes || [{}])].pop()
50
+
51
+ CLI.out(
52
+ CLI.color('#' + CLI.ID + ' ') +
53
+ CLI.name(trax.rules) +
54
+ ' ' +
55
+ CLI.bubble('w' + (trax.turn === 1 ? 'h' : ''), players[0]) +
56
+ CLI.color(' vs ') +
57
+ CLI.bubble('b' + (trax.turn === 2 ? 'h' : ''), players[1]),
58
+ )
59
+ CLI.display(trax, players, undefined, show)
60
+ if (puzzle && (!note || !note.includes(puzzle))) {
61
+ CLI.out(CLI.color('Puzzle ' + puzzle))
62
+ }
63
+
64
+ if (note) CLI.out(CLI.color.help(note))
65
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,105 @@
1
+ /** Type information.
2
+ * @copyright 2022-2023
3
+ * @author Chad Transtrum <chad@transtrum.net>
4
+ * @license Apache-2.0
5
+ */
6
+
7
+ /** Color is a single character to represent white or black. */
8
+ type Color = 'w' | 'b'
9
+
10
+ /** Colorize is a fancy function for colorizing text. */
11
+ type Colorize = (text: string, def?: string | number) => string
12
+
13
+ /** Colorer is a little too fancy. Hence the gnarly typescript. */
14
+ type Colorer = {
15
+ (text: string, def?: string | number): string
16
+ black: Colorize
17
+ command: Colorize
18
+ default: Colorize
19
+ error: Colorize
20
+ fatal: Colorize
21
+ help: Colorize
22
+ id: Colorize
23
+ variable: Colorize
24
+ optional: Colorize
25
+ short: Colorize
26
+ white: Colorize
27
+ }
28
+
29
+ /** A note with a move number */
30
+ type GameNote = {
31
+ move: number
32
+ note: string
33
+ }
34
+
35
+ /** Notes saved in the game object in the CLI. */
36
+ type GameNotes = GameNote[]
37
+
38
+ /** Treat the save state as an opaque object,
39
+ * produced by save() and fed into restore().
40
+ */
41
+ type SaveState = {
42
+ id: string
43
+ move: number
44
+ turn: number
45
+ over: boolean
46
+ left: number
47
+ right: number
48
+ top: number
49
+ bottom: number
50
+ notation: string
51
+ tiles: string
52
+ path: string
53
+ invalid: boolean
54
+ }
55
+
56
+ /** Representations of the different ways a tile can curve.
57
+ * Matches the symbols used in Trax notation.
58
+ */
59
+ type Slash = '/' | '\\' | '+'
60
+
61
+ /** Threat definition. */
62
+ type Threat = {
63
+ depth: number
64
+ pattern: string
65
+ rx: RegExp
66
+ value: number
67
+ }
68
+
69
+ /** A single tile on the board. */
70
+ type Tile = {
71
+ id: TileId
72
+ loc: Point
73
+ type: TileType
74
+ move: number
75
+ seq: number
76
+ }
77
+
78
+ /** When a tile is dropped, this object represents the results. */
79
+ type TileDrop = {
80
+ dropped: Tile[]
81
+ notation: string
82
+ valid: boolean
83
+ }
84
+
85
+ /** A TileId is just a string. */
86
+ type TileId = string
87
+
88
+ /** Invalid tiles are represented by 'x'. */
89
+ type TileType = ValidTiles | 'x'
90
+
91
+ /** All of the variants supported by the engine. */
92
+ type TraxVariant = 'trax' | 'traxloop' | 'trax8'
93
+
94
+ /** Tile type names are determined by listing the line color at each edge,
95
+ * starting from the top and going clockwise, and then sorted alphabetically
96
+ * and given a single letter name, so 'bbww' becomes 'a', which gives us six
97
+ * different tile names: a-f, as follows:
98
+ * a b c d e f
99
+ * +--#--+ +--#--+ +--#--+ +--o--+ +--o--+ +--o--+
100
+ * | # | | # | | # | | o | | o | | o |
101
+ * oo ## ooo#ooo ## oo oo ## ####### ## oo
102
+ * | o | | # | | o | | # | | o | | # |
103
+ * +--o--+ +--#--+ +--o--+ +--#--+ +--o--+ +--#--+
104
+ */
105
+ type ValidTiles = 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
package/src/utils.js ADDED
@@ -0,0 +1,22 @@
1
+ /** Shared utilities
2
+ * @copyright 2023
3
+ * @author Chad Transtrum <chad@transtrum.net>
4
+ * @license Apache-2.0
5
+ */
6
+
7
+ /** Convert milliseconds to a human readable string in ms, sec, or min units.
8
+ * @arg {number} ms - the number of milliseconds
9
+ * @returns {string} a short human-readable representation of the time
10
+ */
11
+ export const timeString = (ms) => {
12
+ if (ms < 1000) {
13
+ return (ms === Math.floor(ms) ? String(ms) : ms.toFixed(2)) + ' ms'
14
+ }
15
+
16
+ ms /= 1000
17
+ if (ms < 60) return ms.toFixed(3) + ' sec'
18
+
19
+ const min = Math.floor(ms / 60)
20
+ ms -= min * 60
21
+ return String(min) + ' min ' + ms.toFixed(3) + ' sec'
22
+ }
package/src/version.js ADDED
@@ -0,0 +1,2 @@
1
+ // Generated by genversion.
2
+ export const version = '0.11.0'