feat: 支持登录、个人信息存储
This commit is contained in:
@@ -15,17 +15,20 @@ interface UserProgress {
|
||||
* 统一管理用户数据的本地持久化存储
|
||||
*/
|
||||
export class StorageManager {
|
||||
/** 生命值存储键 */
|
||||
private static readonly KEY_LIVES = 'game_lives';
|
||||
/** 积分存储键 */
|
||||
private static readonly KEY_POINTS = 'game_points';
|
||||
|
||||
/** 用户进度存储键 */
|
||||
private static readonly KEY_PROGRESS = 'game_progress';
|
||||
|
||||
/** 默认生命值 */
|
||||
private static readonly DEFAULT_LIVES = 10;
|
||||
/** 认证 token 存储键 */
|
||||
private static readonly KEY_TOKEN = 'auth_token';
|
||||
|
||||
/** 最小生命值 */
|
||||
private static readonly MIN_LIVES = 0;
|
||||
/** 默认积分 */
|
||||
private static readonly DEFAULT_POINTS = 10;
|
||||
|
||||
/** 最小积分 */
|
||||
private static readonly MIN_POINTS = 0;
|
||||
|
||||
/** 默认进度 */
|
||||
private static readonly DEFAULT_PROGRESS: UserProgress = {
|
||||
@@ -36,75 +39,101 @@ export class StorageManager {
|
||||
/** 进度缓存(避免重复读取 localStorage) */
|
||||
private static _progressCache: UserProgress | null = null;
|
||||
|
||||
// ==================== 生命值管理 ====================
|
||||
// ==================== 积分管理 ====================
|
||||
|
||||
/**
|
||||
* 获取当前生命值
|
||||
* @returns 当前生命值,新用户返回默认值 10
|
||||
* 获取当前积分
|
||||
* @returns 当前积分,新用户返回默认值 10
|
||||
*/
|
||||
static getLives(): number {
|
||||
const stored = sys.localStorage.getItem(StorageManager.KEY_LIVES);
|
||||
static getPoints(): number {
|
||||
const stored = sys.localStorage.getItem(StorageManager.KEY_POINTS);
|
||||
if (stored === null || stored === '') {
|
||||
// 新用户,设置默认值
|
||||
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
|
||||
return StorageManager.DEFAULT_LIVES;
|
||||
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
|
||||
return StorageManager.DEFAULT_POINTS;
|
||||
}
|
||||
const lives = parseInt(stored, 10);
|
||||
const points = parseInt(stored, 10);
|
||||
// 防止异常数据
|
||||
if (isNaN(lives) || lives < 0) {
|
||||
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
|
||||
return StorageManager.DEFAULT_LIVES;
|
||||
if (isNaN(points) || points < 0) {
|
||||
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
|
||||
return StorageManager.DEFAULT_POINTS;
|
||||
}
|
||||
return lives;
|
||||
return points;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置生命值
|
||||
* @param lives 生命值
|
||||
* 设置积分
|
||||
* @param points 积分
|
||||
*/
|
||||
static setLives(lives: number): void {
|
||||
const validLives = Math.max(StorageManager.MIN_LIVES, lives);
|
||||
sys.localStorage.setItem(StorageManager.KEY_LIVES, validLives.toString());
|
||||
console.log(`[StorageManager] 生命值已更新: ${validLives}`);
|
||||
static setPoints(points: number): void {
|
||||
const validPoints = Math.max(StorageManager.MIN_POINTS, points);
|
||||
sys.localStorage.setItem(StorageManager.KEY_POINTS, validPoints.toString());
|
||||
console.log(`[StorageManager] 积分已更新: ${validPoints}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗一颗生命
|
||||
* @returns 是否消耗成功(生命值不足时返回 false)
|
||||
* 消耗一个积分
|
||||
* @returns 是否消耗成功(积分不足时返回 false)
|
||||
*/
|
||||
static consumeLife(): boolean {
|
||||
const currentLives = StorageManager.getLives();
|
||||
if (currentLives <= 0) {
|
||||
console.warn('[StorageManager] 生命值不足,无法消耗');
|
||||
static consumePoint(): boolean {
|
||||
const currentPoints = StorageManager.getPoints();
|
||||
if (currentPoints <= 0) {
|
||||
console.warn('[StorageManager] 积分不足,无法消耗');
|
||||
return false;
|
||||
}
|
||||
StorageManager.setLives(currentLives - 1);
|
||||
StorageManager.setPoints(currentPoints - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加一颗生命
|
||||
* 增加一个积分
|
||||
*/
|
||||
static addLife(): void {
|
||||
const currentLives = StorageManager.getLives();
|
||||
StorageManager.setLives(currentLives + 1);
|
||||
console.log(`[StorageManager] 获得一颗生命,当前生命值: ${currentLives + 1}`);
|
||||
static addPoint(): void {
|
||||
const currentPoints = StorageManager.getPoints();
|
||||
StorageManager.setPoints(currentPoints + 1);
|
||||
console.log(`[StorageManager] 获得一个积分,当前积分: ${currentPoints + 1}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有足够的生命值
|
||||
* @returns 是否有生命值
|
||||
* 检查是否有足够的积分
|
||||
* @returns 是否有积分
|
||||
*/
|
||||
static hasLives(): boolean {
|
||||
return StorageManager.getLives() > 0;
|
||||
static hasPoints(): boolean {
|
||||
return StorageManager.getPoints() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置生命值为默认值
|
||||
* 重置积分为默认值
|
||||
*/
|
||||
static resetLives(): void {
|
||||
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
|
||||
console.log('[StorageManager] 生命值已重置为默认值');
|
||||
static resetPoints(): void {
|
||||
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
|
||||
console.log('[StorageManager] 积分已重置为默认值');
|
||||
}
|
||||
|
||||
// ==================== 认证 Token 管理 ====================
|
||||
|
||||
/**
|
||||
* 获取认证 token
|
||||
*/
|
||||
static getToken(): string | null {
|
||||
const token = sys.localStorage.getItem(StorageManager.KEY_TOKEN);
|
||||
return (token === null || token === '') ? null : token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置认证 token
|
||||
*/
|
||||
static setToken(token: string): void {
|
||||
sys.localStorage.setItem(StorageManager.KEY_TOKEN, token);
|
||||
console.log('[StorageManager] Token 已保存');
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除认证 token
|
||||
*/
|
||||
static clearToken(): void {
|
||||
sys.localStorage.removeItem(StorageManager.KEY_TOKEN);
|
||||
console.log('[StorageManager] Token 已清除');
|
||||
}
|
||||
|
||||
// ==================== 关卡进度管理 ====================
|
||||
@@ -229,11 +258,12 @@ export class StorageManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置所有数据(生命值 + 进度)
|
||||
* 重置所有数据(积分 + 进度)
|
||||
*/
|
||||
static resetAll(): void {
|
||||
StorageManager.resetLives();
|
||||
StorageManager.resetPoints();
|
||||
StorageManager.resetProgress();
|
||||
StorageManager.clearToken();
|
||||
console.log('[StorageManager] 所有数据已重置');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user