Files
18xchat/assets/Scripts/chat18x/data/GirlData.ts
T

343 lines
11 KiB
TypeScript
Raw Normal View History

2025-08-26 11:10:15 +08:00
/**
* 技师数据
*/
import { BaseData } from "./BaseData";
import proto from 'db://assets/Scripts/proto/proto.pb.js';
2025-08-28 17:22:04 +08:00
// 分类的数据结构
interface CategoryBucket {
// 技师的简要数据
briefs: Map<number, proto.cs.IGirlBrief>;
// 技师的详细数据
details: Map<number, proto.cs.IGirlDetail>;
// 技师的id
girlIds: number[];
2025-08-26 11:10:15 +08:00
}
2025-08-29 11:29:21 +08:00
// 每日推荐专用分类
const DAILY_BUCKET = "__daily__";
2025-08-26 11:10:15 +08:00
export class GirlData extends BaseData {
2025-08-28 17:22:04 +08:00
// 大分类
private _categories = new Map<string, CategoryBucket>();
2025-08-29 11:29:21 +08:00
// 每日推荐(存放在 DAILY_BUCKET 中,同时保留 id 索引便于 UI 使用)
private _dailyRecommendIds: number[] = [];
2025-08-26 11:10:15 +08:00
public reset(): void {
2025-08-28 17:22:04 +08:00
this._categories.clear();
2025-08-26 11:10:15 +08:00
}
public clear(): void {
2025-08-28 17:22:04 +08:00
this._categories.clear();
2025-08-26 11:10:15 +08:00
}
public destroy(): void {
super.destroy();
2025-08-28 17:22:04 +08:00
this._categories = null;
}
/** 保障 _categories 存在 */
private ensureCategories(): Map<string, CategoryBucket> {
if (!this._categories) {
this._categories = new Map<string, CategoryBucket>();
}
return this._categories;
}
/** 获取或创建分类桶 */
private ensureBucket(categoryId: string): CategoryBucket {
const cats = this.ensureCategories();
let bucket = cats.get(categoryId);
if (!bucket) {
bucket = {
briefs: new Map<number, proto.cs.IGirlBrief>(),
details: new Map<number, proto.cs.IGirlDetail>(),
girlIds: [],
};
cats.set(categoryId, bucket);
}
return bucket;
}
2025-08-29 11:29:21 +08:00
/**
* 合并一批简要信息到指定分类;并维护 girlIds 顺序(去重追加)
*/
public mergeBriefs(categoryId: string, respOrList: proto.cs.IGirlBrief[]): void {
if (!respOrList) return;
let allData = this.parseAllGirlBrief(respOrList);
if (!allData) return;
const bucket = this.ensureBucket(categoryId);
for (const g of allData) {
const id = g.id;
bucket.briefs.set(id, g);
// TODO,去重
bucket.girlIds.push(id);
}
console.log("保存技师的简要数据: ", bucket);
}
2025-08-28 17:22:04 +08:00
/** 取分类下的全部 girlIds(若分类不存在返回空数组) */
public getGirlIds(categoryId: string): number[] {
const b = this.ensureCategories().get(categoryId);
return b ? b.girlIds : [];
}
/** --------------------------------------------------------- 技师的简要数据 --------------------------------------------------------- */
// 解析技师简要数据,一个
private parseOneGirlBrief(data: proto.cs.IGirlBrief): proto.cs.IGirlBrief | null {
if (!data) return null;
const oneData: proto.cs.IGirlBrief = {
id: data.id, // id
name: data.name, // 名字
age: data.age, // 年龄
tagKey: data.tagKey, // 标签
priceType: data.priceType, // 付费类型
price: data.price, // 价格
avatar: data.avatar, // 头像
star: data.star, // 星级
};
return oneData;
}
// 解析技师简要数据,多个
private parseAllGirlBrief(data: proto.cs.IGirlBrief[]): proto.cs.IGirlBrief[] | null {
if (!data) return null;
let allData = [];
for (const value of data) {
const oneData: proto.cs.IGirlBrief = this.parseOneGirlBrief(value);
if (oneData) {
allData.push(oneData);
}
}
return allData;
}
/** 保存技师的简要数据 */
public setGirlBriefs(categoryId: string, data: proto.cs.ICSGetGirlListRes): void {
2025-08-29 11:29:21 +08:00
this.mergeBriefs(categoryId, data.girls);
2025-08-28 17:22:04 +08:00
}
/** 取某分类 + girlId 的简要信息 */
private getGrilBrief(categoryId: string, girlId: number): proto.cs.IGirlBrief | null {
const b = this.ensureCategories().get(categoryId);
if (!b) return null;
return b.briefs.get(girlId) ?? null;
}
/** 获取名字 */
public getGrilName(categoryId: string, girlId: number): string {
let oneData = this.getGrilBrief(categoryId, girlId);
if (!oneData) return "";
return oneData.name;
}
/** 获取年龄 */
public getGrilAge(categoryId: string, girlId: number): number {
let oneData = this.getGrilBrief(categoryId, girlId);
if (!oneData) return 0;
return oneData.age;
}
/** 获取标签 */
public getGrilTagKey(categoryId: string, girlId: number): string {
let oneData = this.getGrilBrief(categoryId, girlId);
if (!oneData) return "";
return oneData.tagKey;
2025-08-26 11:10:15 +08:00
}
2025-08-28 17:22:04 +08:00
/** 获取付费类型 */
public getGrilPriceType(categoryId: string, girlId: number): number {
let oneData = this.getGrilBrief(categoryId, girlId);
if (!oneData) return 0;
return oneData.priceType;
}
/** 获取价格 */
public getGrilPrice(categoryId: string, girlId: number): number {
let oneData = this.getGrilBrief(categoryId, girlId);
if (!oneData) return 0;
return oneData.price;
}
/** 获取头像 */
public getGrilAvatar(categoryId: string, girlId: number): string {
let oneData = this.getGrilBrief(categoryId, girlId);
if (!oneData) return "";
return oneData.avatar;
}
/** 获取星级 */
public getGrilStar(categoryId: string, girlId: number): number {
let oneData = this.getGrilBrief(categoryId, girlId);
if (!oneData) return 0;
return oneData.star;
}
/** --------------------------------------------------------- 技师的详细数据 --------------------------------------------------------- */
// 解析技师图片数据,一个
private parseOneGirlPhoto(data: proto.cs.IGirlPhoto): proto.cs.IGirlPhoto | null {
if (!data) return null;
const oneData: proto.cs.IGirlPhoto = {
pic: data.pic, // 图片路径
isRelease: data.isRelease, // 是否解锁
price: data.price, // 价格
};
return oneData;
}
// 解析技师图片数据,多个
private parseAllGirlPhoto(data: proto.cs.IGirlPhoto[]): proto.cs.IGirlPhoto[] | null {
if (!data) return null;
let allData = [];
for (const value of data) {
const oneData: proto.cs.IGirlPhoto = this.parseOneGirlPhoto(value);
if (oneData) {
allData.push(oneData);
}
2025-08-26 11:10:15 +08:00
}
2025-08-28 17:22:04 +08:00
return allData;
2025-08-26 11:10:15 +08:00
}
2025-08-28 17:22:04 +08:00
// 解析技师详细数据,一个
private parseOneGirlDetail(data: proto.cs.IGirlDetail): proto.cs.IGirlDetail | null {
if (!data) return null;
const photoData = this.parseAllGirlPhoto(data.photos);
const briefData = this.parseOneGirlBrief(data.brief);
const oneData: proto.cs.IGirlDetail = {
brief: briefData, // 技师简要数据
desc: data.desc, // 技师描述
photos: photoData, // 技师图片列表
isRelease: data.isRelease, // 技师是否解锁
chatCount: data.chatCount, // 剩余聊天次数
};
return oneData;
}
// 解析技师详细数据,多个
private parseAllGirlDetail(data: proto.cs.IGirlDetail[]): proto.cs.IGirlDetail[] | null {
if (!data) return null;
let allData = [];
for (const value of data) {
const oneData: proto.cs.IGirlDetail = this.parseOneGirlDetail(value);
if (oneData) {
allData.push(oneData);
}
}
return allData;
}
/** 保存技师的详细数据 */
public setGirlDetails(categoryId: string, data: proto.cs.ICSGetGirlDetailRes): void {
2025-08-29 11:29:21 +08:00
const d = data?.detail;
2025-08-28 17:22:04 +08:00
if (!d) return;
2025-08-29 11:29:21 +08:00
// 合并简要信息
if (d.brief) this.mergeBriefs(categoryId, [d.brief]);
const oneDetail = this.parseOneGirlDetail(d);
2025-08-28 17:22:04 +08:00
const bucket = this.ensureBucket(categoryId);
// id
const id = d.brief?.id ?? 0;
// 写入详情
2025-08-29 11:29:21 +08:00
bucket.details.set(id, oneDetail);
2025-08-28 17:22:04 +08:00
console.log("保存技师的详细数据: ", bucket);
}
/** 取某分类 + girlId 的详细信息 */
public getGrilDetail(categoryId: string, girlId: number): proto.cs.IGirlDetail | null {
const b = this.ensureCategories().get(categoryId);
if (!b) return null;
return b.details.get(girlId) ?? null;
}
/** 获取技师描述 */
public getGrilDesc(categoryId: string, girlId: number): string {
let oneData = this.getGrilDetail(categoryId, girlId);
if (!oneData) return "";
return oneData.desc;
}
/** 获取技师是否解锁 */
public getGrilIsRelease(categoryId: string, girlId: number): boolean {
let oneData = this.getGrilDetail(categoryId, girlId);
if (!oneData) return false;
return oneData.isRelease;
}
/** 获取技师剩余聊天次数 */
public getGrilChatCount(categoryId: string, girlId: number): number {
let oneData = this.getGrilDetail(categoryId, girlId);
if (!oneData) return 0;
return oneData.chatCount;
}
/** 获取技师图片数据,通过 index */
public getGrilPhotoData(categoryId: string, girlId: number, index: number): proto.cs.IGirlPhoto | null {
let oneData = this.getGrilDetail(categoryId, girlId);
if (!oneData || !oneData.photos) return null;
// 下标越界保护
if (index < 0 || index >= oneData.photos.length) return null;
return oneData.photos[index] ?? null;
}
/** 获取技师图片的路径 */
public getGrilPhotoPic(categoryId: string, girlId: number, index: number): string {
let oneData = this.getGrilPhotoData(categoryId, girlId, index);
if (!oneData) return "";
return oneData.pic;
}
/** 获取技师图片是否解锁 */
public getGrilPhotoIsRelease(categoryId: string, girlId: number, index: number): boolean {
let oneData = this.getGrilPhotoData(categoryId, girlId, index);
if (!oneData) return false;
return oneData.isRelease;
}
/** 获取技师图片的价格 */
public getGrilPhotoPrice(categoryId: string, girlId: number, index: number): number {
let oneData = this.getGrilPhotoData(categoryId, girlId, index);
if (!oneData) return 0;
return oneData.price;
}
/** --------------------------------------------------------- 每日推荐 --------------------------------------------------------- */
2025-08-29 11:29:21 +08:00
/** 保存每日推荐:放入特殊分类 DAILY_BUCKET,并维护 id 索引 */
public setDailyRecommend(res: proto.cs.ICSDailyRecommendRes): void {
this.mergeBriefs(DAILY_BUCKET, res.girls);
this._dailyRecommendIds = res.girls.map(g => g.id ?? 0);
2025-09-02 15:07:53 +08:00
console.log("保存每日推荐: ", this._dailyRecommendIds);
2025-08-26 11:10:15 +08:00
}
2025-08-29 11:29:21 +08:00
/** 获取每日推荐的简要信息 */
private getRecommendGrilBrief(categoryId: string, index: number): proto.cs.IGirlBrief | null {
const b = this.ensureCategories().get(categoryId);
if (!b) return null;
if (index < 0 || index >= b.girlIds.length) return null;
return b.briefs.get(b.girlIds[index]) ?? null;
}
2025-08-26 11:10:15 +08:00
2025-09-02 15:07:53 +08:00
/** 获取 id */
public getRecommendGrilId(index: number): number {
let oneData = this.getRecommendGrilBrief(DAILY_BUCKET, index);
if (!oneData) return 0;
return oneData.id;
}
2025-08-29 11:29:21 +08:00
/** 获取名字 */
public getRecommendGrilName(index: number): string {
let oneData = this.getRecommendGrilBrief(DAILY_BUCKET, index);
if (!oneData) return "";
return oneData.name;
}
2025-09-02 15:07:53 +08:00
/** 获取头像 */
public getRecommendGrilAvatar(index: number): string {
let oneData = this.getRecommendGrilBrief(DAILY_BUCKET, index);
if (!oneData) return "";
return oneData.avatar;
}
2025-08-26 11:10:15 +08:00
}