-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path.cursorrules
More file actions
265 lines (191 loc) · 6.14 KB
/
.cursorrules
File metadata and controls
265 lines (191 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Rusty Motors Server - Cursor AI Rules
## Project Overview
This is a **monolith game server** for Motor City Online reverse engineering.
- Language: TypeScript
- Runtime: Node.js
- Package Manager: npm
- Test Framework: Vitest
## MUST READ FIRST
Before making changes, read `MASTER_DESIGN.md` in the project root.
It contains all architectural decisions and refactoring guidance.
---
## Quick Rules
### 1. Package Structure
Packages are FLAT. Do NOT create subdirectories unless the package has 20+ files.
**Exception**: `packages/gateway/` has internal structure (`core/`, `infrastructure/`, `handlers/`).
### 2. Layer Rules (CRITICAL)
```
Layer 4: src/nps_server.ts, src/mcots_server.ts (entry points)
Layer 3: packages/gateway, src/chat (orchestration)
Layer 2: packages/login, persona, transactions, lobby, nps, shard, cli (domain)
Layer 1: libs/@rustymotors/*, packages/shared, database, protocol (infrastructure)
```
**FORBIDDEN**: Layer 2 packages cannot import from each other!
```typescript
// ❌ WRONG - login cannot import from transactions
import { something } from "rusty-motors-transactions";
// ✅ OK - login can import from shared (Layer 1)
import { ServerLogger } from "rusty-motors-shared";
```
### 3. Logging
NEVER use `console.log`, `console.error`, `console.warn`, or `console.dir`.
ALWAYS use the logger:
```typescript
import { getServerLogger } from "rusty-motors-shared";
const log = getServerLogger("myModule");
log.info("message");
log.error("error message");
log.warn("warning");
log.verbose("debug info");
```
### 4. Dependency Injection Pattern
ALL public functions should accept dependencies as optional parameters:
```typescript
// ✅ CORRECT pattern
export async function myFunction(
input: InputType,
log: ServerLogger = getServerLogger("myModule"),
): Promise<ResultType> {
log.info("Processing...");
// implementation
}
// ❌ WRONG - no way to inject mock logger for testing
export async function myFunction(input: InputType): Promise<ResultType> {
const log = getServerLogger("myModule");
// implementation
}
```
### 5. Serialization
Use `@rustymotors/binary` for all serialization:
```typescript
// ✅ CORRECT
import { BytableBuffer, BytableMessage } from "@rustymotors/binary";
// ❌ DEPRECATED - do not use
import { SerializedBufferOld } from "rusty-motors-shared";
import { BufferSerializer } from "rusty-motors-protocol";
```
### 6. Error Handling
Wrap errors with context:
```typescript
try {
await riskyOperation();
} catch (error) {
throw new Error("Failed to process login", { cause: error });
}
```
### 7. Exports
Use named exports, NOT default exports:
```typescript
// ✅ CORRECT
export function myFunction() {}
export class MyClass {}
// ❌ WRONG
export default function myFunction() {}
```
### 8. Async/Await
Use async/await, NOT .then().catch():
```typescript
// ✅ CORRECT
try {
const result = await asyncOperation();
} catch (error) {
log.error("Failed", { error });
}
// ❌ WRONG
asyncOperation()
.then(result => {})
.catch(error => {});
```
### 9. Strict Equality
Always use `===` and `!==`, never `==` or `!=`:
```typescript
// ✅ CORRECT
if (value === "expected") {}
if (value !== undefined) {}
// ❌ WRONG
if (value == "expected") {}
if (value != undefined) {}
```
---
## File Locations
### Where to put new code
| Type of Code | Location |
|--------------|----------|
| Shared types/utilities | `packages/shared/src/` |
| Database operations | `packages/database/src/` |
| Protocol definitions | `packages/protocol/src/` |
| Binary serialization | `libs/@rustymotors/binary/src/` |
| Login logic | `packages/login/src/` |
| Persona logic | `packages/persona/src/` |
| Transaction logic | `packages/transactions/src/` |
| Lobby logic | `packages/lobby/src/` |
| NPS handlers | `packages/nps/src/` |
| Gateway/routing | `packages/gateway/src/` |
| Chat | `src/chat/` |
| Entry points | `src/` (root level .ts files) |
### Where NOT to put code
- Do NOT create new top-level directories
- Do NOT create new packages without explicit approval
- Do NOT add files to `libs/` except in existing `@rustymotors/` packages
---
## Testing
### Test file location
Tests go next to the source file or in a `test/` directory:
```
packages/login/src/login.ts
packages/login/src/login.test.ts # Option 1: next to source
packages/login/test/login.test.ts # Option 2: in test dir
```
### Test pattern
```typescript
import { describe, it, expect } from "vitest";
import { mockLogger } from "../../mocks.js";
describe("functionName", () => {
it("should do expected behavior", async () => {
// Arrange
const input = createTestInput();
// Act
const result = await functionName(input, mockLogger);
// Assert
expect(result).toBeDefined();
expect(mockLogger.error).not.toHaveBeenCalled();
});
});
```
**Note**: Use the shared `mockLogger` from `mocks.ts` - don't create inline mocks.
---
## Common Tasks
### Adding a new message handler
1. Find the appropriate package (login, persona, transactions, lobby, nps)
2. Create handler function with dependency injection pattern
3. Register in the package's handler map/registry
4. Add tests
5. Export from package index
### Fixing a bug
1. Write a test that reproduces the bug
2. Fix the bug with minimal changes
3. Verify test passes
4. Do NOT refactor unrelated code in the same commit
### Refactoring
1. Check `MASTER_DESIGN.md` for architectural decisions
2. Run existing tests first
3. Make incremental changes
4. Run tests after each change
5. Update imports if moving files
---
## Do NOT
- ❌ Create directories in flat packages (unless 20+ files)
- ❌ Import Layer 2 packages from other Layer 2 packages
- ❌ Use console.log/error/warn/dir
- ❌ Use default exports
- ❌ Use .then().catch() chains
- ❌ Use == or != for comparisons
- ❌ Use SerializedBufferOld or BufferSerializer (deprecated)
- ❌ Create abstractions "for the future" - only when needed now
- ❌ Add features beyond what was requested
---
## Reference
Full documentation: `MASTER_DESIGN.md`
Layer analysis tool: `tsx scripts/analyze-layers.ts`
Quick wins list: `QUICK_WINS.md`
Test coverage gaps: `CRITICAL_TEST_COVERAGE.md`