@playcademy/sandbox 0.1.0-beta.18 → 0.1.0-beta.19

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,344 +1,222 @@
1
1
  # @playcademy/sandbox
2
2
 
3
- **Local development server for isolated Playcademy game development**
3
+ **Local development server for isolated Playcademy game development.**
4
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.
5
+ The Playcademy sandbox provides a complete local simulation of the Playcademy platform, including API services and a real-time server, for development and testing.
6
6
 
7
- ## Overview
7
+ ## What is the Sandbox?
8
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.
9
+ The sandbox is a local development server that lets you:
10
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
11
+ - **Develop games locally** with full platform integration.
12
+ - **Test SDK functionality** without needing the live platform.
13
+ - **Simulate user accounts** and game data in a controlled environment.
14
+ - **Work offline** without an internet connection.
21
15
 
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
16
+ It's a "mock Playcademy platform" running on your machine that behaves just like the real thing.
27
17
 
28
- ## Architecture
18
+ ### Key Benefits
29
19
 
30
- The sandbox is built on a lightweight stack optimized for development speed:
20
+ - **Isolated Environment**: Completely local execution with no external dependencies.
21
+ - **Zero Configuration**: Automatic database setup and seeding.
22
+ - **Full API Compatibility**: All Playcademy APIs available locally.
23
+ - **Integrated Real-time Server**: Built-in WebSocket server for multiplayer features.
24
+ - **Fast Development Cycle**: Quick startup with an in-memory database.
31
25
 
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
26
+ ## How It Works
37
27
 
38
- ### Directory Structure
28
+ The sandbox runs two local servers to provide a complete development environment. When using our Vite templates, this is handled automatically.
39
29
 
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
30
+ ```mermaid
31
+ graph TD
32
+ subgraph "Your Machine"
33
+ A["Your Game (in browser)"] --> B["@playcademy/sdk"];
34
+ B --> C["API Server (localhost:4321)"];
35
+ B --> D["Real-time Server (localhost:4322)"];
36
+ C --> E["In-Memory Database"];
37
+ D --> E;
38
+ end
55
39
  ```
56
40
 
57
- ## Installation & Usage
41
+ ## Usage
58
42
 
59
- ### Global Installation
43
+ The easiest way to use the sandbox is with our official Vite templates, which handle everything automatically via the `@playcademy/vite-plugin`. You can also run it manually as a standalone CLI for advanced use cases.
60
44
 
61
- ```bash
62
- # Install globally for use anywhere
63
- bun install -g @playcademy/sandbox
45
+ ### Automatic Setup (Recommended)
64
46
 
65
- # Start sandbox in any project
66
- playcademy-sandbox
67
- ```
47
+ When you start your development server with `bun dev`, the Vite plugin will launch the sandbox in the background.
68
48
 
69
- ### Local Installation
49
+ ::: code-group
70
50
 
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
- }
51
+ ```bash [bun]
52
+ bun dev
81
53
  ```
82
54
 
83
- ### Command Line Interface
84
-
85
- ```bash
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
55
+ ```bash [pnpm]
56
+ pnpm dev
97
57
  ```
98
58
 
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
-
59
+ ```bash [yarn]
60
+ yarn dev
137
61
  ```
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
62
 
147
- # Game sessions
148
- http://localhost:4321/api/games/{gameId}/sessions
63
+ ```bash [npm]
64
+ npm run dev
149
65
  ```
150
66
 
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
67
+ :::
170
68
 
171
- The sandbox supports two database storage modes:
69
+ You'll see output confirming that both the API and real-time servers are running:
172
70
 
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
175
-
176
- ```bash
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
183
71
  ```
72
+ VITE v6.3.5
184
73
 
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
74
+ ➜ Local: http://localhost:5173/
75
+ ➜ Network: use --host to expose
192
76
 
193
- ## Integration with Game Development
77
+ PLAYCADEMY v0.1.0
194
78
 
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
- })
79
+ ➜ Game: playground
80
+ ➜ API: http://localhost:4321/api
81
+ ➜ Realtime: ws://localhost:4322
211
82
  ```
