cortico-world-fishx 0.8.1 → 0.8.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-fishx",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "author": "青",
5
5
  "description": "青空极限运动:内置 18 项极限运动与世界知名地标,支持自建运动及导入导出、五域熟练度成长、每日挑战打卡、季节限定活动与明信片 PNG 合成;支持委派插件、直连 API(OpenAI/Gemini)及本地 ComfyUI 出图,纯文字也可完整游玩,数据只存本机 fish-x/。",
6
6
  "type": "module",
package/src/state.js CHANGED
@@ -8,6 +8,8 @@ const HISTORY_LIMIT = 50;
8
8
  const pendingPlays = new Map();
9
9
  const pendingWrites = new Map();
10
10
  const DOMAINS = ['air', 'water', 'snow', 'rock', 'park'];
11
+ const builtinSouvenirById = new Map(builtinSports.flatMap(sport => sport.souvenirs
12
+ .map(({ id, name, desc }) => [id, { name, desc }])));
11
13
 
12
14
  export const EXPEDITIONS = Object.freeze([
13
15
  {
@@ -274,10 +276,34 @@ function cleanProfile(value) {
274
276
  };
275
277
  }
276
278
 
279
+ function repairSouvenir(value) {
280
+ if (!isRecord(value)) return false;
281
+ const builtin = builtinSouvenirById.get(value.id);
282
+ if (!builtin || !['name', 'desc'].some(key => (
283
+ typeof value[key] !== 'string' || !value[key].trim() || /\?{2,}|\uFFFD/u.test(value[key])
284
+ ))) return false;
285
+ Object.assign(value, builtin);
286
+ return true;
287
+ }
288
+
277
289
  export async function loadState(dataDir) {
290
+ return withLock(pendingWrites, dataDir, () => readState(dataDir));
291
+ }
292
+
293
+ async function readState(dataDir) {
294
+ let state;
295
+ let repaired = false;
278
296
  try {
279
297
  const parsed = JSON.parse(await readFile(statePath(dataDir), 'utf8'));
280
298
  if (!isRecord(parsed) || parsed.schemaVersion !== 3 || !Array.isArray(parsed.recent)) return emptyState();
299
+ const snapshots = [
300
+ ...parsed.recent.map(entry => entry?.souvenir),
301
+ parsed.last?.souvenir,
302
+ ...Object.values(isRecord(parsed.souvenirs) ? parsed.souvenirs : {}),
303
+ ];
304
+ for (const souvenir of snapshots) {
305
+ if (repairSouvenir(souvenir)) repaired = true;
306
+ }
281
307
  const recent = parsed.recent
282
308
  .filter(entry => entry && typeof entry.id === 'string' && typeof entry.at === 'string')
283
309
  .map((entry, index) => Object.fromEntries([
@@ -310,9 +336,10 @@ export async function loadState(dataDir) {
310
336
  sha256: typeof image.sha256 === 'string' ? image.sha256.toLowerCase() : sha256.toLowerCase(),
311
337
  }]))
312
338
  : {};
313
- return {
339
+ state = {
314
340
  schemaVersion: 3,
315
341
  recent,
342
+ ...(isRecord(parsed.last) ? { last: parsed.last } : {}),
316
343
  captionRounds,
317
344
  images,
318
345
  customSports: Array.isArray(parsed.customSports) ? parsed.customSports.filter(isRecord) : [],
@@ -335,6 +362,8 @@ export async function loadState(dataDir) {
335
362
  } catch {
336
363
  return emptyState();
337
364
  }
365
+ if (repaired) await persistState(dataDir, state);
366
+ return state;
338
367
  }
339
368
 
340
369
  export async function loadRecent(dataDir) {
@@ -473,7 +502,7 @@ export function addCustomSport(dataDir, sport) {
473
502
  || !DOMAINS.includes(entry.domain)) {
474
503
  throw new TypeError('自定义运动的 id 或名称无效');
475
504
  }
476
- const state = await loadState(dataDir);
505
+ const state = await readState(dataDir);
477
506
  const existingTokens = new Set([...builtinSports, ...state.customSports].flatMap(sportTokens));
478
507
  if (sportTokens(entry).some(token => existingTokens.has(token))) {
479
508
  throw new Error('运动 id、名称或别名已存在');
@@ -488,7 +517,7 @@ export function upsertLocation(dataDir, sportId, value) {
488
517
  return withLock(pendingWrites, dataDir, async () => {
489
518
  const location = cleanLocation(value);
490
519
  if (typeof sportId !== 'string' || !sportId.trim() || !location) throw new TypeError('运动 id 或地点信息无效');
491
- const state = await loadState(dataDir);
520
+ const state = await readState(dataDir);
492
521
  const locations = state.customLocations[sportId] ?? [];
493
522
  const index = locations.findIndex(item => item.id === location.id);
494
523
  if (index >= 0) locations[index] = location;
@@ -504,7 +533,7 @@ export function removeLocation(dataDir, sportId, id, builtinLocation = false) {
504
533
  if (typeof sportId !== 'string' || !sportId.trim() || !/^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(String(id ?? ''))) {
505
534
  throw new TypeError('运动 id 或地点 id 无效');
506
535
  }
507
- const state = await loadState(dataDir);
536
+ const state = await readState(dataDir);
508
537
  const locations = state.customLocations[sportId] ?? [];
509
538
  const index = locations.findIndex(item => item.id === id);
510
539
  if (index >= 0) locations.splice(index, 1);
@@ -520,7 +549,7 @@ export function removeLocation(dataDir, sportId, id, builtinLocation = false) {
520
549
  export function setSeasonOverride(dataDir, value) {
521
550
  return withLock(pendingWrites, dataDir, async () => {
522
551
  if (!['auto', 'off', ...Object.keys(SEASONS)].includes(value)) throw new TypeError('季节设置无效');
523
- const state = await loadState(dataDir);
552
+ const state = await readState(dataDir);
524
553
  state.seasonOverride = value;
525
554
  await persistState(dataDir, state);
526
555
  return value;
@@ -531,7 +560,7 @@ export function startExpedition(dataDir, expeditionId) {
531
560
  return withLock(pendingWrites, dataDir, async () => {
532
561
  const route = expeditionById.get(expeditionId);
533
562
  if (!route) throw new TypeError('远征路线不存在');
534
- const state = await loadState(dataDir);
563
+ const state = await readState(dataDir);
535
564
  if (state.expeditions.activeId === route.id) return state.expeditions;
536
565
  if (state.expeditions.completedIds.includes(route.id)) throw new Error('这条远征路线已经完成');
537
566
  state.expeditions = { ...state.expeditions, activeId: route.id, progress: [] };
@@ -542,7 +571,7 @@ export function startExpedition(dataDir, expeditionId) {
542
571
 
543
572
  export function abandonExpedition(dataDir) {
544
573
  return withLock(pendingWrites, dataDir, async () => {
545
- const state = await loadState(dataDir);
574
+ const state = await readState(dataDir);
546
575
  state.expeditions = { ...state.expeditions, activeId: null, progress: [] };
547
576
  await persistState(dataDir, state);
548
577
  return state.expeditions;
@@ -553,7 +582,7 @@ export function reactTrip(dataDir, tripId, reaction) {
553
582
  return withLock(pendingWrites, dataDir, async () => {
554
583
  if (typeof tripId !== 'string' || !tripId.trim()) throw new TypeError('tripId 必须是非空文本');
555
584
  if (!['praise', 'encourage', 'highfive'].includes(reaction)) throw new TypeError('reaction 无效');
556
- const state = await loadState(dataDir);
585
+ const state = await readState(dataDir);
557
586
  const index = state.recent.findIndex(entry => entry.tripId === tripId);
558
587
  if (index < 0) throw new Error('未找到指定的运动记录');
559
588
  if (state.recent[index].userReaction) throw new Error('这条运动记录已经互动过');
@@ -571,7 +600,7 @@ export function reactTrip(dataDir, tripId, reaction) {
571
600
 
572
601
  export function saveSportPackImport(dataDir, pack, options = {}) {
573
602
  return withLock(pendingWrites, dataDir, async () => {
574
- const state = await loadState(dataDir);
603
+ const state = await readState(dataDir);
575
604
  const result = importSportPack(state, pack, options);
576
605
  if (result.ok) await persistState(dataDir, state);
577
606
  return result;
@@ -580,7 +609,7 @@ export function saveSportPackImport(dataDir, pack, options = {}) {
580
609
 
581
610
  export function removeCustomSport(dataDir, id) {
582
611
  return withLock(pendingWrites, dataDir, async () => {
583
- const state = await loadState(dataDir);
612
+ const state = await readState(dataDir);
584
613
  if (builtinSports.some(sport => sport.id === id)) throw new Error('内置运动不可删除');
585
614
  const index = state.customSports.findIndex(sport => sport.id === id);
586
615
  if (index < 0) throw new Error('未找到该自定义运动');
@@ -604,7 +633,7 @@ async function persistState(dataDir, state) {
604
633
  }
605
634
 
606
635
  async function writePlay(dataDir, id, caption, metadata = {}) {
607
- const state = await loadState(dataDir);
636
+ const state = await readState(dataDir);
608
637
  const sportId = String(metadata.sportId ?? id);
609
638
  const at = metadata.at ?? new Date().toISOString();
610
639
  const recentEntry = {
@@ -691,7 +720,7 @@ async function writePlay(dataDir, id, caption, metadata = {}) {
691
720
  }
692
721
 
693
722
  async function writeImageForLatestPlay(dataDir, image) {
694
- const state = await loadState(dataDir);
723
+ const state = await readState(dataDir);
695
724
  const latestIndex = state.recent.length - 1;
696
725
  if (latestIndex < 0) throw new Error('没有最近一次运动记录,无法关联图片');
697
726
  if (!image || typeof image.name !== 'string' || !/^[a-f0-9]{16}\.(png|jpe?g|gif|webp|bmp|avif)$/i.test(image.name)) {
@@ -4,7 +4,7 @@ import { readFileSync } from 'node:fs';
4
4
  const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
5
5
  const definition = readFileSync(new URL('../src/definition.js', import.meta.url), 'utf8');
6
6
  assert.equal(pkg.name, 'cortico-world-fishx');
7
- assert.equal(pkg.version, '0.8.1');
7
+ assert.equal(pkg.version, '0.8.2');
8
8
  assert.equal(pkg.cortico?.kind, 'world');
9
9
  assert.equal(pkg.cortico?.api, 6);
10
10
  assert.equal(pkg.cortico?.displayName, '青空极限运动');