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

349 lines
12 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-09-09 18:16:13 +08:00
import { DataManager, DataId } from "../../data/DataManager";
import { GirlData } from "../../data/GirlData";
import { WalletData } from "../../data/WalletData";
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
// 每日推荐
2025-09-08 22:59:32 +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);
2025-09-09 15:15:28 +08:00
// 付费图片解锁数据
2025-09-08 15:10:51 +08:00
let unlockImageData: proto.cs.IResourceUnlock[] = [];
for (let id of photoIds) {
2025-09-09 15:15:28 +08:00
// 价格大于0才为付费
let imagePrice = girlDetailConfig.getImagePriceById(girlId, id);
2025-09-08 15:10:51 +08:00
// 偶数才解锁
let isUnlock = (id % 2 === 0);
2025-09-09 15:15:28 +08:00
if (imagePrice > 0 && isUnlock) {
2025-09-08 15:10:51 +08:00
let oneData = {
girlId: girlId,
resId: id,
};
unlockImageData.push(oneData);
}
}
2025-09-09 15:15:28 +08:00
// 付费视频解锁数据
2025-09-08 15:10:51 +08:00
let unlockVideoData: proto.cs.IResourceUnlock[] = [];
for (let id of videoIds) {
2025-09-09 15:15:28 +08:00
// 价格大于0才为付费
let videoPrice = girlDetailConfig.getVideoPriceById(girlId, id);
2025-09-08 15:10:51 +08:00
// 偶数才解锁
let isUnlock = (id % 2 === 0);
2025-09-09 15:15:28 +08:00
if (videoPrice > 0 && isUnlock) {
2025-09-08 15:10:51 +08:00
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
// 解锁技师
2025-09-09 18:16:13 +08:00
async reqUnlockGirl(req: proto.cs.ICSUnlockGirlReq): Promise<ApiResponse<proto.cs.ICSUnlockGirlRes>> {
2025-09-08 19:44:45 +08:00
// 延时
await this.delay(180);
2025-09-09 18:16:13 +08:00
// 请求数据
const reqGirlId = req.id;
// 数据层的数据
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
const category = girlData.getGrilCategoryById(reqGirlId);
const price = girlData.getGrilPrice(category.toString(), reqGirlId);
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
// 余额
let balanceNumber = Number(walletData.balance) - Number(price);
let balance = balanceNumber.toString();
// vip 30天后过期
const nowSeconds = Math.floor(Date.now() / 1000);
let vipExpire = nowSeconds + 30 * 24 * 3600;
2025-09-08 19:44:45 +08:00
// 返回数据
2025-09-09 18:16:13 +08:00
const res: proto.cs.ICSUnlockGirlRes = {
balance,
vipExpire,
};
2025-09-08 19:44:45 +08:00
return this.ok(res);
}
// 解锁资源
2025-09-09 18:16:13 +08:00
async reqUnlockGirlRes(req: proto.cs.ICSUnlockResourceReq): Promise<ApiResponse<proto.cs.ICSUnlockResourceRes>> {
2025-09-08 19:44:45 +08:00
// 延时
await this.delay(180);
2025-09-09 18:16:13 +08:00
// 请求数据
const reqGirlId = req.girlId;
const resId = req.resId;
const resType = req.type;
// 数据层的数据
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
const category = girlData.getGrilCategoryById(reqGirlId);
let price = 0;
if (resType === proto.cs.EnmResType.ERT_Image) {
// 图片
price = girlData.getGrilPhotoPrice(category.toString(), reqGirlId, resId);
} else if (resType === proto.cs.EnmResType.ERT_Video) {
// 视频
price = girlData.getGrilVideoPrice(category.toString(), reqGirlId, resId);
}
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
// 余额
let balanceNumber = Number(walletData.balance) - Number(price);
let balance = balanceNumber.toString();
// vip 30天后过期
const nowSeconds = Math.floor(Date.now() / 1000);
let vipExpire = nowSeconds + 30 * 24 * 3600;
2025-09-08 19:44:45 +08:00
// 返回数据
2025-09-09 18:16:13 +08:00
const res: proto.cs.ICSUnlockResourceRes = {
balance,
vipExpire,
};
2025-09-08 19:44:45 +08:00
return this.ok(res);
2025-09-22 17:54:33 +08:00
}
// 获取可聊天的技师数据
async reqCanChatGirlData(req: proto.cs.ICSGetLastChatListReq): Promise<ApiResponse<proto.cs.ICSGetLastChatListRes>> {
// TODO
// 返回数据
const res: proto.cs.ICSGetLastChatListRes = {
LastChatList: [],
};
return this.ok(res);
}
2025-10-08 15:47:32 +08:00
// 获取排行榜列表
async reqGirlRankData(req: proto.cs.ICSGetRankReq): Promise<ApiResponse<proto.cs.ICSGetRankRes>> {
// TODO
// 返回数据
const res: proto.cs.ICSGetRankRes = {
girls: [],
};
return this.ok(res);
}
2025-10-08 16:03:11 +08:00
// 收藏技师
async reqBookmarkGirl(req: proto.cs.ICSBookmarkReq): Promise<ApiResponse<proto.cs.ICSBookmarkRes>> {
// TODO
// 返回数据
const res: proto.cs.ICSBookmarkRes = {
};
return this.ok(res);
}
// 获取收藏列表
async reqBookmarkGirlList(req: proto.cs.ICSGetBookmarkReq): Promise<ApiResponse<proto.cs.ICSGetBookmarkRes>> {
// TODO
// 返回数据
const res: proto.cs.ICSGetBookmarkRes = {
girls: [],
};
return this.ok(res);
}
2025-08-29 16:39:41 +08:00
}