fix: preserve best-general rank data parity

This commit is contained in:
2026-07-26 06:03:21 +00:00
parent 06179cbd4f
commit 875dc610fb
17 changed files with 460 additions and 197 deletions
@@ -1,6 +1,8 @@
import { describe, expect, it } from 'vitest';
import { LEGACY_RANK_DATA_TYPES } from '@sammo-ts/common';
import type { TurnSchedule } from '@sammo-ts/logic';
import { rankMetaKey } from '../src/turn/rankData.js';
import type { TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
import { createTurnTestHarness } from './helpers/turnTestHarness.js';
@@ -294,6 +296,9 @@ describe('legacy general turn lifecycle', () => {
});
it('retires a player general and resets inherited stats and rank state', async () => {
const legacyRankMeta = Object.fromEntries(
LEGACY_RANK_DATA_TYPES.map((type, index) => [rankMetaKey(type), index + 1])
);
const harness = await createTurnTestHarness({
snapshot: makeSnapshot([
makeGeneral({
@@ -302,11 +307,11 @@ describe('legacy general turn lifecycle', () => {
experience: 101,
dedication: 81,
meta: {
...legacyRankMeta,
killturn: 24,
dex1: 101,
inherit_lived_month: 10,
inherit_active_action: 4,
rank_warnum: 12,
},
}),
]),
@@ -325,7 +330,9 @@ describe('legacy general turn lifecycle', () => {
expect(updated.meta.dex1).toBe(51);
expect(updated.meta.inherit_lived_month).toBe(0);
expect(updated.meta.inherit_active_action).toBe(0);
expect(updated.meta.rank_warnum).toBe(0);
for (const type of LEGACY_RANK_DATA_TYPES) {
expect(updated.meta[rankMetaKey(type)], type).toBe(0);
}
expect(harness.world.peekDirtyState().lifecycleEvents[0]?.outcome).toBe('retired');
});
});
+65
View File
@@ -0,0 +1,65 @@
import { describe, expect, it } from 'vitest';
import { LEGACY_RANK_DATA_TYPES, RANK_DATA_TYPES } from '@sammo-ts/common';
import {
applyPersistedRankRowsToMeta,
buildLegacyComparableRankRows,
buildPersistedRankRows,
rankMetaKey,
} from '../src/turn/rankData.js';
describe('rank data projection', () => {
it('projects every core row while preserving legacy key and integer semantics', () => {
const rows = buildPersistedRankRows({
id: 7,
nationId: 2,
experience: 10.5,
dedication: 20.49,
meta: {
rank_warnum: '3.5',
ttw: 4.5,
inherit_earned: '9',
dex1: 12,
},
});
expect(rows).toHaveLength(RANK_DATA_TYPES.length);
expect(rows).toEqual(
expect.arrayContaining([
{ generalId: 7, nationId: 2, type: 'experience', value: 11 },
{ generalId: 7, nationId: 2, type: 'dedication', value: 20 },
{ generalId: 7, nationId: 2, type: 'warnum', value: 4 },
{ generalId: 7, nationId: 2, type: 'ttw', value: 5 },
{ generalId: 7, nationId: 2, type: 'inherit_earned', value: 9 },
{ generalId: 7, nationId: 2, type: 'dex1', value: 12 },
])
);
expect(buildLegacyComparableRankRows({
id: 7,
nationId: 2,
experience: 10.5,
dedication: 20.49,
meta: {},
})).toHaveLength(LEGACY_RANK_DATA_TYPES.length);
});
it('loads persisted rows into the same raw and prefixed meta keys used by commands', () => {
const meta: Record<string, unknown> = {};
applyPersistedRankRowsToMeta(meta, [
{ type: 'warnum', value: 3 },
{ type: 'ttw', value: 4 },
{ type: 'inherit_spent', value: 5 },
{ type: 'dex1', value: 6 },
{ type: 'unknown', value: 99 },
]);
expect(meta).toEqual({
rank_warnum: 3,
ttw: 4,
inherit_spent: 5,
dex1: 6,
});
expect(rankMetaKey('warnum')).toBe('rank_warnum');
expect(rankMetaKey('betgold')).toBe('betgold');
});
});