@playcademy/sandbox 0.1.0-beta.11 → 0.1.0-beta.13

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.
package/README.md CHANGED
@@ -1,15 +1,344 @@
1
- # sandbox
1
+ # @playcademy/sandbox
2
2
 
3
- To install dependencies:
3
+ **Local development server for isolated Playcademy game development**
4
+
5
+ The sandbox provides a complete, isolated development environment for building games on the Playcademy platform. It runs entirely locally with a PostgreSQL database (PGLite) that can be configured for either persistent storage or in-memory operation, and serves all Playcademy APIs without requiring external services or authentication.
6
+
7
+ ## Overview
8
+
9
+ The sandbox is designed to solve the key challenge of game development: providing a local environment that accurately mirrors the production Playcademy platform while remaining completely isolated and easy to set up.
10
+
11
+ ### Key Benefits
12
+
13
+ - **Isolated Environment**: Completely local execution with no external dependencies
14
+ - **Zero Configuration**: Automatic database setup and seeding
15
+ - **Full API Compatibility**: All Playcademy APIs available locally
16
+ - **Fast Development Cycle**: Quick startup with configurable database storage (persistent or in-memory)
17
+ - **Production Parity**: Same business logic as production platform
18
+ - **Game-Specific Seeding**: Automatically seeds your game's data for development
19
+
20
+ ### Use Cases
21
+
22
+ - **Game Development**: Local testing of Playcademy API integration
23
+ - **Template Development**: Building new game templates
24
+ - **SDK Testing**: Validating Playcademy SDK functionality
25
+ - **API Exploration**: Understanding available platform features
26
+ - **Rapid Prototyping**: Quick game concept validation
27
+
28
+ ## Architecture
29
+
30
+ The sandbox is built on a lightweight stack optimized for development speed:
31
+
32
+ - **Hono**: Fast, lightweight web framework
33
+ - **PGLite**: PostgreSQL database (WebAssembly) - configurable for persistent or in-memory storage
34
+ - **Drizzle ORM**: Type-safe database operations
35
+ - **@playcademy/api-core**: Production API handlers
36
+ - **@playcademy/data**: Database schema and types
37
+
38
+ ### Directory Structure
39
+
40
+ ```
41
+ packages/sandbox/src/
42
+ ├── cli.ts # Command-line interface
43
+ ├── server.ts # HTTP server setup
44
+ ├── constants.ts # Configuration and defaults
45
+ ├── types.ts # TypeScript definitions
46
+ ├── database/ # Database setup and seeding
47
+ │ ├── index.ts # Database initialization
48
+ │ ├── path-manager.ts # Database file management
49
+ │ └── seed.ts # Demo data and game seeding
50
+ ├── lib/ # Core functionality
51
+ │ └── auth.ts # Authentication middleware
52
+ ├── routes/ # API route handlers
53
+ └── utils/ # Utility functions
54
+ └── port.ts # Port availability checking
55
+ ```
56
+
57
+ ## Installation & Usage
58
+
59
+ ### Global Installation
60
+
61
+ ```bash
62
+ # Install globally for use anywhere
63
+ bun install -g @playcademy/sandbox
64
+
65
+ # Start sandbox in any project
66
+ playcademy-sandbox
67
+ ```
68
+
69
+ ### Local Installation
70
+
71
+ ```bash
72
+ # Install in your project
73
+ bun add -D @playcademy/sandbox
74
+
75
+ # Use via package.json script
76
+ {
77
+ "scripts": {
78
+ "sandbox": "playcademy-sandbox"
79
+ }
80
+ }
81
+ ```
82
+
83
+ ### Command Line Interface
4
84
 
5
85
  ```bash
6
- bun install
86
+ # Basic usage
87
+ playcademy-sandbox
88
+
89
+ # Custom port
90
+ playcademy-sandbox --port 3000
91
+
92
+ # With project information (enables game-specific seeding)
93
+ playcademy-sandbox --project-name "My Game" --project-slug "my-game"
94
+
95
+ # Verbose logging
96
+ playcademy-sandbox --verbose
7
97
  ```
8
98
 
