c64-ready 2.4.0 → 3.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 +32 -0
- package/bin/admin.mjs +7 -1
- package/bin/headless.mjs +1 -1
- package/dist/assets/BeachHead-CuSwFD4k.js +1 -0
- package/dist/assets/ChuckieEgg-CPqBnAOF.js +1 -0
- package/dist/assets/ForbiddenForest-DsQ9jsc8.js +1 -0
- package/dist/assets/Galaga-Cutc3p9s.js +1 -0
- package/dist/assets/Gribbly-DdZ_hHFt.js +1 -0
- package/dist/assets/Hercules-vZYKm7GG.js +1 -0
- package/dist/assets/LegendOfWilf-CxSMOjsj.js +1 -0
- package/dist/assets/MariosCementFactory-z0IZ0q30.js +1 -0
- package/dist/assets/MoleAttack-buC6ChYy.js +1 -0
- package/dist/assets/MunchyWorm-D2Zv7i0Q.js +1 -0
- package/dist/assets/ParkPatrol-CVj7hyoB.js +1 -0
- package/dist/assets/PottyPigeon-0SYpIUBa.js +1 -0
- package/dist/assets/RainbowIslands-BjfmJB8z.js +1 -0
- package/dist/assets/Stix-BU-qUCeC.js +1 -0
- package/dist/assets/Tilt-C5KtKisF.js +1 -0
- package/dist/assets/UpNDown-CATRO7Mz.js +1 -0
- package/dist/assets/Uridium-DanS2KoH.js +1 -0
- package/dist/assets/VegetablesDeluxe-C41ViimS.js +1 -0
- package/dist/assets/index-B8DSjPqI.js +243 -0
- package/dist/assets/src-DX0rJQsE.js +1 -0
- package/dist/assets/src-_trgwTvp.js +2 -0
- package/dist/assets/string-utils-CMVABfx0.js +1 -0
- package/dist/index.html +1 -1
- package/dist-ts/emulator/c64-keys.d.ts +69 -0
- package/dist-ts/emulator/c64-keys.d.ts.map +1 -0
- package/dist-ts/emulator/c64-keys.js +69 -0
- package/dist-ts/emulator/c64-keys.js.map +1 -0
- package/dist-ts/emulator/gamepad-mapping.d.ts +64 -0
- package/dist-ts/emulator/gamepad-mapping.d.ts.map +1 -0
- package/dist-ts/emulator/gamepad-mapping.js +154 -0
- package/dist-ts/emulator/gamepad-mapping.js.map +1 -0
- package/dist-ts/emulator/input.d.ts +24 -75
- package/dist-ts/emulator/input.d.ts.map +1 -1
- package/dist-ts/emulator/input.js +93 -214
- package/dist-ts/emulator/input.js.map +1 -1
- package/dist-ts/player/c64-player.d.ts +25 -0
- package/dist-ts/player/c64-player.d.ts.map +1 -1
- package/dist-ts/player/c64-player.js +77 -1
- package/dist-ts/player/c64-player.js.map +1 -1
- package/dist-ts/player/cheevos-dev.d.ts +65 -0
- package/dist-ts/player/cheevos-dev.d.ts.map +1 -0
- package/dist-ts/player/cheevos-dev.js +237 -0
- package/dist-ts/player/cheevos-dev.js.map +1 -0
- package/dist-ts/player/cheevos-tracker-panel.d.ts +21 -0
- package/dist-ts/player/cheevos-tracker-panel.d.ts.map +1 -0
- package/dist-ts/player/cheevos-tracker-panel.js +242 -0
- package/dist-ts/player/cheevos-tracker-panel.js.map +1 -0
- package/dist-ts/player/index.d.ts +3 -0
- package/dist-ts/player/index.d.ts.map +1 -1
- package/dist-ts/player/index.js +2 -0
- package/dist-ts/player/index.js.map +1 -1
- package/dist-ts/player/input-handler.d.ts +12 -1
- package/dist-ts/player/input-handler.d.ts.map +1 -1
- package/dist-ts/player/input-handler.js +46 -1
- package/dist-ts/player/input-handler.js.map +1 -1
- package/dist-ts/player/local-rom-store.d.ts +23 -0
- package/dist-ts/player/local-rom-store.d.ts.map +1 -0
- package/dist-ts/player/local-rom-store.js +87 -0
- package/dist-ts/player/local-rom-store.js.map +1 -0
- package/dist-ts/player/startup-params.d.ts +7 -0
- package/dist-ts/player/startup-params.d.ts.map +1 -0
- package/dist-ts/player/startup-params.js +47 -0
- package/dist-ts/player/startup-params.js.map +1 -0
- package/dist-ts/player/ui-controller.d.ts +22 -0
- package/dist-ts/player/ui-controller.d.ts.map +1 -1
- package/dist-ts/player/ui-controller.js +666 -1
- package/dist-ts/player/ui-controller.js.map +1 -1
- package/package.json +2 -1
- package/src/emulator/c64-keys.ts +68 -0
- package/src/emulator/gamepad-mapping.ts +212 -0
- package/src/emulator/input.ts +106 -209
- package/src/headless/headless-cli.mjs +10 -4
- package/src/headless/input-server.mjs +92 -17
- package/src/headless/webrtc-server.mjs +344 -22
- package/src/main.ts +177 -12
- package/src/player/c64-player.ts +96 -1
- package/src/player/cheevos-dev.ts +340 -0
- package/src/player/cheevos-tracker-panel.ts +309 -0
- package/src/player/index.ts +19 -0
- package/src/player/input-handler.ts +63 -1
- package/src/player/local-rom-store.ts +126 -0
- package/src/player/startup-params.ts +40 -0
- package/src/player/ui-controller.ts +741 -0
- package/src/types/c64-cheevos.d.ts +6 -0
- package/dist/assets/index-Dxuihfdi.js +0 -177
- /package/dist/assets/{marked.esm--kBm026b.js → marked.esm-fTuuvbcB.js} +0 -0
- /package/dist/assets/{purify.es-BcoGFDcJ.js → purify.es-CRM-GE0Y.js} +0 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import type { C64Player } from './c64-player';
|
|
2
|
+
|
|
3
|
+
const STORAGE_PREFIX = 'c64-cheevos-dev';
|
|
4
|
+
|
|
5
|
+
export type CheevosDevAchievement = {
|
|
6
|
+
_id: string;
|
|
7
|
+
title: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type CheevosDevSet = {
|
|
12
|
+
_id?: string;
|
|
13
|
+
romPath?: string;
|
|
14
|
+
rom?: {
|
|
15
|
+
path?: string;
|
|
16
|
+
type?: string;
|
|
17
|
+
};
|
|
18
|
+
cheevos?: CheevosDevAchievement[];
|
|
19
|
+
trackerFields?: CheevosDevTrackerField[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type CheevosDevTrackerField = {
|
|
23
|
+
key: string;
|
|
24
|
+
label?: string;
|
|
25
|
+
source?: 'field';
|
|
26
|
+
field?: string;
|
|
27
|
+
display?: 'text' | 'bar';
|
|
28
|
+
max?: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type CheevosDevScore = {
|
|
32
|
+
gameId: string;
|
|
33
|
+
score: number;
|
|
34
|
+
userId: string;
|
|
35
|
+
username: string;
|
|
36
|
+
variant?: string;
|
|
37
|
+
at: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type CheevosDevState = {
|
|
41
|
+
enabled: boolean;
|
|
42
|
+
detectorId: string;
|
|
43
|
+
cheevosSet: CheevosDevSet;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type CheevosInstance = {
|
|
47
|
+
execute?: () => void;
|
|
48
|
+
cpuReadNS?: (addr: number) => number;
|
|
49
|
+
cpuRead?: (addr: number) => number;
|
|
50
|
+
ramRead?: (addr: number) => number;
|
|
51
|
+
watcher?: {
|
|
52
|
+
on?: (name: string, callback: (payload: unknown) => void) => void;
|
|
53
|
+
};
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type CheevosEventPayload = {
|
|
58
|
+
detectorId: string;
|
|
59
|
+
type: string;
|
|
60
|
+
payload?: unknown;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export class CheevosDevController {
|
|
64
|
+
private player: C64Player | null;
|
|
65
|
+
private enabled = false;
|
|
66
|
+
private detectorId = '';
|
|
67
|
+
private cheevosSet: CheevosDevSet = { cheevos: [] };
|
|
68
|
+
private instance: CheevosInstance | null = null;
|
|
69
|
+
private rafId = 0;
|
|
70
|
+
private lastSnapshotKey = '';
|
|
71
|
+
|
|
72
|
+
constructor(player: C64Player) {
|
|
73
|
+
this.player = player;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
getState(): CheevosDevState {
|
|
77
|
+
return {
|
|
78
|
+
enabled: this.enabled,
|
|
79
|
+
detectorId: this.detectorId,
|
|
80
|
+
cheevosSet: this.cheevosSet,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async enable(detectorId: string, cheevosSet: CheevosDevSet = { cheevos: [] }): Promise<void> {
|
|
85
|
+
const normalisedDetectorId = detectorId.trim().toLowerCase();
|
|
86
|
+
if (!normalisedDetectorId) throw new Error('Cheevos detector ID is required');
|
|
87
|
+
|
|
88
|
+
this.disable();
|
|
89
|
+
this.detectorId = normalisedDetectorId;
|
|
90
|
+
this.cheevosSet = normaliseCheevosSet(cheevosSet, normalisedDetectorId);
|
|
91
|
+
|
|
92
|
+
const { createCheevos } = await import('c64-cheevos');
|
|
93
|
+
const poppedCheevos = this.getPoppedCheevos(normalisedDetectorId);
|
|
94
|
+
const cheevos = (await createCheevos(normalisedDetectorId, {
|
|
95
|
+
gameId: normalisedDetectorId,
|
|
96
|
+
user: { id: 'dev-user', username: 'Developer' },
|
|
97
|
+
cheevosSet: this.cheevosSet,
|
|
98
|
+
poppedCheevos,
|
|
99
|
+
postScore: this.postScore,
|
|
100
|
+
popCheevo: this.popCheevo,
|
|
101
|
+
})) as CheevosInstance;
|
|
102
|
+
|
|
103
|
+
cheevos.cpuReadNS = (addr) => this.player?.cpuReadNS(addr) ?? 0;
|
|
104
|
+
cheevos.cpuRead = (addr) => this.player?.cpuRead(addr) ?? 0;
|
|
105
|
+
cheevos.ramRead = (addr) => this.player?.ramRead(addr) ?? 0;
|
|
106
|
+
|
|
107
|
+
this.attachWatcher(cheevos);
|
|
108
|
+
this.instance = cheevos;
|
|
109
|
+
this.enabled = true;
|
|
110
|
+
this.dispatchStatus('enabled', {
|
|
111
|
+
cheevosCount: this.cheevosSet.cheevos?.length ?? 0,
|
|
112
|
+
cheevosSet: this.cheevosSet,
|
|
113
|
+
poppedCheevos,
|
|
114
|
+
});
|
|
115
|
+
this.tick();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async enableFromJson(detectorId: string, rawJson: string): Promise<void> {
|
|
119
|
+
await this.enable(detectorId, parseCheevosSetJson(rawJson));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
disable(): void {
|
|
123
|
+
if (this.rafId) {
|
|
124
|
+
cancelAnimationFrame(this.rafId);
|
|
125
|
+
this.rafId = 0;
|
|
126
|
+
}
|
|
127
|
+
this.enabled = false;
|
|
128
|
+
this.instance = null;
|
|
129
|
+
this.lastSnapshotKey = '';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
destroy(): void {
|
|
133
|
+
this.disable();
|
|
134
|
+
this.player = null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
clearPopped(detectorId: string = this.detectorId): void {
|
|
138
|
+
const id = detectorId.trim().toLowerCase();
|
|
139
|
+
if (!id) return;
|
|
140
|
+
localStorage.removeItem(getPoppedStorageKey(id));
|
|
141
|
+
this.dispatchStatus('popped-cleared', { poppedCheevos: [] });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
clearScores(detectorId: string = this.detectorId): void {
|
|
145
|
+
const id = detectorId.trim().toLowerCase();
|
|
146
|
+
if (!id) return;
|
|
147
|
+
localStorage.removeItem(getScoresStorageKey(id));
|
|
148
|
+
this.dispatchStatus('scores-cleared');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private tick = (): void => {
|
|
152
|
+
if (!this.enabled) return;
|
|
153
|
+
try {
|
|
154
|
+
this.instance?.execute?.();
|
|
155
|
+
this.dispatchSnapshot();
|
|
156
|
+
} catch (err) {
|
|
157
|
+
this.disable();
|
|
158
|
+
window.dispatchEvent(
|
|
159
|
+
new CustomEvent('c64-cheevos-error', {
|
|
160
|
+
detail: { detectorId: this.detectorId, error: normaliseError(err) },
|
|
161
|
+
}),
|
|
162
|
+
);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
this.rafId = requestAnimationFrame(this.tick);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
private readonly postScore = async (
|
|
169
|
+
gameId: string,
|
|
170
|
+
score: number,
|
|
171
|
+
userId: string,
|
|
172
|
+
username: string,
|
|
173
|
+
variant?: string,
|
|
174
|
+
): Promise<{ ok: true }> => {
|
|
175
|
+
const entry: CheevosDevScore = {
|
|
176
|
+
gameId,
|
|
177
|
+
score,
|
|
178
|
+
userId,
|
|
179
|
+
username,
|
|
180
|
+
variant,
|
|
181
|
+
at: new Date().toISOString(),
|
|
182
|
+
};
|
|
183
|
+
const scores = readJson<CheevosDevScore[]>(getScoresStorageKey(this.detectorId), []);
|
|
184
|
+
scores.push(entry);
|
|
185
|
+
localStorage.setItem(getScoresStorageKey(this.detectorId), JSON.stringify(scores));
|
|
186
|
+
window.dispatchEvent(
|
|
187
|
+
new CustomEvent('c64-cheevos-score', {
|
|
188
|
+
detail: { detectorId: this.detectorId, score: entry },
|
|
189
|
+
}),
|
|
190
|
+
);
|
|
191
|
+
return { ok: true };
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
private readonly popCheevo = async (
|
|
195
|
+
_cheevosSetId: string,
|
|
196
|
+
_userId: string,
|
|
197
|
+
cheevoId: string,
|
|
198
|
+
): Promise<{ achievement: CheevosDevAchievement }> => {
|
|
199
|
+
const achievement = this.findAchievement(cheevoId) ?? {
|
|
200
|
+
_id: cheevoId,
|
|
201
|
+
title: cheevoId,
|
|
202
|
+
description: 'Unlocked in c64-ready dev mode',
|
|
203
|
+
};
|
|
204
|
+
const popped = this.getPoppedCheevos(this.detectorId);
|
|
205
|
+
if (!popped.some((item) => item.achievement._id === cheevoId)) {
|
|
206
|
+
popped.push({ achievement });
|
|
207
|
+
localStorage.setItem(getPoppedStorageKey(this.detectorId), JSON.stringify(popped));
|
|
208
|
+
}
|
|
209
|
+
window.dispatchEvent(
|
|
210
|
+
new CustomEvent('c64-cheevos-popped', {
|
|
211
|
+
detail: { detectorId: this.detectorId, achievement, poppedCheevos: popped },
|
|
212
|
+
}),
|
|
213
|
+
);
|
|
214
|
+
return { achievement };
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
private dispatchSnapshot(): void {
|
|
218
|
+
const instance = this.instance;
|
|
219
|
+
if (!instance) return;
|
|
220
|
+
|
|
221
|
+
const fields = Object.fromEntries(
|
|
222
|
+
(this.cheevosSet.trackerFields ?? []).map((field) => {
|
|
223
|
+
const sourceField = field.field ?? field.key;
|
|
224
|
+
return [field.key, normaliseTrackerValue(instance[sourceField])];
|
|
225
|
+
}),
|
|
226
|
+
);
|
|
227
|
+
const detail = {
|
|
228
|
+
detectorId: this.detectorId,
|
|
229
|
+
score: normaliseTrackerValue(instance.score),
|
|
230
|
+
lives: normaliseTrackerValue(instance.lives),
|
|
231
|
+
isGameOver: typeof instance.isGameOver === 'boolean' ? instance.isGameOver : undefined,
|
|
232
|
+
fields,
|
|
233
|
+
};
|
|
234
|
+
const snapshotKey = JSON.stringify(detail);
|
|
235
|
+
if (snapshotKey === this.lastSnapshotKey) return;
|
|
236
|
+
this.lastSnapshotKey = snapshotKey;
|
|
237
|
+
window.dispatchEvent(new CustomEvent('c64-cheevos-snapshot', { detail }));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private attachWatcher(cheevos: CheevosInstance): void {
|
|
241
|
+
const watcher = cheevos.watcher;
|
|
242
|
+
if (typeof watcher?.on !== 'function') return;
|
|
243
|
+
for (const type of ['cheevo', 'gameOver', 'livesChange', 'newGame']) {
|
|
244
|
+
watcher.on(type, (payload) => {
|
|
245
|
+
const detail: CheevosEventPayload = { detectorId: this.detectorId, type, payload };
|
|
246
|
+
window.dispatchEvent(new CustomEvent('c64-cheevos-event', { detail }));
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private findAchievement(cheevoId: string): CheevosDevAchievement | undefined {
|
|
252
|
+
return this.cheevosSet.cheevos?.find((achievement) => achievement._id === cheevoId);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private getPoppedCheevos(detectorId: string): Array<{ achievement: CheevosDevAchievement }> {
|
|
256
|
+
return readJson<Array<{ achievement: CheevosDevAchievement }>>(
|
|
257
|
+
getPoppedStorageKey(detectorId),
|
|
258
|
+
[],
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private dispatchStatus(status: string, extra: Record<string, unknown> = {}): void {
|
|
263
|
+
window.dispatchEvent(
|
|
264
|
+
new CustomEvent('c64-cheevos-status', {
|
|
265
|
+
detail: { detectorId: this.detectorId, status, ...extra },
|
|
266
|
+
}),
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function parseCheevosSetJson(rawJson: string): CheevosDevSet {
|
|
272
|
+
const parsed = JSON.parse(rawJson) as unknown;
|
|
273
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
274
|
+
throw new Error('Cheevos JSON must be an object');
|
|
275
|
+
}
|
|
276
|
+
return normaliseCheevosSet(parsed as CheevosDevSet);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function normaliseCheevosSet(set: CheevosDevSet, detectorId = 'dev'): CheevosDevSet {
|
|
280
|
+
const cheevos = Array.isArray(set.cheevos) ? set.cheevos : [];
|
|
281
|
+
const trackerFields = Array.isArray(set.trackerFields) ? set.trackerFields : [];
|
|
282
|
+
const romPath = normaliseOptionalString(set.romPath ?? set.rom?.path);
|
|
283
|
+
const romType = normaliseOptionalString(set.rom?.type);
|
|
284
|
+
return {
|
|
285
|
+
_id: set._id ?? `${detectorId}-dev-set`,
|
|
286
|
+
...(romPath ? { romPath } : {}),
|
|
287
|
+
...(romPath || romType
|
|
288
|
+
? { rom: { ...(romPath ? { path: romPath } : {}), ...(romType ? { type: romType } : {}) } }
|
|
289
|
+
: {}),
|
|
290
|
+
trackerFields: trackerFields.map((field, index) => ({
|
|
291
|
+
key: String(field.key ?? field.field ?? `field-${index + 1}`),
|
|
292
|
+
label: field.label ? String(field.label) : undefined,
|
|
293
|
+
source: 'field',
|
|
294
|
+
field: field.field ? String(field.field) : undefined,
|
|
295
|
+
display: field.display === 'bar' ? 'bar' : 'text',
|
|
296
|
+
max: typeof field.max === 'number' && Number.isFinite(field.max) ? field.max : undefined,
|
|
297
|
+
})),
|
|
298
|
+
cheevos: cheevos.map((achievement, index) => ({
|
|
299
|
+
_id: String(achievement._id ?? `dev-${index + 1}`),
|
|
300
|
+
title: String(achievement.title ?? achievement._id ?? `Achievement ${index + 1}`),
|
|
301
|
+
description: achievement.description ? String(achievement.description) : '',
|
|
302
|
+
})),
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function normaliseOptionalString(value: unknown): string | undefined {
|
|
307
|
+
if (typeof value !== 'string') return undefined;
|
|
308
|
+
const trimmed = value.trim();
|
|
309
|
+
return trimmed || undefined;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function readJson<T>(key: string, fallback: T): T {
|
|
313
|
+
try {
|
|
314
|
+
const raw = localStorage.getItem(key);
|
|
315
|
+
return raw ? (JSON.parse(raw) as T) : fallback;
|
|
316
|
+
} catch {
|
|
317
|
+
return fallback;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function getScoresStorageKey(detectorId: string): string {
|
|
322
|
+
return `${STORAGE_PREFIX}:${detectorId}:scores`;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function getPoppedStorageKey(detectorId: string): string {
|
|
326
|
+
return `${STORAGE_PREFIX}:${detectorId}:popped`;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function normaliseError(err: unknown): string {
|
|
330
|
+
return err instanceof Error ? err.message : String(err);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function normaliseTrackerValue(value: unknown): string | number | boolean | undefined {
|
|
334
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
335
|
+
return value;
|
|
336
|
+
}
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export default CheevosDevController;
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import type { CheevosDevAchievement, CheevosDevSet, CheevosDevTrackerField } from './cheevos-dev';
|
|
2
|
+
|
|
3
|
+
export const CHEEVOS_TRACKER_VISIBLE_STORAGE_KEY = 'c64-cheevos-tracker-visible';
|
|
4
|
+
|
|
5
|
+
type PoppedCheevo = { achievement: CheevosDevAchievement };
|
|
6
|
+
|
|
7
|
+
type TrackerState = {
|
|
8
|
+
detectorId: string;
|
|
9
|
+
enabled: boolean;
|
|
10
|
+
visiblePreference: boolean;
|
|
11
|
+
displayMode: string;
|
|
12
|
+
cheevosSet: CheevosDevSet;
|
|
13
|
+
popped: CheevosDevAchievement[];
|
|
14
|
+
score?: string | number | boolean;
|
|
15
|
+
lives?: string | number | boolean;
|
|
16
|
+
isGameOver?: boolean;
|
|
17
|
+
fields: Record<string, string | number | boolean | undefined>;
|
|
18
|
+
lastMessage: string;
|
|
19
|
+
expandedLists: Record<string, boolean>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const COLLAPSED_ACHIEVEMENT_LIMIT = 6;
|
|
23
|
+
|
|
24
|
+
const css = `
|
|
25
|
+
.c64-cheevos-tracker{position:fixed;inset-block-start:50%;inset-inline-start:calc(50% + 410px);transform:translateY(-50%);box-sizing:border-box;inline-size:min(520px,calc(50vw - 430px));max-block-size:calc(100dvh - 48px);overflow:auto;scrollbar-gutter:stable;background:#101623;border:2px solid #333e5f;border-radius:8px;color:#d8def6;font-family:monospace;font-size:13px;padding:14px;z-index:900;box-shadow:0 12px 36px rgba(0,0,0,.35)}.c64-cheevos-tracker.hidden{display:none}.c64-cheevos-tracker h2{margin:0 0 4px;color:#8da2ff;font-size:15px;letter-spacing:.08em;text-transform:uppercase}.c64-cheevos-tracker-subtitle{color:#8790aa;font-size:12px;margin:0 0 10px}.c64-cheevos-tracker-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px;margin:0 0 10px}.c64-cheevos-stat{background:#151d2e;border:1px solid #26314c;border-radius:5px;padding:7px}.c64-cheevos-stat-label{display:block;color:#8995b5;font-size:11px;text-transform:uppercase}.c64-cheevos-stat-value{display:block;color:#fff;font-size:16px;margin-block-start:2px;overflow:hidden;text-overflow:ellipsis}.c64-cheevos-field-list{display:grid;gap:7px;margin:0 0 10px}.c64-cheevos-field{background:#151d2e;border:1px solid #26314c;border-radius:5px;padding:7px}.c64-cheevos-field-row{display:flex;justify-content:space-between;gap:8px}.c64-cheevos-bar{block-size:7px;background:#26314c;border-radius:999px;margin-block-start:6px;overflow:hidden}.c64-cheevos-bar-fill{block-size:100%;background:#8da2ff;border-radius:inherit}.c64-cheevos-list-header{display:flex;align-items:center;justify-content:space-between;gap:8px;margin:10px 0 6px}.c64-cheevos-list-title{margin:0;color:#8da2ff;font-size:12px;text-transform:uppercase}.c64-cheevos-list-toggle{background:transparent;border:1px solid #38486f;border-radius:4px;color:#b8c4ff;cursor:pointer;font:inherit;font-size:11px;padding:2px 6px}.c64-cheevos-list-toggle:hover{border-color:#8da2ff;color:#fff}.c64-cheevos-list{display:grid;gap:6px;margin:0;padding:0;list-style:none}.c64-cheevos-list.cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.c64-cheevos-list.cols-3{grid-template-columns:repeat(3,minmax(0,1fr))}.c64-cheevos-item{background:#151d2e;border:1px solid #26314c;border-radius:5px;padding:6px;min-inline-size:0}.c64-cheevos-item.popped{border-color:#557a36;background:#152416}.c64-cheevos-item-title{display:block;color:#f1f4ff;overflow:hidden;text-overflow:ellipsis}.c64-cheevos-item-description{display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;line-clamp:2;overflow:clip;color:#8790aa;font-size:11px;margin-block-start:2px}.c64-cheevos-empty{color:#8790aa;font-size:12px;margin:0}.c64-cheevos-message{color:#f3c36b;font-size:12px;margin:10px 0 0}@media (max-width:1480px){.c64-cheevos-tracker{inline-size:min(360px,calc(50vw - 430px))}.c64-cheevos-list.cols-3{grid-template-columns:repeat(2,minmax(0,1fr))}}@media (max-width:1180px){.c64-cheevos-tracker{display:none}}
|
|
26
|
+
`;
|
|
27
|
+
|
|
28
|
+
export class CheevosTrackerPanel {
|
|
29
|
+
private root: HTMLElement | null = null;
|
|
30
|
+
private state: TrackerState = {
|
|
31
|
+
detectorId: '',
|
|
32
|
+
enabled: false,
|
|
33
|
+
visiblePreference: localStorage.getItem(CHEEVOS_TRACKER_VISIBLE_STORAGE_KEY) !== '0',
|
|
34
|
+
displayMode: 'standard',
|
|
35
|
+
cheevosSet: { cheevos: [], trackerFields: [] },
|
|
36
|
+
popped: [],
|
|
37
|
+
fields: {},
|
|
38
|
+
lastMessage: '',
|
|
39
|
+
expandedLists: {},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
init(): void {
|
|
43
|
+
this.injectCSS();
|
|
44
|
+
this.root = document.createElement('aside');
|
|
45
|
+
this.root.className = 'c64-cheevos-tracker hidden';
|
|
46
|
+
this.root.setAttribute('aria-label', 'Cheevos tracker');
|
|
47
|
+
document.body.appendChild(this.root);
|
|
48
|
+
this.render();
|
|
49
|
+
|
|
50
|
+
window.addEventListener('c64-cheevos-status', this.handleStatus as EventListener);
|
|
51
|
+
window.addEventListener('c64-cheevos-event', this.handleCheevosEvent as EventListener);
|
|
52
|
+
window.addEventListener('c64-cheevos-score', this.handleScore as EventListener);
|
|
53
|
+
window.addEventListener('c64-cheevos-popped', this.handlePopped as EventListener);
|
|
54
|
+
window.addEventListener('c64-cheevos-snapshot', this.handleSnapshot as EventListener);
|
|
55
|
+
window.addEventListener('c64-cheevos-error', this.handleError as EventListener);
|
|
56
|
+
window.addEventListener('c64-cheevos-tracker-toggle', this.handleToggle as EventListener);
|
|
57
|
+
window.addEventListener('c64-display-mode-changed', this.handleDisplayMode as EventListener);
|
|
58
|
+
this.root.addEventListener('click', this.handleClick);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
destroy(): void {
|
|
62
|
+
window.removeEventListener('c64-cheevos-status', this.handleStatus as EventListener);
|
|
63
|
+
window.removeEventListener('c64-cheevos-event', this.handleCheevosEvent as EventListener);
|
|
64
|
+
window.removeEventListener('c64-cheevos-score', this.handleScore as EventListener);
|
|
65
|
+
window.removeEventListener('c64-cheevos-popped', this.handlePopped as EventListener);
|
|
66
|
+
window.removeEventListener('c64-cheevos-snapshot', this.handleSnapshot as EventListener);
|
|
67
|
+
window.removeEventListener('c64-cheevos-error', this.handleError as EventListener);
|
|
68
|
+
window.removeEventListener('c64-cheevos-tracker-toggle', this.handleToggle as EventListener);
|
|
69
|
+
window.removeEventListener('c64-display-mode-changed', this.handleDisplayMode as EventListener);
|
|
70
|
+
this.root?.removeEventListener('click', this.handleClick);
|
|
71
|
+
this.root?.remove();
|
|
72
|
+
this.root = null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private readonly handleStatus = (event: CustomEvent): void => {
|
|
76
|
+
const detail = event.detail as {
|
|
77
|
+
detectorId?: string;
|
|
78
|
+
status?: string;
|
|
79
|
+
cheevosSet?: CheevosDevSet;
|
|
80
|
+
poppedCheevos?: PoppedCheevo[];
|
|
81
|
+
};
|
|
82
|
+
if (detail.status === 'enabled') {
|
|
83
|
+
this.state.enabled = true;
|
|
84
|
+
this.state.detectorId = detail.detectorId ?? '';
|
|
85
|
+
this.state.cheevosSet = detail.cheevosSet ?? { cheevos: [], trackerFields: [] };
|
|
86
|
+
this.state.popped = normalisePopped(detail.poppedCheevos ?? []);
|
|
87
|
+
this.state.isGameOver = undefined;
|
|
88
|
+
this.state.lastMessage = '';
|
|
89
|
+
this.state.expandedLists = {};
|
|
90
|
+
this.render();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (detail.status === 'popped-cleared') {
|
|
94
|
+
this.state.popped = [];
|
|
95
|
+
this.render();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
private readonly handleCheevosEvent = (event: CustomEvent): void => {
|
|
100
|
+
const detail = event.detail as { type?: string; payload?: Record<string, unknown> };
|
|
101
|
+
if (detail.type === 'newGame') {
|
|
102
|
+
this.state.isGameOver = false;
|
|
103
|
+
this.state.lastMessage = 'New game started';
|
|
104
|
+
} else if (detail.type === 'gameOver') {
|
|
105
|
+
this.state.isGameOver = true;
|
|
106
|
+
this.state.score = normaliseValue(detail.payload?.score) ?? this.state.score;
|
|
107
|
+
this.state.lastMessage = 'Game over';
|
|
108
|
+
} else if (detail.type === 'livesChange') {
|
|
109
|
+
this.state.lives = normaliseValue(detail.payload?.lives) ?? this.state.lives;
|
|
110
|
+
} else if (detail.type === 'cheevo') {
|
|
111
|
+
this.state.lastMessage = String(detail.payload?.title ?? 'Achievement unlocked');
|
|
112
|
+
}
|
|
113
|
+
this.render();
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
private readonly handleScore = (event: CustomEvent): void => {
|
|
117
|
+
const detail = event.detail as { score?: { score?: number } };
|
|
118
|
+
this.state.score = detail.score?.score ?? this.state.score;
|
|
119
|
+
this.render();
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
private readonly handlePopped = (event: CustomEvent): void => {
|
|
123
|
+
const detail = event.detail as {
|
|
124
|
+
achievement?: CheevosDevAchievement;
|
|
125
|
+
poppedCheevos?: PoppedCheevo[];
|
|
126
|
+
};
|
|
127
|
+
if (detail.poppedCheevos) this.state.popped = normalisePopped(detail.poppedCheevos);
|
|
128
|
+
else if (
|
|
129
|
+
detail.achievement &&
|
|
130
|
+
!this.state.popped.some((item) => item._id === detail.achievement?._id)
|
|
131
|
+
) {
|
|
132
|
+
this.state.popped = [...this.state.popped, detail.achievement];
|
|
133
|
+
}
|
|
134
|
+
if (detail.achievement) this.state.lastMessage = `Popped: ${detail.achievement.title}`;
|
|
135
|
+
this.render();
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
private readonly handleSnapshot = (event: CustomEvent): void => {
|
|
139
|
+
const detail = event.detail as {
|
|
140
|
+
score?: string | number | boolean;
|
|
141
|
+
lives?: string | number | boolean;
|
|
142
|
+
isGameOver?: boolean;
|
|
143
|
+
fields?: Record<string, string | number | boolean | undefined>;
|
|
144
|
+
};
|
|
145
|
+
if (detail.score !== undefined) this.state.score = detail.score;
|
|
146
|
+
if (detail.lives !== undefined) this.state.lives = detail.lives;
|
|
147
|
+
if (detail.isGameOver !== undefined) this.state.isGameOver = detail.isGameOver;
|
|
148
|
+
if (detail.fields) this.state.fields = detail.fields;
|
|
149
|
+
this.render();
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
private readonly handleError = (event: CustomEvent): void => {
|
|
153
|
+
const detail = event.detail as { error?: string };
|
|
154
|
+
this.state.lastMessage = `Error: ${detail.error ?? 'Unknown error'}`;
|
|
155
|
+
this.render();
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
private readonly handleToggle = (event: CustomEvent): void => {
|
|
159
|
+
const detail = event.detail as { visible?: boolean };
|
|
160
|
+
this.state.visiblePreference = detail.visible !== false;
|
|
161
|
+
localStorage.setItem(
|
|
162
|
+
CHEEVOS_TRACKER_VISIBLE_STORAGE_KEY,
|
|
163
|
+
this.state.visiblePreference ? '1' : '0',
|
|
164
|
+
);
|
|
165
|
+
this.render();
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
private readonly handleDisplayMode = (event: CustomEvent): void => {
|
|
169
|
+
const detail = event.detail as { mode?: string };
|
|
170
|
+
this.state.displayMode = detail.mode ?? 'standard';
|
|
171
|
+
this.render();
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
private readonly handleClick = (event: MouseEvent): void => {
|
|
175
|
+
const target = event.target;
|
|
176
|
+
if (!(target instanceof HTMLElement)) return;
|
|
177
|
+
const button = target.closest<HTMLButtonElement>('[data-cheevos-list-toggle]');
|
|
178
|
+
if (!button) return;
|
|
179
|
+
const listKey = button.dataset.cheevosListToggle;
|
|
180
|
+
if (!listKey) return;
|
|
181
|
+
this.state.expandedLists = {
|
|
182
|
+
...this.state.expandedLists,
|
|
183
|
+
[listKey]: !this.state.expandedLists[listKey],
|
|
184
|
+
};
|
|
185
|
+
this.render();
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
private render(): void {
|
|
189
|
+
if (!this.root) return;
|
|
190
|
+
this.root.classList.toggle(
|
|
191
|
+
'hidden',
|
|
192
|
+
!this.state.enabled || !this.state.visiblePreference || this.state.displayMode !== 'standard',
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const cheevos = this.state.cheevosSet.cheevos ?? [];
|
|
196
|
+
const poppedIds = new Set(this.state.popped.map((achievement) => achievement._id));
|
|
197
|
+
const remaining = cheevos.filter((achievement) => !poppedIds.has(achievement._id));
|
|
198
|
+
|
|
199
|
+
this.root.innerHTML = `
|
|
200
|
+
<h2>Cheevos</h2>
|
|
201
|
+
<p class="c64-cheevos-tracker-subtitle">${escapeHtml(this.state.detectorId || 'No detector')}</p>
|
|
202
|
+
<div class="c64-cheevos-tracker-grid">
|
|
203
|
+
${renderStat('Score', this.state.score ?? '-')}
|
|
204
|
+
${renderStat('Lives', this.state.lives ?? '-')}
|
|
205
|
+
${renderStat('Status', this.state.isGameOver ? 'Game Over' : this.state.isGameOver === false ? 'Playing' : 'Waiting')}
|
|
206
|
+
${renderStat('Popped', `${this.state.popped.length}/${cheevos.length}`)}
|
|
207
|
+
</div>
|
|
208
|
+
${this.renderFields()}
|
|
209
|
+
${renderList('popped', 'Popped', this.state.popped, true, !!this.state.expandedLists.popped)}
|
|
210
|
+
${renderList('remaining', 'Remaining', remaining, false, !!this.state.expandedLists.remaining)}
|
|
211
|
+
${this.state.lastMessage ? `<p class="c64-cheevos-message">${escapeHtml(this.state.lastMessage)}</p>` : ''}
|
|
212
|
+
`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private renderFields(): string {
|
|
216
|
+
const fields = this.state.cheevosSet.trackerFields ?? [];
|
|
217
|
+
if (fields.length === 0) return '';
|
|
218
|
+
return `<div class="c64-cheevos-field-list">${fields
|
|
219
|
+
.map((field) => renderField(field, this.state.fields[field.key]))
|
|
220
|
+
.join('')}</div>`;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private injectCSS(): void {
|
|
224
|
+
if (document.querySelector('style[data-c64-cheevos-tracker]')) return;
|
|
225
|
+
const style = document.createElement('style');
|
|
226
|
+
style.setAttribute('data-c64-cheevos-tracker', '');
|
|
227
|
+
style.textContent = css;
|
|
228
|
+
document.head.appendChild(style);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function renderStat(label: string, value: string | number | boolean): string {
|
|
233
|
+
return `<div class="c64-cheevos-stat"><span class="c64-cheevos-stat-label">${escapeHtml(label)}</span><span class="c64-cheevos-stat-value">${escapeHtml(String(value))}</span></div>`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function renderField(
|
|
237
|
+
field: CheevosDevTrackerField,
|
|
238
|
+
value: string | number | boolean | undefined,
|
|
239
|
+
): string {
|
|
240
|
+
const label = field.label ?? field.key;
|
|
241
|
+
const displayValue = value ?? '-';
|
|
242
|
+
const bar =
|
|
243
|
+
field.display === 'bar' &&
|
|
244
|
+
typeof value === 'number' &&
|
|
245
|
+
typeof field.max === 'number' &&
|
|
246
|
+
field.max > 0
|
|
247
|
+
? `<div class="c64-cheevos-bar"><div class="c64-cheevos-bar-fill" style="inline-size:${Math.max(0, Math.min(100, (value / field.max) * 100))}%"></div></div>`
|
|
248
|
+
: '';
|
|
249
|
+
return `<div class="c64-cheevos-field"><div class="c64-cheevos-field-row"><span>${escapeHtml(label)}</span><strong>${escapeHtml(String(displayValue))}</strong></div>${bar}</div>`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function renderList(
|
|
253
|
+
key: string,
|
|
254
|
+
title: string,
|
|
255
|
+
achievements: CheevosDevAchievement[],
|
|
256
|
+
popped: boolean,
|
|
257
|
+
expanded: boolean,
|
|
258
|
+
): string {
|
|
259
|
+
const hasMore = achievements.length > COLLAPSED_ACHIEVEMENT_LIMIT;
|
|
260
|
+
const visibleAchievements = expanded
|
|
261
|
+
? achievements
|
|
262
|
+
: achievements.slice(0, COLLAPSED_ACHIEVEMENT_LIMIT);
|
|
263
|
+
const columnClass = getColumnClass(visibleAchievements.length);
|
|
264
|
+
const items = visibleAchievements
|
|
265
|
+
.map(
|
|
266
|
+
(achievement) =>
|
|
267
|
+
`<li class="c64-cheevos-item${popped ? ' popped' : ''}"><span class="c64-cheevos-item-title">${escapeHtml(achievement.title)}</span>${
|
|
268
|
+
achievement.description
|
|
269
|
+
? `<span class="c64-cheevos-item-description" title="${achievement.description}">${escapeHtml(achievement.description)}</span>`
|
|
270
|
+
: ''
|
|
271
|
+
}</li>`,
|
|
272
|
+
)
|
|
273
|
+
.join('');
|
|
274
|
+
const toggle = hasMore
|
|
275
|
+
? `<button class="c64-cheevos-list-toggle" type="button" data-cheevos-list-toggle="${escapeHtml(key)}">${expanded ? 'Show less' : `Show all ${achievements.length}`}</button>`
|
|
276
|
+
: '';
|
|
277
|
+
return `<div class="c64-cheevos-list-header"><h3 class="c64-cheevos-list-title">${escapeHtml(title)}</h3>${toggle}</div>${
|
|
278
|
+
items
|
|
279
|
+
? `<ul class="c64-cheevos-list ${columnClass}">${items}</ul>`
|
|
280
|
+
: '<p class="c64-cheevos-empty">None</p>'
|
|
281
|
+
}`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function getColumnClass(count: number): string {
|
|
285
|
+
if (count >= 9) return 'cols-3';
|
|
286
|
+
if (count >= 4) return 'cols-2';
|
|
287
|
+
return 'cols-1';
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function normalisePopped(popped: PoppedCheevo[]): CheevosDevAchievement[] {
|
|
291
|
+
return popped.map((item) => item.achievement).filter(Boolean);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function normaliseValue(value: unknown): string | number | boolean | undefined {
|
|
295
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
|
|
296
|
+
return value;
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function escapeHtml(value: string): string {
|
|
301
|
+
return value
|
|
302
|
+
.replace(/&/g, '&')
|
|
303
|
+
.replace(/</g, '<')
|
|
304
|
+
.replace(/>/g, '>')
|
|
305
|
+
.replace(/"/g, '"')
|
|
306
|
+
.replace(/'/g, ''');
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export default CheevosTrackerPanel;
|
package/src/player/index.ts
CHANGED
|
@@ -4,6 +4,25 @@ export type { C64PlayerOptions, ProgressCallback } from './c64-player';
|
|
|
4
4
|
export { AudioEngine } from './audio-engine';
|
|
5
5
|
export type { AudioEngineOptions, AudioStateChangeCallback, SidBufferReader } from './audio-engine';
|
|
6
6
|
export { default as InputHandler } from './input-handler';
|
|
7
|
+
export { C64_KEY } from '../emulator/c64-keys';
|
|
8
|
+
export {
|
|
9
|
+
DEFAULT_GAMEPAD_MAPPING,
|
|
10
|
+
GAMEPAD_BUTTONS,
|
|
11
|
+
GAMEPAD_DIRECTIONS,
|
|
12
|
+
resolveGamepadMapping,
|
|
13
|
+
restrictDirections,
|
|
14
|
+
} from '../emulator/gamepad-mapping';
|
|
15
|
+
export type {
|
|
16
|
+
ControlAction,
|
|
17
|
+
GamepadButton,
|
|
18
|
+
GamepadControl,
|
|
19
|
+
GamepadDirection,
|
|
20
|
+
GamepadMapping,
|
|
21
|
+
GamepadState,
|
|
22
|
+
JoystickAction,
|
|
23
|
+
ResolvedGamepadMapping,
|
|
24
|
+
StickMode,
|
|
25
|
+
} from '../emulator/gamepad-mapping';
|
|
7
26
|
export type {
|
|
8
27
|
C64Config,
|
|
9
28
|
C64State,
|