From 85c871cec13d11a68f479aa7cdd72afbc0cffad4 Mon Sep 17 00:00:00 2001 From: Vick Scarlet Date: Thu, 30 Jul 2026 20:29:08 +0800 Subject: [PATCH] update: state FlatState --- packages/core/src/state.spec.ts | 12 +++++------- packages/core/src/state.ts | 19 +++++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/core/src/state.spec.ts b/packages/core/src/state.spec.ts index 055da24..a459567 100644 --- a/packages/core/src/state.spec.ts +++ b/packages/core/src/state.spec.ts @@ -42,8 +42,8 @@ function getPropertiesFromCondition(condition: string): Set { return properties } -describe('策划配置条件完整覆盖率测试', () => { - test('event [include, exclude, branch]', () => { +describe('FlatState', () => { + test('[Cover] event(include, exclude, branch)', () => { const unmappedKeys = new Set() for (const item of events.values()) { const conditionsToTrack: { label: string; text: string }[] = [] @@ -85,7 +85,7 @@ describe('策划配置条件完整覆盖率测试', () => { expect(unmappedKeys.size).toBe(0) }) - test('talent [condition]', () => { + test('[Cover] talent(condition)', () => { const unmappedKeys = new Set() const allTalentItems = talents.values() @@ -111,7 +111,7 @@ describe('策划配置条件完整覆盖率测试', () => { expect(unmappedKeys.size).toBe(0) }) - test('achievement [condition]', () => { + test('[Cover] achievement(condition)', () => { const unmappedKeys = new Set() const allAchievementItems = achievements.values() @@ -136,10 +136,8 @@ describe('策划配置条件完整覆盖率测试', () => { expect(unmappedKeys.size).toBe(0) }) -}) -describe('FlatProperties', () => { - test('check key', () => { + test('get', () => { const game = { lowest: { age: 1, diff --git a/packages/core/src/state.ts b/packages/core/src/state.ts index edb60e1..bf8359c 100644 --- a/packages/core/src/state.ts +++ b/packages/core/src/state.ts @@ -85,13 +85,15 @@ export interface FlatState { SUM: number } -interface FlatPropertiesTarget { +type FlatStateKey = keyof FlatState + +interface FlatTarget { game: GameState profile: ProfileState } -type FlatMapper = ( - state: FlatPropertiesTarget, +type FlatMapper = ( + state: FlatTarget, ) => FlatState[Key] const FlatMappers = { @@ -125,20 +127,17 @@ const FlatMappers = { const s = sum(Object.values(others)) return Math.floor(s * 2 + age / 2) }, -} as Record> +} as { [Key in FlatStateKey]: FlatMapper } -const flatPropertiesHandle = { - get(target: FlatPropertiesTarget, prop: keyof FlatState) { +const flatStateHandle = { + get(target: FlatTarget, prop: Key) { return FlatMappers[prop]?.(target) }, set: () => true, } export function createFlatState(game: GameState, profile: ProfileState) { - return new Proxy( - { game, profile }, - flatPropertiesHandle, - ) as unknown as FlatState + return new Proxy({ game, profile }, flatStateHandle) as unknown as FlatState } export interface Effect {