212
83
 
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',
84
+ ```typescript
85
+ // vite.config.ts
86
+ import { defineConfig } from 'vite'
87
+
88
+ import { playcademy } from '@playcademy/vite-plugin'
89
+
90
+ export default defineConfig({
91
+ plugins: [
92
+ playcademy({
93
+ sandbox: {
94
+ autoStart: true,
95
+ url: 'http://localhost:4321/api',
96
+ realtime: {
97
+ enabled: true,
98
+ port: 4322,
99
+ },
100
+ },
101
+ }),
102
+ ],
221
103
  })
222
-
223
- // Use SDK methods normally
224
- const user = await sdk.users.getProfile()
225
- const inventory = await sdk.inventory.getItems()
226
104
  ```
227
105
 
228
- ## Development
106
+ ### Manual Setup
229
107
 
230
- ### Building the Sandbox
108
+ You can run the sandbox as a standalone process from the command line.
231
109
 
232
- ```bash
233
- # Build for distribution
234
- bun run build
110
+ ::: code-group
235
111
 
236
- # Development with auto-reload
237
- bun run dev
112
+ ```bash [bun]
113
+ # Run with defaults
114
+ bunx @playcademy/sandbox
238
115
 
239
- # Start without building
240
- bun run start
116
+ # Run with custom ports and verbose logging
117
+ bunx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081 --verbose
241
118
  ```
242
119
 
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
120
+ ```bash [pnpm]
121
+ pnpm dlx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081
122
+ ```
252
123
 
253
- When developing a specific game, provide project information for enhanced development experience:
124
+ ```bash [yarn]
125
+ yarn dlx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081
126
+ ```
254
127
 
255
- ```bash
256
- playcademy-sandbox \
257
- --project-name "Equation Arena" \
258
- --project-slug "equation-arena"
128
+ ```bash [npm]
129
+ npx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081
259
130
  ```
260
131
 
261
- This enables:
132
+ :::
133
+
134
+ #### Command-Line Options
262
135
 
263
- - **Automatic Game Registration**: Your game appears in the games API
264
- - **Custom Seeding**: Game-specific items and configuration
136
+ | Option | Alias | Description | Default |
137
+ | :------------------------- | :---- | :--------------------------------------- | :----------- |
138
+ | `--port <number>` | `-p` | Port for the main API server. | `4321` |
139
+ | `--realtime` | | Enables the real-time WebSocket server. | `false` |
140
+ | `--realtime-port <number>` | | Port for the real-time WebSocket server. | API port + 1 |
141
+ | `--verbose` | `-v` | Enables verbose logging for debugging. | `false` |
142
+ | `--no-seed` | | Disables seeding of demo data. | `false` |
143
+ | `--project-name <name>` | | Sets the project name for game seeding. | `undefined` |
144
+ | `--project-slug <slug>` | | Sets the project slug for game seeding. | `undefined` |
265
145
 
266
- ## Advanced Configuration
146
+ #### Manual SDK Configuration
267
147
 
268
- ### Programmatic Usage
148
+ If you run the sandbox manually, you'll need to configure the SDK client to point to it.
269
149
 
270
150
  ```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
- },
151
+ import { PlaycademyClient } from '@playcademy/sdk'
152
+
153
+ const client = await PlaycademyClient.init({
154
+ baseUrl: 'http://localhost:4321/api',
155
+ realtimeUrl: 'ws://localhost:4322',
156
+ // In manual mode, you must provide a mock token
157
+ token: 'mock-dev-token',
283
158
  })
284
159
  ```
285
160
 
