@playcademy/sandbox 0.1.0-beta.17 → 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 +147 -269
- package/dist/cli.js +8167 -72042
- package/dist/database/seed.d.ts +1 -1
- package/dist/routes/achievements.d.ts +3 -0
- package/dist/routes/index.d.ts +2 -0
- package/dist/routes/timeback.d.ts +3 -0
- package/dist/server.js +8139 -70093
- package/dist/utils/port.d.ts +17 -0
- package/dist/utils/port.js +54 -0
- package/package.json +10 -5
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
|
|
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
|
-
##
|
|
7
|
+
## What is the Sandbox?
|
|
8
8
|
|
|
9
|
-
The sandbox is
|
|
9
|
+
The sandbox is a local development server that lets you:
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
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
|
-
|
|
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
|
-
|
|
18
|
+
### Key Benefits
|
|
29
19
|
|
|
30
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
##
|
|
41
|
+
## Usage
|
|
58
42
|
|
|
59
|
-
|
|
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
|
-
|
|
62
|
-
# Install globally for use anywhere
|
|
63
|
-
bun install -g @playcademy/sandbox
|
|
45
|
+
### Automatic Setup (Recommended)
|
|
64
46
|
|
|
65
|
-
|
|
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
|
-
|
|
49
|
+
::: code-group
|
|
70
50
|
|
|
71
|
-
```bash
|
|
72
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
148
|
-
|
|
63
|
+
```bash [npm]
|
|
64
|
+
npm run dev
|
|
149
65
|
```
|
|
150
66
|
|
|
151
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
77
|
+
PLAYCADEMY v0.1.0
|
|
194
78
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
-
|
|
106
|
+
### Manual Setup
|
|
229
107
|
|
|
230
|
-
|
|
108
|
+
You can run the sandbox as a standalone process from the command line.
|
|
231
109
|
|
|
232
|
-
|
|
233
|
-
# Build for distribution
|
|
234
|
-
bun run build
|
|
110
|
+
::: code-group
|
|
235
111
|
|
|
236
|
-
|
|
237
|
-
|
|
112
|
+
```bash [bun]
|
|
113
|
+
# Run with defaults
|
|
114
|
+
bunx @playcademy/sandbox
|
|
238
115
|
|
|
239
|
-
#
|
|
240
|
-
|
|
116
|
+
# Run with custom ports and verbose logging
|
|
117
|
+
bunx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081 --verbose
|
|
241
118
|
```
|
|
242
119
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
-
|
|
124
|
+
```bash [yarn]
|
|
125
|
+
yarn dlx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081
|
|
126
|
+
```
|
|
254
127
|
|
|
255
|
-
```bash
|
|
256
|
-
playcademy
|
|
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
|
-
|
|
132
|
+
:::
|
|
133
|
+
|
|
134
|
+
#### Command-Line Options
|
|
262
135
|
|
|
263
|
-
|
|
264
|
-
|
|
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
|
-
|
|
146
|
+
#### Manual SDK Configuration
|
|
267
147
|
|
|
268
|
-
|
|
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 {
|
|
272
|
-
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
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
|
-
|
|
161
|
+
## Sandbox Features
|
|
287
162
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
-
##
|
|
171
|
+
## Server Endpoints
|
|
297
172
|
|
|
298
|
-
|
|
173
|
+
Once running, the sandbox provides several key endpoints:
|
|
299
174
|
|
|
300
|
-
**
|
|
175
|
+
- **Health Check**: `GET /health`
|
|
176
|
+
- **API Base URL**: `/api`
|
|
177
|
+
- **Real-time URL**: `ws://localhost:{realtimePort}`
|
|
301
178
|
|
|
302
|
-
|
|
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
|
-
|
|
181
|
+
## Debugging
|
|
309
182
|
|
|
310
|
-
|
|
311
|
-
- Verify the port isn't blocked by firewall
|
|
312
|
-
- Try accessing `/health` endpoint first
|
|
183
|
+
### Health Check
|
|
313
184
|
|
|
314
|
-
|
|
185
|
+
You can verify the sandbox API server is running by sending a request to its `/health` endpoint.
|
|
315
186
|
|
|
316
187
|
```bash
|
|
317
|
-
|
|
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
|
-
|
|
191
|
+
This should return a JSON object with the status:
|
|
325
192
|
|
|
326
|
-
|
|
193
|
+
```json
|
|
194
|
+
{
|
|
195
|
+
"status": "ok",
|
|
196
|
+
"timestamp": "2023-10-27T00:00:00.000Z"
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Common Issues
|
|
327
201
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
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
|
-
|
|
210
|
+
## Production vs. Sandbox
|
|
334
211
|
|
|
335
|
-
|
|
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
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
-
##
|
|
220
|
+
## Contributing
|
|
343
221
|
|
|
344
|
-
The sandbox is
|
|
222
|
+
The sandbox is a crucial development tool for the Playcademy ecosystem. For contribution guidelines, see the [monorepo CONTRIBUTING.md](../../CONTRIBUTING.md).
|