refactor: 收口平台钱包余额 delta
This commit is contained in:
@@ -539,6 +539,11 @@ import {
|
||||
buildWoodenFishSessionFromWorkDetail,
|
||||
} from './platformMiniGameSessionMappingModel';
|
||||
import { resolvePlatformPlayedWorkOpenIntent } from './platformPlayedWorkOpenModel';
|
||||
import {
|
||||
adjustProfileDashboardWalletBalance,
|
||||
reconcileProfileWalletLocalDeltaWithServerDashboard,
|
||||
resolveProfileWalletBalance,
|
||||
} from './platformProfileWalletDeltaModel';
|
||||
import {
|
||||
type PlatformPublicCodeSearchStep,
|
||||
resolvePlatformPublicCodeSearchPlan,
|
||||
@@ -1049,60 +1054,6 @@ function openPuzzleRuntimeStage(
|
||||
writePuzzleRuntimeUrlState(state);
|
||||
}
|
||||
|
||||
function resolveProfileWalletBalance(
|
||||
dashboard: { walletBalance?: number | null } | null | undefined,
|
||||
) {
|
||||
const walletBalance = dashboard?.walletBalance;
|
||||
return typeof walletBalance === 'number' && Number.isFinite(walletBalance)
|
||||
? Math.max(0, Math.floor(walletBalance))
|
||||
: 0;
|
||||
}
|
||||
|
||||
function adjustProfileDashboardWalletBalance(
|
||||
dashboard: ProfileDashboardSummary | null,
|
||||
delta: number,
|
||||
): ProfileDashboardSummary | null {
|
||||
if (!dashboard || !Number.isFinite(delta) || delta === 0) {
|
||||
return dashboard;
|
||||
}
|
||||
|
||||
return {
|
||||
...dashboard,
|
||||
walletBalance: Math.max(
|
||||
0,
|
||||
resolveProfileWalletBalance(dashboard) + Math.trunc(delta),
|
||||
),
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
function reconcileProfileWalletLocalDeltaWithServerDashboard(
|
||||
previousDashboard: ProfileDashboardSummary | null,
|
||||
latestDashboard: ProfileDashboardSummary | null,
|
||||
localDelta: number,
|
||||
) {
|
||||
if (
|
||||
!previousDashboard ||
|
||||
!latestDashboard ||
|
||||
!Number.isFinite(localDelta) ||
|
||||
localDelta === 0
|
||||
) {
|
||||
return Number.isFinite(localDelta) ? Math.trunc(localDelta) : 0;
|
||||
}
|
||||
|
||||
const previousBalance = resolveProfileWalletBalance(previousDashboard);
|
||||
const latestBalance = resolveProfileWalletBalance(latestDashboard);
|
||||
const normalizedDelta = Math.trunc(localDelta);
|
||||
|
||||
if (normalizedDelta < 0) {
|
||||
const reflectedDebit = Math.max(0, previousBalance - latestBalance);
|
||||
return Math.min(0, normalizedDelta + reflectedDebit);
|
||||
}
|
||||
|
||||
const reflectedCredit = Math.max(0, latestBalance - previousBalance);
|
||||
return Math.max(0, normalizedDelta - reflectedCredit);
|
||||
}
|
||||
|
||||
function isPuzzleFormOnlyDraft(session: PuzzleAgentSessionSnapshot | null) {
|
||||
return Boolean(
|
||||
session?.stage === 'collecting_anchors' && session.draft?.formDraft,
|
||||
|
||||
Reference in New Issue
Block a user