286
- ### Custom Database Configuration
161
+ ## Sandbox Features
287
162
 
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
- ```
163
+ | Feature | Description |
164
+ | :------------------- | :-------------------------------------------------------------------------------------------- |
165
+ | **API Simulation** | Emulates the entire Playcademy backend API for users, games, inventory, etc. |
166
+ | **Real-time Server** | Provides WebSocket-based real-time channels for multiplayer communication. |
167
+ | **Data Persistence** | Uses an in-memory database. Data persists until the sandbox is restarted. |
168
+ | **Mock Data** | Automatically seeds with a mock user (`developer@example.com`) and other demo data. |
169
+ | **Game Context** | When run via the Vite plugin, it automatically registers your current project as a mock game. |
295
170
 
296
- ## Troubleshooting
171
+ ## Server Endpoints
297
172
 
298
- ### Common Issues
173
+ Once running, the sandbox provides several key endpoints:
299
174
 
300
- **Port Already in Use**
175
+ - **Health Check**: `GET /health`
176
+ - **API Base URL**: `/api`
177
+ - **Real-time URL**: `ws://localhost:{realtimePort}`
301
178
 
302
- ```bash
303
- # Sandbox automatically finds available port
304
- playcademy-sandbox --port 4321
305
- # Will use 4322, 4323, etc. if 4321 is taken
306
- ```
179
+ All production Playcademy APIs are available under the `/api` prefix, including `/users`, `/games`, `/inventory`, and more. Note that `/timeback` endpoints are available but return noop responses due to external API dependencies.
307
180
 
308
- **API Not Responding**
181
+ ## Debugging
309
182
 
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
183
+ ### Health Check
313
184
 
314
- ### Debug Mode
185
+ You can verify the sandbox API server is running by sending a request to its `/health` endpoint.
315
186
 
316
187
  ```bash
317
- # Enable verbose logging
318
- playcademy-sandbox --verbose
319
-
320
- # Check server health
321
- curl http://localhost:4321/health
188
+ curl -sSL http://localhost:4321/health | jq
322
189
  ```
323
190
 
324
- ## Contributing
191
+ This should return a JSON object with the status:
325
192
 
326
- The sandbox is a crucial development tool for the Playcademy ecosystem. When contributing:
193
+ ```json
194
+ {
195
+ "status": "ok",
196
+ "timestamp": "2023-10-27T00:00:00.000Z"
197
+ }
198
+ ```
199
+
200
+ ### Common Issues
327
201
 
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
202
+ | Issue | Solution |
203
+ | :----------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------ |
204
+ | **Sandbox doesn't start** | When using Vite, check that `autoStart: true` in `vite.config.ts` and `@playcademy/vite-plugin` is installed. Check terminal for errors. |
205
+ | **API calls return HTML** | This indicates the API server isn't running or is on a different port. Check your sandbox `url` configuration. |
206
+ | **WebSocket connection fails** | Verify the real-time server is enabled (`--realtime` flag or Vite config) and check its port in the console output. |
207
+ | **Port conflicts** | Use the `--port` and `--realtime-port` options to choose different ports. The sandbox will automatically try the next available port if its default is taken. |
208
+ | **Data doesn't persist** | This is expected. The sandbox uses an in-memory database that resets on every restart to ensure a clean state for development. |
332
209
 
333
- For general contribution guidelines, see the [monorepo CONTRIBUTING.md](../../CONTRIBUTING.md).
210
+ ## Production vs. Sandbox
334
211
 
335
- ## Related Packages
212
+ The SDK is designed to work seamlessly in both environments. `PlaycademyClient.init()` automatically detects whether it's running in a production environment or against a local sandbox.
336
213
 
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
214
+ | Feature | Sandbox | Production |
215
+ | :----------------- | :-------------- | :-------------------- |
216
+ | **Data** | Mock, temporary | Real user data |
217
+ | **Authentication** | Mock tokens | Secure authentication |
218
+ | **Persistence** | Session only | Permanent |
341
219
 
342
- ## Distribution
220
+ ## Contributing
343
221
 
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.
222
+ The sandbox is a crucial development tool for the Playcademy ecosystem. For contribution guidelines, see the [monorepo CONTRIBUTING.md](../../CONTRIBUTING.md).