@astrofoundry/pi-astro 0.12.0 → 0.12.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.
|
@@ -34,3 +34,13 @@ The first directional key launches the snake — until then the game waits, so y
|
|
|
34
34
|
## Notes
|
|
35
35
|
|
|
36
36
|
State lives entirely in memory for the duration of the game; nothing is persisted.
|
|
37
|
+
|
|
38
|
+
## Debugging
|
|
39
|
+
|
|
40
|
+
If keys don't reach the game (no movement, R/Q don't quit), set `ASTRO_SNAKE_DEBUG=1` before launching pi. Each `handleInput` call appends a line to `/tmp/astro-snake-debug.log` with the raw key data and game status. Empty log = pi-tui isn't routing input to the component (pi-side issue, not astro-snake).
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
ASTRO_SNAKE_DEBUG=1 pi
|
|
44
|
+
# /snake, play, observe; in another terminal:
|
|
45
|
+
tail -f /tmp/astro-snake-debug.log
|
|
46
|
+
```
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
1
2
|
import type { Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import { matchesKey, visibleWidth } from "@mariozechner/pi-tui";
|
|
3
|
+
import { matchesKey, visibleWidth, type KeyId } from "@mariozechner/pi-tui";
|
|
3
4
|
import {
|
|
4
5
|
applyTick,
|
|
5
6
|
changeDirection,
|
|
@@ -15,6 +16,12 @@ export interface TuiHandle {
|
|
|
15
16
|
requestRender(): void;
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
type Letter = "q" | "r" | "w" | "a" | "s" | "d";
|
|
20
|
+
|
|
21
|
+
function isLetter(data: string, letter: Letter): boolean {
|
|
22
|
+
return matchesKey(data, letter as KeyId) || matchesKey(data, `shift+${letter}` as KeyId);
|
|
23
|
+
}
|
|
24
|
+
|
|
18
25
|
export class AstroSnakeComponent {
|
|
19
26
|
private state: GameState;
|
|
20
27
|
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
@@ -36,13 +43,23 @@ export class AstroSnakeComponent {
|
|
|
36
43
|
}
|
|
37
44
|
|
|
38
45
|
handleInput(data: string): void {
|
|
46
|
+
if (process.env.ASTRO_SNAKE_DEBUG === "1") {
|
|
47
|
+
try {
|
|
48
|
+
fs.appendFileSync(
|
|
49
|
+
"/tmp/astro-snake-debug.log",
|
|
50
|
+
`[${new Date().toISOString()}] handleInput(${JSON.stringify(data)}) status=${this.state.status}\n`,
|
|
51
|
+
);
|
|
52
|
+
} catch {
|
|
53
|
+
/* ignore log errors */
|
|
54
|
+
}
|
|
55
|
+
}
|
|
39
56
|
if (this.state.status === "gameover" || this.state.status === "won") {
|
|
40
|
-
if (data
|
|
57
|
+
if (isLetter(data, "r") || matchesKey(data, "space")) {
|
|
41
58
|
this.state = createInitialState();
|
|
42
59
|
this.bump();
|
|
43
60
|
return;
|
|
44
61
|
}
|
|
45
|
-
if (matchesKey(data, "escape") || data
|
|
62
|
+
if (matchesKey(data, "escape") || isLetter(data, "q")) {
|
|
46
63
|
this.dispose();
|
|
47
64
|
this.onClose();
|
|
48
65
|
return;
|
|
@@ -50,17 +67,17 @@ export class AstroSnakeComponent {
|
|
|
50
67
|
return;
|
|
51
68
|
}
|
|
52
69
|
|
|
53
|
-
if (matchesKey(data, "escape") || data
|
|
70
|
+
if (matchesKey(data, "escape") || isLetter(data, "q")) {
|
|
54
71
|
this.dispose();
|
|
55
72
|
this.onClose();
|
|
56
73
|
return;
|
|
57
74
|
}
|
|
58
75
|
|
|
59
76
|
let req: Direction | null = null;
|
|
60
|
-
if (matchesKey(data, "up") || data
|
|
61
|
-
else if (matchesKey(data, "down") || data
|
|
62
|
-
else if (matchesKey(data, "left") || data
|
|
63
|
-
else if (matchesKey(data, "right") || data
|
|
77
|
+
if (matchesKey(data, "up") || isLetter(data, "w")) req = "up";
|
|
78
|
+
else if (matchesKey(data, "down") || isLetter(data, "s")) req = "down";
|
|
79
|
+
else if (matchesKey(data, "left") || isLetter(data, "a")) req = "left";
|
|
80
|
+
else if (matchesKey(data, "right") || isLetter(data, "d")) req = "right";
|
|
64
81
|
|
|
65
82
|
if (!req) return;
|
|
66
83
|
|