@playcademy/sandbox 0.1.0-beta.9 → 0.1.0

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,222 @@
1
- # sandbox
1
+ # @playcademy/sandbox
2
2
 
3
- To install dependencies:
3
+ **Local development server for isolated Playcademy game development.**
4
4
 
5
- ```bash
6
- bun install
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
+
7
+ ## What is the Sandbox?
8
+
9
+ The sandbox is a local development server that lets you:
10
+
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.
15
+
16
+ It's a "mock Playcademy platform" running on your machine that behaves just like the real thing.
17
+
18
+ ### Key Benefits
19
+
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.
25
+
26
+ ## How It Works
27
+
28
+ The sandbox runs two local servers to provide a complete development environment. When using our Vite templates, this is handled automatically.
29
+
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
39
+ ```
40
+
41
+ ## Usage
42
+
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.
44
+
45
+ ### Automatic Setup (Recommended)
46
+
47
+ When you start your development server with `bun dev`, the Vite plugin will launch the sandbox in the background.
48
+
49
+ ::: code-group
50
+
51
+ ```bash [bun]
52
+ bun dev
53
+ ```
54
+
55
+ ```bash [pnpm]
56
+ pnpm dev
57
+ ```
58
+
59
+ ```bash [yarn]
60
+ yarn dev
61
+ ```
62
+
63
+ ```bash [npm]
64
+ npm run dev
65
+ ```
66
+
67
+ :::
68
+
69
+ You'll see output confirming that both the API and real-time servers are running:
70
+
71
+ ```
72
+ VITE v6.3.5
73
+
74
+ ➜ Local: http://localhost:5173/
75
+ ➜ Network: use --host to expose
76
+
77
+ PLAYCADEMY v0.1.0
78
+
79
+ ➜ Game: playground
80
+ ➜ API: http://localhost:4321/api
81
+ ➜ Realtime: ws://localhost:4322
82
+ ```
83
+
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
+ ],
103
+ })
104
+ ```
105
+
106
+ ### Manual Setup
107
+
108
+ You can run the sandbox as a standalone process from the command line.
109
+
110
+ ::: code-group
111
+
112
+ ```bash [bun]
113
+ # Run with defaults
114
+ bunx @playcademy/sandbox
115
+
116
+ # Run with custom ports and verbose logging
117
+ bunx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081 --verbose
118
+ ```
119
+
120
+ ```bash [pnpm]
121
+ pnpm dlx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081
122
+ ```
123
+
124
+ ```bash [yarn]
125
+ yarn dlx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081
126
+ ```
127
+
128
+ ```bash [npm]
129
+ npx @playcademy/sandbox --port 8080 --realtime --realtime-port 8081
130
+ ```
131
+
132
+ :::
133
+
134
+ #### Command-Line Options
135
+
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` |
145
+
146
+ #### Manual SDK Configuration
147
+
148
+ If you run the sandbox manually, you'll need to configure the SDK client to point to it.
149
+
150
+ ```typescript
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',
158
+ })
7
159
  ```
8
160
 
9
- To run:
161
+ ## Sandbox Features
162
+
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. |
170
+
171
+ ## Server Endpoints
172
+
173
+ Once running, the sandbox provides several key endpoints:
174
+
175
+ - **Health Check**: `GET /health`
176
+ - **API Base URL**: `/api`
177
+ - **Real-time URL**: `ws://localhost:{realtimePort}`
178
+
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.
180
+
181
+ ## Debugging
182
+
183
+ ### Health Check
184
+
185
+ You can verify the sandbox API server is running by sending a request to its `/health` endpoint.
10
186
 
11
187
  ```bash
12
- bun run index.ts
188
+ curl -sSL http://localhost:4321/health | jq
13
189
  ```
14
190
 
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.
191
+ This should return a JSON object with the status:
192
+
193
+ ```json
194
+ {
195
+ "status": "ok",
196
+ "timestamp": "2023-10-27T00:00:00.000Z"
197
+ }
198
+ ```
199
+
200
+ ### Common Issues
201
+
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. |
209
+
210
+ ## Production vs. Sandbox
211
+
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.
213
+
214
+ | Feature | Sandbox | Production |
215
+ | :----------------- | :-------------- | :-------------------- |
216
+ | **Data** | Mock, temporary | Real user data |
217
+ | **Authentication** | Mock tokens | Secure authentication |
218
+ | **Persistence** | Session only | Permanent |
219
+
220
+ ## Contributing
221
+
222
+ The sandbox is a crucial development tool for the Playcademy ecosystem. For contribution guidelines, see the [monorepo CONTRIBUTING.md](../../CONTRIBUTING.md).