fix: 数据统计错误

This commit is contained in:
unanmed 2025-08-19 14:13:53 +08:00
parent 3ce270e7e6
commit 398de70fee

View File

@ -9,7 +9,7 @@ import { waitbox, ListPage, TextContent } from '../components';
import { DefaultProps } from '@motajs/render-vue'; import { DefaultProps } from '@motajs/render-vue';
import { ItemState } from '@user/data-state'; import { ItemState } from '@user/data-state';
interface StatisticsDataOneFloor { export interface StatisticsDataOneFloor {
enemyCount: number; enemyCount: number;
potionCount: number; potionCount: number;
gemCount: number; gemCount: number;
@ -19,7 +19,7 @@ interface StatisticsDataOneFloor {
mdefValue: number; mdefValue: number;
} }
interface StatisticsData { export interface StatisticsData {
total: StatisticsDataOneFloor; total: StatisticsDataOneFloor;
floors: Map<FloorIds, StatisticsDataOneFloor>; floors: Map<FloorIds, StatisticsDataOneFloor>;
} }
@ -75,7 +75,7 @@ const statisticsPanelProps = {
const TotalStatistics = defineComponent<StatisticsPanelProps>(props => { const TotalStatistics = defineComponent<StatisticsPanelProps>(props => {
return () => ( return () => (
<container> <container>
<TextContent text='' width={310}></TextContent> <TextContent text="" width={310}></TextContent>
</container> </container>
); );
}, statisticsPanelProps); }, statisticsPanelProps);
@ -92,25 +92,31 @@ const PotionStatistics = defineComponent<StatisticsPanelProps>(props => {
return () => <container></container>; return () => <container></container>;
}, statisticsPanelProps); }, statisticsPanelProps);
function calculateStatistics(): StatisticsData { export function calculateStatisticsOne(
floorId: FloorIds,
diff?: Map<string, number>
) {
core.setFlag('__statistics__', true); core.setFlag('__statistics__', true);
const hasDiff = !!diff;
if (!hasDiff) {
diff = new Map();
const hero = core.status.hero; const hero = core.status.hero;
const diff: Record<string | symbol, number> = {};
const handler: ProxyHandler<HeroStatus> = { const handler: ProxyHandler<HeroStatus> = {
set(_target, p, newValue) { set(target, p, newValue) {
if (typeof p !== 'string') return true;
if (typeof newValue === 'number') { if (typeof newValue === 'number') {
diff[p] ??= 0; const value = diff!.get(p) ?? 0;
diff[p] += newValue; const delta =
newValue - (target[p as keyof HeroStatus] as number);
diff!.set(p, value + delta);
} }
return true; return true;
} }
}; };
const proxy = new Proxy(hero, handler); const proxy = new Proxy(hero, handler);
core.status.hero = proxy; core.status.hero = proxy;
}
const floors = new Map<FloorIds, StatisticsDataOneFloor>(); core.extractBlocks(floorId);
core.floorIds.forEach(v => {
core.extractBlocks(v);
const statistics: StatisticsDataOneFloor = { const statistics: StatisticsDataOneFloor = {
enemyCount: 0, enemyCount: 0,
potionCount: 0, potionCount: 0,
@ -120,13 +126,12 @@ function calculateStatistics(): StatisticsData {
defValue: 0, defValue: 0,
mdefValue: 0 mdefValue: 0
}; };
core.status.maps[v].blocks.forEach(v => { if (!diff) return statistics;
core.status.maps[floorId].blocks.forEach(v => {
if (v.event.cls === 'enemys' || v.event.cls === 'enemy48') { if (v.event.cls === 'enemys' || v.event.cls === 'enemy48') {
statistics.enemyCount++; statistics.enemyCount++;
} else if (v.event.cls === 'items') { } else if (v.event.cls === 'items') {
const item = ItemState.items.get( const item = ItemState.items.get(v.event.id as AllIdsOf<'items'>);
v.event.id as AllIdsOf<'items'>
);
if (!item) return; if (!item) return;
if (item.cls === 'items') { if (item.cls === 'items') {
try { try {
@ -134,28 +139,62 @@ function calculateStatistics(): StatisticsData {
} catch { } catch {
// pass // pass
} }
if (diff.hp > 0) { const hp = diff.get('hp') ?? 0;
const atk = diff.get('atk') ?? 0;
const def = diff.get('def') ?? 0;
const mdef = diff.get('mdef') ?? 0;
if (hp > 0) {
statistics.potionCount++; statistics.potionCount++;
statistics.potionValue += diff.hp; statistics.potionValue += hp;
} }
if (diff.atk > 0 || diff.def > 0 || diff.mdef > 0) { if (atk > 0 || def > 0 || mdef > 0) {
statistics.gemCount++; statistics.gemCount++;
} }
if (diff.atk > 0) { if (atk > 0) {
statistics.atkValue += diff.atk; statistics.atkValue += atk;
} }
if (diff.def > 0) { if (def > 0) {
statistics.defValue += diff.def; statistics.defValue += def;
} }
if (diff.mdef > 0) { if (mdef > 0) {
statistics.mdefValue += diff.mdef; statistics.mdefValue += mdef;
} }
} }
} }
for (const key of Object.keys(diff)) { diff.clear();
diff[key] = 0;
}
}); });
if (!hasDiff) {
core.status.hero = hero;
window.hero = hero;
window.flags = core.status.hero.flags;
}
core.removeFlag('__statistics__');
return statistics;
}
export function calculateStatistics(): StatisticsData {
core.setFlag('__statistics__', true);
const hero = core.status.hero;
const diff = new Map<string, number>();
const handler: ProxyHandler<HeroStatus> = {
set(target, p, newValue) {
if (typeof p !== 'string') return true;
if (typeof newValue === 'number') {
const value = diff!.get(p) ?? 0;
const delta =
newValue - (target[p as keyof HeroStatus] as number);
diff!.set(p, value + delta);
}
return true;
}
};
const proxy = new Proxy(hero, handler);
core.status.hero = proxy;
const floors = new Map<FloorIds, StatisticsDataOneFloor>();
core.floorIds.forEach(v => {
const statistics = calculateStatisticsOne(v, diff);
floors.set(v, statistics); floors.set(v, statistics);
}); });