Files
18xchat/assets/Scripts/chat18x/network/services/MockGirlService.ts
T

256 lines
9.2 KiB
TypeScript
Raw Normal View History

2025-08-29 16:39:41 +08:00
import type { ApiResponse } from "../client/types";
import proto from "db://assets/Scripts/proto/proto.pb.js";
import type { IGirlService } from "./IGirlService";
2025-09-02 15:07:53 +08:00
import { ConfigManager, ConfigId } from "../../manager/ConfigManager";
import { GirlConfig } from "../../config/GirlConfig";
2025-09-02 17:22:27 +08:00
import { GirlDetailConfig } from "../../config/GirlDetailConfig";
2025-08-29 16:39:41 +08:00
export class MockGirlService implements IGirlService {
private delay(ms = 180) { return new Promise<void>(r => setTimeout(r, ms)); }
private randInt(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1)) + min; }
private ok<T>(data: T): ApiResponse<T> {
2025-09-02 15:07:53 +08:00
return ({ code: proto.cs.EnmRetCode.SUCCESS, data } as unknown) as ApiResponse<T>;
2025-08-29 16:39:41 +08:00
}
private err<T = unknown>(message = "mock error"): ApiResponse<T> {
2025-09-02 15:07:53 +08:00
return ({ code: 100, error: { message } } as unknown) as ApiResponse<T>;
2025-08-29 16:39:41 +08:00
}
2025-09-08 15:10:51 +08:00
private genBrief(id: number, nameKey: string, age: string, category: number, tagKey: string, priceType: number, price: number, vipUnlock: number, avatarPath: string, listAvatarPath: string): proto.cs.IGirls {
2025-08-29 16:39:41 +08:00
return {
id,
2025-09-08 15:10:51 +08:00
nameKey,
2025-09-02 15:07:53 +08:00
age,
2025-09-08 15:10:51 +08:00
category,
2025-09-02 15:07:53 +08:00
tagKey,
priceType,
price,
2025-09-08 15:10:51 +08:00
vipUnlock,
avatarPath,
2025-09-03 17:35:27 +08:00
listAvatarPath,
2025-08-29 16:39:41 +08:00
};
}
2025-09-08 15:10:51 +08:00
private genPhoto(id: number, imageType: number, unlockCounts: number, imagePrice: number, path: string): proto.cs.IPurchaseCommercialImage {
2025-09-02 17:22:27 +08:00
return {
2025-09-08 15:10:51 +08:00
id,
2025-09-03 17:35:27 +08:00
imageType,
2025-09-08 15:10:51 +08:00
unlockCounts,
2025-09-03 17:35:27 +08:00
imagePrice,
path,
2025-09-02 17:22:27 +08:00
};
2025-08-29 16:39:41 +08:00
}
2025-09-08 15:10:51 +08:00
private genVideo(id: number, path: string, emotion: number, videoPrice: number): proto.cs.IPurchaseCommercialVideo {
return {
id,
2025-09-03 17:35:27 +08:00
path,
emotion,
videoPrice,
};
}
2025-09-08 15:10:51 +08:00
private genDetail(id: number, detailDesc: string, commercialImages: proto.cs.IPurchaseCommercialImage[], commercialVideos: proto.cs.IPurchaseCommercialVideo[]): proto.cs.IGirlsDetail {
2025-09-02 17:22:27 +08:00
return {
2025-09-08 15:10:51 +08:00
id,
detailDesc,
commercialImages,
commercialVideos,
2025-09-02 17:22:27 +08:00
};
2025-08-29 16:39:41 +08:00
}
2025-09-08 15:10:51 +08:00
private genGirlData(
id: number, girl: proto.cs.IGirls, isRelease: boolean, star: number,
detail: proto.cs.IGirlsDetail, images: proto.cs.IResourceUnlock[], videos: proto.cs.IResourceUnlock[],
chatTotalCount: number, chatRemainCount: number): proto.cs.IGirlData {
return {
id,
girl,
isRelease,
star,
detail,
images,
videos,
chatTotalCount,
chatRemainCount,
};
}
2025-08-29 16:39:41 +08:00
// 每日推荐
async reqDailyRecommend(_req: proto.cs.ICSDailyRecommendReq): Promise<ApiResponse<proto.cs.ICSDailyRecommendRes>> {
2025-09-02 15:07:53 +08:00
// 延时
2025-08-29 16:39:41 +08:00
await this.delay(180);
2025-09-02 17:22:27 +08:00
// 配置数据
2025-09-02 15:07:53 +08:00
const configData = ConfigManager.I.getDataById<GirlConfig>(ConfigId.Girl);
const allId = configData.getAllId();
const recId = allId[0];
2025-09-08 15:10:51 +08:00
let nameKey = configData.getNameById(recId);
let age = configData.getAgeById(recId);
2025-09-02 15:07:53 +08:00
let tagKey = configData.getTagKeyById(recId);
let priceType = configData.getPriceTypeById(recId);
2025-09-08 15:10:51 +08:00
let price = configData.getPriceById(recId);
let vipUnlock = configData.getVipUnlockById(recId);
let avatarPath = configData.getAvatarPathById(recId);
2025-09-03 17:35:27 +08:00
let category = configData.getCategoryById(recId);
let listAvatarPath = configData.getListAvatarPathPathById(recId);
2025-09-08 15:10:51 +08:00
let oneData = this.genBrief(recId, nameKey, age, category, tagKey, priceType, price, vipUnlock, avatarPath, listAvatarPath);
2025-09-02 15:07:53 +08:00
// 返回数据
2025-09-08 15:10:51 +08:00
const res: proto.cs.ICSDailyRecommendRes = {girl: oneData};
return this.ok(res);
2025-08-29 16:39:41 +08:00
}
// 获取技师列表
async reqGetGirlList(req: proto.cs.ICSGetGirlListReq): Promise<ApiResponse<proto.cs.ICSGetGirlListRes>> {
2025-09-02 16:38:01 +08:00
// 延时
2025-08-29 16:39:41 +08:00
await this.delay(200);
2025-09-02 17:22:27 +08:00
// 配置数据
2025-09-02 16:38:01 +08:00
const configData = ConfigManager.I.getDataById<GirlConfig>(ConfigId.Girl);
const allId = configData.getAllId();
// 按 category 精确匹配
const category = Number(req?.category ?? -1);
const filteredIds = category >= 0
? allId.filter((id: number) => {
const cat = Number(configData.getCategoryById(id) ?? -1);
return cat === category;
})
: allId;
// 分页
const page = Math.max(1, Number(req?.page ?? 1));
const limit = Math.max(1, Math.min(Number(req?.limit ?? 10), 50));
const start = (page - 1) * limit;
const pageIds = filteredIds.slice(start, start + limit);
2025-09-08 15:10:51 +08:00
// 映射数据
const girls: proto.cs.IGirlData[] = pageIds.map((id: number) => {
// 简要数据
const nameKey = configData.getNameById(id);
const age = configData.getAgeById(id);
2025-09-02 16:38:01 +08:00
const tagKey = configData.getTagKeyById(id);
2025-09-08 15:10:51 +08:00
const priceType = configData.getPriceTypeById(id);
const price = configData.getPriceById(id);
const vipUnlock = configData.getVipUnlockById(id);
const avatarPath = configData.getAvatarPathById(id);
2025-09-03 17:35:27 +08:00
const category = configData.getCategoryById(id);
2025-09-08 15:10:51 +08:00
const listAvatarPath = configData.getListAvatarPathPathById(id);
const briefData = this.genBrief(id, nameKey, age, category, tagKey, priceType, price, vipUnlock, avatarPath, listAvatarPath);
const isRelease = true;
const star = 3;
// 详细数据
const detail = null;
const images = [];
const videos = [];
const chatTotalCount = 0;
const chatRemainCount = 0;
const girlData = this.genGirlData(id, briefData, isRelease, star, detail, images, videos, chatTotalCount, chatRemainCount);
return girlData;
2025-09-02 16:38:01 +08:00
});
2025-09-02 17:22:27 +08:00
// 返回数据
2025-09-08 15:10:51 +08:00
const res: proto.cs.ICSGetGirlListRes = {girls};
return this.ok(res);
2025-08-29 16:39:41 +08:00
}
// 获取技师详细信息
async reqGetGirlDetail(req: proto.cs.ICSGetGirlDetailReq): Promise<ApiResponse<proto.cs.ICSGetGirlDetailRes>> {
2025-09-02 17:22:27 +08:00
// 延时
2025-08-29 16:39:41 +08:00
await this.delay(160);
2025-09-02 17:22:27 +08:00
// 配置数据
const girlConfig = ConfigManager.I.getDataById<GirlConfig>(ConfigId.Girl);
const girlDetailConfig = ConfigManager.I.getDataById<GirlDetailConfig>(ConfigId.GirlDetail);
// id
const girlId = req.id;
2025-09-03 17:35:27 +08:00
// 简要数据
2025-09-08 15:10:51 +08:00
let nameKey = girlConfig.getNameById(girlId);
let age = girlConfig.getAgeById(girlId);
2025-09-02 17:22:27 +08:00
let tagKey = girlConfig.getTagKeyById(girlId);
let priceType = girlConfig.getPriceTypeById(girlId);
2025-09-08 15:10:51 +08:00
let price = girlConfig.getPriceById(girlId);
let vipUnlock = girlConfig.getVipUnlockById(girlId);
let avatarPath = girlConfig.getAvatarPathById(girlId);
2025-09-03 17:35:27 +08:00
let category = girlConfig.getCategoryById(girlId);
let listAvatarPath = girlConfig.getListAvatarPathPathById(girlId);
2025-09-08 15:10:51 +08:00
let briefData = this.genBrief(girlId, nameKey, age, category, tagKey, priceType, price, vipUnlock, avatarPath, listAvatarPath);
2025-09-02 17:22:27 +08:00
let isRelease = Math.random() < 0.4;
2025-09-08 15:10:51 +08:00
let detailDesc = girlDetailConfig.getDetailDescById(girlId);
2025-09-03 17:35:27 +08:00
// 图片
2025-09-02 17:22:27 +08:00
let photoData = [];
2025-09-08 15:10:51 +08:00
let photoIds = girlDetailConfig.getCommercialImagesIds(girlId);
for (let id of photoIds) {
let imageType = girlDetailConfig.getImageTypeById(girlId, id);
let unlockCounts = girlDetailConfig.getImageUnlockCountsById(girlId, id);
let imagePrice = girlDetailConfig.getImagePriceById(girlId, id);
let imagePath = girlDetailConfig.getImagePathById(girlId, id);
let oneData = this.genPhoto(id, imageType, unlockCounts, imagePrice, imagePath);
2025-09-03 17:35:27 +08:00
photoData.push(oneData);
2025-09-02 17:22:27 +08:00
}
2025-09-03 17:35:27 +08:00
// 视频
let videoData = [];
2025-09-08 15:10:51 +08:00
let videoIds = girlDetailConfig.getCommercialVideosIds(girlId);
for (let id of videoIds) {
let videoPath = girlDetailConfig.getVideoPathById(girlId, id);
let videoEmotion = girlDetailConfig.getVideoEmotionById(girlId, id);
let videoPrice = girlDetailConfig.getVideoPriceById(girlId, id);
let oneData = this.genVideo(id, videoPath, videoEmotion, videoPrice);
2025-09-03 17:35:27 +08:00
videoData.push(oneData);
2025-09-08 16:12:40 +08:00
}
2025-09-03 17:35:27 +08:00
// 详细数据
2025-09-08 15:10:51 +08:00
let detail = this.genDetail(girlId, detailDesc, photoData, videoData);
// 图片解锁数据
let unlockImageData: proto.cs.IResourceUnlock[] = [];
for (let id of photoIds) {
// 偶数才解锁
let isUnlock = (id % 2 === 0);
if (isUnlock) {
let oneData = {
girlId: girlId,
resId: id,
};
unlockImageData.push(oneData);
}
}
// 视频解锁数据
let unlockVideoData: proto.cs.IResourceUnlock[] = [];
for (let id of videoIds) {
// 偶数才解锁
let isUnlock = (id % 2 === 0);
if (isUnlock) {
let oneData = {
girlId: girlId,
resId: id,
};
unlockVideoData.push(oneData);
}
}
const star = 3;
2025-09-08 19:44:45 +08:00
const chatTotalCount = 20;
const chatRemainCount = 20;
2025-09-08 15:10:51 +08:00
const girlData = this.genGirlData(girlId, briefData, isRelease, star, detail, unlockImageData, unlockVideoData, chatTotalCount, chatRemainCount);
2025-09-03 11:09:35 +08:00
// 返回数据
2025-09-08 15:10:51 +08:00
const res: proto.cs.ICSGetGirlDetailRes = {girl: girlData};
return this.ok(res);
2025-08-29 16:39:41 +08:00
}
2025-09-08 19:44:45 +08:00
// 解锁技师
async reqUnlockGirl(_req: proto.cs.ICSUnlockGirlReq): Promise<ApiResponse<proto.cs.ICSUnlockGirlRes>> {
// 延时
await this.delay(180);
// 返回数据
const res: proto.cs.ICSUnlockGirlRes = {};
return this.ok(res);
}
// 解锁资源
async reqUnlockGirlRes(_req: proto.cs.ICSUnlockResourceReq): Promise<ApiResponse<proto.cs.ICSUnlockResourceRes>> {
// 延时
await this.delay(180);
// 返回数据
const res: proto.cs.ICSUnlockResourceRes = {};
return this.ok(res);
}
2025-08-29 16:39:41 +08:00
}