121 lines
3.7 KiB
TypeScript
121 lines
3.7 KiB
TypeScript
import { HttpUtil } from './HttpUtil';
|
|
import { StorageManager } from './StorageManager';
|
|
import { AuthManager } from './AuthManager';
|
|
import { API_ENDPOINTS, API_TIMEOUT, POINT_REASONS } from '../config/ApiConfig';
|
|
import { ApiEnvelope, UserAssetsData } from '../types/ApiTypes';
|
|
|
|
/**
|
|
* 用户资产管理器
|
|
* 单例模式,负责积分的服务端同步
|
|
* 以服务端为准,本地 StorageManager 作为缓存
|
|
*/
|
|
export class UserAssetsManager {
|
|
private static _instance: UserAssetsManager | null = null;
|
|
|
|
static get instance(): UserAssetsManager {
|
|
if (!this._instance) {
|
|
this._instance = new UserAssetsManager();
|
|
}
|
|
return this._instance;
|
|
}
|
|
|
|
private constructor() {}
|
|
|
|
/**
|
|
* 从服务端获取最新积分
|
|
*/
|
|
async fetchPoints(): Promise<number> {
|
|
if (!AuthManager.instance.isLoggedIn) {
|
|
return StorageManager.getPoints();
|
|
}
|
|
|
|
try {
|
|
const response = await HttpUtil.get<ApiEnvelope<UserAssetsData>>(
|
|
API_ENDPOINTS.USER_ASSETS,
|
|
API_TIMEOUT.SHORT
|
|
);
|
|
|
|
if (response.success && response.data) {
|
|
StorageManager.setPoints(response.data.points);
|
|
return response.data.points;
|
|
}
|
|
} catch (err) {
|
|
console.error('[UserAssetsManager] 获取积分失败:', err);
|
|
}
|
|
|
|
return StorageManager.getPoints();
|
|
}
|
|
|
|
/**
|
|
* 消耗积分(解锁提示)
|
|
* @returns 是否消耗成功
|
|
*/
|
|
async consumePoint(levelId?: string, hintIndex?: number): Promise<boolean> {
|
|
if (!StorageManager.hasPoints()) {
|
|
return false;
|
|
}
|
|
|
|
if (!AuthManager.instance.isLoggedIn) {
|
|
return StorageManager.consumePoint();
|
|
}
|
|
|
|
try {
|
|
const response = await HttpUtil.post<ApiEnvelope<UserAssetsData>>(
|
|
API_ENDPOINTS.USER_ASSETS_CONSUME,
|
|
{
|
|
reason: POINT_REASONS.HINT_UNLOCK,
|
|
levelId,
|
|
hintIndex,
|
|
},
|
|
API_TIMEOUT.SHORT
|
|
);
|
|
|
|
if (response.success && response.data) {
|
|
StorageManager.setPoints(response.data.points);
|
|
return true;
|
|
} else {
|
|
console.warn('[UserAssetsManager] 消耗积分失败:', response.message);
|
|
return false;
|
|
}
|
|
} catch (err) {
|
|
console.error('[UserAssetsManager] 消耗积分请求失败,降级本地处理:', err);
|
|
return StorageManager.consumePoint();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获得积分(通关奖励)
|
|
* @param levelId 关卡ID
|
|
* @param timeSpent 通关耗时(秒)
|
|
* @returns 获得后的积分数
|
|
*/
|
|
async earnPoint(levelId: string, timeSpent: number): Promise<number> {
|
|
if (!AuthManager.instance.isLoggedIn) {
|
|
StorageManager.addPoint();
|
|
return StorageManager.getPoints();
|
|
}
|
|
|
|
try {
|
|
const response = await HttpUtil.post<ApiEnvelope<UserAssetsData>>(
|
|
API_ENDPOINTS.USER_ASSETS_EARN,
|
|
{
|
|
reason: POINT_REASONS.LEVEL_COMPLETE,
|
|
levelId,
|
|
timeSpent,
|
|
},
|
|
API_TIMEOUT.SHORT
|
|
);
|
|
|
|
if (response.success && response.data) {
|
|
StorageManager.setPoints(response.data.points);
|
|
return response.data.points;
|
|
}
|
|
} catch (err) {
|
|
console.error('[UserAssetsManager] 获得积分请求失败,降级本地处理:', err);
|
|
}
|
|
|
|
StorageManager.addPoint();
|
|
return StorageManager.getPoints();
|
|
}
|
|
}
|