主题数据

This commit is contained in:
chen wei bo
2025-08-25 18:01:50 +08:00
parent aab0caa496
commit 1c4ee104a1
3 changed files with 112 additions and 5 deletions
+6 -5
View File
@@ -8,6 +8,7 @@ import { PlayerData } from "./PlayerData";
import { AccountData } from "./AccountData";
import { MetaData } from "./MetaData";
import { NetTimeData } from "./NetTimeData";
import { ThemeData } from "./ThemeData";
/**
* 集中定义数据ID,避免写错字符串
@@ -18,6 +19,7 @@ export enum DataId {
Account = "account",
Meta = "meta",
NetTime = "netTime",
Theme = "theme",
}
/** 构造器类型 */
@@ -43,13 +45,12 @@ export class DataManager {
*/
public init(): void {
console.log("[DataManager] 数据管理器初始化");
this.register(DataId.Login, LoginData);
this.register(DataId.Player, PlayerData);
this.register(DataId.Account, AccountData);
this.register(DataId.Meta, MetaData);
this.register(DataId.NetTime, NetTimeData);
this.register(DataId.Theme, ThemeData);
}
/**
@@ -66,18 +67,18 @@ export class DataManager {
* 获取数据(已存在则直接返回;否则按配置自动实例化并缓存)
*/
public getDataById<T extends BaseData = BaseData>(dataId: string): T | null {
// 1) 先从缓存取
// 先从缓存取
const cached = this._dataMap.get(dataId) as T | undefined;
if (cached) return cached;
// 2) 缓存没有→ 查找构造器
// 缓存没有→ 查找构造器
const Ctor = this._config.get(dataId) as DataCtor<T> | undefined;
if (!Ctor) {
console.warn(`[DataManager] 未找到 dataId 的构造器配置: ${dataId}`);
return null;
}
// 3) 动态创建 & init & 缓存
// 动态创建 & init & 缓存
const instance = new Ctor();
// 若你的 BaseData.init 接受 dataId,可传入
instance.init?.(dataId);
+97
View File
@@ -0,0 +1,97 @@
/**
* 主题数据
*/
import { BaseData } from "./BaseData";
import proto from 'db://assets/Scripts/proto/proto.pb.js';
export class ThemeData extends BaseData {
// 主题数据
private _themes = new Map<number, proto.cs.IHallTheme>();
// 主题 id
private _themeIds: number[] = [];
public reset(): void {
this._themes.clear();
this._themeIds = [];
}
public clear(): void {
this._themes.clear();
this._themeIds = [];
}
public destroy(): void {
super.destroy();
this._themes = null;
this._themeIds = null;
}
/** 设置主题数据 */
public set themes(res: proto.cs.ICSHallThemeRes) {
const list = res.themes ?? [];
this._themeIds = [];
for (const t of list) {
const vo: proto.cs.IHallTheme = {
id: t.id || 0, // 主题 id
key: t.key || "", // 多语言键
name: t.name || "", // 主题名称
category: t.category || 0, // 主题枚举
isRelease: t.isRelease, // 是否解锁
path: t.path || "", // 图片路径
};
this._themes.set(vo.id, vo);
this._themeIds.push(vo.id);
}
console.log("设置主题数据:", this._themes);
}
/** 获取所有主题数据 */
public get themes(): Map<number, proto.cs.IHallTheme> { return this._themes; }
/** 获取所有主题数据的数量 */
public get themesCount(): number { return this._themes ? this._themes.size : 0; }
/** 获取所有主题 id */
public get themeIds(): number[] { return this._themeIds; }
/** 根据主题 id 获取主题数据 */
private getThemeDataById(id: number): proto.cs.IHallTheme | null {
if (!this._themes) return null;
return this._themes.get(id) ?? null;
}
/** 根据主题 id 获取多语言键 */
public getLanKeyById(id: number): string | null {
let oneData = this.getThemeDataById(id);
if (!oneData) return null;
return oneData.key;
}
/** 根据主题 id 获取主题名称 */
public getNameById(id: number): string | null {
let oneData = this.getThemeDataById(id);
if (!oneData) return null;
return oneData.name;
}
/** 根据主题 id 获取主题枚举 */
public getCategoryById(id: number): number | null {
let oneData = this.getThemeDataById(id);
if (!oneData) return null;
return oneData.category;
}
/** 根据主题 id 获取是否解锁 */
public getIsReleaseById(id: number): boolean | null {
let oneData = this.getThemeDataById(id);
if (!oneData) return null;
return oneData.isRelease;
}
/** 根据主题 id 获取图片路径 */
public getPathById(id: number): string | null {
let oneData = this.getThemeDataById(id);
if (!oneData) return null;
return oneData.path;
}
}
@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "e02f9a30-748b-4546-8b52-3c0161352e09",
"files": [],
"subMetas": {},
"userData": {}
}