9
- To run:
99
+ ### CLI Options
100
+
101
+ | Option | Description | Default |
102
+ | ---------------- | --------------------------- | ------- |
103
+ | `--port, -p` | Server port | `4321` |
104
+ | `--verbose, -v` | Enable verbose logging | `true` |
105
+ | `--project-name` | Display name for your game | - |
106
+ | `--project-slug` | URL-safe slug for your game | - |
107
+
108
+ ## Server Endpoints
109
+
110
+ Once running, the sandbox provides several key endpoints:
111
+
112
+ ### Health & Status
113
+
114
+ - **GET `/health`** - Server health check and status information
115
+
116
+ ### API Base
117
+
118
+ - **GET `/api`** - API manifest and available endpoints
119
+
120
+ ### Playcademy APIs
121
+
122
+ All production Playcademy APIs are available locally:
123
+
124
+ - **`/api/users`** - User management and profiles
125
+ - **`/api/games`** - Game sessions, state, and uploads
126
+ - **`/api/inventory`** - User inventory management
127
+ - **`/api/items`** - Item definitions and operations
128
+ - **`/api/shop`** - In-game shop functionality
129
+ - **`/api/shop-listings`** - Marketplace listings
130
+ - **`/api/currencies`** - Virtual currency systems
131
+ - **`/api/levels`** - User progression and XP
132
+ - **`/api/maps`** - Game world data and elements
133
+ - **`/api/dev`** - Developer tools and utilities
134
+
135
+ ### Example URLs
136
+
137
+ ```
138
+ # Server status
139
+ http://localhost:4321/health
140
+
141
+ # API manifest
142
+ http://localhost:4321/api
143
+
144
+ # User profile
145
+ http://localhost:4321/api/users/me
146
+
147
+ # Game sessions
148
+ http://localhost:4321/api/games/{gameId}/sessions
149
+ ```
150
+
151
+ ## Database Management
152
+
153
+ ### Automatic Setup
154
+
155
+ The sandbox automatically:
156
+
157
+ 1. **Creates Database Schema** - Uses programmatic schema creation from TypeScript definitions
158
+ 2. **Seeds Demo Data** - Provides realistic test data for development
159
+ 3. **Seeds Game Data** - Creates your specific game when project options are provided
160
+
161
+ ### Demo Data Includes
162
+
163
+ - **Users**: Test user accounts with different roles
164
+ - **Games**: Sample games for testing
165
+ - **Items**: Various item types and categories
166
+ - **Currencies**: Virtual currency systems
167
+ - **Maps**: Basic world/map structures
168
+
169
+ ### Database Storage Options
170
+
171
+ The sandbox supports two database storage modes:
172
+
173
+ - **Persistent Storage** (default): Database file stored in project's `node_modules` directory
174
+ - **In-Memory**: No persistent storage, fastest startup, data lost on restart
10
175
 
11
176
  ```bash
12
- bun run index.ts
177
+ # Default persistent storage location:
178
+ # ./node_modules/@playcademy/vite-plugin/node_modules/.playcademy/sandbox.db
179
+
180
+ # In-memory mode can be enabled via:
181
+ # - memoryOnly: true in programmatic usage
182
+ # - setupDatabase(':memory:') for custom path
13
183
  ```
14
184
 
