@sharpee/sharpee 0.9.61-beta

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,274 @@
1
+ /**
2
+ * Browser Entry Point for {{STORY_TITLE}}
3
+ *
4
+ * This file connects your story to the browser UI.
5
+ * Generated by: npx sharpee init-browser
6
+ */
7
+
8
+ import { GameEngine } from '@sharpee/engine';
9
+ import { WorldModel, EntityType } from '@sharpee/world-model';
10
+ import { Parser } from '@sharpee/parser-en-us';
11
+ import { LanguageProvider } from '@sharpee/lang-en-us';
12
+ import { PerceptionService } from '@sharpee/stdlib';
13
+ import { story, config } from './index';
14
+
15
+ // DOM elements
16
+ let statusLocation: HTMLElement | null;
17
+ let statusScore: HTMLElement | null;
18
+ let textContent: HTMLElement | null;
19
+ let mainWindow: HTMLElement | null;
20
+ let commandInput: HTMLInputElement | null;
21
+
22
+ // Game state
23
+ let engine: GameEngine;
24
+ let world: WorldModel;
25
+ let commandHistory: string[] = [];
26
+ let historyIndex = -1;
27
+ let currentTurn = 0;
28
+ let currentScore = 0;
29
+
30
+ /**
31
+ * Initialize the game
32
+ */
33
+ function initializeGame(): void {
34
+ // Create world and player
35
+ world = new WorldModel();
36
+ const player = world.createEntity('player', EntityType.ACTOR);
37
+ world.setPlayer(player.id);
38
+
39
+ // Create parser and language
40
+ const language = new LanguageProvider();
41
+ const parser = new Parser(language);
42
+
43
+ // Extend parser and language with story-specific vocabulary
44
+ if (story.extendParser) {
45
+ story.extendParser(parser);
46
+ }
47
+ if (story.extendLanguage) {
48
+ story.extendLanguage(language);
49
+ }
50
+
51
+ // Create perception service
52
+ const perceptionService = new PerceptionService();
53
+
54
+ // Create engine
55
+ engine = new GameEngine({
56
+ world,
57
+ player,
58
+ parser,
59
+ language,
60
+ perceptionService,
61
+ });
62
+
63
+ // Set up event handlers
64
+ engine.on('text:output', (text: string, turn: number) => {
65
+ displayText(text);
66
+ currentTurn = turn;
67
+ updateStatusLine();
68
+ });
69
+
70
+ engine.on('event', (event: any) => {
71
+ // Track score changes
72
+ if (event.type === 'game.score_changed' && event.data) {
73
+ currentScore = event.data.newScore ?? currentScore;
74
+ updateStatusLine();
75
+ }
76
+ });
77
+
78
+ // Set the story
79
+ engine.setStory(story);
80
+ }
81
+
82
+ /**
83
+ * Set up DOM elements and event handlers
84
+ */
85
+ function setupDOM(): void {
86
+ statusLocation = document.getElementById('location-name');
87
+ statusScore = document.getElementById('score-turns');
88
+ textContent = document.getElementById('text-content');
89
+ mainWindow = document.getElementById('main-window');
90
+ commandInput = document.getElementById('command-input') as HTMLInputElement;
91
+
92
+ if (!commandInput) {
93
+ console.error('Command input element not found');
94
+ return;
95
+ }
96
+
97
+ // Handle keyboard input
98
+ commandInput.addEventListener('keydown', (e: KeyboardEvent) => {
99
+ if (e.key === 'Enter') {
100
+ handleCommand();
101
+ } else if (e.key === 'ArrowUp') {
102
+ e.preventDefault();
103
+ navigateHistory(-1);
104
+ } else if (e.key === 'ArrowDown') {
105
+ e.preventDefault();
106
+ navigateHistory(1);
107
+ }
108
+ });
109
+
110
+ // Keep focus on input
111
+ document.addEventListener('click', () => {
112
+ if (commandInput && !commandInput.disabled) {
113
+ commandInput.focus();
114
+ }
115
+ });
116
+ }
117
+
118
+ /**
119
+ * Handle command submission
120
+ */
121
+ async function handleCommand(): Promise<void> {
122
+ if (!commandInput) return;
123
+
124
+ const command = commandInput.value.trim();
125
+ if (!command) return;
126
+
127
+ // Add to history
128
+ commandHistory.push(command);
129
+ historyIndex = commandHistory.length;
130
+
131
+ // Clear input
132
+ commandInput.value = '';
133
+
134
+ // Display command echo
135
+ displayCommand(command);
136
+
137
+ // Execute command
138
+ try {
139
+ await engine.executeTurn(command);
140
+ } catch (error) {
141
+ const message = error instanceof Error ? error.message : String(error);
142
+ displayText(`[Error: ${message}]`);
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Navigate command history
148
+ */
149
+ function navigateHistory(direction: number): void {
150
+ if (!commandInput) return;
151
+
152
+ const newIndex = historyIndex + direction;
153
+
154
+ if (newIndex < 0) return;
155
+
156
+ if (newIndex >= commandHistory.length) {
157
+ historyIndex = commandHistory.length;
158
+ commandInput.value = '';
159
+ return;
160
+ }
161
+
162
+ historyIndex = newIndex;
163
+ commandInput.value = commandHistory[historyIndex];
164
+
165
+ // Move cursor to end
166
+ commandInput.setSelectionRange(
167
+ commandInput.value.length,
168
+ commandInput.value.length
169
+ );
170
+ }
171
+
172
+ /**
173
+ * Display text in the main window
174
+ */
175
+ function displayText(text: string): void {
176
+ if (!textContent) return;
177
+
178
+ // Split on double newlines to get paragraphs
179
+ const paragraphs = text.split(/\n\n+/);
180
+
181
+ for (const para of paragraphs) {
182
+ const trimmed = para.trim();
183
+ if (trimmed) {
184
+ const p = document.createElement('p');
185
+ p.style.whiteSpace = 'pre-line';
186
+ p.textContent = trimmed;
187
+ textContent.appendChild(p);
188
+ }
189
+ }
190
+
191
+ scrollToBottom();
192
+ }
193
+
194
+ /**
195
+ * Display command echo
196
+ */
197
+ function displayCommand(command: string): void {
198
+ if (!textContent) return;
199
+
200
+ const div = document.createElement('div');
201
+ div.className = 'command-echo';
202
+ div.textContent = `> ${command}`;
203
+ textContent.appendChild(div);
204
+
205
+ scrollToBottom();
206
+ }
207
+
208
+ /**
209
+ * Update the status line
210
+ */
211
+ function updateStatusLine(): void {
212
+ const player = world.getPlayer();
213
+ let locationName = '';
214
+
215
+ if (player) {
216
+ const locationId = world.getLocation(player.id);
217
+ if (locationId) {
218
+ const room = world.getEntity(locationId);
219
+ if (room) {
220
+ locationName = room.name || 'Unknown';
221
+ }
222
+ }
223
+ }
224
+
225
+ if (statusLocation) {
226
+ statusLocation.textContent = locationName;
227
+ }
228
+
229
+ if (statusScore) {
230
+ statusScore.textContent = `Score: ${currentScore} | Turns: ${currentTurn}`;
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Scroll main window to bottom
236
+ */
237
+ function scrollToBottom(): void {
238
+ if (mainWindow) {
239
+ mainWindow.scrollTop = mainWindow.scrollHeight;
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Start the game
245
+ */
246
+ async function start(): Promise<void> {
247
+ console.log('=== {{STORY_TITLE}} BROWSER START ===');
248
+
249
+ try {
250
+ setupDOM();
251
+ initializeGame();
252
+
253
+ // Start the engine
254
+ await engine.start();
255
+
256
+ // Initial look
257
+ await engine.executeTurn('look');
258
+
259
+ // Focus input
260
+ if (commandInput) {
261
+ commandInput.focus();
262
+ }
263
+ } catch (error) {
264
+ console.error('=== STARTUP ERROR ===', error);
265
+ displayText(`[Startup Error: ${error}]`);
266
+ }
267
+ }
268
+
269
+ // Start when DOM is ready
270
+ if (document.readyState === 'loading') {
271
+ document.addEventListener('DOMContentLoaded', start);
272
+ } else {
273
+ start();
274
+ }
@@ -0,0 +1,75 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>{{STORY_TITLE}}</title>
7
+ <link rel="stylesheet" href="styles.css">
8
+ </head>
9
+ <body>
10
+ <div id="game-container">
11
+ <div id="status-line">
12
+ <span id="location-name"></span>
13
+ <span id="score-turns">Score: 0 | Turns: 0</span>
14
+ </div>
15
+ <div id="main-window">
16
+ <div id="text-content"></div>
17
+ </div>
18
+ <div id="input-area">
19
+ <span class="prompt">&gt;</span>
20
+ <input id="command-input" type="text"
21
+ autocomplete="off" autocapitalize="none"
22
+ spellcheck="false" autofocus>
23
+ </div>
24
+ </div>
25
+
26
+ <!-- Save/Restore Modal Dialogs -->
27
+ <div id="modal-overlay" class="modal-hidden">
28
+ <!-- Save Dialog -->
29
+ <div id="save-dialog" class="modal-dialog modal-hidden">
30
+ <div class="modal-title">SAVE GAME</div>
31
+ <div class="modal-content">
32
+ <div class="save-input-row">
33
+ <label for="save-name-input">Save name:</label>
34
+ <input type="text" id="save-name-input" maxlength="30"
35
+ autocomplete="off" spellcheck="false">
36
+ </div>
37
+ <div class="saves-list-label">Existing saves (click to overwrite):</div>
38
+ <div id="save-slots-list" class="saves-list"></div>
39
+ </div>
40
+ <div class="modal-buttons">
41
+ <button id="save-confirm-btn" class="modal-btn">Save</button>
42
+ <button id="save-cancel-btn" class="modal-btn">Cancel</button>
43
+ </div>
44
+ </div>
45
+
46
+ <!-- Restore Dialog -->
47
+ <div id="restore-dialog" class="modal-dialog modal-hidden">
48
+ <div class="modal-title">RESTORE GAME</div>
49
+ <div class="modal-content">
50
+ <div class="saves-list-label">Select a saved game:</div>
51
+ <div id="restore-slots-list" class="saves-list"></div>
52
+ <div id="no-saves-message" class="no-saves-message modal-hidden">No saved games found.</div>
53
+ </div>
54
+ <div class="modal-buttons">
55
+ <button id="restore-confirm-btn" class="modal-btn">Restore</button>
56
+ <button id="restore-cancel-btn" class="modal-btn">Cancel</button>
57
+ </div>
58
+ </div>
59
+
60
+ <!-- Startup Dialog (continue saved game?) -->
61
+ <div id="startup-dialog" class="modal-dialog modal-hidden">
62
+ <div class="modal-title">CONTINUE GAME?</div>
63
+ <div class="modal-content">
64
+ <p id="startup-save-info" class="startup-info"></p>
65
+ <p class="startup-question">Continue where you left off?</p>
66
+ </div>
67
+ <div class="modal-buttons">
68
+ <button id="startup-continue-btn" class="modal-btn">Continue</button>
69
+ <button id="startup-new-btn" class="modal-btn">New Game</button>
70
+ </div>
71
+ </div>
72
+ </div>
73
+ <script src="{{STORY_ID}}.js"></script>
74
+ </body>
75
+ </html>