15
- This project was created using `bun init` in bun v1.2.10. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
185
+ ## Authentication
186
+
187
+ The sandbox includes simplified authentication for development:
188
+
189
+ - **No Real Authentication Required**: Automatically provides a test user context
190
+ - **JWT Token Support**: Compatible with real Playcademy authentication flows
191
+ - **Developer Keys**: Supports development API keys for testing
192
+
193
+ ## Integration with Game Development
194
+
195
+ ### Basic Integration
196
+
197
+ ```javascript
198
+ // Your game code
199
+ const API_BASE = 'http://localhost:4321/api'
200
+
201
+ // Fetch user inventory
202
+ const inventory = await fetch(`${API_BASE}/inventory`)
203
+ const items = await inventory.json()
204
+
205
+ // Add item to user inventory
206
+ await fetch(`${API_BASE}/inventory/add`, {
207
+ method: 'POST',
208
+ headers: { 'Content-Type': 'application/json' },
209
+ body: JSON.stringify({ itemId: 'sword-001', quantity: 1 }),
210
+ })
211
+ ```
212
+
213
+ ### With Playcademy SDK
214
+
215
+ ```javascript
216
+ import { PlaycademySDK } from '@playcademy/sdk'
217
+
218
+ // Initialize SDK with sandbox
219
+ const sdk = new PlaycademySDK({
220
+ apiUrl: 'http://localhost:4321/api',
221
+ })
222
+
223
+ // Use SDK methods normally
224
+ const user = await sdk.users.getProfile()
225
+ const inventory = await sdk.inventory.getItems()
226
+ ```
227
+
228
+ ## Development
229
+
230
+ ### Building the Sandbox
231
+
232
+ ```bash
233
+ # Build for distribution
234
+ bun run build
235
+
236
+ # Development with auto-reload
237
+ bun run dev
238
+
239
+ # Start without building
240
+ bun run start
241
+ ```
242
+
243
+ ### Custom Build Process
244
+
245
+ The build process includes:
246
+
247
+ 1. **TypeScript Compilation**: Builds CLI and server entry points
248
+ 2. **Type Generation**: Creates `.d.ts` files for type safety
249
+ 3. **PGlite Binary Bundling**: Includes WebAssembly binaries for distribution
250
+
251
+ ### Project-Specific Development
252
+
253
+ When developing a specific game, provide project information for enhanced development experience:
254
+
255
+ ```bash
256
+ playcademy-sandbox \
257
+ --project-name "Equation Arena" \
258
+ --project-slug "equation-arena"
259
+ ```
260
+
261
+ This enables:
262
+
263
+ - **Automatic Game Registration**: Your game appears in the games API
264
+ - **Custom Seeding**: Game-specific items and configuration
265
+
266
+ ## Advanced Configuration
267
+
268
+ ### Programmatic Usage
269
+
270
+ ```typescript
271
+ import { startServer } from '@playcademy/sandbox'
272
+
273
+ const server = await startServer({
274
+ port: 4321,
275
+ verbose: true,
276
+ memoryOnly: true,
277
+ seed: true,
278
+ project: {
279
+ slug: 'my-game',
280
+ displayName: 'My Game',
281
+ version: '1.0.0',
282
+ },
283
+ })
284
+ ```
285
+
286
+ ### Custom Database Configuration
287
+
288
+ ```typescript
289
+ // Use in-memory database (no persistence)
290
+ const server = await startServer({
291
+ port: 4321,
292
+ memoryOnly: true, // Use in-memory database
293
+ })
294
+ ```
295
+
296
+ ## Troubleshooting
297
+
298
+ ### Common Issues
299
+
300
+ **Port Already in Use**
301
+
302
+ ```bash
303
+ # Sandbox automatically finds available port
304
+ playcademy-sandbox --port 4321
305
+ # Will use 4322, 4323, etc. if 4321 is taken
306
+ ```
307
+
308
+ **API Not Responding**
309
+
310
+ - Check that server started successfully (look for green startup messages)
311
+ - Verify the port isn't blocked by firewall
312
+ - Try accessing `/health` endpoint first
313
+
314
+ ### Debug Mode
315
+
316
+ ```bash
317
+ # Enable verbose logging
318
+ playcademy-sandbox --verbose
319
+
320
+ # Check server health
321
+ curl http://localhost:4321/health
322
+ ```
323
+
324
+ ## Contributing
325
+
326
+ The sandbox is a crucial development tool for the Playcademy ecosystem. When contributing:
327
+
328
+ 1. **Maintain API Parity**: Ensure sandbox APIs match production behavior
329
+ 2. **Keep It Fast**: Optimize for quick development iteration
330
+ 3. **Update Documentation**: Keep examples current with API changes
331
+ 4. **Test Thoroughly**: Validate with real game development scenarios
332
+
333
+ For general contribution guidelines, see the [monorepo CONTRIBUTING.md](../../CONTRIBUTING.md).
334
+
335
+ ## Related Packages
336
+
337
+ - [`@playcademy/api-core`](../api-core/): API handlers used by sandbox
338
+ - [`@playcademy/data`](../data/): Database schema and types
339
+ - [`@playcademy/sdk`](../sdk/): JavaScript SDK for game development
340
+ - **Game Templates**: See `/templates` for example integrations
341
+
342
+ ## Distribution
343
+
344
+ The sandbox is distributed as a standalone npm package for use in game development. It can be installed globally or as a development dependency in game projects.